Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/test/embind/embind_jspi_test.cpp
4150 views
1
// Copyright 2023 The Emscripten Authors. All rights reserved.
2
// Emscripten is available under two separate licenses, the MIT license and the
3
// University of Illinois/NCSA Open Source License. Both these licenses can be
4
// found in the LICENSE file.
5
6
#include <emscripten.h>
7
#include <emscripten/bind.h>
8
9
using namespace emscripten;
10
11
void voidFunc() { emscripten_sleep(0); }
12
13
int intFunc(int i) {
14
emscripten_sleep(0);
15
return i + 1;
16
}
17
18
class MyClass {
19
public:
20
MyClass() {}
21
22
void voidMethod() { emscripten_sleep(0); }
23
24
int intMethod(int i) {
25
emscripten_sleep(0);
26
return i + 1;
27
}
28
29
static void voidClass() { emscripten_sleep(0); }
30
31
static int intClass(int i) {
32
emscripten_sleep(0);
33
return i + 1;
34
}
35
};
36
37
int stdFunction(const MyClass& target, int i) {
38
emscripten_sleep(0);
39
return i + 1;
40
}
41
42
MyClass* asyncGetMyClass() {
43
emscripten_sleep(0);
44
return new MyClass();
45
}
46
47
EM_ASYNC_JS(void, jsSuspend, (), {
48
await new Promise(resolve => {
49
Module.unsuspendResolve = resolve;
50
});
51
});
52
53
void suspend() {
54
jsSuspend();
55
};
56
57
void unsuspend() {
58
EM_ASM({
59
Module.unsuspendResolve();
60
});
61
}
62
63
EMSCRIPTEN_BINDINGS(xxx) {
64
function("voidFunc", &voidFunc, async());
65
function("intFunc", &intFunc, async());
66
function("unsuspend", &unsuspend);
67
function("suspend", &suspend, async());
68
function("asyncGetMyClass", &asyncGetMyClass, allow_raw_pointers(), async());
69
70
class_<MyClass>("MyClass")
71
.constructor<>()
72
.function("voidMethod", &MyClass::voidMethod, async())
73
.function("intMethod", &MyClass::intMethod, async())
74
.function("stdMethod",
75
std::function<int(const MyClass&, int)>(&stdFunction),
76
async())
77
.function("lambdaMethod",
78
select_overload<int(MyClass&, int)>([](MyClass& self, int i) {
79
emscripten_sleep(0);
80
return i + 1;
81
}),
82
async())
83
.class_function("voidClass", &MyClass::voidClass, async())
84
.class_function("intClass", &MyClass::intClass, async());
85
}
86
87
EM_ASYNC_JS(void, test, (), {
88
async function check(promise, expected) {
89
assert(promise instanceof Promise);
90
var actual = await promise;
91
assert(actual === expected);
92
}
93
try {
94
await check(Module.intFunc(1), 2);
95
var myClass = new Module.MyClass();
96
await check(myClass.voidMethod());
97
await check(myClass.intMethod(1), 2);
98
await check(myClass.stdMethod(1), 2);
99
await check(myClass.lambdaMethod(1), 2);
100
await check(Module.MyClass.voidClass());
101
await check(Module.MyClass.intClass(1), 2);
102
var myClassAsync = await Module.asyncGetMyClass();
103
assert(myClassAsync instanceof Module.MyClass);
104
myClassAsync.delete();
105
106
setTimeout(Module.unsuspend);
107
await Module.suspend();
108
109
out('done');
110
} catch (e) {
111
out('Failed: ' + e.stack);
112
}
113
});
114
115
int main() { test(); }
116
117