I'm quite new with WebAssembly, and I've been struggling on this for a bit now. What I'm trying to achieve is get a file on the server with javascript, get it's raw binary data, and pass it to a wasm module compiled from C.
I've already been able to use C-compiled wasm, and use simple functions (sum, pgcd) for testing.
This is what I have so far :
javascript file :
var importObject = {
imports: {
display_file: function() {},
sum_test: function() {},
}
};
// compile and store the object
function LoadWebAssembly(_fileName, _importObject) {
WebAssembly.instantiateStreaming(fetch(_fileName), _importObject)
.then(obj => {
console.log(obj.instance.exports.sum_test(12, 2));
fetch("data")
.then(response => response.arrayBuffer())
.then(buffer => {
console.log(obj.instance.exports.display_file(buffer, buffer.byteLength));
})
return obj;
});
}
// global for now
var dec_module = LoadWebAssembly("blob.wasm", importObject);
And C file :
#include <stdint.h>
#include <emscripten/emscripten.h>
// for now, let's try to pass a blob as an argument
EMSCRIPTEN_KEEPALIVE
int display_file(uint8_t* data, int size) {
int result = 0;
for(int i = 0; i < size; i++) {
result += data[i];
}
return result;
}
EMSCRIPTEN_KEEPALIVE
int sum_test(int a, int b) {
return a + b;
}
And "data" is a simple text file, not empty.
However, when running the code, I can see the "14" in the console, but then I get "0", which should be anything else right ?
Ideally, I would like a way to get raw binary data from javascript to wasm (here, I interpreted them as uint8 on the C side) but it doesn't seem to work.
Any help ? Thanks !