1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 18:28:12 +00:00

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.
This commit is contained in:
Andreas Kling 2020-03-24 22:03:50 +01:00
parent db024a9cb1
commit faddf3a1db
13 changed files with 160 additions and 7 deletions

View file

@ -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 };
};