diff options
author | Leo Kettmeir <crowlkats@toaxl.com> | 2023-02-07 20:22:46 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-07 20:22:46 +0100 |
commit | b4aa1530970f7b9cc4e6f2f27e077852c4e178d3 (patch) | |
tree | 3d008912affe8550692183bd2697a386db5e3c79 /ext/url/01_urlpattern.js | |
parent | 65500f36e870b4ada3996b06aa287e30177d21a3 (diff) |
refactor: Use ES modules for internal runtime code (#17648)
This PR refactors all internal js files (except core) to be written as
ES modules.
`__bootstrap`has been mostly replaced with static imports in form in
`internal:[path to file from repo root]`.
To specify if files are ESM, an `esm` method has been added to
`Extension`, similar to the `js` method.
A new ModuleLoader called `InternalModuleLoader` has been added to
enable the loading of internal specifiers, which is used in all
situations except when a snapshot is only loaded, and not a new one is
created from it.
---------
Co-authored-by: Bartek IwaĆczuk <biwanczuk@gmail.com>
Diffstat (limited to 'ext/url/01_urlpattern.js')
-rw-r--r-- | ext/url/01_urlpattern.js | 453 |
1 files changed, 224 insertions, 229 deletions
diff --git a/ext/url/01_urlpattern.js b/ext/url/01_urlpattern.js index 14f052551..1c5882553 100644 --- a/ext/url/01_urlpattern.js +++ b/ext/url/01_urlpattern.js @@ -7,268 +7,263 @@ /// <reference path="./internal.d.ts" /> /// <reference path="./lib.deno_url.d.ts" /> -"use strict"; - -((window) => { - const core = window.Deno.core; - const ops = core.ops; - const webidl = window.__bootstrap.webidl; - const { - ArrayPrototypeMap, - ObjectKeys, - ObjectFromEntries, - RegExp, - RegExpPrototypeExec, - RegExpPrototypeTest, - Symbol, - SymbolFor, - TypeError, - } = window.__bootstrap.primordials; - - const _components = Symbol("components"); +const core = globalThis.Deno.core; +const ops = core.ops; +import * as webidl from "internal:ext/webidl/00_webidl.js"; +const primordials = globalThis.__bootstrap.primordials; +const { + ArrayPrototypeMap, + ObjectKeys, + ObjectFromEntries, + RegExp, + RegExpPrototypeExec, + RegExpPrototypeTest, + Symbol, + SymbolFor, + TypeError, +} = primordials; + +const _components = Symbol("components"); + +/** + * @typedef Components + * @property {Component} protocol + * @property {Component} username + * @property {Component} password + * @property {Component} hostname + * @property {Component} port + * @property {Component} pathname + * @property {Component} search + * @property {Component} hash + */ + +/** + * @typedef Component + * @property {string} patternString + * @property {RegExp} regexp + * @property {string[]} groupNameList + */ + +class URLPattern { + /** @type {Components} */ + [_components]; /** - * @typedef Components - * @property {Component} protocol - * @property {Component} username - * @property {Component} password - * @property {Component} hostname - * @property {Component} port - * @property {Component} pathname - * @property {Component} search - * @property {Component} hash + * @param {URLPatternInput} input + * @param {string} [baseURL] */ - - /** - * @typedef Component - * @property {string} patternString - * @property {RegExp} regexp - * @property {string[]} groupNameList - */ - - class URLPattern { - /** @type {Components} */ - [_components]; - - /** - * @param {URLPatternInput} input - * @param {string} [baseURL] - */ - constructor(input, baseURL = undefined) { - this[webidl.brand] = webidl.brand; - const prefix = "Failed to construct 'URLPattern'"; - webidl.requiredArguments(arguments.length, 1, { prefix }); - input = webidl.converters.URLPatternInput(input, { + constructor(input, baseURL = undefined) { + this[webidl.brand] = webidl.brand; + const prefix = "Failed to construct 'URLPattern'"; + webidl.requiredArguments(arguments.length, 1, { prefix }); + input = webidl.converters.URLPatternInput(input, { + prefix, + context: "Argument 1", + }); + if (baseURL !== undefined) { + baseURL = webidl.converters.USVString(baseURL, { prefix, - context: "Argument 1", + context: "Argument 2", }); - if (baseURL !== undefined) { - baseURL = webidl.converters.USVString(baseURL, { - prefix, - context: "Argument 2", - }); - } + } - const components = ops.op_urlpattern_parse(input, baseURL); - - const keys = ObjectKeys(components); - for (let i = 0; i < keys.length; ++i) { - const key = keys[i]; - try { - components[key].regexp = new RegExp( - components[key].regexpString, - "u", - ); - } catch (e) { - throw new TypeError(`${prefix}: ${key} is invalid; ${e.message}`); - } - } + const components = ops.op_urlpattern_parse(input, baseURL); - this[_components] = components; + const keys = ObjectKeys(components); + for (let i = 0; i < keys.length; ++i) { + const key = keys[i]; + try { + components[key].regexp = new RegExp( + components[key].regexpString, + "u", + ); + } catch (e) { + throw new TypeError(`${prefix}: ${key} is invalid; ${e.message}`); + } } - get protocol() { - webidl.assertBranded(this, URLPatternPrototype); - return this[_components].protocol.patternString; - } + this[_components] = components; + } - get username() { - webidl.assertBranded(this, URLPatternPrototype); - return this[_components].username.patternString; - } + get protocol() { + webidl.assertBranded(this, URLPatternPrototype); + return this[_components].protocol.patternString; + } - get password() { - webidl.assertBranded(this, URLPatternPrototype); - return this[_components].password.patternString; - } + get username() { + webidl.assertBranded(this, URLPatternPrototype); + return this[_components].username.patternString; + } - get hostname() { - webidl.assertBranded(this, URLPatternPrototype); - return this[_components].hostname.patternString; - } + get password() { + webidl.assertBranded(this, URLPatternPrototype); + return this[_components].password.patternString; + } - get port() { - webidl.assertBranded(this, URLPatternPrototype); - return this[_components].port.patternString; - } + get hostname() { + webidl.assertBranded(this, URLPatternPrototype); + return this[_components].hostname.patternString; + } - get pathname() { - webidl.assertBranded(this, URLPatternPrototype); - return this[_components].pathname.patternString; - } + get port() { + webidl.assertBranded(this, URLPatternPrototype); + return this[_components].port.patternString; + } - get search() { - webidl.assertBranded(this, URLPatternPrototype); - return this[_components].search.patternString; - } + get pathname() { + webidl.assertBranded(this, URLPatternPrototype); + return this[_components].pathname.patternString; + } - get hash() { - webidl.assertBranded(this, URLPatternPrototype); - return this[_components].hash.patternString; - } + get search() { + webidl.assertBranded(this, URLPatternPrototype); + return this[_components].search.patternString; + } + + get hash() { + webidl.assertBranded(this, URLPatternPrototype); + return this[_components].hash.patternString; + } - /** - * @param {URLPatternInput} input - * @param {string} [baseURL] - * @returns {boolean} - */ - test(input, baseURL = undefined) { - webidl.assertBranded(this, URLPatternPrototype); - const prefix = "Failed to execute 'test' on 'URLPattern'"; - webidl.requiredArguments(arguments.length, 1, { prefix }); - input = webidl.converters.URLPatternInput(input, { + /** + * @param {URLPatternInput} input + * @param {string} [baseURL] + * @returns {boolean} + */ + test(input, baseURL = undefined) { + webidl.assertBranded(this, URLPatternPrototype); + const prefix = "Failed to execute 'test' on 'URLPattern'"; + webidl.requiredArguments(arguments.length, 1, { prefix }); + input = webidl.converters.URLPatternInput(input, { + prefix, + context: "Argument 1", + }); + if (baseURL !== undefined) { + baseURL = webidl.converters.USVString(baseURL, { prefix, - context: "Argument 1", + context: "Argument 2", }); - if (baseURL !== undefined) { - baseURL = webidl.converters.USVString(baseURL, { - prefix, - context: "Argument 2", - }); - } + } - const res = ops.op_urlpattern_process_match_input( - input, - baseURL, - ); - if (res === null) { - return false; - } + const res = ops.op_urlpattern_process_match_input( + input, + baseURL, + ); + if (res === null) { + return false; + } - const values = res[0]; + const values = res[0]; - const keys = ObjectKeys(values); - for (let i = 0; i < keys.length; ++i) { - const key = keys[i]; - if (!RegExpPrototypeTest(this[_components][key].regexp, values[key])) { - return false; - } + const keys = ObjectKeys(values); + for (let i = 0; i < keys.length; ++i) { + const key = keys[i]; + if (!RegExpPrototypeTest(this[_components][key].regexp, values[key])) { + return false; } - - return true; } - /** - * @param {URLPatternInput} input - * @param {string} [baseURL] - * @returns {URLPatternResult | null} - */ - exec(input, baseURL = undefined) { - webidl.assertBranded(this, URLPatternPrototype); - const prefix = "Failed to execute 'exec' on 'URLPattern'"; - webidl.requiredArguments(arguments.length, 1, { prefix }); - input = webidl.converters.URLPatternInput(input, { + return true; + } + + /** + * @param {URLPatternInput} input + * @param {string} [baseURL] + * @returns {URLPatternResult | null} + */ + exec(input, baseURL = undefined) { + webidl.assertBranded(this, URLPatternPrototype); + const prefix = "Failed to execute 'exec' on 'URLPattern'"; + webidl.requiredArguments(arguments.length, 1, { prefix }); + input = webidl.converters.URLPatternInput(input, { + prefix, + context: "Argument 1", + }); + if (baseURL !== undefined) { + baseURL = webidl.converters.USVString(baseURL, { prefix, - context: "Argument 1", + context: "Argument 2", }); - if (baseURL !== undefined) { - baseURL = webidl.converters.USVString(baseURL, { - prefix, - context: "Argument 2", - }); - } + } - const res = ops.op_urlpattern_process_match_input( - input, - baseURL, - ); - if (res === null) { - return null; - } + const res = ops.op_urlpattern_process_match_input( + input, + baseURL, + ); + if (res === null) { + return null; + } - const { 0: values, 1: inputs } = res; - if (inputs[1] === null) { - inputs.pop(); - } + const { 0: values, 1: inputs } = res; + if (inputs[1] === null) { + inputs.pop(); + } - /** @type {URLPatternResult} */ - const result = { inputs }; - - const keys = ObjectKeys(values); - for (let i = 0; i < keys.length; ++i) { - const key = keys[i]; - /** @type {Component} */ - const component = this[_components][key]; - const input = values[key]; - const match = RegExpPrototypeExec(component.regexp, input); - if (match === null) { - return null; - } - const groupEntries = ArrayPrototypeMap( - component.groupNameList, - (name, i) => [name, match[i + 1] ?? ""], - ); - const groups = ObjectFromEntries(groupEntries); - result[key] = { - input, - groups, - }; + /** @type {URLPatternResult} */ + const result = { inputs }; + + const keys = ObjectKeys(values); + for (let i = 0; i < keys.length; ++i) { + const key = keys[i]; + /** @type {Component} */ + const component = this[_components][key]; + const input = values[key]; + const match = RegExpPrototypeExec(component.regexp, input); + if (match === null) { + return null; } - - return result; + const groupEntries = ArrayPrototypeMap( + component.groupNameList, + (name, i) => [name, match[i + 1] ?? ""], + ); + const groups = ObjectFromEntries(groupEntries); + result[key] = { + input, + groups, + }; } - [SymbolFor("Deno.customInspect")](inspect) { - return `URLPattern ${ - inspect({ - protocol: this.protocol, - username: this.username, - password: this.password, - hostname: this.hostname, - port: this.port, - pathname: this.pathname, - search: this.search, - hash: this.hash, - }) - }`; - } + return result; } - webidl.configurePrototype(URLPattern); - const URLPatternPrototype = URLPattern.prototype; - - webidl.converters.URLPatternInit = webidl - .createDictionaryConverter("URLPatternInit", [ - { key: "protocol", converter: webidl.converters.USVString }, - { key: "username", converter: webidl.converters.USVString }, - { key: "password", converter: webidl.converters.USVString }, - { key: "hostname", converter: webidl.converters.USVString }, - { key: "port", converter: webidl.converters.USVString }, - { key: "pathname", converter: webidl.converters.USVString }, - { key: "search", converter: webidl.converters.USVString }, - { key: "hash", converter: webidl.converters.USVString }, - { key: "baseURL", converter: webidl.converters.USVString }, - ]); - - webidl.converters["URLPatternInput"] = (V, opts) => { - // Union for (URLPatternInit or USVString) - if (typeof V == "object") { - return webidl.converters.URLPatternInit(V, opts); - } - return webidl.converters.USVString(V, opts); - }; + [SymbolFor("Deno.customInspect")](inspect) { + return `URLPattern ${ + inspect({ + protocol: this.protocol, + username: this.username, + password: this.password, + hostname: this.hostname, + port: this.port, + pathname: this.pathname, + search: this.search, + hash: this.hash, + }) + }`; + } +} + +webidl.configurePrototype(URLPattern); +const URLPatternPrototype = URLPattern.prototype; + +webidl.converters.URLPatternInit = webidl + .createDictionaryConverter("URLPatternInit", [ + { key: "protocol", converter: webidl.converters.USVString }, + { key: "username", converter: webidl.converters.USVString }, + { key: "password", converter: webidl.converters.USVString }, + { key: "hostname", converter: webidl.converters.USVString }, + { key: "port", converter: webidl.converters.USVString }, + { key: "pathname", converter: webidl.converters.USVString }, + { key: "search", converter: webidl.converters.USVString }, + { key: "hash", converter: webidl.converters.USVString }, + { key: "baseURL", converter: webidl.converters.USVString }, + ]); + +webidl.converters["URLPatternInput"] = (V, opts) => { + // Union for (URLPatternInit or USVString) + if (typeof V == "object") { + return webidl.converters.URLPatternInit(V, opts); + } + return webidl.converters.USVString(V, opts); +}; - window.__bootstrap.urlPattern = { - URLPattern, - }; -})(globalThis); +export { URLPattern }; |