forked from emscripten-core/emscripten
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathembind_tsgen.cpp
More file actions
164 lines (123 loc) · 4.06 KB
/
embind_tsgen.cpp
File metadata and controls
164 lines (123 loc) · 4.06 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <memory>
#include <string>
#include <emscripten/bind.h>
using namespace emscripten;
class Test {
public:
int function_one(char x, int y) { return 42; }
int function_two(unsigned char x, int y) { return 43; }
int function_three(const std::string&) { return 1; }
int function_four(bool x) { return 2; }
int const_fn() const { return 0; }
int getX() const { return x; }
void setX(int x_) { x = x_; }
int getY() const { return y; }
static int static_function(int x) { return 1; }
static int static_property;
private:
int x;
int y;
};
Test class_returning_fn() { return Test(); }
std::unique_ptr<Test> class_unique_ptr_returning_fn() {
return std::make_unique<Test>();
}
class Foo {
public:
void process(const Test& input) {}
};
enum Bar { kValueOne, kValueTwo, kValueThree };
Bar enum_returning_fn() { return kValueOne; }
struct ValArr {
int x, y, z;
};
struct ValObj {
Foo foo;
Bar bar;
};
class ClassWithConstructor {
public:
ClassWithConstructor(int, const ValArr&) {}
int fn(int x) { return 0; }
};
class ClassWithSmartPtrConstructor {
public:
ClassWithSmartPtrConstructor(int, const ValArr&) {}
int fn(int x) { return 0; }
};
int smart_ptr_function(std::shared_ptr<ClassWithSmartPtrConstructor>) {
return 0;
}
int global_fn(int, int) { return 0; }
class BaseClass {
public:
virtual ~BaseClass() = default;
virtual int fn(int x) { return 0; }
};
class DerivedClass : public BaseClass {
public:
int fn(int x) override { return 1; }
int fn2(int x) { return 2; }
};
EMSCRIPTEN_BINDINGS(Test) {
class_<Test>("Test")
.function("functionOne", &Test::function_one)
.function("functionTwo", &Test::function_two)
.function("functionThree", &Test::function_three)
.function("functionFour", &Test::function_four)
.function("functionFive(x, y)", &Test::function_one)
.function("functionSix(str)", &Test::function_three)
.function("constFn", &Test::const_fn)
.property("x", &Test::getX, &Test::setX)
.property("y", &Test::getY)
.class_function("staticFunction", &Test::static_function)
.class_function("staticFunctionWithParam(x)", &Test::static_function)
.class_property("staticProperty", &Test::static_property)
;
function("class_returning_fn", &class_returning_fn);
function("class_unique_ptr_returning_fn",
&class_unique_ptr_returning_fn);
constant("an_int", 5);
constant("a_bool", false);
constant("an_enum", Bar::kValueOne);
constant("a_class_instance", Test());
enum_<Bar>("Bar")
.value("valueOne", Bar::kValueOne)
.value("valueTwo", Bar::kValueTwo)
.value("valueThree", Bar::kValueThree);
function("enum_returning_fn", &enum_returning_fn);
value_array<ValArr>("ValArr")
.element(&ValArr::x)
.element(&ValArr::y)
.element(&ValArr::z);
value_array<std::array<Bar, 4>>("ValArrIx")
.element(emscripten::index<0>())
.element(emscripten::index<1>())
.element(emscripten::index<2>())
.element(emscripten::index<3>());
value_object<ValObj>("ValObj")
.field("foo", &ValObj::foo)
.field("bar", &ValObj::bar);
register_vector<int>("IntVec");
class_<Foo>("Foo").function("process", &Foo::process);
function("global_fn", &global_fn);
class_<ClassWithConstructor>("ClassWithConstructor")
.constructor<int, const ValArr&>()
.function("fn", &ClassWithConstructor::fn);
class_<ClassWithSmartPtrConstructor>("ClassWithSmartPtrConstructor")
.smart_ptr_constructor(
"ClassWithSmartPtrConstructor",
&std::make_shared<ClassWithSmartPtrConstructor, int, const ValArr&>)
.function("fn", &ClassWithSmartPtrConstructor::fn);
function("smart_ptr_function", &smart_ptr_function);
function("smart_ptr_function_with_params(foo)", &smart_ptr_function);
class_<BaseClass>("BaseClass").function("fn", &BaseClass::fn);
class_<DerivedClass, base<BaseClass>>("DerivedClass")
.function("fn2", &DerivedClass::fn2);
}
int Test::static_property = 42;
int main() {
// Main should not be run during TypeScript generation.
abort();
return 0;
}