diff --git a/packages/realm/bindgen/src/templates/wrapper.ts b/packages/realm/bindgen/src/templates/wrapper.ts index 5600939ba8..53e4ca935b 100644 --- a/packages/realm/bindgen/src/templates/wrapper.ts +++ b/packages/realm/bindgen/src/templates/wrapper.ts @@ -280,7 +280,7 @@ export function generate({ spec: boundSpec, rawSpec, file }: TemplateContext): v out( ` Object.defineProperties(binding, { - ${spec.classes.map((cls) => `${cls.jsName}: { get: _throwOnAccess.bind(undefined, "cls.jsName") }`)} + ${spec.classes.map((cls) => `${cls.jsName}: { get: _throwOnAccess.bind(undefined, "${cls.jsName}"), configurable: true }`)} }); `, ); @@ -307,14 +307,16 @@ export function generate({ spec: boundSpec, rawSpec, file }: TemplateContext): v ` type Extras = { Int64: typeof binding.Int64; + WeakRef: typeof binding.WeakRef; }; // eslint-disable-next-line @typescript-eslint/no-explicit-any export function injectNativeModule(nativeModule: any, extras: Extras) { + // eslint-disable-next-line no-console + console.log("injectNativeModule called"); Object.assign(binding, extras); `, ); - // TODO: Handle injection of WeakRef and Int64 // TODO: Handle injectables const injectables = [ @@ -395,10 +397,8 @@ export function generate({ spec: boundSpec, rawSpec, file }: TemplateContext): v out(` Object.defineProperties(binding, { - ${spec.classes.map((cls) => `${cls.jsName}: { value: ${cls.jsName} }`)} + ${spec.classes.map((cls) => `${cls.jsName}: { value: ${cls.jsName}, writable: false, configurable: false }`)} }); - // Freezing the binding ensures injection can happen only once - Object.freeze(binding); `); out(`nativeModule.injectInjectables({ ${injectables} });`); diff --git a/packages/realm/src/platform/node/binding.ts b/packages/realm/src/platform/node/binding.ts index d3795ff773..6476c577bb 100644 --- a/packages/realm/src/platform/node/binding.ts +++ b/packages/realm/src/platform/node/binding.ts @@ -20,4 +20,4 @@ import { NativeBigInt, injectNativeModule } from "../binding"; // eslint-disable-next-line @typescript-eslint/no-var-requires const nativeModule = require("#realm.node"); -injectNativeModule(nativeModule, { Int64: NativeBigInt }); +injectNativeModule(nativeModule, { Int64: NativeBigInt, WeakRef }); diff --git a/packages/realm/src/platform/react-native/binding.ts b/packages/realm/src/platform/react-native/binding.ts index 69a3834fd5..2b93ca2443 100644 --- a/packages/realm/src/platform/react-native/binding.ts +++ b/packages/realm/src/platform/react-native/binding.ts @@ -20,17 +20,32 @@ declare const global: Record; import { NativeModules } from "react-native"; import { NativeBigInt, PolyfilledBigInt, injectNativeModule } from "../binding"; +import { assert } from "../../assert"; -const RealmNativeModule = NativeModules.Realm; - -RealmNativeModule.injectModuleIntoJSGlobal(); -// Read the global into the local scope -const { __injectedRealmBinding } = global; -// Delete the global again -delete global.__injectedRealmBinding; -if (typeof __injectedRealmBinding === "object") { - injectNativeModule(__injectedRealmBinding, { Int64: global.HermesInternal ? NativeBigInt : PolyfilledBigInt }); -} else { +try { + const RealmNativeModule = NativeModules.Realm; + RealmNativeModule.injectModuleIntoJSGlobal(); + // Read the global into the local scope + const { __injectedRealmBinding: nativeModule } = global; + // Delete the global again + delete global.__injectedRealmBinding; + // Inject the native module into the binding + assert.object(nativeModule, "nativeModule"); + injectNativeModule(nativeModule, { + Int64: global.HermesInternal ? NativeBigInt : PolyfilledBigInt, + WeakRef: class WeakRef { + private native: unknown; + constructor(obj: unknown) { + // eslint-disable-next-line @typescript-eslint/no-explicit-any -- See "createWeakRef" protocol in the jsi bindgen template + this.native = (nativeModule as any).createWeakRef(obj); + } + deref() { + // eslint-disable-next-line @typescript-eslint/no-explicit-any -- See "createWeakRef" protocol in the jsi bindgen template + return (nativeModule as any).lockWeakRef(this.native); + } + }, + }); +} catch (err) { throw new Error( "Could not find the Realm binary. Please consult our troubleshooting guide: https://www.mongodb.com/docs/realm-sdks/js/latest/#md:troubleshooting-missing-binary", );