Skip to content

Function unmarshaling with .dump#264

Open
amoffat wants to merge 2 commits into
justjake:mainfrom
amoffat:function-dump-pr
Open

Function unmarshaling with .dump#264
amoffat wants to merge 2 commits into
justjake:mainfrom
amoffat:function-dump-pr

Conversation

@amoffat
Copy link
Copy Markdown

@amoffat amoffat commented May 14, 2026

Purpose

To allow .dump to unmarshal client-side functions that behave like host-side functions:

it("can dump a function", () => {
  const fnHandle = vm.unwrapResult(vm.evalCode("(function add(a, b) { return a + b })"))
  const fn = vm.dump(fnHandle) as (...args: any[]) => any
  fnHandle.dispose()

  const result = fn(3, 7)
  assert.strictEqual(result, 10)
})

Use case

I have client-side code that calls a host-side function and passes a callback as an argument. The host-side function then does some work, and eventually calls the callback. The brdige/shim on the host-side that facilitates calls from the client to the host does not care about the specific arguments being passed around. It just .dumps them before calling the host function.

Correctly unmarshalling functions with .dump allows me to pass callbacks.

Changes made

Refactored .dump to do host-side unmarshalling, instead of client-side JS_JSONStringify (via QTS_Dump) followed by JSON.parse. This allows us to recursively unmarshal individual properties (in the case of nested objects), so that we handle functions that might be buried within those objects.

Added a .wrap method, which acts as sort of the inverse of .dump. It takes a host-side object and converts it to a client-side handle. It handles most of the common objects. .wrap is needed because in order for the unmarshalled functions to behave like host-side functions, they need to transparently convert their arguments into client-side handles. To achieve this, .wrap() is called on them automatically.

Side-effects

The test for OOM is no longer needed (and I was unable to trip it) because the objects are no longer being allocated on the vm heap before being transferred back to the host.

Additional notes

Exposed a QTS_IsError helper, so that .dump can preserve the behavior of unmarshalling error objects correctly (which have non-enumerable properties like name, message, etc).

This PR depends on #262 because of the memory corruption issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant