mirror of
https://github.com/denoland/deno.git
synced 2025-01-06 22:35:51 -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:
parent
3b36c36715
commit
3b5adb13de
3 changed files with 57 additions and 19 deletions
|
@ -578,19 +578,31 @@ Deno.test("KvU64 underflow", () => {
|
||||||
}, RangeError);
|
}, 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", () => {
|
Deno.test("KvU64 unbox", () => {
|
||||||
const a = new Deno.KvU64(1n);
|
const a = new Deno.KvU64(1n);
|
||||||
assertEquals(a.value, 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>(
|
async function collect<T>(
|
||||||
iter: Deno.KvListIterator<T>,
|
iter: Deno.KvListIterator<T>,
|
||||||
): Promise<Deno.KvEntry<T>[]> {
|
): Promise<Deno.KvEntry<T>[]> {
|
||||||
|
|
|
@ -2,8 +2,15 @@
|
||||||
|
|
||||||
// @ts-ignore internal api
|
// @ts-ignore internal api
|
||||||
const {
|
const {
|
||||||
ObjectGetPrototypeOf,
|
|
||||||
AsyncGeneratorPrototype,
|
AsyncGeneratorPrototype,
|
||||||
|
BigIntPrototypeToString,
|
||||||
|
ObjectFreeze,
|
||||||
|
ObjectGetPrototypeOf,
|
||||||
|
ObjectPrototypeIsPrototypeOf,
|
||||||
|
StringPrototypeReplace,
|
||||||
|
SymbolFor,
|
||||||
|
SymbolToStringTag,
|
||||||
|
Uint8ArrayPrototype,
|
||||||
} = globalThis.__bootstrap.primordials;
|
} = globalThis.__bootstrap.primordials;
|
||||||
const core = Deno.core;
|
const core = Deno.core;
|
||||||
const ops = core.ops;
|
const ops = core.ops;
|
||||||
|
@ -289,7 +296,7 @@ const MIN_U64 = BigInt("0");
|
||||||
const MAX_U64 = BigInt("0xffffffffffffffff");
|
const MAX_U64 = BigInt("0xffffffffffffffff");
|
||||||
|
|
||||||
class KvU64 {
|
class KvU64 {
|
||||||
readonly value: bigint;
|
value: bigint;
|
||||||
|
|
||||||
constructor(value: bigint) {
|
constructor(value: bigint) {
|
||||||
if (typeof value !== "bigint") {
|
if (typeof value !== "bigint") {
|
||||||
|
@ -299,11 +306,31 @@ class KvU64 {
|
||||||
throw new RangeError("value must be a positive bigint");
|
throw new RangeError("value must be a positive bigint");
|
||||||
}
|
}
|
||||||
if (value > MAX_U64) {
|
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;
|
this.value = value;
|
||||||
Object.freeze(this);
|
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> {
|
function deserializeValue(entry: RawKvEntry): Deno.KvEntry<unknown> {
|
||||||
|
@ -330,15 +357,15 @@ function deserializeValue(entry: RawKvEntry): Deno.KvEntry<unknown> {
|
||||||
}
|
}
|
||||||
|
|
||||||
function serializeValue(value: unknown): RawValue {
|
function serializeValue(value: unknown): RawValue {
|
||||||
if (value instanceof Uint8Array) {
|
if (ObjectPrototypeIsPrototypeOf(Uint8ArrayPrototype, value)) {
|
||||||
return {
|
return {
|
||||||
kind: "bytes",
|
kind: "bytes",
|
||||||
value,
|
value,
|
||||||
};
|
};
|
||||||
} else if (value instanceof KvU64) {
|
} else if (ObjectPrototypeIsPrototypeOf(KvU64.prototype, value)) {
|
||||||
return {
|
return {
|
||||||
kind: "u64",
|
kind: "u64",
|
||||||
value: value.value,
|
value: value.valueOf(),
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
return {
|
return {
|
||||||
|
@ -398,13 +425,13 @@ class KvListIterator extends AsyncIterator
|
||||||
let start: Deno.KvKey | undefined;
|
let start: Deno.KvKey | undefined;
|
||||||
let end: Deno.KvKey | undefined;
|
let end: Deno.KvKey | undefined;
|
||||||
if ("prefix" in selector && selector.prefix !== undefined) {
|
if ("prefix" in selector && selector.prefix !== undefined) {
|
||||||
prefix = Object.freeze([...selector.prefix]);
|
prefix = ObjectFreeze([...selector.prefix]);
|
||||||
}
|
}
|
||||||
if ("start" in selector && selector.start !== undefined) {
|
if ("start" in selector && selector.start !== undefined) {
|
||||||
start = Object.freeze([...selector.start]);
|
start = ObjectFreeze([...selector.start]);
|
||||||
}
|
}
|
||||||
if ("end" in selector && selector.end !== undefined) {
|
if ("end" in selector && selector.end !== undefined) {
|
||||||
end = Object.freeze([...selector.end]);
|
end = ObjectFreeze([...selector.end]);
|
||||||
}
|
}
|
||||||
if (prefix) {
|
if (prefix) {
|
||||||
if (start && end) {
|
if (start && end) {
|
||||||
|
|
|
@ -53,8 +53,7 @@ impl UnstableChecker {
|
||||||
}
|
}
|
||||||
|
|
||||||
deno_core::extension!(deno_kv,
|
deno_core::extension!(deno_kv,
|
||||||
// TODO(bartlomieju): specify deps
|
deps = [ deno_console ],
|
||||||
deps = [ ],
|
|
||||||
parameters = [ DBH: DatabaseHandler ],
|
parameters = [ DBH: DatabaseHandler ],
|
||||||
ops = [
|
ops = [
|
||||||
op_kv_database_open<DBH>,
|
op_kv_database_open<DBH>,
|
||||||
|
|
Loading…
Reference in a new issue