mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 21:57:43 +00:00
LibJS: Add all the Error subclasses
This patch adds instance, constructor and prototype classes for: - EvalError - InternalError - RangeError - ReferenceError - SyntaxError - TypeError - URIError Enumerator macros are used to reduce the amount of typing. :^)
This commit is contained in:
parent
df7b617ba1
commit
58ab76269c
11 changed files with 157 additions and 7 deletions
|
@ -26,6 +26,15 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#define JS_ENUMERATE_ERROR_SUBCLASSES \
|
||||
__JS_ENUMERATE_ERROR_SUBCLASS(EvalError, eval_error) \
|
||||
__JS_ENUMERATE_ERROR_SUBCLASS(InternalError, internal_error) \
|
||||
__JS_ENUMERATE_ERROR_SUBCLASS(RangeError, range_error) \
|
||||
__JS_ENUMERATE_ERROR_SUBCLASS(ReferenceError, reference_error) \
|
||||
__JS_ENUMERATE_ERROR_SUBCLASS(SyntaxError, syntax_error) \
|
||||
__JS_ENUMERATE_ERROR_SUBCLASS(TypeError, type_error) \
|
||||
__JS_ENUMERATE_ERROR_SUBCLASS(URIError, uri_error)
|
||||
|
||||
namespace JS {
|
||||
|
||||
class ASTNode;
|
||||
|
@ -54,6 +63,13 @@ class Statement;
|
|||
class Value;
|
||||
enum class DeclarationKind;
|
||||
|
||||
#define __JS_ENUMERATE_ERROR_SUBCLASS(TitleCase, snake_case) \
|
||||
class TitleCase; \
|
||||
class TitleCase##Constructor; \
|
||||
class TitleCase##Prototype;
|
||||
JS_ENUMERATE_ERROR_SUBCLASSES
|
||||
#undef __JS_ENUMERATE_ERROR_SUBCLASS
|
||||
|
||||
struct Argument;
|
||||
|
||||
template<class T>
|
||||
|
|
|
@ -57,6 +57,11 @@ Interpreter::Interpreter()
|
|||
m_date_prototype = heap().allocate<DatePrototype>();
|
||||
m_number_prototype = heap().allocate<NumberPrototype>();
|
||||
m_boolean_prototype = heap().allocate<BooleanPrototype>();
|
||||
|
||||
#define __JS_ENUMERATE_ERROR_SUBCLASS(TitleCase, snake_case) \
|
||||
m_##snake_case##_prototype = heap().allocate<TitleCase##Prototype>();
|
||||
JS_ENUMERATE_ERROR_SUBCLASSES
|
||||
#undef __JS_ENUMERATE_ERROR_SUBCLASS
|
||||
}
|
||||
|
||||
Interpreter::~Interpreter()
|
||||
|
@ -178,12 +183,18 @@ 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_date_prototype);
|
||||
roots.set(m_function_prototype);
|
||||
roots.set(m_number_prototype);
|
||||
roots.set(m_boolean_prototype);
|
||||
|
||||
roots.set(m_error_prototype);
|
||||
|
||||
#define __JS_ENUMERATE_ERROR_SUBCLASS(TitleCase, snake_case) \
|
||||
roots.set(m_##snake_case##_prototype);
|
||||
JS_ENUMERATE_ERROR_SUBCLASSES
|
||||
#undef __JS_ENUMERATE_ERROR_SUBCLASS
|
||||
|
||||
roots.set(m_exception);
|
||||
|
||||
if (m_last_value.is_cell())
|
||||
|
|
|
@ -139,12 +139,18 @@ public:
|
|||
Object* string_prototype() { return m_string_prototype; }
|
||||
Object* object_prototype() { return m_object_prototype; }
|
||||
Object* array_prototype() { return m_array_prototype; }
|
||||
Object* error_prototype() { return m_error_prototype; }
|
||||
Object* date_prototype() { return m_date_prototype; }
|
||||
Object* function_prototype() { return m_function_prototype; }
|
||||
Object* number_prototype() { return m_number_prototype; }
|
||||
Object* boolean_prototype() { return m_boolean_prototype; }
|
||||
|
||||
Object* error_prototype() { return m_error_prototype; }
|
||||
|
||||
#define __JS_ENUMERATE_ERROR_SUBCLASS(TitleCase, snake_case) \
|
||||
Object* snake_case##_prototype() { return m_##snake_case##_prototype; }
|
||||
JS_ENUMERATE_ERROR_SUBCLASSES
|
||||
#undef __JS_ENUMERATE_ERROR_SUBCLASS
|
||||
|
||||
Exception* exception() { return m_exception; }
|
||||
void clear_exception() { m_exception = nullptr; }
|
||||
|
||||
|
@ -178,12 +184,18 @@ private:
|
|||
Object* m_string_prototype { nullptr };
|
||||
Object* m_object_prototype { nullptr };
|
||||
Object* m_array_prototype { nullptr };
|
||||
Object* m_error_prototype { nullptr };
|
||||
Object* m_date_prototype { nullptr };
|
||||
Object* m_function_prototype { nullptr };
|
||||
Object* m_number_prototype { nullptr };
|
||||
Object* m_boolean_prototype { nullptr };
|
||||
|
||||
Object* m_error_prototype { nullptr };
|
||||
|
||||
#define __JS_ENUMERATE_ERROR_SUBCLASS(TitleCase, snake_case) \
|
||||
Object* m_##snake_case##_prototype;
|
||||
JS_ENUMERATE_ERROR_SUBCLASSES
|
||||
#undef __JS_ENUMERATE_ERROR_SUBCLASS
|
||||
|
||||
Exception* m_exception { nullptr };
|
||||
|
||||
ScopeType m_unwind_until { ScopeType::None };
|
||||
|
|
|
@ -40,4 +40,16 @@ Error::~Error()
|
|||
{
|
||||
}
|
||||
|
||||
#define __JS_ENUMERATE_ERROR_SUBCLASS(TitleCase, snake_case) \
|
||||
TitleCase::TitleCase(const String& message) \
|
||||
: Error(#TitleCase, message) \
|
||||
{ \
|
||||
set_prototype(interpreter().snake_case##_prototype()); \
|
||||
} \
|
||||
TitleCase::~TitleCase() {} \
|
||||
const char* TitleCase::class_name() const { return #TitleCase; }
|
||||
|
||||
JS_ENUMERATE_ERROR_SUBCLASSES
|
||||
#undef __JS_ENUMERATE_ERROR_SUBCLASS
|
||||
|
||||
}
|
||||
|
|
|
@ -24,6 +24,8 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/FlyString.h>
|
||||
#include <LibJS/Runtime/Object.h>
|
||||
|
||||
|
@ -45,4 +47,18 @@ private:
|
|||
String m_message;
|
||||
};
|
||||
|
||||
#define DECLARE_ERROR_SUBCLASS(TitleCase, snake_case) \
|
||||
class TitleCase final : public Error { \
|
||||
public: \
|
||||
TitleCase(const String& message); \
|
||||
virtual ~TitleCase() override; \
|
||||
\
|
||||
private: \
|
||||
virtual const char* class_name() const override; \
|
||||
};
|
||||
|
||||
#define __JS_ENUMERATE_ERROR_SUBCLASS(TitleCase, snake_case) \
|
||||
DECLARE_ERROR_SUBCLASS(TitleCase, snake_case)
|
||||
JS_ENUMERATE_ERROR_SUBCLASSES
|
||||
#undef __JS_ENUMERATE_ERROR_SUBCLASS
|
||||
}
|
||||
|
|
|
@ -53,4 +53,28 @@ Value ErrorConstructor::construct(Interpreter& interpreter)
|
|||
return interpreter.heap().allocate<Error>("Error", message);
|
||||
}
|
||||
|
||||
#define DEFINE_ERROR_SUBCLASS_CONSTRUCTOR(TitleCase, snake_case) \
|
||||
TitleCase##Constructor::TitleCase##Constructor() \
|
||||
{ \
|
||||
put("prototype", interpreter().snake_case##_prototype()); \
|
||||
put("length", Value(1)); \
|
||||
} \
|
||||
TitleCase##Constructor::~TitleCase##Constructor() {} \
|
||||
Value TitleCase##Constructor::call(Interpreter& interpreter) \
|
||||
{ \
|
||||
return construct(interpreter); \
|
||||
} \
|
||||
Value TitleCase##Constructor::construct(Interpreter& interpreter) \
|
||||
{ \
|
||||
String message = ""; \
|
||||
if (!interpreter.call_frame().arguments.is_empty() && !interpreter.call_frame().arguments[0].is_undefined()) \
|
||||
message = interpreter.call_frame().arguments[0].to_string(); \
|
||||
return interpreter.heap().allocate<TitleCase>(message); \
|
||||
}
|
||||
|
||||
#define __JS_ENUMERATE_ERROR_SUBCLASS(TitleCase, snake_case) \
|
||||
DEFINE_ERROR_SUBCLASS_CONSTRUCTOR(TitleCase, snake_case)
|
||||
JS_ENUMERATE_ERROR_SUBCLASSES
|
||||
#undef __JS_ENUMERATE_ERROR_SUBCLASS
|
||||
|
||||
}
|
||||
|
|
|
@ -26,6 +26,7 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <LibJS/Runtime/Error.h>
|
||||
#include <LibJS/Runtime/NativeFunction.h>
|
||||
|
||||
namespace JS {
|
||||
|
@ -43,4 +44,22 @@ private:
|
|||
virtual const char* class_name() const override { return "ErrorConstructor"; }
|
||||
};
|
||||
|
||||
#define DECLARE_ERROR_SUBCLASS_CONSTRUCTOR(TitleCase, snake_case) \
|
||||
class TitleCase##Constructor final : public NativeFunction { \
|
||||
public: \
|
||||
TitleCase##Constructor(); \
|
||||
virtual ~TitleCase##Constructor() override; \
|
||||
virtual Value call(Interpreter&) override; \
|
||||
virtual Value construct(Interpreter&) override; \
|
||||
\
|
||||
private: \
|
||||
virtual bool has_constructor() const override { return true; } \
|
||||
virtual const char* class_name() const override { return #TitleCase "Constructor"; } \
|
||||
};
|
||||
|
||||
#define __JS_ENUMERATE_ERROR_SUBCLASS(TitleCase, snake_case) \
|
||||
DECLARE_ERROR_SUBCLASS_CONSTRUCTOR(TitleCase, snake_case)
|
||||
JS_ENUMERATE_ERROR_SUBCLASSES
|
||||
#undef __JS_ENUMERATE_ERROR_SUBCLASS
|
||||
|
||||
}
|
||||
|
|
|
@ -88,4 +88,14 @@ Value ErrorPrototype::to_string(Interpreter& interpreter)
|
|||
return js_string(interpreter, String::format("%s: %s", name.characters(), message.characters()));
|
||||
}
|
||||
|
||||
#define DEFINE_ERROR_SUBCLASS_PROTOTYPE(TitleCase, snake_case) \
|
||||
TitleCase::TitleCase() {} \
|
||||
TitleCase::~TitleCase() {} \
|
||||
const char* TitleCase::class_name() const { return #TitleCase; }
|
||||
|
||||
#define __JS_ENUMERATE_ERROR_SUBCLASS(TitleCase, snake_case) \
|
||||
DEFINE_ERROR_SUBCLASS_PROTOTYPE(TitleCase##Prototype, snake_case##_prototype)
|
||||
JS_ENUMERATE_ERROR_SUBCLASSES
|
||||
#undef __JS_ENUMERATE_ERROR_SUBCLASS
|
||||
|
||||
}
|
||||
|
|
|
@ -26,7 +26,7 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <LibJS/Runtime/Object.h>
|
||||
#include <LibJS/Runtime/Error.h>
|
||||
|
||||
namespace JS {
|
||||
|
||||
|
@ -44,4 +44,19 @@ private:
|
|||
static Value message_getter(Interpreter&);
|
||||
};
|
||||
|
||||
#define DECLARE_ERROR_SUBCLASS_PROTOTYPE(TitleCase, snake_case) \
|
||||
class TitleCase final : public Object { \
|
||||
public: \
|
||||
TitleCase(); \
|
||||
virtual ~TitleCase() override; \
|
||||
\
|
||||
private: \
|
||||
virtual const char* class_name() const override; \
|
||||
};
|
||||
|
||||
#define __JS_ENUMERATE_ERROR_SUBCLASS(TitleCase, snake_case) \
|
||||
DECLARE_ERROR_SUBCLASS_PROTOTYPE(TitleCase##Prototype, snake_case##_prototype)
|
||||
JS_ENUMERATE_ERROR_SUBCLASSES
|
||||
#undef __JS_ENUMERATE_ERROR_SUBCLASS
|
||||
|
||||
}
|
||||
|
|
|
@ -72,6 +72,11 @@ GlobalObject::GlobalObject()
|
|||
add_constructor("Function", m_function_constructor, *interpreter().function_prototype());
|
||||
add_constructor("Number", m_number_constructor, *interpreter().number_prototype());
|
||||
add_constructor("Object", m_object_constructor, *interpreter().object_prototype());
|
||||
|
||||
#define __JS_ENUMERATE_ERROR_SUBCLASS(TitleCase, snake_case) \
|
||||
add_constructor(#TitleCase, m_##snake_case##_constructor, *interpreter().snake_case##_prototype());
|
||||
JS_ENUMERATE_ERROR_SUBCLASSES
|
||||
#undef __JS_ENUMERATE_ERROR_SUBCLASS
|
||||
}
|
||||
|
||||
GlobalObject::~GlobalObject()
|
||||
|
|
|
@ -38,10 +38,15 @@ public:
|
|||
ArrayConstructor* array_constructor() { return m_array_constructor; }
|
||||
BooleanConstructor* boolean_constructor() { return m_boolean_constructor; }
|
||||
DateConstructor* date_constructor() { return m_date_constructor; }
|
||||
ErrorConstructor* error_constructor() { return m_error_constructor; }
|
||||
FunctionConstructor* function_constructor() { return m_function_constructor; }
|
||||
NumberConstructor* number_constructor() { return m_number_constructor; };
|
||||
ObjectConstructor* object_constructor() { return m_object_constructor; }
|
||||
ErrorConstructor* error_constructor() { return m_error_constructor; }
|
||||
|
||||
#define __JS_ENUMERATE_ERROR_SUBCLASS(TitleCase, snake_case) \
|
||||
TitleCase##Constructor* snake_case##_constructor() { return m_##snake_case##_constructor; }
|
||||
JS_ENUMERATE_ERROR_SUBCLASSES
|
||||
#undef __JS_ENUMERATE_ERROR_SUBCLASS
|
||||
|
||||
protected:
|
||||
virtual void visit_children(Visitor&) override;
|
||||
|
@ -58,10 +63,15 @@ private:
|
|||
ArrayConstructor* m_array_constructor { nullptr };
|
||||
BooleanConstructor* m_boolean_constructor { nullptr };
|
||||
DateConstructor* m_date_constructor { nullptr };
|
||||
ErrorConstructor* m_error_constructor { nullptr };
|
||||
FunctionConstructor* m_function_constructor { nullptr };
|
||||
NumberConstructor* m_number_constructor { nullptr };
|
||||
ObjectConstructor* m_object_constructor { nullptr };
|
||||
ErrorConstructor* m_error_constructor { nullptr };
|
||||
|
||||
#define __JS_ENUMERATE_ERROR_SUBCLASS(TitleCase, snake_case) \
|
||||
TitleCase##Constructor* m_##snake_case##_constructor;
|
||||
JS_ENUMERATE_ERROR_SUBCLASSES
|
||||
#undef __JS_ENUMERATE_ERROR_SUBCLASS
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue