forked from emscripten-core/emscripten
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathembind_jspi_test.cpp
More file actions
85 lines (70 loc) · 2.16 KB
/
embind_jspi_test.cpp
File metadata and controls
85 lines (70 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright 2023 The Emscripten Authors. All rights reserved.
// Emscripten is available under two separate licenses, the MIT license and the
// University of Illinois/NCSA Open Source License. Both these licenses can be
// found in the LICENSE file.
#include <emscripten.h>
#include <emscripten/bind.h>
using namespace emscripten;
void voidFunc() { emscripten_sleep(0); }
int intFunc(int i) {
emscripten_sleep(0);
return i + 1;
}
class MyClass {
public:
MyClass() {}
void voidMethod() { emscripten_sleep(0); }
int intMethod(int i) {
emscripten_sleep(0);
return i + 1;
}
static void voidClass() { emscripten_sleep(0); }
static int intClass(int i) {
emscripten_sleep(0);
return i + 1;
}
};
int stdFunction(const MyClass& target, int i) {
emscripten_sleep(0);
return i + 1;
}
EMSCRIPTEN_BINDINGS(xxx) {
function("voidFunc", &voidFunc, async());
function("intFunc", &intFunc, async());
class_<MyClass>("MyClass")
.constructor<>()
.function("voidMethod", &MyClass::voidMethod, async())
.function("intMethod", &MyClass::intMethod, async())
.function("stdMethod",
std::function<int(const MyClass&, int)>(&stdFunction),
async())
.function("lambdaMethod",
select_overload<int(MyClass&, int)>([](MyClass& self, int i) {
emscripten_sleep(0);
return i + 1;
}),
async())
.class_function("voidClass", &MyClass::voidClass, async())
.class_function("intClass", &MyClass::intClass, async());
}
EM_ASYNC_JS(void, test, (), {
async function check(promise, expected) {
assert(promise instanceof Promise);
var actual = await promise;
assert(actual === expected);
}
try {
await check(Module.intFunc(1), 2);
var myClass = new Module.MyClass();
await check(myClass.voidMethod());
await check(myClass.intMethod(1), 2);
await check(myClass.stdMethod(1), 2);
await check(myClass.lambdaMethod(1), 2);
await check(Module.MyClass.voidClass());
await check(Module.MyClass.intClass(1), 2);
console.log('done');
} catch (e) {
console.log('Failed: ' + e.stack);
}
});
int main() { test(); }