Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

310
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda