Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

309
Views
JS : fetch file and pass raw binary data to wasm

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 !

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!