1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 03:47:35 +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:
Andreas Kling 2020-04-10 12:42:33 +02:00
parent df7b617ba1
commit 58ab76269c
11 changed files with 157 additions and 7 deletions

View file

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

View file

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

View file

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

View file

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

View file

@ -80,7 +80,7 @@ Value ErrorPrototype::to_string(Interpreter& interpreter)
auto object_message_property = this_object.get("message");
if (object_message_property.has_value() && !object_message_property.value().is_undefined())
message = object_message_property.value().to_string();
if (name.length() == 0)
return js_string(interpreter, message);
if (message.length() == 0)
@ -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
}

View file

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

View file

@ -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()

View file

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