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




commit cc37b3472d12055ea6f3d3fcfcf52bd403f31f5c
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

 gjs/importer.cpp                                    | 15 +++++++--------
 installed-tests/js/jsunit.gresources.xml            |  2 ++
 installed-tests/js/modules/subBadInit/__init__.js   |  1 +
 installed-tests/js/modules/subErrorInit/__init__.js |  1 +
 installed-tests/js/testImporter.js                  |  8 ++++++++
 5 files changed, 19 insertions(+), 8 deletions(-)
---
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..8c2b5c8a
--- /dev/null
+++ b/installed-tests/js/modules/subBadInit/__init__.js
@@ -0,0 +1 @@
+this is gobbledygook
\ No newline at end of file
diff --git a/installed-tests/js/modules/subErrorInit/__init__.js 
b/installed-tests/js/modules/subErrorInit/__init__.js
new file mode 100644
index 00000000..2b4335f1
--- /dev/null
+++ b/installed-tests/js/modules/subErrorInit/__init__.js
@@ -0,0 +1 @@
+throw Error('a bad init!');
\ No newline at end of file
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]