Skip to content

Commit 18badf3

Browse files
authored
Fix craftInvokerFunction on JSPI (ASYNCIFY=2) (#19281)
* Fix craftInvokerFunction on JSPI (ASYNCIFY=2) When the function wrapped by craftInvokerFunction doesn't return, rv is undeclared, which results in an exception being thrown at crafted function runtime.
1 parent e33ed5b commit 18badf3

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/embind/embind.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1129,7 +1129,7 @@ var LibraryEmbind = {
11291129
invokerFnBody += "return Asyncify.currData ? Asyncify.whenDone().then(onDone) : onDone(" + (returns ? "rv" : "") +");\n"
11301130
#elif ASYNCIFY == 2
11311131
invokerFnBody += "}\n";
1132-
invokerFnBody += "return " + (isAsync ? "rv.then(onDone)" : "onDone(rv)") + ";";
1132+
invokerFnBody += "return " + (isAsync ? "rv.then(onDone)" : "onDone(" + (returns ? "rv" : "") + ")") + ";";
11331133
#endif
11341134

11351135
invokerFnBody += "}\n";

test/embind/embind_jspi_test.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,27 @@ int stdFunction(const MyClass& target, int i) {
3939
return i + 1;
4040
}
4141

42+
EM_ASYNC_JS(void, jsSuspend, (), {
43+
await new Promise(resolve => {
44+
Module.unsuspendResolve = resolve;
45+
});
46+
});
47+
48+
void suspend() {
49+
jsSuspend();
50+
};
51+
52+
void unsuspend() {
53+
EM_ASM({
54+
Module.unsuspendResolve();
55+
});
56+
}
57+
4258
EMSCRIPTEN_BINDINGS(xxx) {
4359
function("voidFunc", &voidFunc, async());
4460
function("intFunc", &intFunc, async());
61+
function("unsuspend", &unsuspend);
62+
function("suspend", &suspend, async());
4563

4664
class_<MyClass>("MyClass")
4765
.constructor<>()
@@ -76,6 +94,9 @@ EM_ASYNC_JS(void, test, (), {
7694
await check(Module.MyClass.voidClass());
7795
await check(Module.MyClass.intClass(1), 2);
7896

97+
setTimeout(Module.unsuspend);
98+
await Module.suspend();
99+
79100
console.log('done');
80101
} catch (e) {
81102
console.log('Failed: ' + e.stack);

0 commit comments

Comments
 (0)