ElCapitaine
ElCapitaine

Reputation: 850

How to pass a string to C code compiled with emscripten for WebAssembly

I've been looking a the WebAssembly website and tutorials and I feel a bit lost.

I have the following C code :

void EMSCRIPTEN_KEEPALIVE hello(char * value){
    printf("%s\n", value);
}

I compiled it with (I'm also not sure this part is the best way to go) :

emcc demo.c -s WASM=1 -s NO_EXIT_RUNTIME=1 -o demo.js

From what I understand I can now use the demo.js glue code in my javascript class and call the method that way :

...
<script src="demo.js"></script>
<script>
    function hello(){        
        // Get the value 
        var value = document.getElementById("sample");
        _hello(value.innerHTML);
    }
</script>
...

What I see being printed in the console when I call the method is :

(null)

Is there something I'm missing to pass a string value to C code compiled with WebAssembly ?

Thanks a lot

Upvotes: 6

Views: 8418

Answers (1)

ElCapitaine
ElCapitaine

Reputation: 850

I actually found an answer to my question. I simply had to use the functions that Emscripten builds automatically within the 'Glue' code that's also generated when you build your C++ code to WASM.

So basically, to pass a String to C++ code compiled to WebAssembly with Emscripten you simply do it like this :

// Create a pointer using the 'Glue' method and the String value
var ptr  = allocate(intArrayFromString(myStrValue), 'i8', ALLOC_NORMAL);

// Call the method passing the pointer
val retPtr = _hello(ptr);

// Retransform back your pointer to string using 'Glue' method
var resValue = Pointer_stringify(retPtr);

// Free the memory allocated by 'allocate' 
_free(ptr);   

More complete information on Emscripten's page.

Upvotes: 16

Related Questions