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

Fix issue with invoking main function from browser #55412

Closed
wants to merge 1 commit into from

Conversation

akalankavinda
Copy link

When trying to invoke main method the following error occurs.
image

this happens because of we store the result from WebAssembly.instantiate(), which is a WebAssemblyInstantiatedSource type value. We should store the instance value inside the returned object

image

Copy link

google-cla bot commented Apr 9, 2024

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@akalankavinda akalankavinda changed the title Fix issue with invoking main function from browser Fix issue with invoking main function from browser Apr 9, 2024
Copy link

Thank you for your contribution! This project uses Gerrit for code reviews. Your pull request has automatically been converted into a code review at:

https://dart-review.googlesource.com/c/sdk/+/361980

Please wait for a developer to review your code review at the above link; you can speed up the review if you sign into Gerrit and manually add a reviewer that has recently worked on the relevant code. See CONTRIBUTING.md to learn how to upload changes to Gerrit directly.

Additional commits pushed to this PR will update both the PR and the corresponding Gerrit CL. After the review is complete on the CL, your reviewer will merge the CL (automatically closing this PR).

@osa1
Copy link
Member

osa1 commented Apr 10, 2024

@akalankavinda you are instantiating the Wasm module wrong. You should be using the instantiation function provided by the .mjs file. Here's a full example:

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta charset="utf-8">
  <meta name="dart.unittest" content="full-stack-traces">
  <title> test.dart.wasm </title>
  <link rel="preload" href="/test.wasm" as="fetch" crossorigin>
</head>
<body>
  <script type="module">
  const dartModulePromise = WebAssembly.compileStreaming(fetch('/test.wasm'));
  const imports = {};
  let dart2wasm_runtime = await import('/test.mjs');
  let moduleInstance =
      await dart2wasm_runtime.instantiate(dartModulePromise, imports);
  dart2wasm_runtime.invoke(moduleInstance);
  </script>
</body>
</html>

You can't instantiate the module yourself because you don't know what imports the module expects and how to provide them. Use the code above.

If you really want to instantiate it yourself, you have to use the second overloading of WebAssembly.instantiate here: https://developer.mozilla.org/en-US/docs/WebAssembly/JavaScript_interface/instantiate_static#secondary_overload_%E2%80%94_taking_a_module_object_instance

Which returns "A Promise that resolves to an WebAssembly.Instance object".

@osa1 osa1 closed this Apr 10, 2024
@osa1
Copy link
Member

osa1 commented Apr 10, 2024

Ah, sorry, you're already calling the instantiation function in the .mjs file, but it looks like the argument you're passing to it is the .wasm binary, but it should be a compiled module. If you call WebAssembly.compile (or compileStreaming) on the argument that should do it.

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.

2 participants