[gjs/esm/static-imports: 3/3] fixup - Some renames
- From: Philip Chimento <pchimento src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs/esm/static-imports: 3/3] fixup - Some renames
- Date: Fri, 5 Feb 2021 05:42:32 +0000 (UTC)
commit e5bf4896dae6a96e4992fda680c9fd26fbff1aa4
Author: Philip Chimento <philip chimento gmail com>
Date: Thu Feb 4 21:41:19 2021 -0800
fixup - Some renames
- ESModule -> ModulePrivate
- ModuleLoader -> InternalModuleLoader
- ESModuleLoader -> ModuleLoader
- move internal/modules/gi.js into ModuleLoader
TODO: Add comments to internalLoader.js and loader.js explaining the the
role of each of the module loaders
gjs/context.cpp | 20 ++++-----
js.gresource.xml | 6 +--
modules/internal/entry.js | 7 ----
.../{bootstrap/module.js => internalLoader.js} | 18 ++++-----
modules/internal/{modules/esm.js => loader.js} | 47 +++++++++++++++-------
modules/internal/modules/gi.js | 22 ----------
6 files changed, 53 insertions(+), 67 deletions(-)
---
diff --git a/gjs/context.cpp b/gjs/context.cpp
index 1ec0b97e..ab3da86e 100644
--- a/gjs/context.cpp
+++ b/gjs/context.cpp
@@ -557,29 +557,29 @@ GjsContextPrivate::GjsContextPrivate(JSContext* cx, GjsContext* public_context)
g_error("Failed to define module global in internal global.");
}
- if (!gjs_load_internal_module(cx, "bootstrap/module")) {
+ if (!gjs_load_internal_module(cx, "internalLoader")) {
gjs_log_exception(cx);
g_error("Failed to load internal module loaders.");
}
- JS::RootedObject entry(
+ JS::RootedObject loader(
cx, gjs_module_load(
- cx, "resource:///org/gnome/gjs/modules/internal/entry.js",
- "resource:///org/gnome/gjs/modules/internal/entry.js"));
+ cx, "resource:///org/gnome/gjs/modules/internal/loader.js",
+ "resource:///org/gnome/gjs/modules/internal/loader.js"));
- if (!entry) {
+ if (!loader) {
gjs_log_exception(cx);
- g_error("Failed to load internal entry module.");
+ g_error("Failed to load module loader module.");
}
- if (!JS::ModuleInstantiate(cx, entry)) {
+ if (!JS::ModuleInstantiate(cx, loader)) {
gjs_log_exception(cx);
- g_error("Failed to instantiate internal entry module.");
+ g_error("Failed to instantiate module loader module.");
}
- if (!JS::ModuleEvaluate(cx, entry)) {
+ if (!JS::ModuleEvaluate(cx, loader)) {
gjs_log_exception(cx);
- g_error("Failed to evaluate internal entry module.");
+ g_error("Failed to evaluate module loader module.");
}
}
diff --git a/js.gresource.xml b/js.gresource.xml
index adc8edde..fc55e597 100644
--- a/js.gresource.xml
+++ b/js.gresource.xml
@@ -4,10 +4,8 @@
<gresources>
<gresource prefix="/org/gnome/gjs">
<!-- Internal modules -->
- <file>modules/internal/bootstrap/module.js</file>
- <file>modules/internal/entry.js</file>
- <file>modules/internal/modules/esm.js</file>
- <file>modules/internal/modules/gi.js</file>
+ <file>modules/internal/internalLoader.js</file>
+ <file>modules/internal/loader.js</file>
<!-- ESM-based modules -->
<file>modules/esm/cairo.js</file>
diff --git a/modules/internal/bootstrap/module.js b/modules/internal/internalLoader.js
similarity index 91%
rename from modules/internal/bootstrap/module.js
rename to modules/internal/internalLoader.js
index d6fad676..b9c1b074 100644
--- a/modules/internal/bootstrap/module.js
+++ b/modules/internal/internalLoader.js
@@ -21,9 +21,9 @@ export class ImportError extends Error {
}
/**
- * ESModule is the "private" object of every module.
+ * ModulePrivate is the "private" object of every module.
*/
-export class ESModule {
+export class ModulePrivate {
/**
*
* @param {string} id the module's identifier
@@ -53,7 +53,7 @@ function isRelativePath(path) {
*
* @class
*/
-export class ModuleLoader {
+export class InternalModuleLoader {
/**
* @param {typeof globalThis} global the global object to handle module resolution
*/
@@ -123,7 +123,7 @@ export class ModuleLoader {
/**
* Compiles a module source text with the module's URI
*
- * @param {ESModule} module a module private object
+ * @param {ModulePrivate} module a module private object
* @param {string} text the module source text to compile
* @returns {import("../types").Module}
*/
@@ -164,8 +164,8 @@ export class ModuleLoader {
const [text, internal = false] = result;
- const esmodule = new ESModule(uri.uri, uri.uri, internal);
- const compiled = this.compileModule(esmodule, text);
+ const priv = new ModulePrivate(uri.uri, uri.uri, internal);
+ const compiled = this.compileModule(priv, text);
if (!compiled)
throw new ImportError(`Failed to register module: ${uri}`);
@@ -178,7 +178,7 @@ export class ModuleLoader {
}
}
-export const internalModuleLoader = new ModuleLoader(globalThis);
+export const internalModuleLoader = new InternalModuleLoader(globalThis);
setModuleResolveHook(globalThis, (module, specifier) => {
const resolved = internalModuleLoader.resolveModule(specifier, module?.uri ?? null);
@@ -193,14 +193,14 @@ setModuleMetaHook(globalThis, (module, meta) => {
});
setModuleLoadHook(globalThis, (id, uri) => {
- const m = new ESModule(id, uri);
+ const priv = new ModulePrivate(id, uri);
const result = internalModuleLoader.loadURI(parseURI(uri));
if (!result)
throw new ImportError(`URI not found: ${uri}`);
const [text] = result;
- const compiled = internalModuleLoader.compileModule(m, text);
+ const compiled = internalModuleLoader.compileModule(priv, text);
const registry = getRegistry(globalThis);
registry.set(uri, compiled);
diff --git a/modules/internal/modules/esm.js b/modules/internal/loader.js
similarity index 79%
rename from modules/internal/modules/esm.js
rename to modules/internal/loader.js
index a96707b5..55854c80 100644
--- a/modules/internal/modules/esm.js
+++ b/modules/internal/loader.js
@@ -1,11 +1,9 @@
// SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
// SPDX-FileCopyrightText: 2020 Evan Welsh <contact evanwelsh com>
-import {ESModule, ImportError, ModuleLoader} from '../bootstrap/module.js';
+import {ImportError, InternalModuleLoader, ModulePrivate} from './internalLoader.js';
-import {generateModule} from './gi.js';
-
-export class ESModuleLoader extends ModuleLoader {
+class ModuleLoader extends InternalModuleLoader {
/**
* @param {typeof globalThis} global the global object to register modules with.
*/
@@ -20,7 +18,7 @@ export class ESModuleLoader extends ModuleLoader {
this.moduleURIs = new Set();
/**
- * @type {Map<string, import("../bootstrap/module.js").SchemeHandler>}
+ * @type {Map<string, import("./internalLoader.js").SchemeHandler>}
*
* A map of handlers for URI schemes (e.g. gi://)
*/
@@ -28,7 +26,7 @@ export class ESModuleLoader extends ModuleLoader {
}
/**
- * @param {ESModule} module a module private object
+ * @param {ModulePrivate} module a module private object
* @param {string} text the module source text
*/
compileModule(module, text) {
@@ -57,14 +55,14 @@ export class ESModuleLoader extends ModuleLoader {
/**
* @param {string} scheme the URI scheme to register
- * @param {import("../bootstrap/module.js").SchemeHandler} handler a handler
+ * @param {import("./internalLoader.js").SchemeHandler} handler a handler
*/
registerScheme(scheme, handler) {
this.schemeHandlers.set(scheme, handler);
}
/**
- * @param {import("../bootstrap/module.js").Uri} uri a Uri object to load
+ * @param {import("./internalLoader.js").Uri} uri a Uri object to load
*/
loadURI(uri) {
const {schemeHandlers} = this;
@@ -123,7 +121,7 @@ export class ESModuleLoader extends ModuleLoader {
throw new ImportError('Only file:// and resource:// URIs are currently supported.');
const text = loadResourceOrFile(parsed.uri);
- const priv = new ESModule(specifier, uri, true);
+ const priv = new ModulePrivate(specifier, uri, true);
const compiled = this.compileModule(priv, text);
if (!compiled)
throw new ImportError(`Failed to register module: ${uri}`);
@@ -136,7 +134,7 @@ export class ESModuleLoader extends ModuleLoader {
}
}
-export const moduleLoader = new ESModuleLoader(moduleGlobalThis);
+const moduleLoader = new ModuleLoader(moduleGlobalThis);
// Always let ESM-specific modules take priority over core modules.
moduleLoader.registerModuleURI('resource:///org/gnome/gjs/modules/esm/');
@@ -155,9 +153,29 @@ function getGIVersionMap(lib) {
return giVersionMap.get(lib);
}
+/**
+ * Creates a module source text to expose a GI namespace via a default export.
+ *
+ * @param {string} namespace the GI namespace to import
+ * @param {string} [version] the version string of the namespace
+ *
+ * @returns {string} the generated module source text
+ */
+function generateGIModule(namespace, version) {
+ const source = `
+ import $$gi from 'gi';
+
+ const $$ns = $$gi.require${version ? `('${namespace}', '${version}')` : `('${namespace}')`};
+
+ export default $$ns;
+ `;
+
+ return source;
+}
+
moduleLoader.registerScheme('gi', {
/**
- * @param {import("../bootstrap/module.js").Uri} uri the URI to load
+ * @param {import("./internalLoader.js").Uri} uri the URI to load
*/
load(uri) {
const version = uri.query.version ?? getGIVersionMap(uri.host);
@@ -165,12 +183,12 @@ moduleLoader.registerScheme('gi', {
if (version)
giVersionMap.set(uri.host, version);
- return [generateModule(uri.host, version), true];
+ return [generateGIModule(uri.host, version), true];
},
});
/**
- * @param {ESModule} module
+ * @param {ModulePrivate} module
* @param {ImportMeta} meta
*/
setModuleMetaHook(moduleGlobalThis, (module, meta) => {
@@ -185,7 +203,7 @@ setModuleMetaHook(moduleGlobalThis, (module, meta) => {
* @param {string} uri
*/
setModuleLoadHook(moduleGlobalThis, (id, uri) => {
- const priv = new ESModule(id, uri);
+ const priv = new ModulePrivate(id, uri);
const [text] = moduleLoader.loadURI(parseURI(uri));
const compiled = moduleLoader.compileModule(priv, text);
@@ -200,4 +218,3 @@ setModuleLoadHook(moduleGlobalThis, (id, uri) => {
setModuleResolveHook(moduleGlobalThis, (module, specifier) => {
return moduleLoader.resolveModule(specifier, module.uri);
});
-
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]