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

Bound exports seem to be returning Wasm pointers #13

Closed
lastmjs opened this issue Jan 15, 2020 · 2 comments · Fixed by #18
Closed

Bound exports seem to be returning Wasm pointers #13

lastmjs opened this issue Jan 15, 2020 · 2 comments · Fixed by #18

Comments

@lastmjs
Copy link

lastmjs commented Jan 15, 2020

I'm not sure if I'm doing this incorrectly, but as-bind doesn't seem to be returning the data types that I want, just the Wasm pointers are some other pointer is coming back:

In my AssemblyScript:

export function testString(theString: string): string {
    return theString;
}

In my JavaScript (not exactly my source code as I'm working on integration with Zwitterion):

import { AsBind } from 'as-bind';

(() => {
  const wasm = new Uint8Array([0,97,115,109,1,0,0,0,1,44,8,96,2,127,127,1,127,96,0,0,96,3,...]); // example wasm
  const exports = (await AsBind.instantiate(wasm)).exports;

  console.log(exports.testString('hello world'));
})();

This logs 1888, not hello world. Am I missing something with the API?

@lastmjs
Copy link
Author

lastmjs commented Jan 15, 2020

I think I see the problem, I need to add this file as the entry point when compiling: ./node_modules/as-bind/lib/assembly/as-bind.ts

Hmmm...is there any way that requirement could be dropped? It was confusing and seems like it could be unnecessary.

The file seems simple enough. Could you modify the Wasm that is passed into AsBind.instantiate and just put the compiled code at the front? Or something like that?

@torch2424
Copy link
Owner

I think I see the problem, I need to add this file as the entry point when compiling: ./node_modules/as-bind/lib/assembly/as-bind.ts

Ah yes! It is a requirement and covered on the quick start in the README. 😄 I should figure out how to make that more apparent (throw a warning or error in the JS lib if we notice it wasn't added? 🤔 )

Hmmm...is there any way that requirement could be dropped? It was confusing and seems like it could be unnecessary.

Unfortunately no. So by default, AssemblyScript will export some of the Class IDs, but not all of them (For example, most typed array classes are not exported by default). Therefore, we need to export them. I also wanted this, as it gives us more freedom to export more things in the future, and requires less lines of code in terms of exporting from the llibrary in the users index.js. Does that make sense? I can send you some old versions of the API where I didn't have this 😄

The file seems simple enough. Could you modify the Wasm that is passed into AsBind.instantiate and just put the compiled code at the front? Or something like that?

So we could totally do that! But it'd have to be like a compile time tool. To do this at runtime would be extremely slow at instantiation time, since we'd have to essentially, parse, and recompile the module at runtime 👍

Let me know what you think! Stoked to see your interest in the library 😄 🎉

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 a pull request may close this issue.

2 participants