Skip to content

Commit

Permalink
Refacoted Int64 to use a class
Browse files Browse the repository at this point in the history
  • Loading branch information
kraenhansen committed Aug 14, 2024
1 parent 11b5529 commit af18e26
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 47 deletions.
17 changes: 8 additions & 9 deletions packages/realm/bindgen/src/templates/wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,15 +243,14 @@ export function generate({ spec: boundSpec, rawSpec, file }: TemplateContext): v
deref(): T | undefined;
}
export const enum Int64Type {} // This shouldn't need to be exported, but rollup complains if it isn't.
export type Int64 = Int64Type;
export declare const Int64: {
add(a: Int64, b: Int64): Int64;
equals(a: Int64, b: Int64 | number | string): boolean;
isInt(a: unknown): a is Int64;
numToInt(a: number): Int64;
strToInt(a: string): Int64;
intToNum(a: Int64): number;
export declare class Int64 {
private brandForInt64;
static add(a: Int64, b: Int64): Int64;
static equals(a: Int64, b: Int64 | number | string): boolean;
static isInt(a: unknown): a is Int64;
static numToInt(a: number): Int64;
static strToInt(a: string): Int64;
static intToNum(a: Int64): number;
}
`,
);
Expand Down
2 changes: 1 addition & 1 deletion packages/realm/src/app-services/SyncSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ function toBindingProgressNotificationCallback(callback: ProgressNotificationCal
} else {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
return (transferredBytes: binding.Int64, transferrableBytes: binding.Int64, _: number) =>
callback(transferredBytes, transferrableBytes);
callback(binding.Int64.intToNum(transferredBytes), binding.Int64.intToNum(transferrableBytes));
}
}

Expand Down
42 changes: 26 additions & 16 deletions packages/realm/src/binding/NativeBigInt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,35 @@
//
////////////////////////////////////////////////////////////////////////////

import { assert } from "../assert";
import type { binding } from "./wrapper.generated";

export const NativeBigInt: typeof binding.Int64 = Object.freeze({
add(a: binding.Int64, b: binding.Int64) {
return a + b;
},
equals(a: binding.Int64, b: binding.Int64 | number | string) {
return a == b;
}, // using == rather than === to support number and string RHS!
isInt(a: unknown): a is binding.Int64 {
export class NativeBigInt {
static add(a: binding.Int64, b: binding.Int64): binding.Int64 {
assert(typeof a === "bigint");
assert(typeof b === "bigint");
return (a + b) as unknown as binding.Int64;
}

static equals(a: binding.Int64, b: binding.Int64 | number | string) {
assert(typeof a === "bigint");
assert(typeof b === "bigint" || typeof b === "number" || typeof b === "string");
return a == b; // using == rather than === to support number and string RHS!
}

static isInt(a: unknown): a is binding.Int64 {
return typeof a === "bigint";
},
numToInt(a: number) {
}

static numToInt(a: number) {
return BigInt(a) as unknown as binding.Int64;
},
strToInt(a: string) {
}

static strToInt(a: string) {
return BigInt(a) as unknown as binding.Int64;
},
intToNum(a: binding.Int64) {
}

static intToNum(a: binding.Int64) {
return Number(a);
},
});
}
}
35 changes: 20 additions & 15 deletions packages/realm/src/binding/PolyfilledBigInt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,36 @@ import { Long } from "bson";
import type { binding } from "./wrapper.generated";
import { assert } from "../assert";

export const PolyfilledBigInt: typeof binding.Int64 = Object.freeze({
add(a: binding.Int64, b: binding.Int64) {
export class PolyfilledBigInt {
static add(a: binding.Int64, b: binding.Int64) {
assert.instanceOf(a, Long);
assert.instanceOf(b, Long);
return a.add(b) as unknown as binding.Int64;
},
equals(a: binding.Int64, b: binding.Int64 | number | string) {
}

static equals(a: binding.Int64, b: binding.Int64 | number | string) {
assert.instanceOf(a, Long);
assert(
a instanceof Long || typeof a === "number" || typeof a === "string",
typeof b === "number" || typeof b === "string" || (typeof b === "object" && b instanceof Long),
"Expected a 'BSON.Long', or number, or string.",
);
return a.equals(b);
},
isInt(a: unknown): a is binding.Int64 {
}

static isInt(a: unknown): a is binding.Int64 {
return a instanceof Long;
},
numToInt(a: number) {
}

static numToInt(a: number) {
return Long.fromNumber(a) as unknown as binding.Int64;
},
strToInt(a: string) {
}

static strToInt(a: string) {
return Long.fromString(a) as unknown as binding.Int64;
},
intToNum(a: binding.Int64) {
}

static intToNum(a: binding.Int64) {
assert.instanceOf(a, Long);
return a.toNumber();
},
});
}
}
4 changes: 2 additions & 2 deletions packages/realm/src/binding/patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ export function applyPatch(binding: Binding) {
}
}

function fromBindingTimeoutSignal(timeoutMs: binding.Int64Type): AbortSignal | undefined {
const timeout = Number(timeoutMs);
function fromBindingTimeoutSignal(timeoutMs: binding.Int64): AbortSignal | undefined {
const timeout = binding.Int64.intToNum(timeoutMs);
return timeout > 0 ? AbortSignal.timeout(timeout) : undefined;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/realm/src/platform/node/binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
//
////////////////////////////////////////////////////////////////////////////

import { NativeBigInt, injectNativeModule } from "../binding";
import { NativeBigInt, type binding, injectNativeModule } from "../binding";

// eslint-disable-next-line @typescript-eslint/no-var-requires
const nativeModule = require("#realm.node");
injectNativeModule(nativeModule, { Int64: NativeBigInt, WeakRef });
injectNativeModule(nativeModule, { Int64: NativeBigInt as typeof binding.Int64, WeakRef });
4 changes: 2 additions & 2 deletions packages/realm/src/platform/react-native/binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
declare const global: Record<string, unknown>;

import { NativeModules } from "react-native";
import { NativeBigInt, PolyfilledBigInt, injectNativeModule } from "../binding";
import { NativeBigInt, PolyfilledBigInt, type binding, injectNativeModule } from "../binding";
import { assert } from "../../assert";

try {
Expand All @@ -32,7 +32,7 @@ try {
// Inject the native module into the binding
assert.object(nativeModule, "nativeModule");
injectNativeModule(nativeModule, {
Int64: global.HermesInternal ? NativeBigInt : PolyfilledBigInt,
Int64: (global.HermesInternal ? NativeBigInt : PolyfilledBigInt) as typeof binding.Int64,
WeakRef: class WeakRef {
private native: unknown;
constructor(obj: unknown) {
Expand Down

0 comments on commit af18e26

Please sign in to comment.