1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-16 19:45:07 +00:00

LibJS: Implement basic exception throwing

You can now throw exceptions by calling Interpreter::throw_exception().
Anyone who calls ASTNode::execute() needs to check afterwards if the
Interpreter now has an exception(), and if so, stop what they're doing
and simply return.

When catching an exception, we'll first execute the CatchClause node
if present. After that, we'll execute the finalizer block if present.

This is unlikely to be completely correct, but it's a start! :^)
This commit is contained in:
Andreas Kling 2020-03-24 14:37:39 +01:00
parent c33d4aefc3
commit 343e224aa8
11 changed files with 257 additions and 5 deletions

View file

@ -28,6 +28,8 @@
#include <LibJS/AST.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/ArrayPrototype.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/ErrorPrototype.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/NativeFunction.h>
#include <LibJS/Runtime/Object.h>
@ -44,6 +46,7 @@ Interpreter::Interpreter()
m_object_prototype = heap().allocate<ObjectPrototype>();
m_string_prototype = heap().allocate<StringPrototype>();
m_array_prototype = heap().allocate<ArrayPrototype>();
m_error_prototype = heap().allocate<ErrorPrototype>();
}
Interpreter::~Interpreter()
@ -154,6 +157,9 @@ void Interpreter::gather_roots(Badge<Heap>, HashTable<Cell*>& roots)
roots.set(m_string_prototype);
roots.set(m_object_prototype);
roots.set(m_array_prototype);
roots.set(m_error_prototype);
roots.set(m_exception);
for (auto& scope : m_scope_stack) {
for (auto& it : scope.variables) {
@ -182,4 +188,11 @@ Value Interpreter::call(Function* function, Value this_value, const Vector<Value
return result;
}
Value Interpreter::throw_exception(Error* exception)
{
m_exception = exception;
unwind(ScopeType::Try);
return {};
}
}