Skip to content

Export function returning a JSValue #149

Closed
@AndrewBarba

Description

@AndrewBarba

I've followed the basic docs to call an exported function that takes in string/numbers and returns string/numbers but I'm struggling to figure out how to get the JS runtime to await a returned JSPromise from the Swift runtime.

I started with this approach of returning a pointer to the JSPromise:

@_cdecl("fetch")
func fetch() -> UnsafeRawPointer {
    let promise = JSPromise { resolve in
        resolve(.success(5))
    }
    var value = promise.jsValue()
    return withUnsafePointer(to: &value) { UnsafeRawPointer($0) }
}

But it's obviously not so clear how to deserialize this to an actual Promise in JS:

async function jsFetch() {
  let ptr = instance.exports.fetch()
  let promise = /** ??? */
  await promise
}

I'm very new to WASM so I apologize if this is a completely wrong approach, I just needed to get the ball rolling because I can't find anything else online about returning more complex values. I've been trying to understand how its accomplished in Rust but they have fancy annotations from the wasm-bindgen crate which is frankly over my head.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions