diff options
Diffstat (limited to 'runtime/js/06_util.js')
-rw-r--r-- | runtime/js/06_util.js | 255 |
1 files changed, 126 insertions, 129 deletions
diff --git a/runtime/js/06_util.js b/runtime/js/06_util.js index 391497ba8..6ad1b3ce1 100644 --- a/runtime/js/06_util.js +++ b/runtime/js/06_util.js @@ -1,150 +1,147 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. -"use strict"; -((window) => { - const { - decodeURIComponent, - ObjectPrototypeIsPrototypeOf, - Promise, - SafeArrayIterator, - StringPrototypeReplace, - TypeError, - } = window.__bootstrap.primordials; - const { build } = window.__bootstrap.build; - const { URLPrototype } = window.__bootstrap.url; - let logDebug = false; - let logSource = "JS"; +const internals = globalThis.__bootstrap.internals; +const primordials = globalThis.__bootstrap.primordials; +const { + decodeURIComponent, + ObjectPrototypeIsPrototypeOf, + Promise, + SafeArrayIterator, + StringPrototypeReplace, + TypeError, +} = primordials; +import { build } from "internal:runtime/js/01_build.js"; +import { URLPrototype } from "internal:ext/url/00_url.js"; +let logDebug = false; +let logSource = "JS"; - function setLogDebug(debug, source) { - logDebug = debug; - if (source) { - logSource = source; - } +function setLogDebug(debug, source) { + logDebug = debug; + if (source) { + logSource = source; } +} - function log(...args) { - if (logDebug) { - // if we destructure `console` off `globalThis` too early, we don't bind to - // the right console, therefore we don't log anything out. - globalThis.console.log( - `DEBUG ${logSource} -`, - ...new SafeArrayIterator(args), - ); - } +function log(...args) { + if (logDebug) { + // if we destructure `console` off `globalThis` too early, we don't bind to + // the right console, therefore we don't log anything out. + globalThis.console.log( + `DEBUG ${logSource} -`, + ...new SafeArrayIterator(args), + ); } +} - function createResolvable() { - let resolve; - let reject; - const promise = new Promise((res, rej) => { - resolve = res; - reject = rej; - }); - promise.resolve = resolve; - promise.reject = reject; - return promise; - } +function createResolvable() { + let resolve; + let reject; + const promise = new Promise((res, rej) => { + resolve = res; + reject = rej; + }); + promise.resolve = resolve; + promise.reject = reject; + return promise; +} - // Keep in sync with `fromFileUrl()` in `std/path/win32.ts`. - function pathFromURLWin32(url) { - let p = StringPrototypeReplace( - url.pathname, - /^\/*([A-Za-z]:)(\/|$)/, - "$1/", - ); - p = StringPrototypeReplace( - p, - /\//g, - "\\", - ); - p = StringPrototypeReplace( - p, - /%(?![0-9A-Fa-f]{2})/g, - "%25", - ); - let path = decodeURIComponent(p); - if (url.hostname != "") { - // Note: The `URL` implementation guarantees that the drive letter and - // hostname are mutually exclusive. Otherwise it would not have been valid - // to append the hostname and path like this. - path = `\\\\${url.hostname}${path}`; - } - return path; +// Keep in sync with `fromFileUrl()` in `std/path/win32.ts`. +function pathFromURLWin32(url) { + let p = StringPrototypeReplace( + url.pathname, + /^\/*([A-Za-z]:)(\/|$)/, + "$1/", + ); + p = StringPrototypeReplace( + p, + /\//g, + "\\", + ); + p = StringPrototypeReplace( + p, + /%(?![0-9A-Fa-f]{2})/g, + "%25", + ); + let path = decodeURIComponent(p); + if (url.hostname != "") { + // Note: The `URL` implementation guarantees that the drive letter and + // hostname are mutually exclusive. Otherwise it would not have been valid + // to append the hostname and path like this. + path = `\\\\${url.hostname}${path}`; } + return path; +} - // Keep in sync with `fromFileUrl()` in `std/path/posix.ts`. - function pathFromURLPosix(url) { - if (url.hostname !== "") { - throw new TypeError(`Host must be empty.`); - } - - return decodeURIComponent( - StringPrototypeReplace(url.pathname, /%(?![0-9A-Fa-f]{2})/g, "%25"), - ); +// Keep in sync with `fromFileUrl()` in `std/path/posix.ts`. +function pathFromURLPosix(url) { + if (url.hostname !== "") { + throw new TypeError(`Host must be empty.`); } - function pathFromURL(pathOrUrl) { - if (ObjectPrototypeIsPrototypeOf(URLPrototype, pathOrUrl)) { - if (pathOrUrl.protocol != "file:") { - throw new TypeError("Must be a file URL."); - } + return decodeURIComponent( + StringPrototypeReplace(url.pathname, /%(?![0-9A-Fa-f]{2})/g, "%25"), + ); +} - return build.os == "windows" - ? pathFromURLWin32(pathOrUrl) - : pathFromURLPosix(pathOrUrl); +function pathFromURL(pathOrUrl) { + if (ObjectPrototypeIsPrototypeOf(URLPrototype, pathOrUrl)) { + if (pathOrUrl.protocol != "file:") { + throw new TypeError("Must be a file URL."); } - return pathOrUrl; - } - - window.__bootstrap.internals = { - ...window.__bootstrap.internals ?? {}, - pathFromURL, - }; - function writable(value) { - return { - value, - writable: true, - enumerable: true, - configurable: true, - }; + return build.os == "windows" + ? pathFromURLWin32(pathOrUrl) + : pathFromURLPosix(pathOrUrl); } + return pathOrUrl; +} - function nonEnumerable(value) { - return { - value, - writable: true, - enumerable: false, - configurable: true, - }; - } +// TODO(bartlomieju): remove +internals.pathFromURL = pathFromURL; - function readOnly(value) { - return { - value, - enumerable: true, - writable: false, - configurable: true, - }; - } +function writable(value) { + return { + value, + writable: true, + enumerable: true, + configurable: true, + }; +} - function getterOnly(getter) { - return { - get: getter, - set() {}, - enumerable: true, - configurable: true, - }; - } +function nonEnumerable(value) { + return { + value, + writable: true, + enumerable: false, + configurable: true, + }; +} - window.__bootstrap.util = { - log, - setLogDebug, - createResolvable, - pathFromURL, - writable, - nonEnumerable, - readOnly, - getterOnly, +function readOnly(value) { + return { + value, + enumerable: true, + writable: false, + configurable: true, }; -})(this); +} + +function getterOnly(getter) { + return { + get: getter, + set() {}, + enumerable: true, + configurable: true, + }; +} + +export { + createResolvable, + getterOnly, + log, + nonEnumerable, + pathFromURL, + readOnly, + setLogDebug, + writable, +}; |