emscripten-core / emscripten-core/emscripten

Undefined is returned from function implementing multiple fibers

Open
#12,270 3 comments 0 reactions 0 assignees View on GitHub
wontfix
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

Hello! I'm writing an API in C++ that I'm exporting as WASM/JS to my React application with Emscripten and Embind. My class exports what appears to be a synchronous API to the calling JavaScript, but inside the C++ it implements some basic context switching between two fibers. Using good ol' print debugging, I've found that the control logic is all correct and everything seems to function as expected, but my return values always come back as `undefined` when they reach the calling JavaScript. My class:

````
class MyClass {
public:
MyClass();
val myFunc(val myData);
private:
emscripten_fiber_t m_mainFiber;
// Stack for the fiber, dynamically allocated...
emscripten_fiber_t m_subFiber;
// Stacks for this fiber, dynamically allocated...
};

EMSCRIPTEN_BINDINGS(myModule) {
class_("MyClass")
.constructor<>()
.function("myFunc", &MyClass::myFunc);
}

MyClass::MyClass()
: // Do initialization for the fibers here
{}

val MyClass::myFunc(val myData) {
val output = val::array();
// Do some complicated stuff with myData that involves at least two calls to emscripten_fiber_swap, put it into output

// Even if I were to just return val::array() here, it always comes back as undefined when I console.log() from JavaScript.
// I've even tried changing the return type to int and just returning a random integer, e.g. 'return 65432;'--it prints as '0' in JavaScript.
return output;
````

Compiled with:

````
em++ --bind -Wall -Wextra -Os -s FILESYSTEM=0 -s ENVIRONMENT="web" -s MODULARIZE=1 -s ASYNCIFY=1 -std=c++17
````

And from JavaScript:

````
Module().then(module => {
const myInstance = new module.MyClass();
console.log(myInstance.myFunc(new Uint8Array([ ..My Data... ]));
});
````

It's occurred to me that I may be using the fibers API in a way not originally intended by the developers, but I feel like I've scoured the documentation, and I haven't seen anything that says one _can't_ do this. Any help would be greatly appreciated, and if I can clarify any points, please don't hesitate to ask. Thank you, in advance.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.