'How to return a string from emscripten/webassembly function to a c caller

Trying to follow emscripten tutorial, passing parameters between C calls to emscripten, but only numbers are passed properly, not strings. How do I return a string back to C from js library call?

test.c:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <emscripten.h>
extern char *getText(void);

int main() {
    printf(getText());
    return 0;
}

mylib.js:

mergeInto(LibraryManager.library, {
    getText: function() {
        return "a test string";
    }
});

build command:

emcc test.c -o test.html --js-library mylib.js

The output I expect to see is "a test string", but what I actually see is "emcc"

I have looked far and wide on stackoverflow ans elsewhere, but there is nothing I could find about returning string values from JS to C, only about passing them to JS from C, so this is not a duplicate of any other question I could find.

What gives?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source