From 93a78d3d4aedb96cfc2641048532c95197f661bb Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Wed, 3 May 2023 12:44:00 +0100 Subject: fix(ext/kv): KvU64#valueOf and KvU64 inspect (#18656) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `new Deno.KvU64(1n) + 2n == 3n` is now true. `new Deno.KvU64(1n)` is now inspected as `[Deno.KvU64: 1n]` (`Object(1n)` is inspected as `[BigInt: 1n]`). --------- Co-authored-by: Bartek IwaƄczuk --- ext/kv/01_db.ts | 45 ++++++++++++++++++++++++++++++++++++--------- ext/kv/lib.rs | 3 +-- 2 files changed, 37 insertions(+), 11 deletions(-) (limited to 'ext') diff --git a/ext/kv/01_db.ts b/ext/kv/01_db.ts index 72d358005..ca37aa840 100644 --- a/ext/kv/01_db.ts +++ b/ext/kv/01_db.ts @@ -2,8 +2,15 @@ // @ts-ignore internal api const { - ObjectGetPrototypeOf, AsyncGeneratorPrototype, + BigIntPrototypeToString, + ObjectFreeze, + ObjectGetPrototypeOf, + ObjectPrototypeIsPrototypeOf, + StringPrototypeReplace, + SymbolFor, + SymbolToStringTag, + Uint8ArrayPrototype, } = globalThis.__bootstrap.primordials; const core = Deno.core; const ops = core.ops; @@ -289,7 +296,7 @@ const MIN_U64 = BigInt("0"); const MAX_U64 = BigInt("0xffffffffffffffff"); class KvU64 { - readonly value: bigint; + value: bigint; constructor(value: bigint) { if (typeof value !== "bigint") { @@ -299,11 +306,31 @@ class KvU64 { throw new RangeError("value must be a positive bigint"); } if (value > MAX_U64) { - throw new RangeError("value must be a 64-bit unsigned integer"); + throw new RangeError("value must fit in a 64-bit unsigned integer"); } this.value = value; Object.freeze(this); } + + valueOf() { + return this.value; + } + + toString() { + return BigIntPrototypeToString(this.value); + } + + get [SymbolToStringTag]() { + return "Deno.KvU64"; + } + + [SymbolFor("Deno.privateCustomInspect")](inspect, inspectOptions) { + return StringPrototypeReplace( + inspect(Object(this.value), inspectOptions), + "BigInt", + "Deno.KvU64", + ); + } } function deserializeValue(entry: RawKvEntry): Deno.KvEntry { @@ -330,15 +357,15 @@ function deserializeValue(entry: RawKvEntry): Deno.KvEntry { } function serializeValue(value: unknown): RawValue { - if (value instanceof Uint8Array) { + if (ObjectPrototypeIsPrototypeOf(Uint8ArrayPrototype, value)) { return { kind: "bytes", value, }; - } else if (value instanceof KvU64) { + } else if (ObjectPrototypeIsPrototypeOf(KvU64.prototype, value)) { return { kind: "u64", - value: value.value, + value: value.valueOf(), }; } else { return { @@ -398,13 +425,13 @@ class KvListIterator extends AsyncIterator let start: Deno.KvKey | undefined; let end: Deno.KvKey | undefined; if ("prefix" in selector && selector.prefix !== undefined) { - prefix = Object.freeze([...selector.prefix]); + prefix = ObjectFreeze([...selector.prefix]); } if ("start" in selector && selector.start !== undefined) { - start = Object.freeze([...selector.start]); + start = ObjectFreeze([...selector.start]); } if ("end" in selector && selector.end !== undefined) { - end = Object.freeze([...selector.end]); + end = ObjectFreeze([...selector.end]); } if (prefix) { if (start && end) { diff --git a/ext/kv/lib.rs b/ext/kv/lib.rs index f17ed55e3..dbc626225 100644 --- a/ext/kv/lib.rs +++ b/ext/kv/lib.rs @@ -53,8 +53,7 @@ impl UnstableChecker { } deno_core::extension!(deno_kv, - // TODO(bartlomieju): specify deps - deps = [ ], + deps = [ deno_console ], parameters = [ DBH: DatabaseHandler ], ops = [ op_kv_database_open, -- cgit v1.2.3