[gjs/ewlsh/fix-init-errors] Don't clear pending exceptions from module init files




commit df056db2725492ba677824792e0e9991360c3782
Author: Evan Welsh <contact evanwelsh com>
Date:   Sat Aug 28 16:14:23 2021 -0700

    Don't clear pending exceptions from module init files
    
    import_module_init previously returned false without an error in some
    cases and load_module_init cleared all pending exceptions.
    
    Fixes #343

 .eslintignore                                       |  1 +
 gjs/importer.cpp                                    | 15 +++++++--------
 installed-tests/js/jsunit.gresources.xml            |  2 ++
 installed-tests/js/modules/subBadInit/__init__.js   |  4 ++++
 installed-tests/js/modules/subErrorInit/__init__.js |  4 ++++
 installed-tests/js/testImporter.js                  |  8 ++++++++
 6 files changed, 26 insertions(+), 8 deletions(-)
---
diff --git a/.eslintignore b/.eslintignore
index 9ee950d3..87df4814 100644
--- a/.eslintignore
+++ b/.eslintignore
@@ -3,4 +3,5 @@
 
 installed-tests/js/jasmine.js
 installed-tests/js/modules/badOverrides/WarnLib.js
+installed-tests/js/modules/subBadInit/__init__.js
 modules/script/jsUnit.js
diff --git a/gjs/importer.cpp b/gjs/importer.cpp
index ad8742b8..71713133 100644
--- a/gjs/importer.cpp
+++ b/gjs/importer.cpp
@@ -308,12 +308,13 @@ import_module_init(JSContext       *context,
                               &error)) {
         if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY) &&
             !g_error_matches(error, G_IO_ERROR, G_IO_ERROR_NOT_DIRECTORY) &&
-            !g_error_matches(error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
+            !g_error_matches(error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND)) {
             gjs_throw_gerror_message(context, error);
-        else
-            g_error_free(error);
+            return false;
+        }
 
-        return false;
+        g_error_free(error);
+        return true;
     }
     g_assert(script);
 
@@ -350,10 +351,8 @@ static JSObject* load_module_init(JSContext* cx, JS::HandleObject in_object,
     if (!module_obj)
         return nullptr;
 
-    if (!import_module_init(cx, file, module_obj)) {
-        JS_ClearPendingException(cx);
-        return module_obj;
-    }
+    if (!import_module_init(cx, file, module_obj))
+        return nullptr;
 
     if (!JS_DefinePropertyById(cx, in_object, atoms.module_init(), module_obj,
                                GJS_MODULE_PROP_FLAGS & ~JSPROP_PERMANENT))
diff --git a/installed-tests/js/jsunit.gresources.xml b/installed-tests/js/jsunit.gresources.xml
index fea06b50..537bfb2e 100644
--- a/installed-tests/js/jsunit.gresources.xml
+++ b/installed-tests/js/jsunit.gresources.xml
@@ -12,6 +12,8 @@
     <file>modules/badOverrides/Gio.js</file>
     <file>modules/badOverrides/Regress.js</file>
     <file>modules/badOverrides/WarnLib.js</file>
+    <file>modules/subBadInit/__init__.js</file>
+    <file>modules/subErrorInit/__init__.js</file>
     <file>modules/data.txt</file>
     <file>modules/encodings.json</file>
     <file>modules/dynamic.js</file>
diff --git a/installed-tests/js/modules/subBadInit/__init__.js 
b/installed-tests/js/modules/subBadInit/__init__.js
new file mode 100644
index 00000000..0e1acc69
--- /dev/null
+++ b/installed-tests/js/modules/subBadInit/__init__.js
@@ -0,0 +1,4 @@
+// SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
+// SPDX-FileCopyrightText: 2021 Evan Welsh <contact evanwelsh com>
+
+this is gobbledygook
diff --git a/installed-tests/js/modules/subErrorInit/__init__.js 
b/installed-tests/js/modules/subErrorInit/__init__.js
new file mode 100644
index 00000000..8f043e97
--- /dev/null
+++ b/installed-tests/js/modules/subErrorInit/__init__.js
@@ -0,0 +1,4 @@
+// SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
+// SPDX-FileCopyrightText: 2021 Evan Welsh <contact evanwelsh com>
+
+throw Error('a bad init!');
diff --git a/installed-tests/js/testImporter.js b/installed-tests/js/testImporter.js
index 5202abce..8380af20 100644
--- a/installed-tests/js/testImporter.js
+++ b/installed-tests/js/testImporter.js
@@ -176,6 +176,14 @@ describe('Importer', function () {
         expect(subB.testImporterFunction()).toEqual('__init__ function tested');
     });
 
+    it('throws on an __init__.js file with a syntax error', function () {
+        expect(() => imports.subBadInit.SOMETHING).toThrowError(SyntaxError);
+    });
+
+    it('throws when an __init__.js throws an error', function () {
+        expect(() => imports.subErrorInit.SOMETHING).toThrowError('a bad init!');
+    });
+
     it('accesses a class defined in an __init__.js file', function () {
         let o = new subB.ImporterClass();
         expect(o).not.toBeNull();


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