Skip to content

Compiler error on call_indirect/function pointer #2349

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

Closed
dsyer opened this issue Jun 30, 2022 · 5 comments · Fixed by #2382
Closed

Compiler error on call_indirect/function pointer #2349

dsyer opened this issue Jun 30, 2022 · 5 comments · Fixed by #2382

Comments

@dsyer
Copy link

dsyer commented Jun 30, 2022

> asc assembly/index.ts --target debug


▌ Whoops, the AssemblyScript compiler has crashed during buildJS :-(
▌ 
▌ Here is the stack trace hinting at the problem, perhaps it's useful?
▌ 
▌ /home/dsyer/dev/scratch/hello-as/node_modules/assemblyscript/std/portable/index.js:200
▌     throw new AssertionError(message);
▌           ^
▌ 
▌ AssertionError: assertion failed
▌     at null.Q.assert (/home/dsyer/dev/scratch/hello-as/node_modules/assemblyscript/std/portable/index.js:200:11)
▌     at er.makeLowerToValue (/home/dsyer/dev/scratch/hello-as/node_modules/assemblyscript/src/bindings/js.ts:1020:21)
▌     at er.visitFunction (/home/dsyer/dev/scratch/hello-as/node_modules/assemblyscript/src/bindings/js.ts:390:16)
▌     at er.visitFunctionInstances (/home/dsyer/dev/scratch/hello-as/node_modules/assemblyscript/src/bindings/util.ts:132:53)
▌     at er.visitElement (/home/dsyer/dev/scratch/hello-as/node_modules/assemblyscript/src/bindings/util.ts:91:14)
▌     at er.visitFile (/home/dsyer/dev/scratch/hello-as/node_modules/assemblyscript/src/bindings/util.ts:59:14)
▌     at er.walk (/home/dsyer/dev/scratch/hello-as/node_modules/assemblyscript/src/bindings/util.ts:47:65)
▌     at er.build (/home/dsyer/dev/scratch/hello-as/node_modules/assemblyscript/src/bindings/js.ts:616:10)
▌     at Function.build (/home/dsyer/dev/scratch/hello-as/node_modules/assemblyscript/src/bindings/js.ts:99:40)
▌     at Module.mS (/home/dsyer/dev/scratch/hello-as/node_modules/assemblyscript/src/index-wasm.ts:335:20)
▌ 
▌ If you see where the error is, feel free to send us a pull request. If not,
▌ please let us know: https://github.com/AssemblyScript/assemblyscript/issues
▌ 
▌ Thank you!

Simple program to reproduce:

@external("env", "console.log")
declare function print(s: i32): void

function increment(input: i32): i32 {
  return input + 1;
}

export function callback(fn: (input: i32) => i32, input: i32): i32 {
  return fn(input);
}

export function call(input: i32): i32 {
  return callback(increment, input);
}
@MaxGraey
Copy link
Member

Yeah. It looks like binding generator doesn't support functions as argument during codegen.

@MaxGraey
Copy link
Member

For now can you could just remove export for callback if this possible?

@dsyer
Copy link
Author

dsyer commented Jun 30, 2022

Yes, that works for the simple repro above. It's not possible in my more real use case unfortunately where actually it is the host runtime that has to call callback. Any other way for the host to invoke call_indirect? I know emscripten exposes the function table as __indirect_function_table for example. Anything like that here?

@MaxGraey
Copy link
Member

MaxGraey commented Jun 30, 2022

Well In this case, you can refactor you code to this:

function increment(input: i32): i32 {
  return input + 1;
}

export function callback(fn: i32, input: i32): i32 {
  return call_indirect(fn, input);
}

export function call(input: i32): i32 {
  return callback(increment.index, input);
}

add --importTable or --exportTable (it depends what you want to get or to set indexes manually from host) option to asc.

Example in playground

Or wait for fix this bindgen issue =)

@dsyer
Copy link
Author

dsyer commented Jun 30, 2022

Amazing. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants