summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cli/tests/unit/kv_test.ts26
-rw-r--r--ext/kv/01_db.ts45
-rw-r--r--ext/kv/lib.rs3
3 files changed, 56 insertions, 18 deletions
diff --git a/cli/tests/unit/kv_test.ts b/cli/tests/unit/kv_test.ts
index 5a202fb0b..3a3476857 100644
--- a/cli/tests/unit/kv_test.ts
+++ b/cli/tests/unit/kv_test.ts
@@ -578,17 +578,29 @@ Deno.test("KvU64 underflow", () => {
}, RangeError);
});
-Deno.test("KvU64 frozen", () => {
+Deno.test("KvU64 unbox", () => {
const a = new Deno.KvU64(1n);
- assertThrows(() => {
- // @ts-expect-error value is readonly
- a.value = 2n;
- }, TypeError);
+ assertEquals(a.value, 1n);
});
-Deno.test("KvU64 unbox", () => {
+Deno.test("KvU64 unbox with valueOf", () => {
const a = new Deno.KvU64(1n);
- assertEquals(a.value, 1n);
+ assertEquals(a.valueOf(), 1n);
+});
+
+Deno.test("KvU64 auto-unbox", () => {
+ const a = new Deno.KvU64(1n);
+ assertEquals(a as unknown as bigint + 1n, 2n);
+});
+
+Deno.test("KvU64 toString", () => {
+ const a = new Deno.KvU64(1n);
+ assertEquals(a.toString(), "1");
+});
+
+Deno.test("KvU64 inspect", () => {
+ const a = new Deno.KvU64(1n);
+ assertEquals(Deno.inspect(a), "[Deno.KvU64: 1n]");
});
async function collect<T>(
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<unknown> {
@@ -330,15 +357,15 @@ function deserializeValue(entry: RawKvEntry): Deno.KvEntry<unknown> {
}
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<DBH>,