Skip to content

Commit

Permalink
fix(ext,runtime): add missing custom inspections (#21219)
Browse files Browse the repository at this point in the history
  • Loading branch information
petamoriken committed Nov 19, 2023
1 parent a7548af commit c806fbd
Show file tree
Hide file tree
Showing 31 changed files with 848 additions and 376 deletions.
8 changes: 4 additions & 4 deletions cli/tests/integration/run_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 357,7 @@ fn webstorage_location_shares_origin() {
.wait_with_output()
.unwrap();
assert!(output.status.success());
assert_eq!(output.stdout, b"Storage { length: 1, hello: \"deno\" }\n");
assert_eq!(output.stdout, b"Storage { hello: \"deno\", length: 1 }\n");
}

// test to ensure that when a --config file is set, but no --location, that
Expand All @@ -384,7 384,7 @@ fn webstorage_config_file() {
.new_command()
.args("run --config run/webstorage/config_a.jsonc run/webstorage/logger.ts")
.run()
.assert_matches_text("Storage { length: 1, hello: \"deno\" }\n");
.assert_matches_text("Storage { hello: \"deno\", length: 1 }\n");
}

// tests to ensure `--config` does not effect persisted storage when a
Expand All @@ -401,7 401,7 @@ fn webstorage_location_precedes_config() {
context.new_command()
.args("run --location https://example.com/b.ts --config run/webstorage/config_b.jsonc run/webstorage/logger.ts")
.run()
.assert_matches_text("Storage { length: 1, hello: \"deno\" }\n");
.assert_matches_text("Storage { hello: \"deno\", length: 1 }\n");
}

// test to ensure that when there isn't a configuration or location, that the
Expand All @@ -426,7 426,7 @@ fn webstorage_main_module() {
.new_command()
.args("run run/webstorage/fixture.ts")
.run()
.assert_matches_text("Storage { length: 1, hello: \"deno\" }\n");
.assert_matches_text("Storage { hello: \"deno\", length: 1 }\n");
}

itest!(_075_import_local_query_hash {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 1,9 @@
[Object: null prototype] {
thrown: DOMException: foo,
thrown: DOMException: foo
at new DOMException (ext:deno_web/01_dom_exception.js:[WILDCARD])
at [WILDCARD]
at Object.evalContext (ext:core/01_core.js:[WILDCARD])
at file:///[WILDCARD]/eval_context_throw_dom_exception.js:1:48,
isNativeError: true,
isCompileError: false
}
2 changes: 1 addition & 1 deletion cli/tests/testdata/run/webstorage/serialization.ts.out
Original file line number Diff line number Diff line change
@@ -1,2 1,2 @@
Storage {[WILDCARD]
Storage { length: 1, hello: "deno" }
Storage { hello: "deno", length: 1 }
7 changes: 2 additions & 5 deletions cli/tests/unit/dom_exception_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 7,8 @@ import {
} from "./test_util.ts";

Deno.test(function customInspectFunction() {
const blob = new DOMException("test");
assertEquals(
Deno.inspect(blob),
`DOMException: test`,
);
const exception = new DOMException("test");
assertEquals(Deno.inspect(exception), exception.stack);
assertStringIncludes(Deno.inspect(DOMException.prototype), "DOMException");
});

Expand Down
18 changes: 18 additions & 0 deletions ext/broadcast_channel/01_broadcast_channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 5,7 @@
const core = globalThis.Deno.core;
const ops = core.ops;
import * as webidl from "ext:deno_webidl/00_webidl.js";
import { createFilteredInspectProxy } from "ext:deno_console/01_console.js";
import {
defineEventHandler,
EventTarget,
Expand All @@ -17,8 18,10 @@ const {
ArrayPrototypeIndexOf,
ArrayPrototypePush,
ArrayPrototypeSplice,
ObjectPrototypeIsPrototypeOf,
PromisePrototypeThen,
Symbol,
SymbolFor,
Uint8Array,
} = primordials;

Expand Down Expand Up @@ -141,6 144,21 @@ class BroadcastChannel extends EventTarget {
ops.op_broadcast_unsubscribe(rid);
}
}

[SymbolFor("Deno.privateCustomInspect")](inspect, inspectOptions) {
return inspect(
createFilteredInspectProxy({
object: this,
evaluate: ObjectPrototypeIsPrototypeOf(BroadcastChannelPrototype, this),
keys: [
"name",
"onmessage",
"onmessageerror",
],
}),
inspectOptions,
);
}
}

defineEventHandler(BroadcastChannel.prototype, "message");
Expand Down
9 changes: 9 additions & 0 deletions ext/cache/01_cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 8,7 @@ const {
StringPrototypeSplit,
StringPrototypeTrim,
Symbol,
SymbolFor,
TypeError,
} = primordials;
import {
Expand Down Expand Up @@ -59,6 60,10 @@ class CacheStorage {
cacheName = webidl.converters["DOMString"](cacheName, prefix, "Argument 1");
return await op_cache_storage_delete(cacheName);
}

[SymbolFor("Deno.privateCustomInspect")](inspect, inspectOptions) {
return `${this.constructor.name} ${inspect({}, inspectOptions)}`;
}
}

const _matchAll = Symbol("[[matchAll]]");
Expand Down Expand Up @@ -275,6 280,10 @@ class Cache {

return responses;
}

[SymbolFor("Deno.privateCustomInspect")](inspect, inspectOptions) {
return `${this.constructor.name} ${inspect({}, inspectOptions)}`;
}
}

webidl.configureInterface(CacheStorage);
Expand Down
39 changes: 28 additions & 11 deletions ext/crypto/00_crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 10,7 @@ const core = globalThis.Deno.core;
const ops = core.ops;
const primordials = globalThis.__bootstrap.primordials;
import * as webidl from "ext:deno_webidl/00_webidl.js";
import { createFilteredInspectProxy } from "ext:deno_console/01_console.js";
import DOMException from "ext:deno_web/01_dom_exception.js";
const {
ArrayBufferIsView,
Expand Down Expand Up @@ -349,15 350,20 @@ class CryptoKey {
return this[_algorithm];
}

[SymbolFor("Deno.customInspect")](inspect) {
return `${this.constructor.name} ${
inspect({
type: this.type,
extractable: this.extractable,
algorithm: this.algorithm,
usages: this.usages,
})
}`;
[SymbolFor("Deno.privateCustomInspect")](inspect, inspectOptions) {
return inspect(
createFilteredInspectProxy({
object: this,
evaluate: ObjectPrototypeIsPrototypeOf(CryptoKeyPrototype, this),
keys: [
"type",
"extractable",
"algorithm",
"usages",
],
}),
inspectOptions,
);
}
}

Expand Down Expand Up @@ -1710,6 1716,10 @@ class SubtleCrypto {

return result;
}

[SymbolFor("Deno.privateCustomInspect")](inspect, inspectOptions) {
return `${this.constructor.name} ${inspect({}, inspectOptions)}`;
}
}
const SubtleCryptoPrototype = SubtleCrypto.prototype;

Expand Down Expand Up @@ -4730,8 4740,15 @@ class Crypto {
return subtle;
}

[SymbolFor("Deno.customInspect")](inspect) {
return `${this.constructor.name} ${inspect({})}`;
[SymbolFor("Deno.privateCustomInspect")](inspect, inspectOptions) {
return inspect(
createFilteredInspectProxy({
object: this,
evaluate: ObjectPrototypeIsPrototypeOf(CryptoPrototype, this),
keys: ["subtle"],
}),
inspectOptions,
);
}
}

Expand Down
15 changes: 9 additions & 6 deletions ext/fetch/20_headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 26,9 @@ const {
ArrayPrototypeSort,
ArrayPrototypeJoin,
ArrayPrototypeSplice,
ObjectFromEntries,
ObjectHasOwn,
ObjectPrototypeIsPrototypeOf,
RegExpPrototypeTest,
Symbol,
SymbolFor,
Expand Down Expand Up @@ -441,13 443,14 @@ class Headers {
}
}

[SymbolFor("Deno.privateCustomInspect")](inspect) {
const headers = {};
// deno-lint-ignore prefer-primordials
for (const header of this) {
headers[header[0]] = header[1];
[SymbolFor("Deno.privateCustomInspect")](inspect, inspectOptions) {
if (ObjectPrototypeIsPrototypeOf(HeadersPrototype, this)) {
return `${this.constructor.name} ${
inspect(ObjectFromEntries(this), inspectOptions)
}`;
} else {
return `${this.constructor.name} ${inspect({}, inspectOptions)}`;
}
return `Headers ${inspect(headers)}`;
}
}

Expand Down
12 changes: 12 additions & 0 deletions ext/fetch/21_formdata.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 26,12 @@ const {
MapPrototypeSet,
MathRandom,
ObjectFreeze,
ObjectFromEntries,
ObjectPrototypeIsPrototypeOf,
SafeMap,
SafeRegExp,
Symbol,
SymbolFor,
StringFromCharCode,
StringPrototypeCharCodeAt,
StringPrototypeTrim,
Expand Down Expand Up @@ -262,6 264,16 @@ class FormData {
ArrayPrototypePush(list, entry);
}
}

[SymbolFor("Deno.privateCustomInspect")](inspect, inspectOptions) {
if (ObjectPrototypeIsPrototypeOf(FormDataPrototype, this)) {
return `${this.constructor.name} ${
inspect(ObjectFromEntries(this), inspectOptions)
}`;
} else {
return `${this.constructor.name} ${inspect({}, inspectOptions)}`;
}
}
}

webidl.mixinPairIterable("FormData", FormData, entryList, "name", "value");
Expand Down
27 changes: 15 additions & 12 deletions ext/fetch/23_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,18 483,21 @@ class Request {
);
}

[SymbolFor("Deno.customInspect")](inspect) {
return inspect(createFilteredInspectProxy({
object: this,
evaluate: ObjectPrototypeIsPrototypeOf(RequestPrototype, this),
keys: [
"bodyUsed",
"headers",
"method",
"redirect",
"url",
],
}));
[SymbolFor("Deno.privateCustomInspect")](inspect, inspectOptions) {
return inspect(
createFilteredInspectProxy({
object: this,
evaluate: ObjectPrototypeIsPrototypeOf(RequestPrototype, this),
keys: [
"bodyUsed",
"headers",
"method",
"redirect",
"url",
],
}),
inspectOptions,
);
}
}

Expand Down
33 changes: 18 additions & 15 deletions ext/fetch/23_response.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,21 408,24 @@ class Response {
return second;
}

[SymbolFor("Deno.customInspect")](inspect) {
return inspect(createFilteredInspectProxy({
object: this,
evaluate: ObjectPrototypeIsPrototypeOf(ResponsePrototype, this),
keys: [
"body",
"bodyUsed",
"headers",
"ok",
"redirected",
"status",
"statusText",
"url",
],
}));
[SymbolFor("Deno.privateCustomInspect")](inspect, inspectOptions) {
return inspect(
createFilteredInspectProxy({
object: this,
evaluate: ObjectPrototypeIsPrototypeOf(ResponsePrototype, this),
keys: [
"body",
"bodyUsed",
"headers",
"ok",
"redirected",
"status",
"statusText",
"url",
],
}),
inspectOptions,
);
}
}

Expand Down
21 changes: 21 additions & 0 deletions ext/fetch/27_eventsource.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 5,7 @@
const core = globalThis.Deno.core;

import * as webidl from "ext:deno_webidl/00_webidl.js";
import { createFilteredInspectProxy } from "ext:deno_console/01_console.js";
import { URL } from "ext:deno_url/00_url.js";
import DOMException from "ext:deno_web/01_dom_exception.js";
import {
Expand All @@ -25,6 26,7 @@ const {
NumberIsFinite,
NumberIsNaN,
ObjectDefineProperties,
ObjectPrototypeIsPrototypeOf,
Promise,
StringPrototypeEndsWith,
StringPrototypeIncludes,
Expand All @@ -34,6 36,7 @@ const {
StringPrototypeStartsWith,
StringPrototypeToLowerCase,
Symbol,
SymbolFor,
} = primordials;

// Copied from https://github.com/denoland/deno_std/blob/e0753abe0c8602552862a568348c046996709521/streams/text_line_stream.ts#L20-L74
Expand Down Expand Up @@ -345,6 348,24 @@ class EventSource extends EventTarget {
}
}
}

[SymbolFor("Deno.privateCustomInspect")](inspect, inspectOptions) {
return inspect(
createFilteredInspectProxy({
object: this,
evaluate: ObjectPrototypeIsPrototypeOf(EventSourcePrototype, this),
keys: [
"readyState",
"url",
"withCredentials",
"onopen",
"onmessage",
"onerror",
],
}),
inspectOptions,
);
}
}

const EventSourcePrototype = EventSource.prototype;
Expand Down
Loading

0 comments on commit c806fbd

Please sign in to comment.