[gjs/esm/static-imports: 40/52] WIP - implement resolveRelativeResourceOrFile in C++
- From: Evan Welsh <ewlsh src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs/esm/static-imports: 40/52] WIP - implement resolveRelativeResourceOrFile in C++
- Date: Fri, 29 Jan 2021 02:14:04 +0000 (UTC)
commit c3e171ad5df7dde5f0cd27fcaf5c296f2e22f229
Author: Philip Chimento <philip chimento gmail com>
Date: Fri Dec 11 18:28:34 2020 -0800
WIP - implement resolveRelativeResourceOrFile in C++
gjs/global.cpp | 2 ++
gjs/internal.cpp | 39 +++++++++++++++++++++++++++++++++++++++
gjs/internal.h | 5 +++++
lib/.eslintrc.yml | 1 +
lib/bootstrap/module.js | 25 ++++---------------------
5 files changed, 51 insertions(+), 21 deletions(-)
---
diff --git a/gjs/global.cpp b/gjs/global.cpp
index 9e2e1b5a..e4b5b766 100644
--- a/gjs/global.cpp
+++ b/gjs/global.cpp
@@ -273,6 +273,8 @@ class GjsInternalGlobal : GjsBaseGlobal {
JS_FN("getRegistry", gjs_internal_global_get_registry, 1, 0),
JS_FN("importSync", gjs_internal_global_import_sync, 1, 0),
JS_FN("parseURI", gjs_internal_parse_uri, 1, 0),
+ JS_FN("resolveRelativeResourceOrFile",
+ gjs_internal_resolve_relative_resource_or_file, 2, 0),
JS_FN("setModuleLoadHook", gjs_internal_global_set_module_hook, 3, 0),
JS_FN("setModuleMetaHook", gjs_internal_global_set_module_meta_hook, 2,
0),
diff --git a/gjs/internal.cpp b/gjs/internal.cpp
index a62d5bfb..ef7082b2 100644
--- a/gjs/internal.cpp
+++ b/gjs/internal.cpp
@@ -473,3 +473,42 @@ bool gjs_internal_parse_uri(JSContext* cx, unsigned argc, JS::Value* vp) {
args.rval().setObject(*return_obj);
return true;
}
+
+bool gjs_internal_resolve_relative_resource_or_file(JSContext* cx,
+ unsigned argc,
+ JS::Value* vp) {
+ JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
+
+ g_assert(args.length() == 2 && "resolveRelativeResourceOrFile(str, str)");
+ g_assert(args[0].isString() && "resolveRelativeResourceOrFile(str, str)");
+ g_assert(args[1].isString() && "resolveRelativeResourceOrFile(str, str)");
+
+ JS::RootedString string_arg(cx, args[0].toString());
+ JS::UniqueChars uri = JS_EncodeStringToUTF8(cx, string_arg);
+ if (!uri)
+ return false;
+ string_arg = args[1].toString();
+ JS::UniqueChars relative_path = JS_EncodeStringToUTF8(cx, string_arg);
+ if (!relative_path)
+ return false;
+
+ GjsAutoUnref<GFile> module_file = g_file_new_for_uri(uri.get());
+ GjsAutoUnref<GFile> module_parent_file = g_file_get_parent(module_file);
+
+ if (module_parent_file) {
+ GjsAutoUnref<GFile> output = g_file_resolve_relative_path(
+ module_parent_file, relative_path.get());
+ GjsAutoChar output_uri = g_file_get_uri(output);
+
+ JS::ConstUTF8CharsZ uri_chars(output_uri, strlen(output_uri));
+ JS::RootedString retval(cx, JS_NewStringCopyUTF8Z(cx, uri_chars));
+ if (!retval)
+ return false;
+
+ args.rval().setString(retval);
+ return true;
+ }
+
+ args.rval().setNull();
+ return true;
+}
diff --git a/gjs/internal.h b/gjs/internal.h
index e6d1a4a3..f347040c 100644
--- a/gjs/internal.h
+++ b/gjs/internal.h
@@ -39,4 +39,9 @@ bool gjs_internal_global_set_module_resolve_hook(JSContext* cx, unsigned argc,
GJS_JSAPI_RETURN_CONVENTION
bool gjs_internal_parse_uri(JSContext* cx, unsigned argc, JS::Value* vp);
+GJS_JSAPI_RETURN_CONVENTION
+bool gjs_internal_resolve_relative_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 53cfdf5c..fa5bab05 100644
--- a/lib/.eslintrc.yml
+++ b/lib/.eslintrc.yml
@@ -21,6 +21,7 @@ globals:
compileModule: readonly
compileInternalModule: readonly
parseURI: readonly
+ resolveRelativeResourceOrFile: readonly
setModuleResolveHook: readonly
setModuleMetaHook: readonly
setModuleLoadHook: readonly
diff --git a/lib/bootstrap/module.js b/lib/bootstrap/module.js
index 6b5c5f09..7d6287a6 100644
--- a/lib/bootstrap/module.js
+++ b/lib/bootstrap/module.js
@@ -84,26 +84,6 @@ export function loadResourceOrFile(uri) {
return loadFileSync(output);
}
-/**
- * Resolves a relative path against a URI.
- *
- * @param {string} uri the base URI
- * @param {string} relativePath the relative path to resolve against the base URI
- * @returns {Uri}
- */
-function resolveRelativeResourceOrFile(uri, relativePath) {
- let module_file = Gio.File.new_for_uri(uri);
- let module_parent_file = module_file.get_parent();
-
- if (module_parent_file) {
- let output = module_parent_file.resolve_relative_path(relativePath);
-
- return parseURI(output.get_uri());
- }
-
- throw new ImportError('File does not have a valid parent!');
-}
-
/**
* Handles resolving and loading URIs.
*
@@ -170,7 +150,10 @@ export class ModuleLoader {
parseURI(parentURI);
// Handle relative imports from URI-based modules.
- return resolveRelativeResourceOrFile(parentURI, relativePath);
+ const relativeURI = resolveRelativeResourceOrFile(parentURI, relativePath);
+ if (!relativeURI)
+ throw new ImportError('File does not have a valid parent!');
+ return parseURI(relativeURI);
}
/**
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]