Skip to content

Commit

Permalink
fix(ext/kv): KvU64#valueOf and KvU64 inspect
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacasonato committed May 1, 2023
1 parent 6728ad4 commit 4e7106e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
16 changes: 9 additions & 7 deletions cli/tests/unit/kv_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -578,17 +578,19 @@ 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);
});

async function collect<T>(
Expand Down
27 changes: 23 additions & 4 deletions ext/kv/01_db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

// @ts-ignore internal api
const {
ObjectGetPrototypeOf,
AsyncGeneratorPrototype,
SymbolToStringTag,
ObjectGetPrototypeOf,
SymbolFor,
} = globalThis.__bootstrap.primordials;
const core = Deno.core;
const ops = core.ops;
Expand Down Expand Up @@ -289,7 +291,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") {
Expand All @@ -299,11 +301,28 @@ 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 this.value.toString();
}

get [SymbolToStringTag]() {
return "Deno.KvU64";
}

[SymbolFor("Deno.privateCustomInspect")](inspect, inspectOptions) {
return inspect(Object(this.value), inspectOptions)
.replace("BigInt", "Deno.KvU64");
}
}

function deserializeValue(entry: RawKvEntry): Deno.KvEntry<unknown> {
Expand Down Expand Up @@ -338,7 +357,7 @@ function serializeValue(value: unknown): RawValue {
} else if (value instanceof KvU64) {
return {
kind: "u64",
value: value.value,
value: value.valueOf(),
};
} else {
return {
Expand Down
3 changes: 1 addition & 2 deletions ext/kv/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand Down

0 comments on commit 4e7106e

Please sign in to comment.