File: embind_tsgen.cpp

package info (click to toggle)
emscripten 3.1.69%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 121,860 kB
  • sloc: ansic: 636,110; cpp: 425,974; javascript: 78,401; python: 58,404; sh: 49,154; pascal: 5,237; makefile: 3,366; asm: 2,415; lisp: 1,869
file content (255 lines) | stat: -rw-r--r-- 6,687 bytes parent folder | download | duplicates (2)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include <stdio.h>
#include <memory>
#include <string>
#include <optional>
#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; }

  long long_fn(unsigned long a) { return 3; }

  int const_fn() const { return 0; }

  int getX() const { return x; }
  void setX(int x_) { x = x_; }

  int getY() const { return y; }

  std::string string_property;

  static int static_function(int x) { return 1; }

  static int static_property;
  static std::string static_string_property;

private:
  int x;
  int y;
};

class Foo {
 public:
  void process(const Test& input) {}
};

Test class_returning_fn() { return Test(); }

std::unique_ptr<Test> class_unique_ptr_returning_fn() {
  return std::make_unique<Test>();
}

enum Bar { kValueOne, kValueTwo, kValueThree };

enum EmptyEnum {};

Bar enum_returning_fn() { return kValueOne; }

struct ValArr {
  int x, y, z;
};

EMSCRIPTEN_DECLARE_VAL_TYPE(CallbackType);

struct ValObj {
  Foo foo;
  Bar bar;
  CallbackType callback;
  ValObj() : callback(val::undefined()) {}
};

class ClassWithConstructor {
 public:
  ClassWithConstructor(int, const ValArr&) {}

  int fn(int x) { return 0; }
};

class ClassWithTwoConstructors {
 public:
  ClassWithTwoConstructors() {}
  ClassWithTwoConstructors(int) {}
};

class ClassWithSmartPtrConstructor {
 public:
  ClassWithSmartPtrConstructor(int, const ValArr&) {}

  int fn(int x) { return 0; }
};

int smart_ptr_function(std::shared_ptr<ClassWithSmartPtrConstructor>) {
  return 0;
}

struct Obj {};
Obj* get_pointer(Obj* ptr) { return ptr; }
Obj* get_nonnull_pointer() { return new Obj(); }

int function_with_callback_param(CallbackType ct) {
  ct(val("hello"));
  return 0;
}

int global_fn(int, int) { return 0; }

std::string string_test(std::string arg) {
  return "hi";
}

std::wstring wstring_test(std::wstring arg) {
  return L"hi";
}

std::optional<int> optional_test(std::optional<Foo> arg) {
  return {};
}

std::optional<int> optional_and_nonoptional_test(std::optional<Foo> arg1, int arg2) {
  return {};
}

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; }
};

struct Interface {
  virtual void invoke(const std::string& str) = 0;
  virtual ~Interface() {}
};

struct InterfaceWrapper : public wrapper<Interface> {
  EMSCRIPTEN_WRAPPER(InterfaceWrapper);
  void invoke(const std::string& str) {
      return call<void>("invoke", str);
  }
};

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("longFn", &Test::long_fn)
      .function("constFn", &Test::const_fn)
      .property("x", &Test::getX, &Test::setX)
      .property("y", &Test::getY)
      .property("stringProperty", &Test::string_property)
      .class_function("staticFunction", &Test::static_function)
      .class_function("staticFunctionWithParam(x)", &Test::static_function)
      .class_property("staticProperty", &Test::static_property)
      .class_property("staticStringProperty", &Test::static_string_property)
	;

  function("class_returning_fn", &class_returning_fn);
  function("class_unique_ptr_returning_fn",
                   &class_unique_ptr_returning_fn);
  class_<Obj>("Obj");
  function("getPointer", &get_pointer, allow_raw_pointers());
  function("getNonnullPointer", &get_nonnull_pointer, allow_raw_pointers(), nonnull<ret_val>());

  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);
  enum_<EmptyEnum>("EmptyEnum");

  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)
      .field("callback", &ValObj::callback);

  register_vector<int>("IntVec");

  register_map<int, int>("MapIntInt");

  class_<Foo>("Foo").function("process", &Foo::process);

  function("global_fn", &global_fn);

  register_optional<int>();
  register_optional<Foo>();
  function("optional_test", &optional_test);
  function("optional_and_nonoptional_test", &optional_and_nonoptional_test);

  function("string_test", &string_test);
  function("wstring_test", &wstring_test);

  class_<ClassWithConstructor>("ClassWithConstructor")
      .constructor<int, const ValArr&>()
      .function("fn", &ClassWithConstructor::fn);

  // The last defined constructor should be used in the definition.
  class_<ClassWithTwoConstructors>("ClassWithTwoConstructors")
      .constructor<>()
      .constructor<int>();

  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);

  function("function_with_callback_param",
           &function_with_callback_param);

  register_type<CallbackType>("(message: string) => void");

  class_<BaseClass>("BaseClass").function("fn", &BaseClass::fn);

  class_<DerivedClass, base<BaseClass>>("DerivedClass")
      .function("fn2", &DerivedClass::fn2);

  class_<Interface>("Interface")
    .function("invoke", &Interface::invoke, pure_virtual())
    .allow_subclass<InterfaceWrapper>("InterfaceWrapper")
    ;
}

int Test::static_property = 42;
std::string Test::static_string_property = "";

int main() {
  // Main should not be run during TypeScript generation, but should run when
  // the program is run normally.
  printf("main ran\n");
  return 0;
}