1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

fix(ext/kv): KvU64#valueOf and KvU64 inspect (#18656)

`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 <biwanczuk@gmail.com>
This commit is contained in:
Luca Casonato 2023-05-03 12:44:00 +01:00 committed by GitHub
parent 798c1ad0f1
commit 93a78d3d4a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 19 deletions

View file

@ -578,19 +578,31 @@ Deno.test("KvU64 underflow", () => {
}, RangeError);
});
Deno.test("KvU64 frozen", () => {
const a = new Deno.KvU64(1n);
assertThrows(() => {
// @ts-expect-error value is readonly
a.value = 2n;
}, TypeError);
});
Deno.test("KvU64 unbox", () => {
const a = new Deno.KvU64(1n);
assertEquals(a.value, 1n);
});
Deno.test("KvU64 unbox with valueOf", () => {
const a = new Deno.KvU64(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>(
iter: Deno.KvListIterator<T>,
): Promise<Deno.KvEntry<T>[]> {

View file

@ -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) {

View file

@ -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>,