@crossbind/core-embind-jsi 2.0.0-beta.74 → 2.0.0-beta.75

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 (2) hide show
  1. package/js/embind.js +29 -2
  2. package/package.json +1 -1
package/js/embind.js CHANGED
@@ -2565,6 +2565,33 @@ function ownHandle(handle) {
2565
2565
  }
2566
2566
  return handle;
2567
2567
  }
2568
+ // A pointer a member function or property hands out can point into the object it was called on, or name that
2569
+ // object: its handle holds a clone of the object's handle, on the count the pointer's clones share, so the object goes
2570
+ // with the last of them, as the wasm glue does after utils/embindPointerOwners.js. Only a raw pointer, a NativePointer
2571
+ // and an emscripten::val can borrow: a copy and a smart pointer hold what they point at.
2572
+ function mayBorrow(type) {
2573
+ if (!type) return false;
2574
+ if (type.name === 'emscripten::val') return true;
2575
+ var cls = type.registeredClass;
2576
+ return !!cls && !type.isReference && (!type.isSmartPointer || cls.name === 'NativePointer');
2577
+ }
2578
+ function keepOwner(value, owner) {
2579
+ var $$ = value && value.$$;
2580
+ var held = owner && owner.$$;
2581
+ if (!$$ || !$$.count || $$.count.owner || !held || !held.ptr || held.preservePointerOnDelete || $$.count === held.count) {
2582
+ return value;
2583
+ }
2584
+ if ($$.smartPtr ? $$.ptrType.registeredClass.name === 'NativePointer' : !$$.count.owned) $$.count.owner = owner.clone();
2585
+ return value;
2586
+ }
2587
+ function keepsOwner(fn, type) {
2588
+ if (!mayBorrow(type)) return fn;
2589
+ var kept = function () {
2590
+ return keepOwner(fn.apply(this, arguments), this);
2591
+ };
2592
+ Object.defineProperty(kept, 'name', { value: fn.name });
2593
+ return kept;
2594
+ }
2568
2595
  function ClassHandle_clone() {
2569
2596
  if (!this.$$.ptr) {
2570
2597
  throwInstanceAlreadyDeleted(this);
@@ -3126,7 +3153,7 @@ function __embind_register_class_function(rawClassType,
3126
3153
 
3127
3154
  whenDependentTypesAreResolved([], rawArgTypes, function (argTypes) {
3128
3155
  //console.log(methodName);
3129
- var memberFunction = craftInvokerFunction(humanName, argTypes, classType, rawInvoker, context, isAsync);
3156
+ var memberFunction = keepsOwner(craftInvokerFunction(humanName, argTypes, classType, rawInvoker, context, isAsync), argTypes[0]);
3130
3157
 
3131
3158
  // Replace the initial unbound-handler-stub function with the appropriate member function, now that all types
3132
3159
  // are resolved. If multiple overloads are registered for this function, the function goes into an overload table.
@@ -3212,7 +3239,7 @@ function __embind_register_class_property(classType,
3212
3239
  get: function () {
3213
3240
  // console.log('get -> ', this.$$.ptr);
3214
3241
  var ptr = validateThis(this, classType, humanName + ' getter');
3215
- return getterReturnType['fromWireType'](getter(getterContext, ptr));
3242
+ return keepOwner(getterReturnType['fromWireType'](getter(getterContext, ptr)), this);
3216
3243
  },
3217
3244
  enumerable: true
3218
3245
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crossbind/core-embind-jsi",
3
- "version": "2.0.0-beta.74",
3
+ "version": "2.0.0-beta.75",
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": {