@crossbind/core-embind-jsi 2.0.0-beta.62 → 2.0.0-beta.64

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.
package/LICENSE CHANGED
@@ -19,3 +19,79 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
19
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
20
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
21
  SOFTWARE.
22
+
23
+ ==============================================================================
24
+
25
+ The embind sources in cpp/src/emscripten and js/embind.js are adapted from
26
+ Emscripten (https://github.com/emscripten-core/emscripten), whose license
27
+ follows. The notes on Node.js, musl and third_party/ that end the original file
28
+ concern parts of Emscripten this package does not include.
29
+
30
+ Emscripten is available under 2 licenses, the MIT license and the
31
+ University of Illinois/NCSA Open Source License.
32
+
33
+ Both are permissive open source licenses, with little if any
34
+ practical difference between them.
35
+
36
+ The reason for offering both is that (1) the MIT license is
37
+ well-known, while (2) the University of Illinois/NCSA Open Source
38
+ License allows Emscripten's code to be integrated upstream into
39
+ LLVM, which uses that license, should the opportunity arise.
40
+
41
+ The full text of both licenses follows.
42
+
43
+ ==============================================================================
44
+
45
+ Copyright (c) 2010-2014 Emscripten authors, see AUTHORS file.
46
+
47
+ Permission is hereby granted, free of charge, to any person obtaining a copy
48
+ of this software and associated documentation files (the "Software"), to deal
49
+ in the Software without restriction, including without limitation the rights
50
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
51
+ copies of the Software, and to permit persons to whom the Software is
52
+ furnished to do so, subject to the following conditions:
53
+
54
+ The above copyright notice and this permission notice shall be included in
55
+ all copies or substantial portions of the Software.
56
+
57
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
58
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
59
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
60
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
61
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
62
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
63
+ THE SOFTWARE.
64
+
65
+ ==============================================================================
66
+
67
+ Copyright (c) 2010-2014 Emscripten authors, see AUTHORS file.
68
+ All rights reserved.
69
+
70
+ Permission is hereby granted, free of charge, to any person obtaining a
71
+ copy of this software and associated documentation files (the
72
+ "Software"), to deal with the Software without restriction, including
73
+ without limitation the rights to use, copy, modify, merge, publish,
74
+ distribute, sublicense, and/or sell copies of the Software, and to
75
+ permit persons to whom the Software is furnished to do so, subject to
76
+ the following conditions:
77
+
78
+ Redistributions of source code must retain the above copyright
79
+ notice, this list of conditions and the following disclaimers.
80
+
81
+ Redistributions in binary form must reproduce the above
82
+ copyright notice, this list of conditions and the following disclaimers
83
+ in the documentation and/or other materials provided with the
84
+ distribution.
85
+
86
+ Neither the names of Mozilla,
87
+ nor the names of its contributors may be used to endorse
88
+ or promote products derived from this Software without specific prior
89
+ written permission.
90
+
91
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
92
+ OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
93
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
94
+ IN NO EVENT SHALL THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR
95
+ ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
96
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
97
+ SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE.
package/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  The Embind JSI integration tool enables seamless C++ integration with React Native and Expo.
4
4
 
5
5
  <a href="https://www.npmjs.com/package/@crossbind/core-embind-jsi">
6
- <img alt="NPM version" src="https://img.shields.io/npm/v/@crossbind/core-embind-jsi?style=for-the-badge" />
6
+ <img alt="NPM version" src="https://img.shields.io/npm/v/@crossbind/core-embind-jsi/beta?style=for-the-badge" />
7
7
  </a>
8
8
  <a href="https://github.com/crossbind/crossbind/blob/main/LICENSE">
9
9
  <img alt="License" src="https://img.shields.io/github/license/crossbind/crossbind?style=for-the-badge" />
@@ -1054,6 +1054,10 @@ EMSCRIPTEN_BINDINGS(builtin) {
1054
1054
  register_bigint<long long>("long long");
1055
1055
  register_bigint<unsigned long long>("unsigned long long");
1056
1056
  }
1057
+ if constexpr (sizeof(long) == 4) {
1058
+ register_integer<long>("long");
1059
+ register_integer<unsigned long>("unsigned long");
1060
+ }
1057
1061
 
1058
1062
  register_float<float>("float");
1059
1063
  register_float<double>("double");
@@ -17,6 +17,7 @@
17
17
  #include <map>
18
18
  #include <string>
19
19
  #include <type_traits>
20
+ #include <utility>
20
21
  #include <vector>
21
22
 
22
23
  #include "./em_macros.h"
@@ -630,42 +631,21 @@ struct SignatureTranslator<ReturnType (*)(Args...)> { using type = void*; };
630
631
  #define call_invoke_4 ({return invoke(fn, args[0], args[1], args[2], args[3]);})
631
632
  #define call_invoke(count) (call_invoke_##count)
632
633
 
634
+ namespace internal {
635
+ // Every JS argument of a call, however many parameters the bound function has.
636
+ template<typename Invoke, typename Fn, std::size_t... I>
637
+ EMSCRIPTEN_ALWAYS_INLINE decltype(auto) invokeWithArgs(facebook::jsi::Runtime& rt, Invoke&& invoke, Fn&& fn,
638
+ const facebook::jsi::Value* args, std::index_sequence<I...>) {
639
+ return invoke(rt, std::forward<Fn>(fn), args[I]...);
640
+ }
641
+ } // end namespace internal
642
+
633
643
  #define callInvoke(count, invokeVar, fnVar) ({ \
634
- if constexpr (count == 1) { return invokeVar(rt, fnVar, args[0]); }\
635
- else if constexpr (count == 2) { return invokeVar(rt, fnVar, args[0], args[1]); }\
636
- else if constexpr (count == 3) { return invokeVar(rt, fnVar, args[0], args[1], args[2]); }\
637
- else if constexpr (count == 4) { return invokeVar(rt, fnVar, args[0], args[1], args[2], args[3]); }\
638
- else if constexpr (count == 5) { return invokeVar(rt, fnVar, args[0], args[1], args[2], args[3], args[4]); }\
639
- else if constexpr (count == 6) { return invokeVar(rt, fnVar, args[0], args[1], args[2], args[3], args[4], args[5]); }\
640
- else if constexpr (count == 7) { return invokeVar(rt, fnVar, args[0], args[1], args[2], args[3], args[4], args[5], args[6]); }\
641
- else if constexpr (count == 8) { return invokeVar(rt, fnVar, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7]); }\
642
- else if constexpr (count == 9) { return invokeVar(rt, fnVar, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8]); }\
643
- else if constexpr (count == 10) { return invokeVar(rt, fnVar, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9]); }\
644
- else if constexpr (count == 11) { return invokeVar(rt, fnVar, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10]); }\
645
- else if constexpr (count == 12) { return invokeVar(rt, fnVar, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10], args[11]); }\
646
- else if constexpr (count == 13) { return invokeVar(rt, fnVar, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10], args[11], args[12]); }\
647
- else if constexpr (count == 14) { return invokeVar(rt, fnVar, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10], args[11], args[12], args[13]); }\
648
- else if constexpr (count == 15) { return invokeVar(rt, fnVar, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10], args[11], args[12], args[13], args[14]); }\
649
- else { return invokeVar(rt, fnVar); } \
644
+ return ::emscripten::internal::invokeWithArgs(rt, invokeVar, fnVar, args, std::make_index_sequence<(count)>{}); \
650
645
  })
651
646
 
652
647
  #define callInvokeVoid(count, invokeVar, fnVar) ({ \
653
- if constexpr (count == 1) { invokeVar(rt, fnVar, args[0]); }\
654
- else if constexpr (count == 2) { invokeVar(rt, fnVar, args[0], args[1]); }\
655
- else if constexpr (count == 3) { invokeVar(rt, fnVar, args[0], args[1], args[2]); }\
656
- else if constexpr (count == 4) { invokeVar(rt, fnVar, args[0], args[1], args[2], args[3]); }\
657
- else if constexpr (count == 5) { invokeVar(rt, fnVar, args[0], args[1], args[2], args[3], args[4]); }\
658
- else if constexpr (count == 6) { invokeVar(rt, fnVar, args[0], args[1], args[2], args[3], args[4], args[5]); }\
659
- else if constexpr (count == 7) { invokeVar(rt, fnVar, args[0], args[1], args[2], args[3], args[4], args[5], args[6]); }\
660
- else if constexpr (count == 8) { invokeVar(rt, fnVar, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7]); }\
661
- else if constexpr (count == 9) { invokeVar(rt, fnVar, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8]); }\
662
- else if constexpr (count == 10) { invokeVar(rt, fnVar, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9]); }\
663
- else if constexpr (count == 11) { invokeVar(rt, fnVar, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10]); }\
664
- else if constexpr (count == 12) { invokeVar(rt, fnVar, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10], args[11]); }\
665
- else if constexpr (count == 13) { invokeVar(rt, fnVar, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10], args[11], args[12]); }\
666
- else if constexpr (count == 14) { invokeVar(rt, fnVar, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10], args[11], args[12], args[13]); }\
667
- else if constexpr (count == 15) { invokeVar(rt, fnVar, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10], args[11], args[12], args[13], args[14]); }\
668
- else { invokeVar(rt, fnVar); } \
648
+ ::emscripten::internal::invokeWithArgs(rt, invokeVar, fnVar, args, std::make_index_sequence<(count)>{}); \
669
649
  })
670
650
 
671
651
  template<typename ReturnType, typename... Args, typename... Policies>
@@ -463,6 +463,30 @@ struct BindingType<T, std::enable_if_t<isOther64BitInteger<T>>> {
463
463
  }
464
464
  };
465
465
 
466
+ // Windows keeps long at 32 bits, still a type apart from int: it crosses as a Number like int does.
467
+ template<typename T>
468
+ constexpr bool isNarrowLong = (std::is_same_v<T, long> || std::is_same_v<T, unsigned long>) && sizeof(T) == 4;
469
+
470
+ template<typename T>
471
+ struct BindingType<T, std::enable_if_t<isNarrowLong<T>>> {
472
+ typedef T WireType;
473
+ typedef const facebook::jsi::Value WireType2;
474
+
475
+ static WireType toWireType(WireType b) {
476
+ return b;
477
+ }
478
+ static WireType fromWireType(WireType wt) {
479
+ return wt;
480
+ }
481
+
482
+ static WireType2 toWireType2(facebook::jsi::Runtime& rt, WireType b) {
483
+ return WireType2((double) b);
484
+ }
485
+ static WireType fromWireType2(facebook::jsi::Runtime& rt, WireType2& wt) {
486
+ return (WireType) wt.getNumber();
487
+ }
488
+ };
489
+
466
490
  template<>
467
491
  struct BindingType<facebook::jsi::Value> {
468
492
  typedef const facebook::jsi::Value WireType;
package/js/embind.js CHANGED
@@ -4,11 +4,6 @@ function warnOnce(msg) {
4
4
 
5
5
  function callRuntimeCallbacks() { }
6
6
 
7
- // Upstream's helper, left out of this fork. Pointers arrive as BigInt or Number.
8
- function ptrToString(ptr) {
9
- return '0x' + ptr.toString(16).padStart(8, '0');
10
- }
11
-
12
7
  // include: shell.js
13
8
  /**
14
9
  * @license
@@ -1813,12 +1808,14 @@ function craftInvokerFunction(humanName, argTypes, classType, cppInvokerFunc, cp
1813
1808
  if (cppInvokerFunc.rawFunction) {
1814
1809
  // Call the host function with a fixed argument list: going through the dyn caller cost a
1815
1810
  // rest array, a slice and a spread call on every call.
1816
- args1.push("raw", "throwParked");
1817
- args2.push(cppInvokerFunc.rawFunction, throwParkedError);
1811
+ // The generated code runs in the global scope, so it reaches the parked error through the object this
1812
+ // module calls globalThis: a Node loader bundle gives each runtime its own.
1813
+ args1.push("raw", "throwParked", "crossbindScope");
1814
+ args2.push(cppInvokerFunc.rawFunction, throwParkedError, globalThis);
1818
1815
  invokerFnBody +=
1819
- "globalThis.__crossbindParkedError = null;\n" +
1816
+ "crossbindScope.__crossbindParkedError = null;\n" +
1820
1817
  (returns || isAsync ? "var rv = " : "") + "raw(" + argsListWired + ");\n" +
1821
- "if (globalThis.__crossbindParkedError) throwParked();\n";
1818
+ "if (crossbindScope.__crossbindParkedError) throwParked();\n";
1822
1819
  } else {
1823
1820
  invokerFnBody +=
1824
1821
  (returns || isAsync ? "var rv = " : "") + "invoker(fn" + (argsListWired.length > 0 ? ", " : "") + argsListWired + ");\n";
@@ -2437,7 +2434,13 @@ var MIN_INT53 = -9007199254740992;
2437
2434
  function bigintToI53Checked(num) {
2438
2435
  return (num < MIN_INT53 || num > MAX_INT53) ? NaN : Number(num);
2439
2436
  }
2437
+ // A value C++ returns by copy crosses through the converter `isReference` marks, though the handle it makes is
2438
+ // typed as a pointer to the object's own class: JavaScript owns that copy (ownHandle).
2440
2439
  function RegisteredPointer_fromWireType(ptr) {
2440
+ var handle = handleFromWire.call(this, ptr);
2441
+ return this.isReference && handle ? ownHandle(handle) : handle;
2442
+ }
2443
+ function handleFromWire(ptr) {
2441
2444
  //console.log('RegisteredPointer_fromWireType', ptr);
2442
2445
  // ptr is a raw pointer (or a raw smartpointer)
2443
2446
  // ptr = bigintToI53Checked(ptr);
@@ -2534,35 +2537,34 @@ function attachFinalizer(handle) {
2534
2537
  // https://github.com/tc39/proposal-weakrefs), then attach finalizers
2535
2538
  // for class handles. We check for the presence of FinalizationRegistry
2536
2539
  // at run-time, not build-time.
2540
+ // A collected handle is released without the warning upstream prints under ASSERTIONS: it is freed by design.
2537
2541
  finalizationRegistry = new FinalizationRegistry((info) => {
2538
- console.warn(info.leakWarning.stack.replace(/^Error: /, ''));
2539
2542
  releaseClassHandle(info.$$);
2540
2543
  });
2541
2544
  attachFinalizer = (handle) => {
2542
2545
  var $$ = handle.$$;
2543
2546
  var hasSmartPtr = !!$$.smartPtr;
2544
- if (hasSmartPtr) {
2545
- // We should not call the destructor on raw pointers in case other code expects the pointee to live
2546
- var info = { $$: $$ };
2547
- // Create a warning as an Error instance in advance so that we can store
2548
- // the current stacktrace and point to it when / if a leak is detected.
2549
- // This is more useful than the empty stacktrace of `FinalizationRegistry`
2550
- // callback.
2551
- var cls = $$.ptrType.registeredClass;
2552
- info.leakWarning = new Error(`Embind found a leaked C++ instance ${cls.name} <${ptrToString($$.ptr)}>.\n` +
2553
- "We'll free it automatically in this case, but this functionality is not reliable across various environments.\n" +
2554
- "Make sure to invoke .delete() manually once you're done with the instance instead.\n" +
2555
- "Originally allocated"); // `.stack` will add "at ..." after this sentence
2556
- if ('captureStackTrace' in Error) {
2557
- Error.captureStackTrace(info.leakWarning, RegisteredPointer_fromWireType);
2558
- }
2559
- finalizationRegistry.register(handle, info, handle);
2547
+ // JavaScript owns an object behind a smart pointer, and one ownHandle marked on the count a handle shares with
2548
+ // its clones, as the wasm glue does after utils/embindObjectLifetime.js. A raw pointer stays its library's:
2549
+ // other code may expect the pointee to live.
2550
+ if (hasSmartPtr || $$.count.owned) {
2551
+ finalizationRegistry.register(handle, { $$: $$ }, handle);
2560
2552
  }
2561
2553
  return handle;
2562
2554
  };
2563
2555
  detachFinalizer = (handle) => finalizationRegistry.unregister(handle);
2564
2556
  return attachFinalizer(handle);
2565
2557
  }
2558
+ // A copy embind made of a returned value and an object JavaScript constructed through a raw constructor (a vector,
2559
+ // a Rust class) are released as one behind a smart pointer is.
2560
+ function ownHandle(handle) {
2561
+ var $$ = handle.$$;
2562
+ if (!$$.smartPtr && !$$.count.owned) {
2563
+ $$.count.owned = true;
2564
+ if (finalizationRegistry) finalizationRegistry.register(handle, { $$: $$ }, handle);
2565
+ }
2566
+ return handle;
2567
+ }
2566
2568
  function ClassHandle_clone() {
2567
2569
  if (!this.$$.ptr) {
2568
2570
  throwInstanceAlreadyDeleted(this);
@@ -2960,7 +2962,7 @@ function __embind_register_class(rawType,
2960
2962
  if (undefined === body) {
2961
2963
  throw new BindingError(`Tried to invoke ctor of ${name} with invalid number of parameters (${arguments.length}) - expected (${Object.keys(registeredClass.constructor_body).toString()}) parameters instead!`);
2962
2964
  }
2963
- return body.apply(this, arguments);
2965
+ return ownHandle(body.apply(this, arguments));
2964
2966
  });
2965
2967
 
2966
2968
  var instancePrototype = Object.create(basePrototype, {
@@ -3698,9 +3700,10 @@ const globalIgnoreList = [
3698
3700
  function emval_get_global() {
3699
3701
  if (typeof globalThis == 'object') {
3700
3702
  const global = {};
3701
- Object.keys(globalThis).filter(key => !key.startsWith('_') && !globalIgnoreList.includes(key)).forEach(key => {
3702
- global[key] = globalThis[key];
3703
- });
3703
+ // for...in: a Node loader bundle's globalThis inherits the global's properties.
3704
+ for (const key in globalThis) {
3705
+ if (!key.startsWith('_') && !globalIgnoreList.includes(key)) global[key] = globalThis[key];
3706
+ }
3704
3707
 
3705
3708
  return global;
3706
3709
  }
@@ -4255,7 +4258,8 @@ function toArray(vector) {
4255
4258
  return output;
4256
4259
  }
4257
4260
 
4258
- function toVector(VectorClass, array = []) {
4261
+ function toVector(classOrName, array = []) {
4262
+ const VectorClass = typeof classOrName === 'string' ? Module[classOrName] : classOrName;
4259
4263
  const vector = new VectorClass();
4260
4264
  array.forEach((item) => {
4261
4265
  vector.push_back(item);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crossbind/core-embind-jsi",
3
- "version": "2.0.0-beta.62",
3
+ "version": "2.0.0-beta.64",
4
4
  "description": "The Embind JSI integration tool enables seamless C++ integration with React Native and Expo.",
5
5
  "homepage": "https://github.com/crossbind/crossbind/tree/main/core/embind-jsi#readme",
6
6
  "repository": {