mirror of
https://github.com/denoland/deno.git
synced 2025-07-24 05:35:33 +00:00
Add Deno.symbols and move internal fields for test (#3693)
This commit is contained in:
parent
cad7b3e4fe
commit
0a78bfb836
15 changed files with 92 additions and 25 deletions
|
@ -4,6 +4,7 @@ import { TypedArray } from "./types.ts";
|
||||||
import { TextEncoder } from "./text_encoding.ts";
|
import { TextEncoder } from "./text_encoding.ts";
|
||||||
import { File, stdout } from "./files.ts";
|
import { File, stdout } from "./files.ts";
|
||||||
import { cliTable } from "./console_table.ts";
|
import { cliTable } from "./console_table.ts";
|
||||||
|
import { exposeForTest } from "./internals.ts";
|
||||||
|
|
||||||
type ConsoleContext = Set<unknown>;
|
type ConsoleContext = Set<unknown>;
|
||||||
type ConsoleOptions = Partial<{
|
type ConsoleOptions = Partial<{
|
||||||
|
@ -363,9 +364,7 @@ function createObjectString(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** TODO Do not expose this from "deno" namespace.
|
/** @internal */
|
||||||
* @internal
|
|
||||||
*/
|
|
||||||
export function stringifyArgs(
|
export function stringifyArgs(
|
||||||
args: unknown[],
|
args: unknown[],
|
||||||
options: ConsoleOptions = {}
|
options: ConsoleOptions = {}
|
||||||
|
@ -785,3 +784,7 @@ export function inspect(value: unknown, options?: ConsoleOptions): string {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Expose these fields to internalObject for tests.
|
||||||
|
exposeForTest("Console", Console);
|
||||||
|
exposeForTest("stringifyArgs", stringifyArgs);
|
||||||
|
|
|
@ -4,15 +4,19 @@ import { assert, assertEquals, test } from "./test_util.ts";
|
||||||
// Some of these APIs aren't exposed in the types and so we have to cast to any
|
// Some of these APIs aren't exposed in the types and so we have to cast to any
|
||||||
// in order to "trick" TypeScript.
|
// in order to "trick" TypeScript.
|
||||||
const {
|
const {
|
||||||
Console,
|
|
||||||
customInspect,
|
|
||||||
stringifyArgs,
|
|
||||||
inspect,
|
inspect,
|
||||||
writeSync,
|
writeSync,
|
||||||
stdout
|
stdout
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
} = Deno as any;
|
} = Deno as any;
|
||||||
|
|
||||||
|
const customInspect = Deno.symbols.customInspect;
|
||||||
|
const {
|
||||||
|
Console,
|
||||||
|
stringifyArgs
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
|
} = Deno[Deno.symbols.internal] as any;
|
||||||
|
|
||||||
function stringify(...args: unknown[]): string {
|
function stringify(...args: unknown[]): string {
|
||||||
return stringifyArgs(args).replace(/\n$/, "");
|
return stringifyArgs(args).replace(/\n$/, "");
|
||||||
}
|
}
|
||||||
|
|
|
@ -100,7 +100,7 @@ export {
|
||||||
Signal
|
Signal
|
||||||
} from "./process.ts";
|
} from "./process.ts";
|
||||||
export { transpileOnly, compile, bundle } from "./compiler_api.ts";
|
export { transpileOnly, compile, bundle } from "./compiler_api.ts";
|
||||||
export { inspect, customInspect } from "./console.ts";
|
export { inspect } from "./console.ts";
|
||||||
export { build, OperatingSystem, Arch } from "./build.ts";
|
export { build, OperatingSystem, Arch } from "./build.ts";
|
||||||
export { version } from "./version.ts";
|
export { version } from "./version.ts";
|
||||||
export const args: string[] = [];
|
export const args: string[] = [];
|
||||||
|
@ -110,18 +110,10 @@ export const args: string[] = [];
|
||||||
/** @internal */
|
/** @internal */
|
||||||
export { core } from "./core.ts";
|
export { core } from "./core.ts";
|
||||||
|
|
||||||
/** @internal */
|
|
||||||
export { setPrepareStackTrace } from "./error_stack.ts";
|
|
||||||
|
|
||||||
// TODO Don't expose Console nor stringifyArgs.
|
|
||||||
/** @internal */
|
|
||||||
export { Console, stringifyArgs } from "./console.ts";
|
|
||||||
// TODO Don't expose DomIterableMixin.
|
|
||||||
/** @internal */
|
|
||||||
export { DomIterableMixin } from "./mixins/dom_iterable.ts";
|
|
||||||
|
|
||||||
/** The current process id of the runtime. */
|
/** The current process id of the runtime. */
|
||||||
export let pid: number;
|
export let pid: number;
|
||||||
|
|
||||||
/** Reflects the NO_COLOR environment variable: https://no-color.org/ */
|
/** Reflects the NO_COLOR environment variable: https://no-color.org/ */
|
||||||
export let noColor: boolean;
|
export let noColor: boolean;
|
||||||
|
|
||||||
|
export { symbols } from "./symbols.ts";
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
import * as dispatch from "./dispatch.ts";
|
import * as dispatch from "./dispatch.ts";
|
||||||
import { sendSync } from "./dispatch_json.ts";
|
import { sendSync } from "./dispatch_json.ts";
|
||||||
import { assert } from "./util.ts";
|
import { assert } from "./util.ts";
|
||||||
|
import { exposeForTest } from "./internals.ts";
|
||||||
|
|
||||||
export interface Location {
|
export interface Location {
|
||||||
/** The full url for the module, e.g. `file://some/file.ts` or
|
/** The full url for the module, e.g. `file://some/file.ts` or
|
||||||
|
@ -271,3 +272,5 @@ function prepareStackTrace(
|
||||||
export function setPrepareStackTrace(ErrorConstructor: typeof Error): void {
|
export function setPrepareStackTrace(ErrorConstructor: typeof Error): void {
|
||||||
ErrorConstructor.prepareStackTrace = prepareStackTrace;
|
ErrorConstructor.prepareStackTrace = prepareStackTrace;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
exposeForTest("setPrepareStackTrace", setPrepareStackTrace);
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
import { test, assert } from "./test_util.ts";
|
import { test, assert } from "./test_util.ts";
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
const { setPrepareStackTrace } = Deno as any;
|
const { setPrepareStackTrace } = Deno[Deno.symbols.internal] as any;
|
||||||
|
|
||||||
interface CallSite {
|
interface CallSite {
|
||||||
getThis(): unknown;
|
getThis(): unknown;
|
||||||
|
|
|
@ -32,6 +32,8 @@ import * as request from "./request.ts";
|
||||||
// symbols required.
|
// symbols required.
|
||||||
import { core } from "./core.ts";
|
import { core } from "./core.ts";
|
||||||
|
|
||||||
|
import { internalObject } from "./internals.ts";
|
||||||
|
|
||||||
// During the build process, augmentations to the variable `window` in this
|
// During the build process, augmentations to the variable `window` in this
|
||||||
// file are tracked and created as part of default library that is built into
|
// file are tracked and created as part of default library that is built into
|
||||||
// Deno, we only need to declare the enough to compile Deno.
|
// Deno, we only need to declare the enough to compile Deno.
|
||||||
|
@ -69,6 +71,10 @@ declare global {
|
||||||
// A self reference to the global object.
|
// A self reference to the global object.
|
||||||
window.window = window;
|
window.window = window;
|
||||||
|
|
||||||
|
// Add internal object to Deno object.
|
||||||
|
// This is not exposed as part of the Deno types.
|
||||||
|
// @ts-ignore
|
||||||
|
Deno[Deno.symbols.internal] = internalObject;
|
||||||
// This is the Deno namespace, it is handled differently from other window
|
// This is the Deno namespace, it is handled differently from other window
|
||||||
// properties when building the runtime type library, as the whole module
|
// properties when building the runtime type library, as the whole module
|
||||||
// is flattened into a single namespace.
|
// is flattened into a single namespace.
|
||||||
|
|
|
@ -3,7 +3,7 @@ import { test, assert, assertEquals } from "./test_util.ts";
|
||||||
const {
|
const {
|
||||||
stringifyArgs
|
stringifyArgs
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
} = Deno as any;
|
} = Deno[Deno.symbols.internal] as any;
|
||||||
|
|
||||||
// Logic heavily copied from web-platform-tests, make
|
// Logic heavily copied from web-platform-tests, make
|
||||||
// sure pass mostly header basic test
|
// sure pass mostly header basic test
|
||||||
|
|
16
cli/js/internals.ts
Normal file
16
cli/js/internals.ts
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||||
|
export const internalSymbol = Symbol("Deno.internal");
|
||||||
|
|
||||||
|
// The object where all the internal fields for testing will be living.
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
|
export const internalObject: { [key: string]: any } = {};
|
||||||
|
|
||||||
|
// Register a field to internalObject for test access,
|
||||||
|
// through Deno[Deno.symbols.internal][name].
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
|
export function exposeForTest(name: string, value: any): void {
|
||||||
|
Object.defineProperty(internalObject, name, {
|
||||||
|
value,
|
||||||
|
enumerable: false
|
||||||
|
});
|
||||||
|
}
|
10
cli/js/internals_test.ts
Normal file
10
cli/js/internals_test.ts
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||||
|
import { test, assert } from "./test_util.ts";
|
||||||
|
|
||||||
|
test(function internalsExists(): void {
|
||||||
|
const {
|
||||||
|
stringifyArgs
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
|
} = Deno[Deno.symbols.internal] as any;
|
||||||
|
assert(!!stringifyArgs);
|
||||||
|
});
|
14
cli/js/lib.deno_runtime.d.ts
vendored
14
cli/js/lib.deno_runtime.d.ts
vendored
|
@ -1507,10 +1507,6 @@ declare namespace Deno {
|
||||||
colors: boolean;
|
colors: boolean;
|
||||||
indentLevel: number;
|
indentLevel: number;
|
||||||
}>;
|
}>;
|
||||||
/** A symbol which can be used as a key for a custom method which will be called
|
|
||||||
* when `Deno.inspect()` is called, or when the object is logged to the console.
|
|
||||||
*/
|
|
||||||
export const customInspect: unique symbol;
|
|
||||||
/**
|
/**
|
||||||
* `inspect()` converts input into string that has the same format
|
* `inspect()` converts input into string that has the same format
|
||||||
* as printed by `console.log(...)`;
|
* as printed by `console.log(...)`;
|
||||||
|
@ -1947,6 +1943,16 @@ declare namespace Deno {
|
||||||
// @url js/deno.d.ts
|
// @url js/deno.d.ts
|
||||||
|
|
||||||
export const args: string[];
|
export const args: string[];
|
||||||
|
|
||||||
|
/** Special Deno related symbols. */
|
||||||
|
export const symbols: {
|
||||||
|
/** Symbol to access exposed internal Deno API */
|
||||||
|
readonly internal: unique symbol;
|
||||||
|
/** A symbol which can be used as a key for a custom method which will be called
|
||||||
|
* when `Deno.inspect()` is called, or when the object is logged to the console.
|
||||||
|
*/
|
||||||
|
readonly customInspect: unique symbol;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// @url js/globals.ts
|
// @url js/globals.ts
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
import { DomIterable } from "../dom_types.ts";
|
import { DomIterable } from "../dom_types.ts";
|
||||||
import { window } from "../window.ts";
|
import { window } from "../window.ts";
|
||||||
import { requiredArguments } from "../util.ts";
|
import { requiredArguments } from "../util.ts";
|
||||||
|
import { exposeForTest } from "../internals.ts";
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
type Constructor<T = {}> = new (...args: any[]) => T;
|
type Constructor<T = {}> = new (...args: any[]) => T;
|
||||||
|
@ -10,7 +11,6 @@ type Constructor<T = {}> = new (...args: any[]) => T;
|
||||||
/** Mixes in a DOM iterable methods into a base class, assumes that there is
|
/** Mixes in a DOM iterable methods into a base class, assumes that there is
|
||||||
* a private data iterable that is part of the base class, located at
|
* a private data iterable that is part of the base class, located at
|
||||||
* `[dataSymbol]`.
|
* `[dataSymbol]`.
|
||||||
* TODO Don't expose DomIterableMixin from "deno" namespace.
|
|
||||||
*/
|
*/
|
||||||
export function DomIterableMixin<K, V, TBase extends Constructor>(
|
export function DomIterableMixin<K, V, TBase extends Constructor>(
|
||||||
Base: TBase,
|
Base: TBase,
|
||||||
|
@ -80,3 +80,5 @@ export function DomIterableMixin<K, V, TBase extends Constructor>(
|
||||||
|
|
||||||
return DomIterable;
|
return DomIterable;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
exposeForTest("DomIterableMixin", DomIterableMixin);
|
||||||
|
|
|
@ -21,7 +21,10 @@ function setup() {
|
||||||
// This is using an internal API we don't want published as types, so having
|
// This is using an internal API we don't want published as types, so having
|
||||||
// to cast to any to "trick" TypeScript
|
// to cast to any to "trick" TypeScript
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
DomIterable: (Deno as any).DomIterableMixin(Base, dataSymbol)
|
DomIterable: (Deno[Deno.symbols.internal] as any).DomIterableMixin(
|
||||||
|
Base,
|
||||||
|
dataSymbol
|
||||||
|
)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
13
cli/js/symbols.ts
Normal file
13
cli/js/symbols.ts
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||||
|
import { internalSymbol } from "./internals.ts";
|
||||||
|
import { customInspect } from "./console.ts";
|
||||||
|
|
||||||
|
/** Special Deno related symbols. */
|
||||||
|
export const symbols = {
|
||||||
|
/** Symbol to access exposed internal Deno API */
|
||||||
|
internal: internalSymbol,
|
||||||
|
/** A symbol which can be used as a key for a custom method which will be called
|
||||||
|
* when `Deno.inspect()` is called, or when the object is logged to the console.
|
||||||
|
*/
|
||||||
|
customInspect
|
||||||
|
};
|
7
cli/js/symbols_test.ts
Normal file
7
cli/js/symbols_test.ts
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||||
|
import { test, assert } from "./test_util.ts";
|
||||||
|
|
||||||
|
test(function symbolsExists(): void {
|
||||||
|
assert("internal" in Deno.symbols);
|
||||||
|
assert("customInspect" in Deno.symbols);
|
||||||
|
});
|
|
@ -25,6 +25,7 @@ import "./form_data_test.ts";
|
||||||
import "./get_random_values_test.ts";
|
import "./get_random_values_test.ts";
|
||||||
import "./globals_test.ts";
|
import "./globals_test.ts";
|
||||||
import "./headers_test.ts";
|
import "./headers_test.ts";
|
||||||
|
import "./internals_test.ts";
|
||||||
import "./link_test.ts";
|
import "./link_test.ts";
|
||||||
import "./location_test.ts";
|
import "./location_test.ts";
|
||||||
import "./make_temp_dir_test.ts";
|
import "./make_temp_dir_test.ts";
|
||||||
|
@ -42,6 +43,7 @@ import "./rename_test.ts";
|
||||||
import "./request_test.ts";
|
import "./request_test.ts";
|
||||||
import "./resources_test.ts";
|
import "./resources_test.ts";
|
||||||
import "./stat_test.ts";
|
import "./stat_test.ts";
|
||||||
|
import "./symbols_test.ts";
|
||||||
import "./symlink_test.ts";
|
import "./symlink_test.ts";
|
||||||
import "./text_encoding_test.ts";
|
import "./text_encoding_test.ts";
|
||||||
import "./timers_test.ts";
|
import "./timers_test.ts";
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue