Skip to content

Commit

Permalink
LibJS: Tweak FunctionPrototype::to_string and constructors
Browse files Browse the repository at this point in the history
The output of FunctionPrototype::to_string is now more in line
with the output in Firefox. The builtin constructors have been
extended to include their function name in the output.
  • Loading branch information
sunverwerth authored and awesomekling committed Apr 12, 2020
1 parent 0d41e54 commit bbd592c
Show file tree
Hide file tree
Showing 13 changed files with 22 additions and 4 deletions.
1 change: 1 addition & 0 deletions Libraries/LibJS/Runtime/ArrayConstructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
namespace JS {

ArrayConstructor::ArrayConstructor()
: NativeFunction("Array")
{
put("prototype", interpreter().array_prototype());
put("length", Value(1));
Expand Down
1 change: 1 addition & 0 deletions Libraries/LibJS/Runtime/BooleanConstructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
namespace JS {

BooleanConstructor::BooleanConstructor()
: NativeFunction("Boolean")
{
put("prototype", Value(interpreter().boolean_prototype()));
put("length", Value(1));
Expand Down
1 change: 1 addition & 0 deletions Libraries/LibJS/Runtime/DateConstructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
namespace JS {

DateConstructor::DateConstructor()
: NativeFunction("Date")
{
put("prototype", interpreter().date_prototype());
put("length", Value(7));
Expand Down
1 change: 1 addition & 0 deletions Libraries/LibJS/Runtime/ErrorConstructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
namespace JS {

ErrorConstructor::ErrorConstructor()
: NativeFunction("Error")
{
put("prototype", interpreter().error_prototype());
put("length", Value(1));
Expand Down
1 change: 1 addition & 0 deletions Libraries/LibJS/Runtime/FunctionConstructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
namespace JS {

FunctionConstructor::FunctionConstructor()
: NativeFunction("Function")
{
put("prototype", interpreter().function_prototype());
put("length", Value(1));
Expand Down
6 changes: 5 additions & 1 deletion Libraries/LibJS/Runtime/FunctionPrototype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,11 @@ Value FunctionPrototype::to_string(Interpreter& interpreter)
// function_body = body.to_source();
function_body = " ???";
}
auto function_source = String::format("function %s(%s) {\n%s\n}", function_name.characters(), function_parameters.characters(), function_body.characters());

auto function_source = String::format("function %s(%s) {\n%s\n}",
function_name.is_null() ? "" : function_name.characters(),
function_parameters.characters(),
function_body.characters());
return js_string(interpreter, function_source);
}

Expand Down
5 changes: 5 additions & 0 deletions Libraries/LibJS/Runtime/NativeFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ NativeFunction::NativeFunction(const FlyString& name, AK::Function<Value(Interpr
{
}

NativeFunction::NativeFunction(const FlyString& name)
: m_name(name)
{
}

NativeFunction::~NativeFunction()
{
}
Expand Down
1 change: 1 addition & 0 deletions Libraries/LibJS/Runtime/NativeFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class NativeFunction : public Function {
virtual bool has_constructor() const { return false; }

protected:
NativeFunction(const FlyString& name);
NativeFunction() {}

private:
Expand Down
1 change: 1 addition & 0 deletions Libraries/LibJS/Runtime/NumberConstructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
namespace JS {

NumberConstructor::NumberConstructor()
: NativeFunction("Number")
{
put_native_function("isSafeInteger", is_safe_integer, 1);

Expand Down
1 change: 1 addition & 0 deletions Libraries/LibJS/Runtime/ObjectConstructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
namespace JS {

ObjectConstructor::ObjectConstructor()
: NativeFunction("Object")
{
put("prototype", interpreter().object_prototype());

Expand Down
1 change: 1 addition & 0 deletions Libraries/LibJS/Runtime/StringConstructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
namespace JS {

StringConstructor::StringConstructor()
: NativeFunction("String")
{
put("prototype", interpreter().string_prototype());
put("length", Value(1));
Expand Down
4 changes: 2 additions & 2 deletions Libraries/LibJS/Tests/Function.prototype.toString.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ try {
}
return bar + 42;
}).toString() === "function (foo, bar, baz) {\n ???\n}");
assert(console.log.toString() === "function () {\n [NativeFunction]\n}");
assert(Function.toString() === "function () {\n [FunctionConstructor]\n}");
assert(console.log.toString() === "function log() {\n [NativeFunction]\n}");
assert(Function.toString() === "function Function() {\n [FunctionConstructor]\n}");

console.log("PASS");
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion Libraries/LibJS/Tests/function-TypeError.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ try {
new isNaN();
} catch(e) {
assert(e.name === "TypeError");
assert(e.message === "function () {\n [NativeFunction]\n} is not a constructor");
assert(e.message === "function isNaN() {\n [NativeFunction]\n} is not a constructor");
}

console.log("PASS");
Expand Down

0 comments on commit bbd592c

Please sign in to comment.