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



commit b22151e863f3453b7286d073dd625c8ce9330b5b
Author: Philip Chimento <philip chimento gmail com>
Date:   Fri Dec 11 18:38:34 2020 -0800

    WIP - implement loadResourceOrFile in C++
    
    Question: does this need to throw ImportError like the original function
    did? My guess is yes, since this exception is dependent on user content

 gjs/global.cpp          |  1 +
 gjs/internal.cpp        | 32 ++++++++++++++++++++++++++++++++
 gjs/internal.h          |  4 ++++
 lib/.eslintrc.yml       |  1 +
 lib/bootstrap/module.js | 36 ------------------------------------
 lib/modules/esm.js      |  2 +-
 6 files changed, 39 insertions(+), 37 deletions(-)
---
diff --git a/gjs/global.cpp b/gjs/global.cpp
index e4b5b766..2be08fdc 100644
--- a/gjs/global.cpp
+++ b/gjs/global.cpp
@@ -272,6 +272,7 @@ class GjsInternalGlobal : GjsBaseGlobal {
               0),
         JS_FN("getRegistry", gjs_internal_global_get_registry, 1, 0),
         JS_FN("importSync", gjs_internal_global_import_sync, 1, 0),
+        JS_FN("loadResourceOrFile", gjs_internal_load_resource_or_file, 1, 0),
         JS_FN("parseURI", gjs_internal_parse_uri, 1, 0),
         JS_FN("resolveRelativeResourceOrFile",
               gjs_internal_resolve_relative_resource_or_file, 2, 0),
diff --git a/gjs/internal.cpp b/gjs/internal.cpp
index ef7082b2..d861bf90 100644
--- a/gjs/internal.cpp
+++ b/gjs/internal.cpp
@@ -512,3 +512,35 @@ bool gjs_internal_resolve_relative_resource_or_file(JSContext* cx,
     args.rval().setNull();
     return true;
 }
+
+bool gjs_internal_load_resource_or_file(JSContext* cx, unsigned argc,
+                                        JS::Value* vp) {
+    JS::CallArgs args = CallArgsFromVp(argc, vp);
+
+    g_assert(args.length() == 1 && "loadResourceOrFile(str)");
+    g_assert(args[0].isString() && "loadResourceOrFile(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());
+
+    char* contents;
+    size_t length;
+    GError* error = nullptr;
+    if (!g_file_load_contents(file, /* cancellable = */ nullptr, &contents,
+                              &length, /* etag_out = */ nullptr, &error))
+        return gjs_throw_gerror_message(cx, error);
+
+    JS::ConstUTF8CharsZ contents_chars{contents, length};
+    JS::RootedString contents_str(cx,
+                                  JS_NewStringCopyUTF8Z(cx, contents_chars));
+    g_free(contents);
+    if (!contents_str)
+        return false;
+
+    args.rval().setString(contents_str);
+    return true;
+}
diff --git a/gjs/internal.h b/gjs/internal.h
index f347040c..830903a7 100644
--- a/gjs/internal.h
+++ b/gjs/internal.h
@@ -44,4 +44,8 @@ bool gjs_internal_resolve_relative_resource_or_file(JSContext* cx,
                                                     unsigned argc,
                                                     JS::Value* vp);
 
+GJS_JSAPI_RETURN_CONVENTION
+bool gjs_internal_load_resource_or_file(JSContext* cx, unsigned argc,
+                                        JS::Value* vp);
+
 #endif  // GJS_INTERNAL_H_
diff --git a/lib/.eslintrc.yml b/lib/.eslintrc.yml
index fa5bab05..5b278080 100644
--- a/lib/.eslintrc.yml
+++ b/lib/.eslintrc.yml
@@ -20,6 +20,7 @@ globals:
   moduleGlobalThis: readonly
   compileModule: readonly
   compileInternalModule: readonly
+  loadResourceOrFile: readonly
   parseURI: readonly
   resolveRelativeResourceOrFile: readonly
   setModuleResolveHook: readonly
diff --git a/lib/bootstrap/module.js b/lib/bootstrap/module.js
index 7d6287a6..d6fad676 100644
--- a/lib/bootstrap/module.js
+++ b/lib/bootstrap/module.js
@@ -48,42 +48,6 @@ function isRelativePath(path) {
     return path.startsWith('./') || path.startsWith('../');
 }
 
-/**
- * Encodes a Uint8Array into a UTF-8 string
- *
- * @param {Uint8Array} bytes the bytes to convert
- * @returns {string}
- */
-function fromBytes(bytes) {
-    return ByteUtils.toString(bytes, 'utf-8');
-}
-
-/**
- * @param {import("gio").File} file the Gio.File to load from.
- * @returns {string}
- */
-function loadFileSync(file) {
-    try {
-        const [, bytes] = file.load_contents(null);
-
-        return fromBytes(bytes);
-    } catch (error) {
-        throw new ImportError(`Unable to load file from: ${file.get_uri()}`);
-    }
-}
-
-/**
- * Synchronously loads a file's text from a URI.
- *
- * @param {string} uri the URI to load
- * @returns {string}
- */
-export function loadResourceOrFile(uri) {
-    let output = Gio.File.new_for_uri(uri);
-
-    return loadFileSync(output);
-}
-
 /**
  * Handles resolving and loading URIs.
  *
diff --git a/lib/modules/esm.js b/lib/modules/esm.js
index 443ff273..97d986e1 100644
--- a/lib/modules/esm.js
+++ b/lib/modules/esm.js
@@ -3,7 +3,7 @@
 
 /* global debug, finishDynamicModuleImport, initAndEval, setModuleDynamicImportHook, parseURI */
 
-import {ESModule, ImportError, loadResourceOrFile, ModuleLoader} from '../bootstrap/module.js';
+import {ESModule, ImportError, ModuleLoader} from '../bootstrap/module.js';
 
 import {generateModule} from './gi.js';
 


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