diff options
Diffstat (limited to 'runtime/js')
-rw-r--r-- | runtime/js/90_deno_ns.js | 119 | ||||
-rw-r--r-- | runtime/js/98_global_scope.js | 8 | ||||
-rw-r--r-- | runtime/js/99_main.js | 52 |
3 files changed, 111 insertions, 68 deletions
diff --git a/runtime/js/90_deno_ns.js b/runtime/js/90_deno_ns.js index 834e5d1a3..83c4a4d03 100644 --- a/runtime/js/90_deno_ns.js +++ b/runtime/js/90_deno_ns.js @@ -155,64 +155,69 @@ const denoNs = { }; // NOTE(bartlomieju): keep IDs in sync with `cli/main.rs` -const denoNsUnstableById = { - // BroadcastChannel is always available? - // 1: {}, - - 2: { - cron: cron.cron, - }, - - // FFI - 3: { - dlopen: ffi.dlopen, - UnsafeCallback: ffi.UnsafeCallback, - UnsafePointer: ffi.UnsafePointer, - UnsafePointerView: ffi.UnsafePointerView, - UnsafeFnPointer: ffi.UnsafeFnPointer, - }, - - // FS - 4: { - flock: fs.flock, - flockSync: fs.flockSync, - funlock: fs.funlock, - funlockSync: fs.funlockSync, - umask: fs.umask, - }, - - // HTTP - 5: { - HttpClient: httpClient.HttpClient, - createHttpClient: httpClient.createHttpClient, - // TODO(bartlomieju): why is it needed? - http, - upgradeHttp: http.upgradeHttp, - }, - - // KV - 6: { - openKv: kv.openKv, - AtomicOperation: kv.AtomicOperation, - Kv: kv.Kv, - KvU64: kv.KvU64, - KvListIterator: kv.KvListIterator, - }, - - // net - 7: { - listenDatagram: net.createListenDatagram( - ops.op_net_listen_udp, - ops.op_net_listen_unixpacket, - ), - }, - // Unsafe proto - // 8: {}, - - // Worker options - // 9: {} +const unstableIds = { + broadcastChannel: 1, + cron: 2, + ffi: 3, + fs: 4, + http: 5, + kv: 6, + net: 7, + unsafeProto: 8, + workerOptions: 9, }; +const denoNsUnstableById = {}; + +// denoNsUnstableById[unstableIds.broadcastChannel] = {} + +denoNsUnstableById[unstableIds.cron] = { + cron: cron.cron, +}; + +denoNsUnstableById[unstableIds.ffi] = { + dlopen: ffi.dlopen, + UnsafeCallback: ffi.UnsafeCallback, + UnsafePointer: ffi.UnsafePointer, + UnsafePointerView: ffi.UnsafePointerView, + UnsafeFnPointer: ffi.UnsafeFnPointer, +}; + +denoNsUnstableById[unstableIds.fs] = { + flock: fs.flock, + flockSync: fs.flockSync, + funlock: fs.funlock, + funlockSync: fs.funlockSync, + umask: fs.umask, +}; + +denoNsUnstableById[unstableIds.http] = { + HttpClient: httpClient.HttpClient, + createHttpClient: httpClient.createHttpClient, + // TODO(bartlomieju): why is it needed? + http, + upgradeHttp: http.upgradeHttp, +}; + +denoNsUnstableById[unstableIds.kv] = { + openKv: kv.openKv, + AtomicOperation: kv.AtomicOperation, + Kv: kv.Kv, + KvU64: kv.KvU64, + KvListIterator: kv.KvListIterator, +}; + +denoNsUnstableById[unstableIds.net] = { + listenDatagram: net.createListenDatagram( + ops.op_net_listen_udp, + ops.op_net_listen_unixpacket, + ), +}; + +// denoNsUnstableById[unstableIds.unsafeProto] = {} + +// denoNsUnstableById[unstableIds.workerOptions] = {} + // when editing this list, also update unstableDenoProps in cli/tsc/99_main_compiler.js const denoNsUnstable = { listenDatagram: net.createListenDatagram( @@ -242,4 +247,4 @@ const denoNsUnstable = { cron: cron.cron, }; -export { denoNs, denoNsUnstable, denoNsUnstableById }; +export { denoNs, denoNsUnstable, denoNsUnstableById, unstableIds }; diff --git a/runtime/js/98_global_scope.js b/runtime/js/98_global_scope.js index 56354948e..65a9c6933 100644 --- a/runtime/js/98_global_scope.js +++ b/runtime/js/98_global_scope.js @@ -42,6 +42,7 @@ 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 { unstableIds } from "ext:runtime/90_deno_ns.js"; // https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope const windowOrWorkerGlobalScope = { @@ -143,8 +144,11 @@ const windowOrWorkerGlobalScope = { [webidl.brand]: util.nonEnumerable(webidl.brand), }; -const unstableWindowOrWorkerGlobalScope = { +const unstableForWindowOrWorkerGlobalScope = {}; +unstableForWindowOrWorkerGlobalScope[unstableIds.broadcastChannel] = { BroadcastChannel: util.nonEnumerable(broadcastChannel.BroadcastChannel), +}; +unstableForWindowOrWorkerGlobalScope[unstableIds.net] = { WebSocketStream: util.nonEnumerable(webSocketStream.WebSocketStream), }; @@ -312,7 +316,7 @@ const workerRuntimeGlobalProperties = { export { mainRuntimeGlobalProperties, memoizeLazy, - unstableWindowOrWorkerGlobalScope, + unstableForWindowOrWorkerGlobalScope, windowOrWorkerGlobalScope, workerRuntimeGlobalProperties, }; diff --git a/runtime/js/99_main.js b/runtime/js/99_main.js index 22352bf29..0469b38bf 100644 --- a/runtime/js/99_main.js +++ b/runtime/js/99_main.js @@ -17,8 +17,10 @@ const { ObjectAssign, ObjectDefineProperties, ObjectDefineProperty, + ObjectKeys, ObjectPrototypeIsPrototypeOf, ObjectSetPrototypeOf, + ObjectValues, PromisePrototypeThen, PromiseResolve, Symbol, @@ -47,6 +49,7 @@ import { denoNs, denoNsUnstable, denoNsUnstableById, + unstableIds, } from "ext:runtime/90_deno_ns.js"; import { errors } from "ext:runtime/01_errors.js"; import * as webidl from "ext:deno_webidl/00_webidl.js"; @@ -54,7 +57,7 @@ import DOMException from "ext:deno_web/01_dom_exception.js"; import { mainRuntimeGlobalProperties, memoizeLazy, - unstableWindowOrWorkerGlobalScope, + unstableForWindowOrWorkerGlobalScope, windowOrWorkerGlobalScope, workerRuntimeGlobalProperties, } from "ext:runtime/98_global_scope.js"; @@ -370,6 +373,35 @@ let hasBootstrapped = false; delete globalThis.console; // Set up global properties shared by main and worker runtime. ObjectDefineProperties(globalThis, windowOrWorkerGlobalScope); + +// Set up global properties shared by main and worker runtime that are exposed +// by unstable features if those are enabled. +function exposeUnstableFeaturesForWindowOrWorkerGlobalScope(options) { + const { unstableFlag, unstableFeatures } = options; + if (unstableFlag) { + const all = ObjectValues(unstableForWindowOrWorkerGlobalScope); + for (let i = 0; i <= all.length; i++) { + const props = all[i]; + ObjectDefineProperties(globalThis, { ...props }); + } + } else { + const featureIds = ArrayPrototypeMap( + ObjectKeys( + unstableForWindowOrWorkerGlobalScope, + ), + (k) => k | 0, + ); + + for (let i = 0; i <= featureIds.length; i++) { + const featureId = featureIds[i]; + if (ArrayPrototypeIncludes(unstableFeatures, featureId)) { + const props = unstableForWindowOrWorkerGlobalScope[featureId]; + ObjectDefineProperties(globalThis, { ...props }); + } + } + } +} + // FIXME(bartlomieju): temporarily add whole `Deno.core` to // `Deno[Deno.internal]` namespace. It should be removed and only necessary // methods should be left there. @@ -430,9 +462,10 @@ function bootstrapMainRuntime(runtimeOptions) { location.setLocationHref(location_); } - if (unstableFlag) { - ObjectDefineProperties(globalThis, unstableWindowOrWorkerGlobalScope); - } + exposeUnstableFeaturesForWindowOrWorkerGlobalScope({ + unstableFlag, + unstableFeatures, + }); ObjectDefineProperties(globalThis, mainRuntimeGlobalProperties); ObjectDefineProperties(globalThis, { // TODO(bartlomieju): in the future we might want to change the @@ -501,7 +534,7 @@ function bootstrapMainRuntime(runtimeOptions) { } } - if (!ArrayPrototypeIncludes(unstableFeatures, /* unsafe-proto */ 8)) { + if (!ArrayPrototypeIncludes(unstableFeatures, unstableIds.unsafeProto)) { // Removes the `__proto__` for security reasons. // https://tc39.es/ecma262/#sec-get-object.prototype.__proto__ delete Object.prototype.__proto__; @@ -547,9 +580,10 @@ function bootstrapWorkerRuntime( delete globalThis.nodeBootstrap; hasBootstrapped = true; - if (unstableFlag) { - ObjectDefineProperties(globalThis, unstableWindowOrWorkerGlobalScope); - } + exposeUnstableFeaturesForWindowOrWorkerGlobalScope({ + unstableFlag, + unstableFeatures, + }); ObjectDefineProperties(globalThis, workerRuntimeGlobalProperties); ObjectDefineProperties(globalThis, { name: util.writable(name), @@ -604,7 +638,7 @@ function bootstrapWorkerRuntime( } } - if (!ArrayPrototypeIncludes(unstableFeatures, /* unsafe-proto */ 8)) { + if (!ArrayPrototypeIncludes(unstableFeatures, unstableIds.unsafeProto)) { // Removes the `__proto__` for security reasons. // https://tc39.es/ecma262/#sec-get-object.prototype.__proto__ delete Object.prototype.__proto__; |