From d1706c2ac99eb82a937dff1c6a4c861aa24b1210 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Sat, 6 Jan 2024 14:40:26 +0100 Subject: [PATCH] refactor: split runtime/98_global_scope.js (#21785) --- ext/node/polyfills/console.ts | 2 +- ...bal_scope.js => 98_global_scope_shared.js} | 205 +----------------- runtime/js/98_global_scope_window.js | 119 ++++++++++ runtime/js/98_global_scope_worker.js | 112 ++++++++++ runtime/js/99_main.js | 10 +- runtime/shared.rs | 4 +- 6 files changed, 245 insertions(+), 207 deletions(-) rename runtime/js/{98_global_scope.js => 98_global_scope_shared.js} (63%) create mode 100644 runtime/js/98_global_scope_window.js create mode 100644 runtime/js/98_global_scope_worker.js diff --git a/ext/node/polyfills/console.ts b/ext/node/polyfills/console.ts index 7c76ef24c6..c72cfb1604 100644 --- a/ext/node/polyfills/console.ts +++ b/ext/node/polyfills/console.ts @@ -4,7 +4,7 @@ // deno-lint-ignore-file prefer-primordials import { Console } from "ext:deno_node/internal/console/constructor.mjs"; -import { windowOrWorkerGlobalScope } from "ext:runtime/98_global_scope.js"; +import { windowOrWorkerGlobalScope } from "ext:runtime/98_global_scope_shared.js"; // Don't rely on global `console` because during bootstrapping, it is pointing // to native `console` object provided by V8. const console = windowOrWorkerGlobalScope.console.value; diff --git a/runtime/js/98_global_scope.js b/runtime/js/98_global_scope_shared.js similarity index 63% rename from runtime/js/98_global_scope.js rename to runtime/js/98_global_scope_shared.js index de83195282..04a6e4bd38 100644 --- a/runtime/js/98_global_scope.js +++ b/runtime/js/98_global_scope_shared.js @@ -1,15 +1,8 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. -import { core, primordials } from "ext:core/mod.js"; -const ops = core.ops; -const { - ObjectDefineProperties, - ObjectPrototypeIsPrototypeOf, - SymbolFor, -} = primordials; +import { core } from "ext:core/mod.js"; import * as util from "ext:runtime/06_util.js"; -import * as location from "ext:deno_web/12_location.js"; import * as event from "ext:deno_web/02_event.js"; import * as timers from "ext:deno_web/02_timers.js"; import * as base64 from "ext:deno_web/05_base64.js"; @@ -38,15 +31,8 @@ import * as messagePort from "ext:deno_web/13_message_port.js"; import * as webidl from "ext:deno_webidl/00_webidl.js"; import { DOMException } from "ext:deno_web/01_dom_exception.js"; import * as abortSignal from "ext:deno_web/03_abort_signal.js"; -import * as globalInterfaces from "ext:deno_web/04_global_interfaces.js"; -import * as webStorage from "ext:deno_webstorage/01_webstorage.js"; -import * as prompt from "ext:runtime/41_prompt.js"; import * as imageData from "ext:deno_web/16_image_data.js"; -import { - loadWebGPU, - webgpu, - webGPUNonEnumerable, -} from "ext:deno_webgpu/00_init.js"; +import { webgpu, webGPUNonEnumerable } from "ext:deno_webgpu/00_init.js"; import * as webgpuSurface from "ext:deno_webgpu/02_surface.js"; import { unstableIds } from "ext:runtime/90_deno_ns.js"; @@ -198,189 +184,4 @@ unstableForWindowOrWorkerGlobalScope[unstableIds.webgpu] = { GPUCanvasContext: webGPUNonEnumerable(() => webgpuSurface.GPUCanvasContext), }; -class Navigator { - constructor() { - webidl.illegalConstructor(); - } - - [SymbolFor("Deno.privateCustomInspect")](inspect, inspectOptions) { - return inspect( - console.createFilteredInspectProxy({ - object: this, - evaluate: ObjectPrototypeIsPrototypeOf(NavigatorPrototype, this), - keys: [ - "hardwareConcurrency", - "userAgent", - "language", - "languages", - ], - }), - inspectOptions, - ); - } -} - -const navigator = webidl.createBranded(Navigator); - -function memoizeLazy(f) { - let v_ = null; - return () => { - if (v_ === null) { - v_ = f(); - } - return v_; - }; -} - -const numCpus = memoizeLazy(() => ops.op_bootstrap_numcpus()); -const userAgent = memoizeLazy(() => ops.op_bootstrap_user_agent()); -const language = memoizeLazy(() => ops.op_bootstrap_language()); - -ObjectDefineProperties(Navigator.prototype, { - gpu: { - configurable: true, - enumerable: true, - get() { - webidl.assertBranded(this, NavigatorPrototype); - loadWebGPU(); - return webgpu.gpu; - }, - }, - hardwareConcurrency: { - configurable: true, - enumerable: true, - get() { - webidl.assertBranded(this, NavigatorPrototype); - return numCpus(); - }, - }, - userAgent: { - configurable: true, - enumerable: true, - get() { - webidl.assertBranded(this, NavigatorPrototype); - return userAgent(); - }, - }, - language: { - configurable: true, - enumerable: true, - get() { - webidl.assertBranded(this, NavigatorPrototype); - return language(); - }, - }, - languages: { - configurable: true, - enumerable: true, - get() { - webidl.assertBranded(this, NavigatorPrototype); - return [language()]; - }, - }, -}); -const NavigatorPrototype = Navigator.prototype; - -class WorkerNavigator { - constructor() { - webidl.illegalConstructor(); - } - - [SymbolFor("Deno.privateCustomInspect")](inspect, inspectOptions) { - return inspect( - console.createFilteredInspectProxy({ - object: this, - evaluate: ObjectPrototypeIsPrototypeOf(WorkerNavigatorPrototype, this), - keys: [ - "hardwareConcurrency", - "userAgent", - "language", - "languages", - ], - }), - inspectOptions, - ); - } -} - -const workerNavigator = webidl.createBranded(WorkerNavigator); - -ObjectDefineProperties(WorkerNavigator.prototype, { - gpu: { - configurable: true, - enumerable: true, - get() { - webidl.assertBranded(this, WorkerNavigatorPrototype); - loadWebGPU(); - return webgpu.gpu; - }, - }, - hardwareConcurrency: { - configurable: true, - enumerable: true, - get() { - webidl.assertBranded(this, WorkerNavigatorPrototype); - return numCpus(); - }, - }, - userAgent: { - configurable: true, - enumerable: true, - get() { - webidl.assertBranded(this, WorkerNavigatorPrototype); - return userAgent(); - }, - }, - language: { - configurable: true, - enumerable: true, - get() { - webidl.assertBranded(this, WorkerNavigatorPrototype); - return language(); - }, - }, - languages: { - configurable: true, - enumerable: true, - get() { - webidl.assertBranded(this, WorkerNavigatorPrototype); - return [language()]; - }, - }, -}); -const WorkerNavigatorPrototype = WorkerNavigator.prototype; - -const mainRuntimeGlobalProperties = { - Location: location.locationConstructorDescriptor, - location: location.locationDescriptor, - Window: globalInterfaces.windowConstructorDescriptor, - window: util.getterOnly(() => globalThis), - self: util.getterOnly(() => globalThis), - Navigator: util.nonEnumerable(Navigator), - navigator: util.getterOnly(() => navigator), - alert: util.writable(prompt.alert), - confirm: util.writable(prompt.confirm), - prompt: util.writable(prompt.prompt), - localStorage: util.getterOnly(webStorage.localStorage), - sessionStorage: util.getterOnly(webStorage.sessionStorage), - Storage: util.nonEnumerable(webStorage.Storage), -}; - -const workerRuntimeGlobalProperties = { - WorkerLocation: location.workerLocationConstructorDescriptor, - location: location.workerLocationDescriptor, - WorkerGlobalScope: globalInterfaces.workerGlobalScopeConstructorDescriptor, - DedicatedWorkerGlobalScope: - globalInterfaces.dedicatedWorkerGlobalScopeConstructorDescriptor, - WorkerNavigator: util.nonEnumerable(WorkerNavigator), - navigator: util.getterOnly(() => workerNavigator), - self: util.getterOnly(() => globalThis), -}; - -export { - mainRuntimeGlobalProperties, - memoizeLazy, - unstableForWindowOrWorkerGlobalScope, - windowOrWorkerGlobalScope, - workerRuntimeGlobalProperties, -}; +export { unstableForWindowOrWorkerGlobalScope, windowOrWorkerGlobalScope }; diff --git a/runtime/js/98_global_scope_window.js b/runtime/js/98_global_scope_window.js new file mode 100644 index 0000000000..42cefdfb0c --- /dev/null +++ b/runtime/js/98_global_scope_window.js @@ -0,0 +1,119 @@ +// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. + +import { core, primordials } from "ext:core/mod.js"; +const ops = core.ops; +const { + ObjectDefineProperties, + ObjectPrototypeIsPrototypeOf, + SymbolFor, +} = primordials; + +import * as util from "ext:runtime/06_util.js"; +import * as location from "ext:deno_web/12_location.js"; +import * as console from "ext:deno_console/01_console.js"; +import * as webidl from "ext:deno_webidl/00_webidl.js"; +import * as globalInterfaces from "ext:deno_web/04_global_interfaces.js"; +import * as webStorage from "ext:deno_webstorage/01_webstorage.js"; +import * as prompt from "ext:runtime/41_prompt.js"; +import { loadWebGPU, webgpu } from "ext:deno_webgpu/00_init.js"; + +class Navigator { + constructor() { + webidl.illegalConstructor(); + } + + [SymbolFor("Deno.privateCustomInspect")](inspect, inspectOptions) { + return inspect( + console.createFilteredInspectProxy({ + object: this, + evaluate: ObjectPrototypeIsPrototypeOf(NavigatorPrototype, this), + keys: [ + "hardwareConcurrency", + "userAgent", + "language", + "languages", + ], + }), + inspectOptions, + ); + } +} + +const navigator = webidl.createBranded(Navigator); + +function memoizeLazy(f) { + let v_ = null; + return () => { + if (v_ === null) { + v_ = f(); + } + return v_; + }; +} + +const numCpus = memoizeLazy(() => ops.op_bootstrap_numcpus()); +const userAgent = memoizeLazy(() => ops.op_bootstrap_user_agent()); +const language = memoizeLazy(() => ops.op_bootstrap_language()); + +ObjectDefineProperties(Navigator.prototype, { + gpu: { + configurable: true, + enumerable: true, + get() { + webidl.assertBranded(this, NavigatorPrototype); + loadWebGPU(); + return webgpu.gpu; + }, + }, + hardwareConcurrency: { + configurable: true, + enumerable: true, + get() { + webidl.assertBranded(this, NavigatorPrototype); + return numCpus(); + }, + }, + userAgent: { + configurable: true, + enumerable: true, + get() { + webidl.assertBranded(this, NavigatorPrototype); + return userAgent(); + }, + }, + language: { + configurable: true, + enumerable: true, + get() { + webidl.assertBranded(this, NavigatorPrototype); + return language(); + }, + }, + languages: { + configurable: true, + enumerable: true, + get() { + webidl.assertBranded(this, NavigatorPrototype); + return [language()]; + }, + }, +}); +const NavigatorPrototype = Navigator.prototype; + +const mainRuntimeGlobalProperties = { + Location: location.locationConstructorDescriptor, + location: location.locationDescriptor, + Window: globalInterfaces.windowConstructorDescriptor, + window: util.getterOnly(() => globalThis), + self: util.getterOnly(() => globalThis), + Navigator: util.nonEnumerable(Navigator), + navigator: util.getterOnly(() => navigator), + alert: util.writable(prompt.alert), + confirm: util.writable(prompt.confirm), + prompt: util.writable(prompt.prompt), + localStorage: util.getterOnly(webStorage.localStorage), + sessionStorage: util.getterOnly(webStorage.sessionStorage), + Storage: util.nonEnumerable(webStorage.Storage), +}; + +export { mainRuntimeGlobalProperties, memoizeLazy }; diff --git a/runtime/js/98_global_scope_worker.js b/runtime/js/98_global_scope_worker.js new file mode 100644 index 0000000000..6b0ad480cd --- /dev/null +++ b/runtime/js/98_global_scope_worker.js @@ -0,0 +1,112 @@ +// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. + +import { core, primordials } from "ext:core/mod.js"; +const ops = core.ops; +const { + ObjectDefineProperties, + ObjectPrototypeIsPrototypeOf, + SymbolFor, +} = primordials; + +import * as util from "ext:runtime/06_util.js"; +import * as location from "ext:deno_web/12_location.js"; +import * as console from "ext:deno_console/01_console.js"; +import * as webidl from "ext:deno_webidl/00_webidl.js"; +import * as globalInterfaces from "ext:deno_web/04_global_interfaces.js"; +import { loadWebGPU, webgpu } from "ext:deno_webgpu/00_init.js"; + +function memoizeLazy(f) { + let v_ = null; + return () => { + if (v_ === null) { + v_ = f(); + } + return v_; + }; +} + +const numCpus = memoizeLazy(() => ops.op_bootstrap_numcpus()); +const userAgent = memoizeLazy(() => ops.op_bootstrap_user_agent()); +const language = memoizeLazy(() => ops.op_bootstrap_language()); + +class WorkerNavigator { + constructor() { + webidl.illegalConstructor(); + } + + [SymbolFor("Deno.privateCustomInspect")](inspect, inspectOptions) { + return inspect( + console.createFilteredInspectProxy({ + object: this, + evaluate: ObjectPrototypeIsPrototypeOf(WorkerNavigatorPrototype, this), + keys: [ + "hardwareConcurrency", + "userAgent", + "language", + "languages", + ], + }), + inspectOptions, + ); + } +} + +const workerNavigator = webidl.createBranded(WorkerNavigator); + +ObjectDefineProperties(WorkerNavigator.prototype, { + gpu: { + configurable: true, + enumerable: true, + get() { + webidl.assertBranded(this, WorkerNavigatorPrototype); + loadWebGPU(); + return webgpu.gpu; + }, + }, + hardwareConcurrency: { + configurable: true, + enumerable: true, + get() { + webidl.assertBranded(this, WorkerNavigatorPrototype); + return numCpus(); + }, + }, + userAgent: { + configurable: true, + enumerable: true, + get() { + webidl.assertBranded(this, WorkerNavigatorPrototype); + return userAgent(); + }, + }, + language: { + configurable: true, + enumerable: true, + get() { + webidl.assertBranded(this, WorkerNavigatorPrototype); + return language(); + }, + }, + languages: { + configurable: true, + enumerable: true, + get() { + webidl.assertBranded(this, WorkerNavigatorPrototype); + return [language()]; + }, + }, +}); +const WorkerNavigatorPrototype = WorkerNavigator.prototype; + +const workerRuntimeGlobalProperties = { + WorkerLocation: location.workerLocationConstructorDescriptor, + location: location.workerLocationDescriptor, + WorkerGlobalScope: globalInterfaces.workerGlobalScopeConstructorDescriptor, + DedicatedWorkerGlobalScope: + globalInterfaces.dedicatedWorkerGlobalScopeConstructorDescriptor, + WorkerNavigator: util.nonEnumerable(WorkerNavigator), + navigator: util.getterOnly(() => workerNavigator), + self: util.getterOnly(() => globalThis), +}; + +export { workerRuntimeGlobalProperties }; diff --git a/runtime/js/99_main.js b/runtime/js/99_main.js index 625d77f403..4644d2d08c 100644 --- a/runtime/js/99_main.js +++ b/runtime/js/99_main.js @@ -57,12 +57,16 @@ import { errors } from "ext:runtime/01_errors.js"; import * as webidl from "ext:deno_webidl/00_webidl.js"; import { DOMException } from "ext:deno_web/01_dom_exception.js"; import { - mainRuntimeGlobalProperties, - memoizeLazy, unstableForWindowOrWorkerGlobalScope, windowOrWorkerGlobalScope, +} from "ext:runtime/98_global_scope_shared.js"; +import { + mainRuntimeGlobalProperties, + memoizeLazy, +} from "ext:runtime/98_global_scope_window.js"; +import { workerRuntimeGlobalProperties, -} from "ext:runtime/98_global_scope.js"; +} from "ext:runtime/98_global_scope_worker.js"; import { SymbolAsyncDispose, SymbolDispose } from "ext:deno_web/00_infra.js"; // deno-lint-ignore prefer-primordials diff --git a/runtime/shared.rs b/runtime/shared.rs index 7dddbf9ece..692be94b85 100644 --- a/runtime/shared.rs +++ b/runtime/shared.rs @@ -49,7 +49,9 @@ extension!(runtime, "40_tty.js", "41_prompt.js", "90_deno_ns.js", - "98_global_scope.js" + "98_global_scope_shared.js", + "98_global_scope_window.js", + "98_global_scope_worker.js" ], customizer = |ext: &mut Extension| { #[cfg(not(feature = "exclude_runtime_main_js"))]