[gjs/ewlsh/fix-error-prototype] modules: Ensure ImportError is an instance of globalThis.Error




commit 700fc11d8b3a1f55e2785c5c9f6fbc317f7cc9b8
Author: Evan Welsh <contact evanwelsh com>
Date:   Sun Jan 30 15:34:49 2022 -0800

    modules: Ensure ImportError is an instance of globalThis.Error
    
    If we throw the error with the wrong global (e.g. the internal
    global) then ImportError is no longer an instance of
    globalThis.Error

 gjs/internal.cpp                   | 8 ++++++++
 modules/internal/internalLoader.js | 2 +-
 2 files changed, 9 insertions(+), 1 deletion(-)
---
diff --git a/gjs/internal.cpp b/gjs/internal.cpp
index 9b2e552a6..57f3d97b6 100644
--- a/gjs/internal.cpp
+++ b/gjs/internal.cpp
@@ -281,6 +281,7 @@ bool gjs_internal_parse_uri(JSContext* cx, unsigned argc, JS::Value* vp) {
     using AutoHashTable =
         GjsAutoPointer<GHashTable, GHashTable, g_hash_table_destroy>;
     using AutoURI = GjsAutoPointer<GUri, GUri, g_uri_unref>;
+    GjsContextPrivate* gjs = GjsContextPrivate::from_cx(cx);
 
     JS::CallArgs args = CallArgsFromVp(argc, vp);
     JS::RootedString string_arg(cx);
@@ -294,6 +295,8 @@ bool gjs_internal_parse_uri(JSContext* cx, unsigned argc, JS::Value* vp) {
     GError* error = nullptr;
     AutoURI parsed = g_uri_parse(uri.get(), G_URI_FLAGS_NONE, &error);
     if (!parsed) {
+        JSAutoRealm ar(cx, gjs->global());
+
         gjs_throw_custom(cx, JSProto_Error, "ImportError",
                          "Attempted to import invalid URI: %s (%s)", uri.get(),
                          error->message);
@@ -310,6 +313,8 @@ bool gjs_internal_parse_uri(JSContext* cx, unsigned argc, JS::Value* vp) {
         AutoHashTable query =
             g_uri_parse_params(raw_query, -1, "&", G_URI_PARAMS_NONE, &error);
         if (!query) {
+            JSAutoRealm ar(cx, gjs->global());
+
             gjs_throw_custom(cx, JSProto_Error, "ImportError",
                              "Attempted to import invalid URI: %s (%s)",
                              uri.get(), error->message);
@@ -412,6 +417,9 @@ bool gjs_internal_load_resource_or_file(JSContext* cx, unsigned argc,
     GError* error = nullptr;
     if (!g_file_load_contents(file, /* cancellable = */ nullptr, &contents,
                               &length, /* etag_out = */ nullptr, &error)) {
+        GjsContextPrivate* gjs = GjsContextPrivate::from_cx(cx);
+        JSAutoRealm ar(cx, gjs->global());
+
         gjs_throw_custom(cx, JSProto_Error, "ImportError",
                          "Unable to load file from: %s (%s)", uri.get(),
                          error->message);
diff --git a/modules/internal/internalLoader.js b/modules/internal/internalLoader.js
index b10231324..b76797053 100644
--- a/modules/internal/internalLoader.js
+++ b/modules/internal/internalLoader.js
@@ -23,7 +23,7 @@
 /**
  * Thrown when there is an error importing a module.
  */
-export class ImportError extends Error {
+export class ImportError extends moduleGlobalThis.Error {
     /**
      * @param {string | undefined} message the import error message
      */


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