Description
This is rather a question or feature request rather than an actual issue. I've made sure to thoroughly search for similar issues before both on GitHub and Google, but I have found no relevant results.
I'm generated on generating WebAssembly from C/C++ code and executing it in seamless manner, i.e. using just function pointers. Let me explain this with an example:
On native targets it is possible to JIT-compile code like this:
int main() {
const char code[] = {
0x48, 0x89, 0xF8, // mov rax, rdi
0x48, 0x01, 0xC0, // add rax, rax
0xC3 // ret
};
void* ptr = mmap(0, 0x1000, PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
memcpy(ptr, code, sizeof(code));
long (*function)(long) = (long(*)(long))(ptr);
function(7); // == 14
return 0;
}
Question is: Is there something similar we could use from C/C++ code when compiling to WebAssembly?
I'm thinking of something like this:
int main() {
const char code[] = { /* WASM bytecode of function */ };
#ifdef __EMSCRIPTEN__
void* ptr = emscripten_map_wasm(code, sizeof(code));
long (*function)(long) = (long(*)(long))(ptr);
function(7); // == 14
emscripten_unmap_wasm(ptr);
#endif // __EMSCRIPTEN__
return 0;
}
The reason I'm asking for this, is that the only binary translators that can be compiled with Emscripten are interpreters which are known for heavy performance penalties (ranging x20-x100). By having an interface to create WebAssembly functions and seamlessly execute them, we could add a WebAssembly backend to many binary translators and have near-native performance.
As a further example: This is already possible with JavaScript (i.e. eval
'ing strings) and some projects use that for performance reasons, e.g. see jslm32.
I wonder whether the WebAssembly specification is ready for such a feature. To be honest I'm not that familiar with it (maybe @kripken can comment about it?).