WebAssembly Runtime - WebGHC/wasm-cross GitHub Wiki
WebAssembly Runtime
Refer https://developer.mozilla.org/en-US/docs/WebAssembly for WebAssembly overview.
Execution environment
-
Main thread
This can do all the DOM operations.
The
jsaddle
JS code run on the main thread and it communicates with WebWorker thread using message passing with channel (https://developer.mozilla.org/en-US/docs/Web/API/Channel_Messaging_API/Using_channel_messaging) -
WebWorker thread.
The
wasm
code (kernel + haskell runtime) runs on a WebWorker thread. The functionality available in these threads are limited (https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Functions_and_classes_available_to_workers), therefore one cannot execute arbitrary JS in them. -
Communication between Wasm and JavaScript
-
Through syscall import/export APIs
The kernel APIs like
syscall
are implemented in thewebabi
JS code, and they are imported in the wasm code. Using thesesyscall
APIs all the communication between wasm and JS can be implemented by reading and writing to unix style devices.We have currently chosen this approach over importing arbitrary user-defined JS APIs.
-
Memory
The memory allocated to a WebAssembly executable is a linear array (with index starting from 0), which is accessible from both JavaScript and WebAssembly code.
The memory is just a plain continuous block of byte array, which has to be used by the runtime as either Heap or stack.
The memory block only grows, there is no concept of freeing the memory.
The pointer values are simply the indices of the array.
Accessing memory from JavaScript
// Example C
myfun (int n, int* iPtr, int** iSPtr) {
int i = 0;
for (; i < n; i++) {
int j = iPtr[i];
iSPtr[i][j] = i * j;
}
}
// JS
myfun (n, iPtr_, iSPtr_) {
var iPtr = iPtr_ / 4;
var iSPtr = iSPtr_ / 4;
for (var i = 0; i < n; i++) {
var j = heap_uint32[iPtr + i];
var ptr_ = heap_uint32[iSPtr + i];
var ptr = ptr_ / 4;
heap_uint32[ptr + j] = i * j;
}
}
Kernel
Overview of kernel Sys calls http://man7.org/linux/man-pages/dir_section_2.html
Input / Output
FileSystem
Concurrency
This looks promising https://github.com/lars-t-hansen/parlib-simple
https://github.com/kripken/emscripten/blob/incoming/src/pthread-main.js