@crossbind/core-embind-napi 0.0.0-bootstrap.0 → 2.0.0-beta.62

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (27) hide show
  1. package/README.md +41 -3
  2. package/cpp/CMakeLists.txt +69 -0
  3. package/cpp/check-undefined.cmake +18 -0
  4. package/cpp/src/node_api_module.cpp +153 -0
  5. package/js/loader.js +70 -0
  6. package/package.json +29 -6
  7. package/third_party/node-api-jsi/LICENSE +21 -0
  8. package/third_party/node-api-jsi/README.md +55 -0
  9. package/third_party/node-api-jsi/jsi/jsi/instrumentation.h +150 -0
  10. package/third_party/node-api-jsi/jsi/jsi/jsi-inl.h +425 -0
  11. package/third_party/node-api-jsi/jsi/jsi/jsi.cpp +990 -0
  12. package/third_party/node-api-jsi/jsi/jsi/jsi.h +2031 -0
  13. package/third_party/node-api-jsi/node-api/js_native_api.h +627 -0
  14. package/third_party/node-api-jsi/node-api/js_native_api_types.h +211 -0
  15. package/third_party/node-api-jsi/node-api/js_runtime_api.h +214 -0
  16. package/third_party/node-api-jsi/node-api/node_api.h +270 -0
  17. package/third_party/node-api-jsi/node-api/node_api_types.h +52 -0
  18. package/third_party/node-api-jsi/src/ApiLoaders/JSRuntimeApi.cpp +79 -0
  19. package/third_party/node-api-jsi/src/ApiLoaders/JSRuntimeApi.h +51 -0
  20. package/third_party/node-api-jsi/src/ApiLoaders/JSRuntimeApi.inc +50 -0
  21. package/third_party/node-api-jsi/src/ApiLoaders/NodeApi.cpp +41 -0
  22. package/third_party/node-api-jsi/src/ApiLoaders/NodeApi.h +127 -0
  23. package/third_party/node-api-jsi/src/ApiLoaders/NodeApi.inc +125 -0
  24. package/third_party/node-api-jsi/src/ApiLoaders/NodeApi_posix.cpp +16 -0
  25. package/third_party/node-api-jsi/src/ApiLoaders/NodeApi_win.cpp +23 -0
  26. package/third_party/node-api-jsi/src/NodeApiJsiRuntime.cpp +3861 -0
  27. package/third_party/node-api-jsi/src/NodeApiJsiRuntime.h +38 -0
@@ -0,0 +1,425 @@
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ #pragma once
9
+
10
+ namespace facebook {
11
+ namespace jsi {
12
+ namespace detail {
13
+
14
+ inline Value toValue(Runtime&, std::nullptr_t) {
15
+ return Value::null();
16
+ }
17
+ inline Value toValue(Runtime&, bool b) {
18
+ return Value(b);
19
+ }
20
+ inline Value toValue(Runtime&, double d) {
21
+ return Value(d);
22
+ }
23
+ inline Value toValue(Runtime&, float f) {
24
+ return Value(static_cast<double>(f));
25
+ }
26
+ inline Value toValue(Runtime&, int i) {
27
+ return Value(i);
28
+ }
29
+ inline Value toValue(Runtime& runtime, const char* str) {
30
+ return String::createFromAscii(runtime, str);
31
+ }
32
+ inline Value toValue(Runtime& runtime, const std::string& str) {
33
+ return String::createFromUtf8(runtime, str);
34
+ }
35
+ template <typename T>
36
+ inline Value toValue(Runtime& runtime, const T& other) {
37
+ static_assert(
38
+ std::is_base_of<Pointer, T>::value,
39
+ "This type cannot be converted to Value");
40
+ return Value(runtime, other);
41
+ }
42
+ inline Value toValue(Runtime& runtime, const Value& value) {
43
+ return Value(runtime, value);
44
+ }
45
+ inline Value&& toValue(Runtime&, Value&& value) {
46
+ return std::move(value);
47
+ }
48
+
49
+ inline PropNameID toPropNameID(Runtime& runtime, const char* name) {
50
+ return PropNameID::forAscii(runtime, name);
51
+ }
52
+ inline PropNameID toPropNameID(Runtime& runtime, const std::string& name) {
53
+ return PropNameID::forUtf8(runtime, name);
54
+ }
55
+ inline PropNameID&& toPropNameID(Runtime&, PropNameID&& name) {
56
+ return std::move(name);
57
+ }
58
+
59
+ /// Helper to throw while still compiling with exceptions turned off.
60
+ template <typename E, typename... Args>
61
+ [[noreturn]] inline void throwOrDie(Args&&... args) {
62
+ std::rethrow_exception(
63
+ std::make_exception_ptr(E{std::forward<Args>(args)...}));
64
+ }
65
+
66
+ } // namespace detail
67
+
68
+ template <typename T>
69
+ inline T Runtime::make(Runtime::PointerValue* pv) {
70
+ return T(pv);
71
+ }
72
+
73
+ #if JSI_VERSION >= 3
74
+ inline Runtime::PointerValue* Runtime::getPointerValue(jsi::Pointer& pointer) {
75
+ return pointer.ptr_;
76
+ }
77
+ #endif
78
+
79
+ inline const Runtime::PointerValue* Runtime::getPointerValue(
80
+ const jsi::Pointer& pointer) {
81
+ return pointer.ptr_;
82
+ }
83
+
84
+ inline const Runtime::PointerValue* Runtime::getPointerValue(
85
+ const jsi::Value& value) {
86
+ return value.data_.pointer.ptr_;
87
+ }
88
+
89
+ #if JSI_VERSION >= 20
90
+ inline void Runtime::setRuntimeData(
91
+ const UUID& uuid,
92
+ const std::shared_ptr<void>& data) {
93
+ auto* dataPtr = new std::shared_ptr<void>(data);
94
+ setRuntimeDataImpl(uuid, dataPtr, [](const void* data) {
95
+ delete (const std::shared_ptr<void>*)data;
96
+ });
97
+ }
98
+
99
+ inline std::shared_ptr<void> Runtime::getRuntimeData(const UUID& uuid) {
100
+ auto* data = (const std::shared_ptr<void>*)getRuntimeDataImpl(uuid);
101
+ return data ? *data : nullptr;
102
+ }
103
+ #endif
104
+
105
+ #if JSI_VERSION >= 17
106
+ Value Object::getPrototype(Runtime& runtime) const {
107
+ return runtime.getPrototypeOf(*this);
108
+ }
109
+ #endif
110
+
111
+ inline Value Object::getProperty(Runtime& runtime, const char* name) const {
112
+ return getProperty(runtime, String::createFromAscii(runtime, name));
113
+ }
114
+
115
+ inline Value Object::getProperty(Runtime& runtime, const String& name) const {
116
+ return runtime.getProperty(*this, name);
117
+ }
118
+
119
+ inline Value Object::getProperty(Runtime& runtime, const PropNameID& name)
120
+ const {
121
+ return runtime.getProperty(*this, name);
122
+ }
123
+
124
+ #if JSI_VERSION >= 21
125
+ inline Value Object::getProperty(Runtime& runtime, const Value& name) const {
126
+ return runtime.getProperty(*this, name);
127
+ }
128
+ #endif
129
+
130
+ inline bool Object::hasProperty(Runtime& runtime, const char* name) const {
131
+ return hasProperty(runtime, String::createFromAscii(runtime, name));
132
+ }
133
+
134
+ inline bool Object::hasProperty(Runtime& runtime, const String& name) const {
135
+ return runtime.hasProperty(*this, name);
136
+ }
137
+
138
+ inline bool Object::hasProperty(Runtime& runtime, const PropNameID& name)
139
+ const {
140
+ return runtime.hasProperty(*this, name);
141
+ }
142
+
143
+ #if JSI_VERSION >= 21
144
+ inline bool Object::hasProperty(Runtime& runtime, const Value& name) const {
145
+ return runtime.hasProperty(*this, name);
146
+ }
147
+ #endif
148
+
149
+ template <typename T>
150
+ void Object::setProperty(Runtime& runtime, const char* name, T&& value)
151
+ JSI_CONST_10 {
152
+ setProperty(
153
+ runtime, String::createFromAscii(runtime, name), std::forward<T>(value));
154
+ }
155
+
156
+ template <typename T>
157
+ void Object::setProperty(Runtime& runtime, const String& name, T&& value)
158
+ JSI_CONST_10 {
159
+ setPropertyValue(
160
+ runtime, name, detail::toValue(runtime, std::forward<T>(value)));
161
+ }
162
+
163
+ template <typename T>
164
+ void Object::setProperty(Runtime& runtime, const PropNameID& name, T&& value)
165
+ JSI_CONST_10 {
166
+ setPropertyValue(
167
+ runtime, name, detail::toValue(runtime, std::forward<T>(value)));
168
+ }
169
+
170
+ #if JSI_VERSION >= 21
171
+ template <typename T>
172
+ void Object::setProperty(Runtime& runtime, const Value& name, T&& value) const {
173
+ setPropertyValue(
174
+ runtime, name, detail::toValue(runtime, std::forward<T>(value)));
175
+ }
176
+
177
+ inline void Object::deleteProperty(Runtime& runtime, const char* name) const {
178
+ deleteProperty(runtime, String::createFromAscii(runtime, name));
179
+ }
180
+
181
+ inline void Object::deleteProperty(Runtime& runtime, const String& name) const {
182
+ runtime.deleteProperty(*this, name);
183
+ }
184
+
185
+ inline void Object::deleteProperty(Runtime& runtime, const PropNameID& name)
186
+ const {
187
+ runtime.deleteProperty(*this, name);
188
+ }
189
+
190
+ inline void Object::deleteProperty(Runtime& runtime, const Value& name) const {
191
+ runtime.deleteProperty(*this, name);
192
+ }
193
+ #endif
194
+
195
+ inline Array Object::getArray(Runtime& runtime) const& {
196
+ assert(runtime.isArray(*this));
197
+ (void)runtime; // when assert is disabled we need to mark this as used
198
+ return Array(runtime.cloneObject(ptr_));
199
+ }
200
+
201
+ inline Array Object::getArray(Runtime& runtime) && {
202
+ assert(runtime.isArray(*this));
203
+ (void)runtime; // when assert is disabled we need to mark this as used
204
+ Runtime::PointerValue* value = ptr_;
205
+ ptr_ = nullptr;
206
+ return Array(value);
207
+ }
208
+
209
+ inline ArrayBuffer Object::getArrayBuffer(Runtime& runtime) const& {
210
+ assert(runtime.isArrayBuffer(*this));
211
+ (void)runtime; // when assert is disabled we need to mark this as used
212
+ return ArrayBuffer(runtime.cloneObject(ptr_));
213
+ }
214
+
215
+ inline ArrayBuffer Object::getArrayBuffer(Runtime& runtime) && {
216
+ assert(runtime.isArrayBuffer(*this));
217
+ (void)runtime; // when assert is disabled we need to mark this as used
218
+ Runtime::PointerValue* value = ptr_;
219
+ ptr_ = nullptr;
220
+ return ArrayBuffer(value);
221
+ }
222
+
223
+ inline Function Object::getFunction(Runtime& runtime) const& {
224
+ assert(runtime.isFunction(*this));
225
+ return Function(runtime.cloneObject(ptr_));
226
+ }
227
+
228
+ inline Function Object::getFunction(Runtime& runtime) && {
229
+ assert(runtime.isFunction(*this));
230
+ (void)runtime; // when assert is disabled we need to mark this as used
231
+ Runtime::PointerValue* value = ptr_;
232
+ ptr_ = nullptr;
233
+ return Function(value);
234
+ }
235
+
236
+ template <typename T>
237
+ inline bool Object::isHostObject(Runtime& runtime) const {
238
+ return runtime.isHostObject(*this) &&
239
+ std::dynamic_pointer_cast<T>(runtime.getHostObject(*this));
240
+ }
241
+
242
+ template <>
243
+ inline bool Object::isHostObject<HostObject>(Runtime& runtime) const {
244
+ return runtime.isHostObject(*this);
245
+ }
246
+
247
+ template <typename T>
248
+ inline std::shared_ptr<T> Object::getHostObject(Runtime& runtime) const {
249
+ assert(isHostObject<T>(runtime));
250
+ return std::static_pointer_cast<T>(runtime.getHostObject(*this));
251
+ }
252
+
253
+ template <typename T>
254
+ inline std::shared_ptr<T> Object::asHostObject(Runtime& runtime) const {
255
+ if (!isHostObject<T>(runtime)) {
256
+ detail::throwOrDie<JSINativeException>(
257
+ "Object is not a HostObject of desired type");
258
+ }
259
+ return std::static_pointer_cast<T>(runtime.getHostObject(*this));
260
+ }
261
+
262
+ template <>
263
+ inline std::shared_ptr<HostObject> Object::getHostObject<HostObject>(
264
+ Runtime& runtime) const {
265
+ assert(runtime.isHostObject(*this));
266
+ return runtime.getHostObject(*this);
267
+ }
268
+
269
+ #if JSI_VERSION >= 7
270
+ template <typename T>
271
+ inline bool Object::hasNativeState(Runtime& runtime) const {
272
+ return runtime.hasNativeState(*this) &&
273
+ std::dynamic_pointer_cast<T>(runtime.getNativeState(*this));
274
+ }
275
+
276
+ template <>
277
+ inline bool Object::hasNativeState<NativeState>(Runtime& runtime) const {
278
+ return runtime.hasNativeState(*this);
279
+ }
280
+
281
+ template <typename T>
282
+ inline std::shared_ptr<T> Object::getNativeState(Runtime& runtime) const {
283
+ assert(hasNativeState<T>(runtime));
284
+ return std::static_pointer_cast<T>(runtime.getNativeState(*this));
285
+ }
286
+
287
+ inline void Object::setNativeState(
288
+ Runtime& runtime,
289
+ std::shared_ptr<NativeState> state) const {
290
+ runtime.setNativeState(*this, state);
291
+ }
292
+ #endif
293
+
294
+ #if JSI_VERSION >= 11
295
+ inline void Object::setExternalMemoryPressure(Runtime& runtime, size_t amt)
296
+ const {
297
+ runtime.setExternalMemoryPressure(*this, amt);
298
+ }
299
+ #endif
300
+
301
+ inline Array Object::getPropertyNames(Runtime& runtime) const {
302
+ return runtime.getPropertyNames(*this);
303
+ }
304
+
305
+ inline Value WeakObject::lock(Runtime& runtime) JSI_CONST_10 {
306
+ return runtime.lockWeakObject(*this);
307
+ }
308
+
309
+ template <typename T>
310
+ void Array::setValueAtIndex(Runtime& runtime, size_t i, T&& value)
311
+ JSI_CONST_10 {
312
+ setValueAtIndexImpl(
313
+ runtime, i, detail::toValue(runtime, std::forward<T>(value)));
314
+ }
315
+
316
+ inline Value Array::getValueAtIndex(Runtime& runtime, size_t i) const {
317
+ return runtime.getValueAtIndex(*this, i);
318
+ }
319
+
320
+ inline Function Function::createFromHostFunction(
321
+ Runtime& runtime,
322
+ const jsi::PropNameID& name,
323
+ unsigned int paramCount,
324
+ jsi::HostFunctionType func) {
325
+ return runtime.createFunctionFromHostFunction(
326
+ name, paramCount, std::move(func));
327
+ }
328
+
329
+ inline Value Function::call(Runtime& runtime, const Value* args, size_t count)
330
+ const {
331
+ return runtime.call(*this, Value::undefined(), args, count);
332
+ }
333
+
334
+ inline Value Function::call(Runtime& runtime, std::initializer_list<Value> args)
335
+ const {
336
+ return call(runtime, args.begin(), args.size());
337
+ }
338
+
339
+ template <typename... Args>
340
+ inline Value Function::call(Runtime& runtime, Args&&... args) const {
341
+ // A more awesome version of this would be able to create raw values
342
+ // which can be used directly without wrapping and unwrapping, but
343
+ // this will do for now.
344
+ return call(runtime, {detail::toValue(runtime, std::forward<Args>(args))...});
345
+ }
346
+
347
+ inline Value Function::callWithThis(
348
+ Runtime& runtime,
349
+ const Object& jsThis,
350
+ const Value* args,
351
+ size_t count) const {
352
+ return runtime.call(*this, Value(runtime, jsThis), args, count);
353
+ }
354
+
355
+ inline Value Function::callWithThis(
356
+ Runtime& runtime,
357
+ const Object& jsThis,
358
+ std::initializer_list<Value> args) const {
359
+ return callWithThis(runtime, jsThis, args.begin(), args.size());
360
+ }
361
+
362
+ template <typename... Args>
363
+ inline Value Function::callWithThis(
364
+ Runtime& runtime,
365
+ const Object& jsThis,
366
+ Args&&... args) const {
367
+ // A more awesome version of this would be able to create raw values
368
+ // which can be used directly without wrapping and unwrapping, but
369
+ // this will do for now.
370
+ return callWithThis(
371
+ runtime, jsThis, {detail::toValue(runtime, std::forward<Args>(args))...});
372
+ }
373
+
374
+ template <typename... Args>
375
+ inline Array Array::createWithElements(Runtime& runtime, Args&&... args) {
376
+ return createWithElements(
377
+ runtime, {detail::toValue(runtime, std::forward<Args>(args))...});
378
+ }
379
+
380
+ template <typename... Args>
381
+ inline std::vector<PropNameID> PropNameID::names(
382
+ Runtime& runtime,
383
+ Args&&... args) {
384
+ return names({detail::toPropNameID(runtime, std::forward<Args>(args))...});
385
+ }
386
+
387
+ template <size_t N>
388
+ inline std::vector<PropNameID> PropNameID::names(
389
+ PropNameID (&&propertyNames)[N]) {
390
+ std::vector<PropNameID> result;
391
+ result.reserve(N);
392
+ for (auto& name : propertyNames) {
393
+ result.push_back(std::move(name));
394
+ }
395
+ return result;
396
+ }
397
+
398
+ inline Value Function::callAsConstructor(
399
+ Runtime& runtime,
400
+ const Value* args,
401
+ size_t count) const {
402
+ return runtime.callAsConstructor(*this, args, count);
403
+ }
404
+
405
+ inline Value Function::callAsConstructor(
406
+ Runtime& runtime,
407
+ std::initializer_list<Value> args) const {
408
+ return callAsConstructor(runtime, args.begin(), args.size());
409
+ }
410
+
411
+ template <typename... Args>
412
+ inline Value Function::callAsConstructor(Runtime& runtime, Args&&... args)
413
+ const {
414
+ return callAsConstructor(
415
+ runtime, {detail::toValue(runtime, std::forward<Args>(args))...});
416
+ }
417
+
418
+ #if JSI_VERSION >= 8
419
+ String BigInt::toString(Runtime& runtime, int radix) const {
420
+ return runtime.bigintToString(*this, radix);
421
+ }
422
+ #endif
423
+
424
+ } // namespace jsi
425
+ } // namespace facebook