Skip to content

Commit

Permalink
Injecting WeakRef
Browse files Browse the repository at this point in the history
  • Loading branch information
kraenhansen committed Aug 12, 2024
1 parent 0cb2162 commit efe6904
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
10 changes: 5 additions & 5 deletions packages/realm/bindgen/src/templates/wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }`)}
});
`,
);
Expand All @@ -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 = [
Expand Down Expand Up @@ -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} });`);
Expand Down
2 changes: 1 addition & 1 deletion packages/realm/src/platform/node/binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
35 changes: 25 additions & 10 deletions packages/realm/src/platform/react-native/binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,32 @@ declare const global: Record<string, unknown>;

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",
);
Expand Down

0 comments on commit efe6904

Please sign in to comment.