Coder Social home page Coder Social logo

Comments (3)

KTRosenberg avatar KTRosenberg commented on August 15, 2024 1

Just for an update, I've figured out how to mix the old C API with the new more limited Swift-specific APIs. I haven't yet made sure I'm not leaking memory, but it looks like I've found what I needed, hopefully.

In case you ever wanted to know:

@objc protocol JSMemoryExports: JSExport {
    // note that I'm returning an optional
    static func Uint8ArrayMake(_ count : JSValue) -> JSValue?
}

class JSMemory: NSObject, JSMemoryExports { 
    class func UInt8ArrayMake(_ count : JSValue) -> JSValue? {
        guard !count.isUndefined && !count.isNull else {
            return nil
        }
        
        let ref : JSValueRef = JSObjectMakeTypedArray(
            JS_Controller.js.ctx.jsGlobalContextRef!,
            kJSTypedArrayTypeUint8Array,
            Int(count.toInt32()),
            nil
        )!
        
        // if you want to modify the data
        // let ptr = JSObjectGetTypedArrayBytesPtr(
        //    JS_Controller.js.ctx.jsGlobalContextRef!, ref, nil
        // )

        return JSValue(jsValueRef: ref, in: JS_Controller.js.ctx)    
    }
}

from ejecta.

phoboslab avatar phoboslab commented on August 15, 2024

I'm sorry, I don't think I can help you much. I don't know anything about Swift and very little about JSC's newer APIs.

Are you sure the APIs supports exporting static class methods? A quick google search at least returned an article that explains how to bind a Swift class and instantiate it in JS - with instance methods and all: https://nshipster.com/javascriptcore/

Just for reference, the EJ_BIND_* macros define a static objc class method that returns a function pointer to the actual implementation (the body of the macro), that is later collected by EJClassLoader and handed over to JSCs JSClassCreate(). EJClassLoader (and all the rest of Ejecta) uses JSCs older C API, listed here under Legacy JavaScriptCore APIs, not the newer ObjC API.

from ejecta.

KTRosenberg avatar KTRosenberg commented on August 15, 2024

Hmmm, yes I'm sure that static functions are supported. The link you have there shows the following:

// Protocol must be declared with `@objc`
@objc protocol PersonJSExports: JSExport {
    var firstName: String { get set }
    var lastName: String { get set }
    var birthYear: NSNumber? { get set }

    var fullName: String { get }

    // Imported as `Person.createWithFirstNameLastName(_:_:)`
    static func createWith(firstName: String, lastName: String) -> Person
}

That is basically what I am doing. I've found several examples online of passing back and forth custom classes, but none showing how to pass raw data such as a TypedArray or JSValueRef, which is what seems to cause issues.

So now I'm wondering what the proper way of passing byte data back and forth without copying is. It's admittedly frustrating not to see any real examples of this.

Also, I wasn't aware that there WAS a newer Objective C API. Everything's bunched together. It's possible that I'm mixing some old and new API functionality in some invalid way.

from ejecta.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.