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

feature request: pass arbitrary functions (not just exports) from AS to JS, or vice versa. #64

Open
trusktr opened this issue Feb 15, 2021 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@trusktr
Copy link

trusktr commented Feb 15, 2021

Use case:

We want to make, for example, a requestAnimationFrame function and pass that into Wasm via the imports object.

The AssemblyScript user should be able to dynamically create and pass any callback to this function.

So on the JavaScript side, it would be something like:

const imports = {
  requestAnimationFrame(assemblyscriptFunction) {
    // call assemblyscriptFunction at some point, f.e.
    requestAnimationFrame(assemblyscriptFunction)
  }
}

Problem is, at the moment, assemblyscriptFunction is an AssemblyScript pointer number. So the above won't work.

I'm not sure how to get a function using its pointer and call it, but once we know how to do that, then we can abstract it in as-bind.


Similarly, although maybe less useful, we should be able to pass JS functions into an AS function going the other way.

@trusktr trusktr changed the title feature request: pass arbitrary functions (not just exports) from AS to JS. feature request: pass arbitrary functions (not just exports) from AS to JS, or vice versa. Feb 15, 2021
@torch2424 torch2424 self-assigned this Feb 16, 2021
@torch2424 torch2424 added the enhancement New feature or request label Feb 16, 2021
@mathe42
Copy link
Contributor

mathe42 commented Sep 29, 2021

Got a similar example kind of working:

In Website:

import * as AsBind from "/node_modules/as-bind/dist/as-bind.esm.js";

const asyncTask = async () => {
  const wasm = await fetch("/build/optimized.wasm").then(v => v.arrayBuffer());

  const asBindInstance = await AsBind.instantiate(wasm);

  let e = asBindInstance.exports
  console.log(e.getA()) // logs 0
  e.call(e.change())
  console.log(e.getA()) // logs 42


};
asyncTask();

Assemblyscript:

let a: u8 = 0;

export function getA(): u8 {
  return a;
}

export function change(): () => void {
  return () => {
    a = 42;
  };
}

export function call(ptr: usize): void {
  const fn = changetype<() => void>(ptr);

  return fn();
}

For this use case we would only have to inject a call function to assemblyscript. Currently not shure with function arguments... I will test some stuff.

@mathe42
Copy link
Contributor

mathe42 commented Sep 29, 2021

Solution inject for each set of argumenttypes and return type.

@mathe42
Copy link
Contributor

mathe42 commented Sep 29, 2021

I will have a deeper look into it (with modifications to the transform) when #100 is merged. (I will also look at classes then so we can get also all of stdLib to be handled properly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants