deno/runtime/js/98_global_scope_worker.js
Kenta Moriuchi f0a3d20642
fix(runtime): use more null proto objects again (#25040)
proceed with #23921

This PR is a preparation for
https://github.com/denoland/deno_lint/pull/1307

---------

Signed-off-by: Kenta Moriuchi <moriken@kimamass.com>
Co-authored-by: Luca Casonato <hello@lcas.dev>
2024-09-06 12:52:59 +02:00

121 lines
3.1 KiB
JavaScript

// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { core, primordials } from "ext:core/mod.js";
import {
op_bootstrap_language,
op_bootstrap_numcpus,
op_bootstrap_user_agent,
} from "ext:core/ops";
const {
ObjectDefineProperties,
ObjectPrototypeIsPrototypeOf,
SymbolFor,
} = primordials;
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 } from "ext:deno_webgpu/00_init.js";
function memoizeLazy(f) {
let v_ = null;
return () => {
if (v_ === null) {
v_ = f();
}
return v_;
};
}
const numCpus = memoizeLazy(() => op_bootstrap_numcpus());
const userAgent = memoizeLazy(() => op_bootstrap_user_agent());
const language = memoizeLazy(() => 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: {
__proto__: null,
configurable: true,
enumerable: true,
get() {
webidl.assertBranded(this, WorkerNavigatorPrototype);
const webgpu = loadWebGPU();
return webgpu.gpu;
},
},
hardwareConcurrency: {
__proto__: null,
configurable: true,
enumerable: true,
get() {
webidl.assertBranded(this, WorkerNavigatorPrototype);
return numCpus();
},
},
userAgent: {
__proto__: null,
configurable: true,
enumerable: true,
get() {
webidl.assertBranded(this, WorkerNavigatorPrototype);
return userAgent();
},
},
language: {
__proto__: null,
configurable: true,
enumerable: true,
get() {
webidl.assertBranded(this, WorkerNavigatorPrototype);
return language();
},
},
languages: {
__proto__: null,
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: core.propNonEnumerable(WorkerNavigator),
navigator: core.propGetterOnly(() => workerNavigator),
self: core.propGetterOnly(() => globalThis),
};
export { workerRuntimeGlobalProperties };