1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:07:47 +00:00

LibJS+LibWeb: Move native JS functions into dedicated member functions

Instead of implementing every native function as a lambda function,
use static member functions instead.

This makes it easier to navigate the code + backtraces look nicer. :^)
This commit is contained in:
Andreas Kling 2020-03-28 23:10:37 +01:00
parent 7c4e53f31e
commit 56936b97d0
20 changed files with 233 additions and 149 deletions

View file

@ -35,36 +35,43 @@ namespace JS {
ArrayPrototype::ArrayPrototype()
{
put_native_function("shift", [](Interpreter& interpreter) -> Value {
auto* this_object = interpreter.this_value().to_object(interpreter.heap());
if (!this_object)
return {};
ASSERT(this_object->is_array());
return static_cast<Array*>(this_object)->shift();
});
put_native_function("pop", [](Interpreter& interpreter) -> Value {
auto* this_object = interpreter.this_value().to_object(interpreter.heap());
if (!this_object)
return {};
ASSERT(this_object->is_array());
return static_cast<Array*>(this_object)->pop();
});
put_native_function("push", [](Interpreter& interpreter) -> Value {
auto* this_object = interpreter.this_value().to_object(interpreter.heap());
if (!this_object)
return {};
ASSERT(this_object->is_array());
if (interpreter.call_frame().arguments.is_empty())
return js_undefined();
static_cast<Array*>(this_object)->push(interpreter.call_frame().arguments[0]);
return Value(static_cast<const Array*>(this_object)->length());
});
put_native_function("shift", shift);
put_native_function("pop", pop);
put_native_function("push", push);
}
ArrayPrototype::~ArrayPrototype()
{
}
Value ArrayPrototype::push(Interpreter& interpreter)
{
auto* this_object = interpreter.this_value().to_object(interpreter.heap());
if (!this_object)
return {};
ASSERT(this_object->is_array());
if (interpreter.call_frame().arguments.is_empty())
return js_undefined();
static_cast<Array*>(this_object)->push(interpreter.call_frame().arguments[0]);
return Value(static_cast<const Array*>(this_object)->length());
}
Value ArrayPrototype::pop(Interpreter& interpreter)
{
auto* this_object = interpreter.this_value().to_object(interpreter.heap());
if (!this_object)
return {};
ASSERT(this_object->is_array());
return static_cast<Array*>(this_object)->pop();
}
Value ArrayPrototype::shift(Interpreter& interpreter)
{
auto* this_object = interpreter.this_value().to_object(interpreter.heap());
if (!this_object)
return {};
ASSERT(this_object->is_array());
return static_cast<Array*>(this_object)->shift();
}
}

View file

@ -37,6 +37,10 @@ public:
private:
virtual const char* class_name() const override { return "ArrayPrototype"; }
static Value push(Interpreter&);
static Value shift(Interpreter&);
static Value pop(Interpreter&);
};
}

View file

@ -34,19 +34,22 @@ namespace JS {
ConsoleObject::ConsoleObject()
{
put_native_function("log", [](Interpreter& interpreter) -> Value {
for (size_t i = 0; i < interpreter.call_frame().arguments.size(); ++i) {
printf("%s", interpreter.call_frame().arguments[i].to_string().characters());
if (i != interpreter.call_frame().arguments.size() - 1)
putchar(' ');
}
putchar('\n');
return js_undefined();
});
put_native_function("log", log);
}
ConsoleObject::~ConsoleObject()
{
}
Value ConsoleObject::log(Interpreter& interpreter)
{
for (size_t i = 0; i < interpreter.call_frame().arguments.size(); ++i) {
printf("%s", interpreter.call_frame().arguments[i].to_string().characters());
if (i != interpreter.call_frame().arguments.size() - 1)
putchar(' ');
}
putchar('\n');
return js_undefined();
}
}

View file

@ -37,6 +37,8 @@ public:
private:
virtual const char* class_name() const override { return "ConsoleObject"; }
static Value log(Interpreter&);
};
}

View file

@ -13,17 +13,10 @@ namespace JS {
GlobalObject::GlobalObject()
{
put_native_function("gc", gc);
put_native_function("isNaN", is_nan);
put("console", heap().allocate<ConsoleObject>());
put_native_function("gc", [](Interpreter& interpreter) -> Value {
dbg() << "Forced garbage collection requested!";
interpreter.heap().collect_garbage();
return js_undefined();
});
put_native_function("isNaN", [](Interpreter& interpreter) -> Value {
if (interpreter.call_frame().arguments.size() < 1)
return js_undefined();
return Value(interpreter.call_frame().arguments[0].to_number().is_nan());
});
put("Math", heap().allocate<MathObject>());
put("Object", heap().allocate<ObjectConstructor>());
}
@ -32,4 +25,18 @@ GlobalObject::~GlobalObject()
{
}
Value GlobalObject::gc(Interpreter& interpreter)
{
dbg() << "Forced garbage collection requested!";
interpreter.heap().collect_garbage();
return js_undefined();
}
Value GlobalObject::is_nan(Interpreter& interpreter)
{
if (interpreter.call_frame().arguments.size() < 1)
return js_undefined();
return Value(interpreter.call_frame().arguments[0].to_number().is_nan());
}
}

View file

@ -11,6 +11,9 @@ public:
private:
virtual const char* class_name() const override { return "GlobalObject"; }
static Value gc(Interpreter&);
static Value is_nan(Interpreter&);
};
}

View file

@ -32,18 +32,21 @@ namespace JS {
MathObject::MathObject()
{
put_native_function("random", [](Interpreter&) {
#ifdef __serenity__
double r = (double)arc4random() / (double)UINT32_MAX;
#else
double r = (double)rand() / (double)RAND_MAX;
#endif
return Value(r);
});
put_native_function("random", random);
}
MathObject::~MathObject()
{
}
Value MathObject::random(Interpreter&)
{
#ifdef __serenity__
double r = (double)arc4random() / (double)UINT32_MAX;
#else
double r = (double)rand() / (double)RAND_MAX;
#endif
return Value(r);
}
}

View file

@ -37,6 +37,8 @@ public:
private:
virtual const char* class_name() const override { return "MathObject"; }
static Value random(Interpreter&);
};
}

View file

@ -37,32 +37,39 @@ ObjectPrototype::ObjectPrototype()
{
set_prototype(nullptr);
put_native_function("hasOwnProperty", [](Interpreter& interpreter) -> Value {
auto* this_object = interpreter.this_value().to_object(interpreter.heap());
if (!this_object)
return {};
if (interpreter.call_frame().arguments.is_empty())
return js_undefined();
return Value(this_object->has_own_property(interpreter.call_frame().arguments[0].to_string()));
});
put_native_function("toString", [](Interpreter& interpreter) -> Value {
auto* this_object = interpreter.this_value().to_object(interpreter.heap());
if (!this_object)
return {};
return Value(this_object->to_string());
});
put_native_function("valueOf", [](Interpreter& interpreter) -> Value {
auto* this_object = interpreter.this_value().to_object(interpreter.heap());
if (!this_object)
return {};
return this_object->value_of();
});
put_native_function("hasOwnProperty", has_own_property);
put_native_function("toString", to_string);
put_native_function("valueOf", value_of);
}
ObjectPrototype::~ObjectPrototype()
{
}
Value ObjectPrototype::has_own_property(Interpreter& interpreter)
{
auto* this_object = interpreter.this_value().to_object(interpreter.heap());
if (!this_object)
return {};
if (interpreter.call_frame().arguments.is_empty())
return js_undefined();
return Value(this_object->has_own_property(interpreter.call_frame().arguments[0].to_string()));
}
Value ObjectPrototype::to_string(Interpreter& interpreter)
{
auto* this_object = interpreter.this_value().to_object(interpreter.heap());
if (!this_object)
return {};
return Value(this_object->to_string());
}
Value ObjectPrototype::value_of(Interpreter& interpreter)
{
auto* this_object = interpreter.this_value().to_object(interpreter.heap());
if (!this_object)
return {};
return this_object->value_of();
}
}

View file

@ -37,6 +37,10 @@ public:
private:
virtual const char* class_name() const override { return "ObjectPrototype"; }
static Value has_own_property(Interpreter&);
static Value value_of(Interpreter&);
static Value to_string(Interpreter&);
};
}

View file

@ -44,42 +44,48 @@ StringPrototype::StringPrototype()
return Value((i32) static_cast<const StringObject*>(this_object)->primitive_string()->string().length());
},
nullptr);
put_native_function("charAt", [](Interpreter& interpreter) -> Value {
auto* this_object = interpreter.this_value().to_object(interpreter.heap());
if (!this_object)
return {};
i32 index = 0;
if (!interpreter.call_frame().arguments.is_empty())
index = interpreter.call_frame().arguments[0].to_i32();
ASSERT(this_object->is_string_object());
auto underlying_string = static_cast<const StringObject*>(this_object)->primitive_string()->string();
if (index < 0 || index >= static_cast<i32>(underlying_string.length()))
return js_string(this_object->heap(), String::empty());
return js_string(this_object->heap(), underlying_string.substring(index, 1));
});
put_native_function("repeat", [](Interpreter& interpreter) -> Value {
auto* this_object = interpreter.this_value().to_object(interpreter.heap());
if (!this_object)
return {};
ASSERT(this_object->is_string_object());
if (interpreter.call_frame().arguments.is_empty())
return js_string(this_object->heap(), String::empty());
i32 count = 0;
count = interpreter.call_frame().arguments[0].to_i32();
if (count < 0) {
// FIXME: throw RangeError
return js_undefined();
}
auto* string_object = static_cast<StringObject*>(this_object);
StringBuilder builder;
for (i32 i = 0; i < count; ++i)
builder.append(string_object->primitive_string()->string());
return js_string(this_object->heap(), builder.to_string());
});
put_native_function("charAt", char_at);
put_native_function("repeat", repeat);
}
StringPrototype::~StringPrototype()
{
}
Value StringPrototype::char_at(Interpreter& interpreter)
{
auto* this_object = interpreter.this_value().to_object(interpreter.heap());
if (!this_object)
return {};
i32 index = 0;
if (!interpreter.call_frame().arguments.is_empty())
index = interpreter.call_frame().arguments[0].to_i32();
ASSERT(this_object->is_string_object());
auto underlying_string = static_cast<const StringObject*>(this_object)->primitive_string()->string();
if (index < 0 || index >= static_cast<i32>(underlying_string.length()))
return js_string(interpreter.heap(), String::empty());
return js_string(interpreter.heap(), underlying_string.substring(index, 1));
}
Value StringPrototype::repeat(Interpreter& interpreter)
{
auto* this_object = interpreter.this_value().to_object(interpreter.heap());
if (!this_object)
return {};
ASSERT(this_object->is_string_object());
if (interpreter.call_frame().arguments.is_empty())
return js_string(interpreter.heap(), String::empty());
i32 count = 0;
count = interpreter.call_frame().arguments[0].to_i32();
if (count < 0) {
// FIXME: throw RangeError
return js_undefined();
}
auto* string_object = static_cast<StringObject*>(this_object);
StringBuilder builder;
for (i32 i = 0; i < count; ++i)
builder.append(string_object->primitive_string()->string());
return js_string(interpreter.heap(), builder.to_string());
}
}

View file

@ -37,6 +37,9 @@ public:
private:
virtual const char* class_name() const override { return "StringPrototype"; }
static Value char_at(Interpreter&);
static Value repeat(Interpreter&);
};
}