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

Show an example using a function passed into AssemblyScript using the importObject on the README #12

Closed
torch2424 opened this issue Jan 15, 2020 · 4 comments · Fixed by #18
Assignees
Labels
documentation Improvements or additions to documentation

Comments

@torch2424
Copy link
Owner

Brought up by @jayphelps in https://github.com/AssemblyScript/meta/issues/19 . Was a bit unclear to them, thus, maybe should make it more prominent.

Thanks! 😄 👍

@torch2424 torch2424 added the documentation Improvements or additions to documentation label Jan 15, 2020
@torch2424 torch2424 self-assigned this Jan 15, 2020
@torch2424
Copy link
Owner Author

Also, @jayphelps, if you don't mind taking a quick look:

Is this what you meant: https://github.com/torch2424/as-bind/blob/master/test/test.js#L185

It's a test that ensures AssemblyScript can call (From within AssemblyScript) a passed JS function, that receives a string passed by AssemblyScript.

@jayphelps
Copy link

jayphelps commented Jan 15, 2020

@torch2424 I don't think so.

My questions were about passing and receiving functions (in addition to actual closures) from and to both JS and AS both ways.

So as one example, somehow you could pass a JS function as an argument to an AS exported function.

/* pseudo code in AssemblyScript */
export function takesAFunction(callback: (str: string) => void): void {
  callback('hello world');
}
/* in JavaScript/TypeScript */
const asyncTask = async () => {
  const instance = await AsBind.instantiate(wasm);
  // even though this is a JS function, as-bind would have to wrap it
  // so that it can be called as an AS function
  instance.exports.takesAFunction((str: string) => {
    // as-bind would also have had to convert `str` automatically from an AS string to a JS string
    console.log(str.charAt(0)); // some how use the string
  });
};
asyncTask();

The opposite eventually would be cool too, passing an AS function as an argument to a JS function that was imported. AS doesn't support real closures right now, but passing regular functions (that don't close over variables) is still useful.

Here's some various wasm-bindgen resources:

https://rustwasm.github.io/docs/wasm-bindgen/reference/passing-rust-closures-to-js.html
https://rustwasm.github.io/docs/wasm-bindgen/reference/receiving-js-closures-in-rust.html

@jayphelps
Copy link

Keep in mind I was merely curious if this interop was supported out of box, for my future reference. At the moment I don't have a blocking requirement on it, as I'm not yet using as-bind.

@torch2424
Copy link
Owner Author

@jayphelps Ahhh, thanks for expanding on that! Yes, currently that is not supported. So I opened an issue at: #17

Though, I'll keep this issue open with it's original intent, as I still think it'd be good to show an example of this 😄

torch2424 added a commit that referenced this issue Jan 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants