4 ms·
There is almost nothing to the FFI interface: Basically, the asm FFI can only have asm types, as you would expect. Then you can just call normal JS methods that
by azakai 12y ago
There is almost nothing to the FFI interface: Basically, the asm FFI can only have asm types, as you would expect. Then you can just call normal JS methods that were imported. So stuff like
function asmFunc(import) {
'use asm';
var externFunc = import.externFunc;
function asmFunc() {
externFunc(123, -0.5);
}
return asmFunc;
}
What extra info are you looking for?
- jvilk 12y ago> What extra info are you looking for? Essentially, a complete executable example of a simple ASM.js program that involves a foreign function, including processing return values. (I imagine if it's this simple, then return values are handled using the same annotations as ASM.js functions?) While I understand that specs don't necessarily have to have examples, I believe it would benefit the community to have one in the ASM.js spec. Thanks for clarifying here, though! I now understand.
- azakai 12y agoYeah, ffis are just calls like any other, with the usual annotations and types on parameters and return values, etc. They just happen to be to functions on the outside of the asm module. edit: btw, there is a full example here https://github.com/dherman/asm.js/#example https://github.com/dherman/asm.js/#example
- jvilk 12y agoAh, I wasn't aware there was an actual repo associated with ASM.js. The example doesn't show how you call into the module with `consoleDotLog` defined, though. I know that sounds like a triviality, but complete examples are really useful to cut through any possible ambiguity! I would definitely lobby to get something smaller and concise into the spec under the "Putting it All Together" heading! You could modify the existing example in the spec to include an external function definition and call.
- deleted 12y ago[deleted]
- jvilk 12y agoFollow-up question. How does it deduce the return type of a foreign function? Can foreign functions return values? Normally, ASM.js functions require an annotation on the return statement [0]. [0] http://asmjs.org/spec/latest/#return-type-annotations http://asmjs.org/spec/latest/#return-type-annotations
- azakai 12y agoFFI calls are just like normal calls: they need a coercion on them. i = ffiFunc() | 0; // returns an int d = +ffiFunc(); // returns a double This is identical for FFI and non-FFI calls. A difference though is that asm functions have specific types (an asm function returns either nothing, an int, or a double). But an FFI is just any old JS function. So you could not write the above 2 lines together with an asm function, only an FFI can get an int one time and a double another time from the same function.
- jvilk 12y agoUnderstood. If `ffiFunc()` returns an empty array at a double call site, does ASM.js just accept it as `0`? (`+[]` is `0`). Or are there runtime checks that verify that the return value is a numeric type? btw, I hope these questions make it clearer why I grumped a bit about the specification, and result in a more robust spec!