Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interaction between JSPI and WebAssembly.Function? #50

Open
hoodmane opened this issue Aug 1, 2024 · 1 comment
Open

Interaction between JSPI and WebAssembly.Function? #50

hoodmane opened this issue Aug 1, 2024 · 1 comment

Comments

@hoodmane
Copy link

hoodmane commented Aug 1, 2024

I'm concerned about the interaction between this proposal and the JSPI, in particular about the behavior of new WebAssembly.Function(sig, new WebAssembly.Suspending(jsFunc)). I believe the two specs as they currently exist says this should throw an error, because IsCallable(new WebAssembly.Suspending(jsFunc)) should return false and then new Webassembly.Function should fail step 1 "Assert: =IsCallable=".

In v8 the current implementation seems to just delete the new WebAssembly.Suspending(jsFunc).

What I would like is for new WebAssembly.Function(sig, func) to behave like reexporting the function, basically the following pseudocode from #16:

function wasmFunctionPolyfill({ parameters, results }, f) {
  const builder = new WasmModuleBuilder();
  const functionIndex = builder.addImport("env", "f", { parameters, results });
  builder.addExport("exportedFunction", functionIndex);
  const buffer = builder.toBuffer();

  const module = new WebAssembly.Module(buffer);
  const instance = new WebAssembly.Instance(module, {"env": { f } });
  return instance.exports.exportedFunction;
}

To update the spec so that new WebAssembly.Function behaves like the above wasmFunctionPolyfill when passed a suspendable object I think we would need something like the following diff:

--- a/document/js-api/index.bs
+++ b/document/js-api/index.bs
@@ -1319,8 +1319,13 @@ The <dfn method for="Function">type()</dfn> method steps are:
 </div>
 
 <div algorithm>
-  To <dfn>construct a new WebAssembly Function</dfn> from a JavaScript [=callable=] object |callable| and {{FunctionType}} |signature|, perform the following steps:
+  To <dfn>construct a new WebAssembly Function</dfn> from a JavaScript [=callable=] or [=suspendable=] object |callable| and {{FunctionType}} |signature|, perform the following steps:
 
+    1. If |callable| has a \[[wrappedFunction]] internal slot:
+        1. Let |func| be |callable|'s \[[wrappedFunction]] slot.
+        1. Assert [=IsCallable=](|func|).
+        1. Create [=a new suspending function=] from |func| and |functype|, and let |funcaddr| be the result.
+        1. Return the result of creating [=a new Exported Function=] from |funcaddr|.
     1. Assert: [=IsCallable=](|callable|).
     1. Let |store| be the [=surrounding agent=]'s [=associated store=].
     1. Let |signature| be «[ "{{FunctionType/parameters}}" → |parameters|, "{{FunctionType/results}}" → |results| ]».

xrefs:
the jspi issue
the v8 issue.

@Ms2ger
Copy link
Contributor

Ms2ger commented Aug 1, 2024

In any case the IDL would also need to be adjusted, though I don't know how - it's nonsensical now anyway (#16).

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

No branches or pull requests

2 participants