function asmFunc(import) {
'use asm';
var externFunc = import.externFunc;
function asmFunc() {
externFunc(123, -0.5);
}
return asmFunc;
}
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.
edit: btw, there is a full example here https://github.com/dherman/asm.js/#example
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.
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].
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.btw, I hope these questions make it clearer why I grumped a bit about the specification, and result in a more robust spec!
See "Putting It All Together" http://asmjs.org/spec/latest/
From that example, the list of return types from operators (section 8), and the graph of value types (section 2.1), it is simple to figure out, if not a little foreign at first.
Some other FFIs require you to encode type information in some manner to uphold the guarantees of the language. It's not clear from scanning the specification if that is the case here, as the FFI is only briefly mentioned in passing.
Finally, ASM.js doesn't validate the return expression of external functions (it does for ASM.js code [0]). How does it deal with foreign functions that return values? What type does the return value take? Or does there need to be an annotation at the call site?
Note that the FFI is a crucial component of ASM.js, as everything in the browser is external to ASM.js (DOM/any strings/any objects/etc). It seems weird that there's so much hype around ASM.js, yet there's very little written about the FFI or how to use it. Any project that uses ASM.js is going to need to interact with it.
Also, are there any projects besides Emscripten that use ASM.js, or is the push to put ASM.js into these browsers due purely to Emscripten?