summaryrefslogtreecommitdiff
path: root/Libraries/LibJS/Interpreter.h
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-03-24 22:03:50 +0100
committerAndreas Kling <kling@serenityos.org>2020-03-24 22:21:58 +0100
commitfaddf3a1db6d9c8bb4106eb2259f91fe8e58b817 (patch)
treead93acf4f78d132e27259fc4ef5109e3266f9efe /Libraries/LibJS/Interpreter.h
parentdb024a9cb147731cfb14ef60a862a3e392eedc9b (diff)
downloadserenity-faddf3a1db6d9c8bb4106eb2259f91fe8e58b817.zip
LibJS: Implement "throw"
You can now throw an expression to the nearest catcher! :^) To support throwing arbitrary values, I added an Exception class that sits as a wrapper around whatever is thrown. In the future it will be a logical place to store a call stack.
Diffstat (limited to 'Libraries/LibJS/Interpreter.h')
-rw-r--r--Libraries/LibJS/Interpreter.h20
1 files changed, 16 insertions, 4 deletions
diff --git a/Libraries/LibJS/Interpreter.h b/Libraries/LibJS/Interpreter.h
index 6c2beecd9e..fe806f9a4f 100644
--- a/Libraries/LibJS/Interpreter.h
+++ b/Libraries/LibJS/Interpreter.h
@@ -26,12 +26,13 @@
#pragma once
+#include <AK/FlyString.h>
#include <AK/HashMap.h>
#include <AK/String.h>
-#include <AK/FlyString.h>
#include <AK/Vector.h>
#include <LibJS/Forward.h>
#include <LibJS/Heap/Heap.h>
+#include <LibJS/Runtime/Exception.h>
#include <LibJS/Runtime/Value.h>
namespace JS {
@@ -107,9 +108,20 @@ public:
Object* array_prototype() { return m_array_prototype; }
Object* error_prototype() { return m_error_prototype; }
- Error* exception() { return m_exception; }
+ Exception* exception() { return m_exception; }
void clear_exception() { m_exception = nullptr; }
- Value throw_exception(Error*);
+
+ template<typename T, typename... Args>
+ Value throw_exception(Args&&... args)
+ {
+ return throw_exception(heap().allocate<T>(forward<Args>(args)...));
+ }
+
+ Value throw_exception(Exception*);
+ Value throw_exception(Value value)
+ {
+ return throw_exception(heap().allocate<Exception>(value));
+ }
private:
Heap m_heap;
@@ -123,7 +135,7 @@ private:
Object* m_array_prototype { nullptr };
Object* m_error_prototype { nullptr };
- Error* m_exception { nullptr };
+ Exception* m_exception { nullptr };
ScopeType m_unwind_until { ScopeType::None };
};