LibJS: Split more native object constructors into construct/initialize

This commit is contained in:
Andreas Kling 2020-06-20 17:11:11 +02:00
parent 9610d18ebb
commit 06e29fac57
16 changed files with 64 additions and 33 deletions

View file

@ -30,13 +30,18 @@
namespace JS {
BoundFunction::BoundFunction(Function& target_function, Value bound_this, Vector<Value> arguments, i32 length, Object* constructor_prototype)
: Function::Function(*interpreter().global_object().function_prototype(), bound_this, move(arguments))
BoundFunction::BoundFunction(GlobalObject& global_object, Function& target_function, Value bound_this, Vector<Value> arguments, i32 length, Object* constructor_prototype)
: Function::Function(*global_object.function_prototype(), bound_this, move(arguments))
, m_target_function(&target_function)
, m_constructor_prototype(constructor_prototype)
, m_name(String::format("bound %s", target_function.name().characters()))
, m_length(length)
{
define_property("length", Value(length), Attribute::Configurable);
}
void BoundFunction::initialize(Interpreter&, GlobalObject&)
{
define_property("length", Value(m_length), Attribute::Configurable);
}
BoundFunction::~BoundFunction()

View file

@ -32,8 +32,8 @@ namespace JS {
class BoundFunction final : public Function {
public:
BoundFunction(Function& target_function, Value bound_this, Vector<Value> arguments, i32 length, Object* constructor_prototype);
BoundFunction(GlobalObject&, Function& target_function, Value bound_this, Vector<Value> arguments, i32 length, Object* constructor_prototype);
virtual void initialize(Interpreter&, GlobalObject&) override;
virtual ~BoundFunction();
virtual Value call(Interpreter& interpreter) override;
@ -61,6 +61,7 @@ private:
Function* m_target_function = nullptr;
Object* m_constructor_prototype = nullptr;
FlyString m_name;
i32 m_length { 0 };
};
}

View file

@ -35,8 +35,12 @@
namespace JS {
ConsoleObject::ConsoleObject()
: Object(interpreter().global_object().object_prototype())
ConsoleObject::ConsoleObject(GlobalObject& global_object)
: Object(global_object.object_prototype())
{
}
void ConsoleObject::initialize(Interpreter&, GlobalObject&)
{
define_native_function("log", log);
define_native_function("debug", debug);

View file

@ -32,7 +32,8 @@ namespace JS {
class ConsoleObject final : public Object {
public:
ConsoleObject();
explicit ConsoleObject(GlobalObject&);
virtual void initialize(Interpreter&, GlobalObject&) override;
virtual ~ConsoleObject() override;
private:

View file

@ -39,9 +39,9 @@ DateConstructor::DateConstructor(GlobalObject& global_object)
{
}
void DateConstructor::initialize(Interpreter&, GlobalObject&)
void DateConstructor::initialize(Interpreter&, GlobalObject& global_object)
{
define_property("prototype", interpreter().global_object().date_prototype(), 0);
define_property("prototype", global_object.date_prototype(), 0);
define_property("length", Value(7), Attribute::Configurable);
define_native_function("now", now, 0, Attribute::Writable | Attribute::Configurable);

View file

@ -82,7 +82,7 @@ BoundFunction* Function::bind(Value bound_this_value, Vector<Value> arguments)
auto all_bound_arguments = bound_arguments();
all_bound_arguments.append(move(arguments));
return interpreter().heap().allocate<BoundFunction>(global_object(), target_function, bound_this_object, move(all_bound_arguments), computed_length, constructor_prototype);
return interpreter().heap().allocate<BoundFunction>(global_object(), global_object(), target_function, bound_this_object, move(all_bound_arguments), computed_length, constructor_prototype);
}
void Function::visit_children(Visitor& visitor)

View file

@ -95,10 +95,10 @@ void GlobalObject::initialize()
define_property("undefined", js_undefined(), 0);
define_property("globalThis", this, attr);
define_property("console", heap().allocate<ConsoleObject>(*this), attr);
define_property("Math", heap().allocate<MathObject>(*this), attr);
define_property("JSON", heap().allocate<JSONObject>(*this), attr);
define_property("Reflect", heap().allocate<ReflectObject>(*this), attr);
define_property("console", heap().allocate<ConsoleObject>(*this, *this), attr);
define_property("Math", heap().allocate<MathObject>(*this, *this), attr);
define_property("JSON", heap().allocate<JSONObject>(*this, *this), attr);
define_property("Reflect", heap().allocate<ReflectObject>(*this, *this), attr);
add_constructor("Array", m_array_constructor, *m_array_prototype);
add_constructor("BigInt", m_bigint_constructor, *m_bigint_prototype);

View file

@ -37,8 +37,12 @@
namespace JS {
JSONObject::JSONObject()
: Object(interpreter().global_object().object_prototype())
JSONObject::JSONObject(GlobalObject& global_object)
: Object(global_object.object_prototype())
{
}
void JSONObject::initialize(Interpreter&, GlobalObject&)
{
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function("stringify", stringify, 3, attr);

View file

@ -32,7 +32,8 @@ namespace JS {
class JSONObject final : public Object {
public:
JSONObject();
explicit JSONObject(GlobalObject&);
virtual void initialize(Interpreter&, GlobalObject&) override;
virtual ~JSONObject() override;
private:

View file

@ -34,8 +34,12 @@
namespace JS {
MathObject::MathObject()
: Object(interpreter().global_object().object_prototype())
MathObject::MathObject(GlobalObject& global_object)
: Object(global_object.object_prototype())
{
}
void MathObject::initialize(Interpreter&, GlobalObject&)
{
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function("abs", abs, 1, attr);

View file

@ -32,7 +32,8 @@ namespace JS {
class MathObject final : public Object {
public:
MathObject();
explicit MathObject(GlobalObject&);
virtual void initialize(Interpreter&, GlobalObject&) override;
virtual ~MathObject() override;
private:

View file

@ -42,15 +42,15 @@ NumberConstructor::NumberConstructor(GlobalObject& global_object)
{
}
void NumberConstructor::initialize(Interpreter&, GlobalObject&)
void NumberConstructor::initialize(Interpreter&, GlobalObject& global_object)
{
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function("isFinite", is_finite, 1, attr);
define_native_function("isInteger", is_integer, 1, attr);
define_native_function("isNaN", is_nan, 1, attr);
define_native_function("isSafeInteger", is_safe_integer, 1, attr);
define_property("parseFloat", interpreter().global_object().get("parseFloat"));
define_property("prototype", interpreter().global_object().number_prototype(), 0);
define_property("parseFloat", global_object.get("parseFloat"));
define_property("prototype", global_object.number_prototype(), 0);
define_property("length", Value(1), Attribute::Configurable);
define_property("EPSILON", Value(EPSILON), 0);
define_property("MAX_SAFE_INTEGER", Value(MAX_SAFE_INTEGER), 0);

View file

@ -75,8 +75,12 @@ static void prepare_arguments_list(Interpreter& interpreter, Value value, Marked
}
}
ReflectObject::ReflectObject()
: Object(interpreter().global_object().object_prototype())
ReflectObject::ReflectObject(GlobalObject& global_object)
: Object(global_object.object_prototype())
{
}
void ReflectObject::initialize(Interpreter&, GlobalObject&)
{
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function("apply", apply, 3, attr);

View file

@ -32,7 +32,8 @@ namespace JS {
class ReflectObject final : public Object {
public:
ReflectObject();
explicit ReflectObject(GlobalObject&);
virtual void initialize(Interpreter&, GlobalObject&) override;
virtual ~ReflectObject() override;
private:

View file

@ -49,11 +49,11 @@ static ScriptFunction* typed_this(Interpreter& interpreter, GlobalObject& global
ScriptFunction* ScriptFunction::create(GlobalObject& global_object, const FlyString& name, const Statement& body, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, LexicalEnvironment* parent_environment, bool is_arrow_function)
{
return global_object.heap().allocate<ScriptFunction>(global_object, name, body, move(parameters), m_function_length, parent_environment, *global_object.function_prototype(), is_arrow_function);
return global_object.heap().allocate<ScriptFunction>(global_object, global_object, name, body, move(parameters), m_function_length, parent_environment, *global_object.function_prototype(), is_arrow_function);
}
ScriptFunction::ScriptFunction(const FlyString& name, const Statement& body, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, LexicalEnvironment* parent_environment, Object& prototype, bool is_arrow_function)
: Function(prototype, is_arrow_function ? interpreter().this_value(interpreter().global_object()) : Value(), {})
ScriptFunction::ScriptFunction(GlobalObject& global_object, const FlyString& name, const Statement& body, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, LexicalEnvironment* parent_environment, Object& prototype, bool is_arrow_function)
: Function(prototype, is_arrow_function ? interpreter().this_value(global_object) : Value(), {})
, m_name(name)
, m_body(body)
, m_parameters(move(parameters))
@ -61,8 +61,12 @@ ScriptFunction::ScriptFunction(const FlyString& name, const Statement& body, Vec
, m_function_length(m_function_length)
, m_is_arrow_function(is_arrow_function)
{
if (!is_arrow_function)
define_property("prototype", Object::create_empty(interpreter(), interpreter().global_object()), 0);
}
void ScriptFunction::initialize(Interpreter& interpreter, GlobalObject& global_object)
{
if (!m_is_arrow_function)
define_property("prototype", Object::create_empty(interpreter, global_object), 0);
define_native_property("length", length_getter, nullptr, Attribute::Configurable);
define_native_property("name", name_getter, nullptr, Attribute::Configurable);
}

View file

@ -35,7 +35,8 @@ class ScriptFunction final : public Function {
public:
static ScriptFunction* create(GlobalObject&, const FlyString& name, const Statement& body, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, LexicalEnvironment* parent_environment, bool is_arrow_function = false);
ScriptFunction(const FlyString& name, const Statement& body, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, LexicalEnvironment* parent_environment, Object& prototype, bool is_arrow_function = false);
ScriptFunction(GlobalObject&, const FlyString& name, const Statement& body, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, LexicalEnvironment* parent_environment, Object& prototype, bool is_arrow_function = false);
virtual void initialize(Interpreter&, GlobalObject&) override;
virtual ~ScriptFunction();
const Statement& body() const { return m_body; }