Преглед изворни кода

LibJS: Add set_exception() and change throw_exception() to take a reference

Sometimes we just want to set m_exception to some value we stored
previously, without really "throwing" it again - that's what
set_exception() does now. Since we have clear_exception(), it does take
a reference, i.e. you don't set_exception(nullptr). For consistency I
updated throw_exception() to do the same.
Linus Groh пре 4 година
родитељ
комит
4ee965f916

+ 1 - 3
Userland/Libraries/LibJS/AST.cpp

@@ -2006,10 +2006,8 @@ Value TryStatement::execute(Interpreter& interpreter, GlobalObject& global_objec
 
             // If we previously had an exception and the finalizer didn't
             // throw a new one, restore the old one.
-            // FIXME: This will print debug output in throw_exception() for
-            // a second time with m_should_log_exceptions enabled.
             if (previous_exception && !interpreter.exception())
-                interpreter.vm().throw_exception(previous_exception);
+                interpreter.vm().set_exception(*previous_exception);
         }
     }
 

+ 3 - 3
Userland/Libraries/LibJS/Runtime/VM.cpp

@@ -290,10 +290,10 @@ Value VM::construct(Function& function, Function& new_target, Optional<MarkedVal
     return this_value;
 }
 
-void VM::throw_exception(Exception* exception)
+void VM::throw_exception(Exception& exception)
 {
     if (should_log_exceptions()) {
-        auto value = exception->value();
+        auto value = exception.value();
         if (value.is_object()) {
             auto& object = value.as_object();
             auto name = object.get_without_side_effects(names.name).value_or(js_undefined());
@@ -317,7 +317,7 @@ void VM::throw_exception(Exception* exception)
         }
     }
 
-    m_exception = exception;
+    set_exception(exception);
     unwind(ScopeType::Try);
 }
 

+ 4 - 7
Userland/Libraries/LibJS/Runtime/VM.h

@@ -86,11 +86,8 @@ public:
     void push_interpreter(Interpreter&);
     void pop_interpreter(Interpreter&);
 
-    Exception* exception()
-    {
-        return m_exception;
-    }
-
+    Exception* exception() { return m_exception; }
+    void set_exception(Exception& exception) { m_exception = &exception; }
     void clear_exception() { m_exception = nullptr; }
 
     class InterpreterExecutionScope {
@@ -207,10 +204,10 @@ public:
         return throw_exception(global_object, T::create(global_object, forward<Args>(args)...));
     }
 
-    void throw_exception(Exception*);
+    void throw_exception(Exception&);
     void throw_exception(GlobalObject& global_object, Value value)
     {
-        return throw_exception(heap().allocate<Exception>(global_object, value));
+        return throw_exception(*heap().allocate<Exception>(global_object, value));
     }
 
     template<typename T, typename... Args>