[gjs/esm/static-imports] WIP - implement uriExists in C++



commit 63190f433ae8d021757633f85e9d44ad0e85e196
Author: Philip Chimento <philip chimento gmail com>
Date:   Sun Dec 13 15:22:23 2020 -0800

    WIP - implement uriExists in C++
    
    Instead of using `Gio.File.query_exists()` directly in the internal JS
    Note to self, check: do we really need to first check if the URI exists,
    could we instead rely on an error occurring if we tried to load it?

 gjs/global.cpp     |  1 +
 gjs/internal.cpp   | 17 +++++++++++++++++
 gjs/internal.h     |  7 +++++++
 lib/modules/esm.js |  7 ++-----
 4 files changed, 27 insertions(+), 5 deletions(-)
---
diff --git a/gjs/global.cpp b/gjs/global.cpp
index 2be08fdc..b7c9497c 100644
--- a/gjs/global.cpp
+++ b/gjs/global.cpp
@@ -282,6 +282,7 @@ class GjsInternalGlobal : GjsBaseGlobal {
         JS_FN("setModulePrivate", gjs_internal_set_module_private, 2, 0),
         JS_FN("setModuleResolveHook",
               gjs_internal_global_set_module_resolve_hook, 2, 0),
+        JS_FN("uriExists", gjs_internal_uri_exists, 1, 0),
         JS_FS_END};
 
     static constexpr JSClassOps classops = {nullptr,  // addProperty
diff --git a/gjs/internal.cpp b/gjs/internal.cpp
index d861bf90..0c14af7e 100644
--- a/gjs/internal.cpp
+++ b/gjs/internal.cpp
@@ -544,3 +544,20 @@ bool gjs_internal_load_resource_or_file(JSContext* cx, unsigned argc,
     args.rval().setString(contents_str);
     return true;
 }
+
+bool gjs_internal_uri_exists(JSContext* cx, unsigned argc, JS::Value* vp) {
+    JS::CallArgs args = CallArgsFromVp(argc, vp);
+
+    g_assert(args.length() >= 1 && "uriExists(str)");  // extra args are OK
+    g_assert(args[0].isString() && "uriExists(str)");
+
+    JS::RootedString string_arg(cx, args[0].toString());
+    JS::UniqueChars uri = JS_EncodeStringToUTF8(cx, string_arg);
+    if (!uri)
+        return false;
+
+    GjsAutoUnref<GFile> file = g_file_new_for_uri(uri.get());
+
+    args.rval().setBoolean(g_file_query_exists(file, nullptr));
+    return true;
+}
diff --git a/gjs/internal.h b/gjs/internal.h
index 830903a7..883189d6 100644
--- a/gjs/internal.h
+++ b/gjs/internal.h
@@ -48,4 +48,11 @@ GJS_JSAPI_RETURN_CONVENTION
 bool gjs_internal_load_resource_or_file(JSContext* cx, unsigned argc,
                                         JS::Value* vp);
 
+GJS_JSAPI_RETURN_CONVENTION
+bool gjs_internal_load_resource_or_file_async(JSContext* cx, unsigned argc,
+                                              JS::Value* vp);
+
+GJS_JSAPI_RETURN_CONVENTION
+bool gjs_internal_uri_exists(JSContext* cx, unsigned argc, JS::Value* vp);
+
 #endif  // GJS_INTERNAL_H_
diff --git a/lib/modules/esm.js b/lib/modules/esm.js
index 97d986e1..73b79373 100644
--- a/lib/modules/esm.js
+++ b/lib/modules/esm.js
@@ -1,7 +1,7 @@
 // SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
 // SPDX-FileCopyrightText: 2020 Evan Welsh <contact evanwelsh com>
 
-/* global debug, finishDynamicModuleImport, initAndEval, setModuleDynamicImportHook, parseURI */
+/* global debug, finishDynamicModuleImport, initAndEval, setModuleDynamicImportHook, parseURI, uriExists */
 
 import {ESModule, ImportError, ModuleLoader} from '../bootstrap/module.js';
 
@@ -115,10 +115,7 @@ export class ESModuleLoader extends ModuleLoader {
 
         // 2) Resolve internal imports.
 
-        const uri = this.buildInternalURIs(specifier).find(u => {
-            let file = Gio.File.new_for_uri(u);
-            return file && file.query_exists(null);
-        });
+        const uri = this.buildInternalURIs(specifier).find(uriExists);
 
         if (!uri)
             throw new ImportError(`Attempted to load unregistered global module: ${specifier}`);


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