0

I'm trying to get this simple scenario working, but I can't understand how to get the data from Memory:

in hello.cpp:

void EMSCRIPTEN_KEEPALIVE modifyDOM(const char *text, const char *id) {
    printf("modify dom %s with %s\n", id, text);
    EM_ASM_({
        console.log($0);
        console.log($1);
        console.log(arguments);
        const elt = document.getElementById($0);
        elt.innerHTML = $1;
    }, id, text);
}

in hello.html:

document.querySelector('.addDom').addEventListener('click', function(){
    Module.ccall('modifyDOM', null, ['string', 'string'], ['Some fun text', 'textDom']);
});

What I get is that printf outputs correct values (i.e. modify dom textDom with Some fun text) but the console.log outputs numbers like 6736 and 6672.

I guess that's because inside Javascript world those values are stored like pointers in memory. If that's the case, what would be the best way to get those values as strings?

Filburt
  • 17,626
  • 12
  • 64
  • 115
x3derr8
  • 63
  • 7
  • 1
    Possible duplicate of [How can I return a JavaScript string from a Web Assembly function](https://stackoverflow.com/questions/41353389/how-can-i-return-a-javascript-string-from-a-web-assembly-function) – JF Bastien May 31 '17 at 14:16

1 Answers1

1

Yep, those are pointers. The Javascript glue file that emscripten generates provides you with a helper function to convert a pointer to a null-terminated ASCII-encoded string to a Javascript String object: Module.AsciiToString(ptr)

There is also UTF8ToString, UTF16ToString, and UTF32ToString.

Ghillie
  • 869
  • 5
  • 9