I'm quite new to combining C and JavaScript and I'm having trouble seeing how I can possibly access a string in C (on the WASM heap) made as a result of a promise. Here's the function I have so far:
EM_JS(void, em_enumerate_av_devices, (),
{
navigator.mediaDevices.enumerateDevices()
.then(function(devices) {
var str="";
devices.forEach( function(device) { str += device.kind + " \"" + device.label + "\" id = " + device.deviceId + "\n"; });
var c_str = js_string_to_c_string(str); // this is the C string on the WASM heap I'm trying to access in C, I don't know what to do with it
})
.catch(function(err) { console.log(
err.name + ": " + err.message); });
});
I need c_str to somehow be accessed from my C code, but how?
Another smaller problem is the function it relies on, js_string_to_c_string(), which is as follows:
function js_string_to_c_string(js_string)
{
var len = lengthBytesUTF8(js_string) + 1;
var c_string = _malloc(len);
stringToUTF8(js_string, c_string, len);
return c_string;
}
I tried putting the whole thing inside an EM_ASM block, but the compiler didn't like that and I haven't seen anyone else use EM_ASM for entire functions. It would make sense to put such utility functions in one .js file, but I can't see how to make emcc include that .js file anywhere (ideally it would include the whole thing into myproject.js like it already does with required files such as runtime_safe_heap.js and others).
Thanks in advance,
Michel Rouzic