summaryrefslogtreecommitdiff
path: root/Libraries
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-06-20 17:11:11 +0200
committerAndreas Kling <kling@serenityos.org>2020-06-20 17:50:48 +0200
commit06e29fac57f0c2f2647aea6686337b3e28b6fa85 (patch)
tree1ade439f1b066a7461a6e37839905d2312718f78 /Libraries
parent9610d18ebbfbcf01eb064e205bc66207230c713b (diff)
downloadserenity-06e29fac57f0c2f2647aea6686337b3e28b6fa85.zip
LibJS: Split more native object constructors into construct/initialize
Diffstat (limited to 'Libraries')
-rw-r--r--Libraries/LibJS/Runtime/BoundFunction.cpp11
-rw-r--r--Libraries/LibJS/Runtime/BoundFunction.h5
-rw-r--r--Libraries/LibJS/Runtime/ConsoleObject.cpp8
-rw-r--r--Libraries/LibJS/Runtime/ConsoleObject.h3
-rw-r--r--Libraries/LibJS/Runtime/DateConstructor.cpp4
-rw-r--r--Libraries/LibJS/Runtime/Function.cpp2
-rw-r--r--Libraries/LibJS/Runtime/GlobalObject.cpp8
-rw-r--r--Libraries/LibJS/Runtime/JSONObject.cpp8
-rw-r--r--Libraries/LibJS/Runtime/JSONObject.h3
-rw-r--r--Libraries/LibJS/Runtime/MathObject.cpp8
-rw-r--r--Libraries/LibJS/Runtime/MathObject.h3
-rw-r--r--Libraries/LibJS/Runtime/NumberConstructor.cpp6
-rw-r--r--Libraries/LibJS/Runtime/ReflectObject.cpp8
-rw-r--r--Libraries/LibJS/Runtime/ReflectObject.h3
-rw-r--r--Libraries/LibJS/Runtime/ScriptFunction.cpp14
-rw-r--r--Libraries/LibJS/Runtime/ScriptFunction.h3
16 files changed, 64 insertions, 33 deletions
diff --git a/Libraries/LibJS/Runtime/BoundFunction.cpp b/Libraries/LibJS/Runtime/BoundFunction.cpp
index 090f4d6e48..c6efbbc013 100644
--- a/Libraries/LibJS/Runtime/BoundFunction.cpp
+++ b/Libraries/LibJS/Runtime/BoundFunction.cpp
@@ -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()
diff --git a/Libraries/LibJS/Runtime/BoundFunction.h b/Libraries/LibJS/Runtime/BoundFunction.h
index 27387843df..67252ff360 100644
--- a/Libraries/LibJS/Runtime/BoundFunction.h
+++ b/Libraries/LibJS/Runtime/BoundFunction.h
@@ -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 };
};
}
diff --git a/Libraries/LibJS/Runtime/ConsoleObject.cpp b/Libraries/LibJS/Runtime/ConsoleObject.cpp
index 8489403ea9..449ab36dcf 100644
--- a/Libraries/LibJS/Runtime/ConsoleObject.cpp
+++ b/Libraries/LibJS/Runtime/ConsoleObject.cpp
@@ -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);
diff --git a/Libraries/LibJS/Runtime/ConsoleObject.h b/Libraries/LibJS/Runtime/ConsoleObject.h
index 0f2a947faa..77a764bd6a 100644
--- a/Libraries/LibJS/Runtime/ConsoleObject.h
+++ b/Libraries/LibJS/Runtime/ConsoleObject.h
@@ -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:
diff --git a/Libraries/LibJS/Runtime/DateConstructor.cpp b/Libraries/LibJS/Runtime/DateConstructor.cpp
index 41a02dd903..bb68057122 100644
--- a/Libraries/LibJS/Runtime/DateConstructor.cpp
+++ b/Libraries/LibJS/Runtime/DateConstructor.cpp
@@ -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);
diff --git a/Libraries/LibJS/Runtime/Function.cpp b/Libraries/LibJS/Runtime/Function.cpp
index 329386cedd..47dee74d7c 100644
--- a/Libraries/LibJS/Runtime/Function.cpp
+++ b/Libraries/LibJS/Runtime/Function.cpp
@@ -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)
diff --git a/Libraries/LibJS/Runtime/GlobalObject.cpp b/Libraries/LibJS/Runtime/GlobalObject.cpp
index a524db8407..4ce48b3eb6 100644
--- a/Libraries/LibJS/Runtime/GlobalObject.cpp
+++ b/Libraries/LibJS/Runtime/GlobalObject.cpp
@@ -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);
diff --git a/Libraries/LibJS/Runtime/JSONObject.cpp b/Libraries/LibJS/Runtime/JSONObject.cpp
index a0c00773f6..24f1e04f68 100644
--- a/Libraries/LibJS/Runtime/JSONObject.cpp
+++ b/Libraries/LibJS/Runtime/JSONObject.cpp
@@ -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);
diff --git a/Libraries/LibJS/Runtime/JSONObject.h b/Libraries/LibJS/Runtime/JSONObject.h
index 7d097793a9..3ce62280a2 100644
--- a/Libraries/LibJS/Runtime/JSONObject.h
+++ b/Libraries/LibJS/Runtime/JSONObject.h
@@ -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:
diff --git a/Libraries/LibJS/Runtime/MathObject.cpp b/Libraries/LibJS/Runtime/MathObject.cpp
index 1dcc6c2b3a..eb1384331d 100644
--- a/Libraries/LibJS/Runtime/MathObject.cpp
+++ b/Libraries/LibJS/Runtime/MathObject.cpp
@@ -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);
diff --git a/Libraries/LibJS/Runtime/MathObject.h b/Libraries/LibJS/Runtime/MathObject.h
index d1da19a672..c5a5868a0b 100644
--- a/Libraries/LibJS/Runtime/MathObject.h
+++ b/Libraries/LibJS/Runtime/MathObject.h
@@ -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:
diff --git a/Libraries/LibJS/Runtime/NumberConstructor.cpp b/Libraries/LibJS/Runtime/NumberConstructor.cpp
index 8e2df5a3f0..963aa4b0a2 100644
--- a/Libraries/LibJS/Runtime/NumberConstructor.cpp
+++ b/Libraries/LibJS/Runtime/NumberConstructor.cpp
@@ -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);
diff --git a/Libraries/LibJS/Runtime/ReflectObject.cpp b/Libraries/LibJS/Runtime/ReflectObject.cpp
index 510f5f2a26..b2fc5abf42 100644
--- a/Libraries/LibJS/Runtime/ReflectObject.cpp
+++ b/Libraries/LibJS/Runtime/ReflectObject.cpp
@@ -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);
diff --git a/Libraries/LibJS/Runtime/ReflectObject.h b/Libraries/LibJS/Runtime/ReflectObject.h
index 219485b03f..eeae86f308 100644
--- a/Libraries/LibJS/Runtime/ReflectObject.h
+++ b/Libraries/LibJS/Runtime/ReflectObject.h
@@ -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:
diff --git a/Libraries/LibJS/Runtime/ScriptFunction.cpp b/Libraries/LibJS/Runtime/ScriptFunction.cpp
index c5bf98b587..368dbd21d1 100644
--- a/Libraries/LibJS/Runtime/ScriptFunction.cpp
+++ b/Libraries/LibJS/Runtime/ScriptFunction.cpp
@@ -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);
}
diff --git a/Libraries/LibJS/Runtime/ScriptFunction.h b/Libraries/LibJS/Runtime/ScriptFunction.h
index 53b3c23a92..201e5d1af3 100644
--- a/Libraries/LibJS/Runtime/ScriptFunction.h
+++ b/Libraries/LibJS/Runtime/ScriptFunction.h
@@ -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; }