[gjs/esm/static-imports] temp - cleanup



commit 1e21c29ba248e766d6fd431da4530f7ee3f57e52
Author: Evan Welsh <contact evanwelsh com>
Date:   Sat Feb 6 18:57:25 2021 -0800

    temp - cleanup

 gjs/context.cpp                    |  2 +-
 modules/internal/internalLoader.js | 57 ++++++++++++++++++++++++++++----------
 modules/internal/loader.js         | 47 +++++++++++++++++--------------
 3 files changed, 69 insertions(+), 37 deletions(-)
---
diff --git a/gjs/context.cpp b/gjs/context.cpp
index ab3da86e..2874dfbe 100644
--- a/gjs/context.cpp
+++ b/gjs/context.cpp
@@ -1111,7 +1111,7 @@ bool GjsContextPrivate::eval_module(const char* identifier,
     JS::RootedObject obj(m_cx);
     if (!gjs_global_registry_get(m_cx, registry, key, &obj) || !obj) {
         g_set_error(error, GJS_ERROR, GJS_ERROR_FAILED,
-                    "Cannot find module with identifier: '%s'", identifier);
+                    "Cannot load module with identifier: '%s'", identifier);
         return false;
     }
 
diff --git a/modules/internal/internalLoader.js b/modules/internal/internalLoader.js
index 09acc16c..ac642945 100644
--- a/modules/internal/internalLoader.js
+++ b/modules/internal/internalLoader.js
@@ -9,8 +9,16 @@
 
 /** @typedef {{ uri: string; scheme: string; host: string; path: string; query: Query }} Uri */
 
-/** @typedef {{ load(uri: Uri): [string, boolean]; }} SchemeHandler */
+/**
+ * Use '__internal: never' to prevent any object from being type compatible with Module
+ * because it is an internal type.
+ *
+ * @typedef {{__internal: never;}} Module
+ */
+/** @typedef {typeof moduleGlobalThis | typeof globalThis} Global */
+/** @typedef {{ load(uri: Uri): [contents: string, internal: boolean]; }} SchemeHandler */
 /** @typedef {{ [key: string]: string | undefined; }} Query */
+/** @typedef {(uri: string, contents: string) => Module} CompileFunc */
 
 /**
  * Thrown when there is an error importing a module.
@@ -63,20 +71,19 @@ function isRelativePath(path) {
  */
 export class InternalModuleLoader {
     /**
-     * @param {typeof globalThis} global the global object to handle module resolution
+     * @param {Global} global the global object to handle module resolution
+     * @param {CompileFunc} compileFunc the function to compile a source into a module
      */
-    constructor(global) {
+    constructor(global, compileFunc) {
         this.global = global;
-        // Controls whether modules are compiled for the internal global or the
-        // imports global. Overridden to be false in ModuleLoader.
-        this.compileInternal = true;
+        this.compileFunc = compileFunc;
     }
 
     /**
      * Loads a file or resource URI synchronously
      *
      * @param {Uri} uri the file or resource URI to load
-     * @returns {[string] | [string, boolean] | null}
+     * @returns {[contents: string, internal?: boolean] | null}
      */
     loadURI(uri) {
         if (uri.scheme === 'file' || uri.scheme === 'resource')
@@ -104,7 +111,7 @@ export class InternalModuleLoader {
 
         if (isRelativePath(specifier)) {
             if (!parentURI)
-                throw new ImportError('Cannot import from relative path when module path is unknown.');
+                throw new ImportError('Cannot import relative path when module path is unknown.');
 
             return this.resolveRelativePath(specifier, parentURI);
         }
@@ -137,10 +144,10 @@ export class InternalModuleLoader {
      *
      * @param {ModulePrivate} priv a module private object
      * @param {string} text the module source text to compile
-     * @returns {import("../types").Module}
+     * @returns {Module}
      */
     compileModule(priv, text) {
-        const compileFunc = this.compileInternal ? compileInternalModule : compileModule;
+        const {compileFunc} = this;
         const compiled = compileFunc(priv.uri, text);
 
         setModulePrivate(compiled, priv);
@@ -153,7 +160,7 @@ export class InternalModuleLoader {
      * @param {string | null} importingModuleURI the URI of the module
      *   triggering this resolve
      *
-     * @returns {import("../types").Module | null}
+     * @returns {Module | null}
      */
     resolveModule(specifier, importingModuleURI) {
         const registry = getRegistry(this.global);
@@ -181,9 +188,6 @@ export class InternalModuleLoader {
             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}`);
-
             registry.set(uri.uri, compiled);
             return compiled;
         }
@@ -218,5 +222,28 @@ export class InternalModuleLoader {
     }
 }
 
-export const internalModuleLoader = new InternalModuleLoader(globalThis);
+export const internalModuleLoader = new InternalModuleLoader(globalThis, compileInternalModule);
 setGlobalModuleLoader(globalThis, internalModuleLoader);
+
+/* eslint-disable no-unused-expressions */
+
+/** @type {CompileFunc} */
+globalThis.compileModule;
+/** @type {CompileFunc} */
+globalThis.compileInternalModule;
+/** @type {{__moduleGlobal: never;}} */
+globalThis.moduleGlobalThis;
+/** @type {(uri: string) => boolean} */
+globalThis.uriExists;
+/** @type {(global: Global, loader: InternalModuleLoader) => void} */
+globalThis.setGlobalModuleLoader;
+/** @type {(uri: string) => string} */
+globalThis.loadResourceOrFile;
+/** @type {(uri: string) => Uri} */
+globalThis.parseURI;
+/** @type {(relativePath: string, parentURI: string) => string | null} */
+globalThis.resolveRelativeResourceOrFile;
+/** @type {(global: Global) => Map<string, Module>} */
+globalThis.getRegistry;
+/** @type {(module: Module, priv: ModulePrivate) => void} */
+globalThis.setModulePrivate;
diff --git a/modules/internal/loader.js b/modules/internal/loader.js
index a44e8127..fde96eb0 100644
--- a/modules/internal/loader.js
+++ b/modules/internal/loader.js
@@ -5,10 +5,11 @@ import {ImportError, InternalModuleLoader, ModulePrivate} from './internalLoader
 
 class ModuleLoader extends InternalModuleLoader {
     /**
-     * @param {typeof globalThis} global the global object to register modules with.
+     * @param {typeof moduleGlobalThis} global the global object to register modules with.
      */
     constructor(global) {
-        super(global);
+        // Sets 'compileFunc' in InternalModuleLoader to be 'compileModule'
+        super(global, compileModule);
 
         /**
          * @type {Set<string>}
@@ -19,9 +20,7 @@ class ModuleLoader extends InternalModuleLoader {
          * "resource:///org/gnome/gjs/modules/esm/system.js" exists.
          */
         this.moduleURIs = new Set([
-            // Always let ESM-specific modules take priority over core modules.
             'resource:///org/gnome/gjs/modules/esm/',
-            'resource:///org/gnome/gjs/modules/core/',
         ]);
 
         /**
@@ -30,9 +29,6 @@ class ModuleLoader extends InternalModuleLoader {
          * A map of handlers for URI schemes (e.g. gi://)
          */
         this.schemeHandlers = new Map();
-
-        // overrides InternalModuleLoader.compileInternal
-        this.compileInternal = false;
     }
 
     /**
@@ -77,29 +73,23 @@ class ModuleLoader extends InternalModuleLoader {
         if (result)
             return result;
 
-        throw new ImportError(`Unable to load module from invalid URI: ${uri.uri}`);
+        throw new ImportError(`Invalid module URI: ${uri.uri}`);
     }
 
     /**
-     * Resolves a module import with optional handling for relative imports.
-     * Overrides InternalModuleLoader.moduleResolveHook
+     * Resolves a bare specifier like 'system' against internal resources,
+     * erroring if no resource is found.
      *
-     * @param {import("./internalLoader.js").ModulePrivate} importingModulePriv
-     *   the private object of the module initiating the import
      * @param {string} specifier the module specifier to resolve for an import
-     * @returns {import("../types").Module}
+     * @returns {import("./internalLoader").Module}
      */
-    moduleResolveHook(importingModulePriv, specifier) {
-        const module = this.resolveModule(specifier, importingModulePriv.uri);
-        if (module)
-            return module;
-
+    resolveBareSpecifier(specifier) {
         // 2) Resolve internal imports.
 
         const uri = this.buildInternalURIs(specifier).find(uriExists);
 
         if (!uri)
-            throw new ImportError(`Attempted to load unregistered global module: ${specifier}`);
+            throw new ImportError(`Unknown module: '${specifier}'`);
 
         const parsed = parseURI(uri);
         if (parsed.scheme !== 'file' && parsed.scheme !== 'resource')
@@ -108,8 +98,6 @@ class ModuleLoader extends InternalModuleLoader {
         const text = loadResourceOrFile(parsed.uri);
         const priv = new ModulePrivate(specifier, uri, true);
         const compiled = this.compileModule(priv, text);
-        if (!compiled)
-            throw new ImportError(`Failed to register module: ${uri}`);
 
         const registry = getRegistry(this.global);
         if (!registry.has(specifier))
@@ -117,6 +105,23 @@ class ModuleLoader extends InternalModuleLoader {
 
         return compiled;
     }
+
+    /**
+     * Resolves a module import with optional handling for relative imports.
+     * Overrides InternalModuleLoader.moduleResolveHook
+     *
+     * @param {import("./internalLoader.js").ModulePrivate} importingModulePriv
+     *   the private object of the module initiating the import
+     * @param {string} specifier the module specifier to resolve for an import
+     * @returns {import("./internalLoader").Module}
+     */
+    moduleResolveHook(importingModulePriv, specifier) {
+        const module = this.resolveModule(specifier, importingModulePriv.uri);
+        if (module)
+            return module;
+
+        return this.resolveBareSpecifier(specifier);
+    }
 }
 
 const moduleLoader = new ModuleLoader(moduleGlobalThis);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]