From bbd592cb6c449dd0b96b8e6a8103126d698f8262 Mon Sep 17 00:00:00 2001 From: Stephan Unverwerth Date: Mon, 13 Apr 2020 01:07:31 +0200 Subject: [PATCH] LibJS: Tweak FunctionPrototype::to_string and constructors 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. --- Libraries/LibJS/Runtime/ArrayConstructor.cpp | 1 + Libraries/LibJS/Runtime/BooleanConstructor.cpp | 1 + Libraries/LibJS/Runtime/DateConstructor.cpp | 1 + Libraries/LibJS/Runtime/ErrorConstructor.cpp | 1 + Libraries/LibJS/Runtime/FunctionConstructor.cpp | 1 + Libraries/LibJS/Runtime/FunctionPrototype.cpp | 6 +++++- Libraries/LibJS/Runtime/NativeFunction.cpp | 5 +++++ Libraries/LibJS/Runtime/NativeFunction.h | 1 + Libraries/LibJS/Runtime/NumberConstructor.cpp | 1 + Libraries/LibJS/Runtime/ObjectConstructor.cpp | 1 + Libraries/LibJS/Runtime/StringConstructor.cpp | 1 + Libraries/LibJS/Tests/Function.prototype.toString.js | 4 ++-- Libraries/LibJS/Tests/function-TypeError.js | 2 +- 13 files changed, 22 insertions(+), 4 deletions(-) diff --git a/Libraries/LibJS/Runtime/ArrayConstructor.cpp b/Libraries/LibJS/Runtime/ArrayConstructor.cpp index 6be1953fff..bfaaccd05f 100644 --- a/Libraries/LibJS/Runtime/ArrayConstructor.cpp +++ b/Libraries/LibJS/Runtime/ArrayConstructor.cpp @@ -34,6 +34,7 @@ namespace JS { ArrayConstructor::ArrayConstructor() + : NativeFunction("Array") { put("prototype", interpreter().array_prototype()); put("length", Value(1)); diff --git a/Libraries/LibJS/Runtime/BooleanConstructor.cpp b/Libraries/LibJS/Runtime/BooleanConstructor.cpp index c8254abd66..3f5d54a68a 100644 --- a/Libraries/LibJS/Runtime/BooleanConstructor.cpp +++ b/Libraries/LibJS/Runtime/BooleanConstructor.cpp @@ -33,6 +33,7 @@ namespace JS { BooleanConstructor::BooleanConstructor() + : NativeFunction("Boolean") { put("prototype", Value(interpreter().boolean_prototype())); put("length", Value(1)); diff --git a/Libraries/LibJS/Runtime/DateConstructor.cpp b/Libraries/LibJS/Runtime/DateConstructor.cpp index 407c95d38d..9f825a6b96 100644 --- a/Libraries/LibJS/Runtime/DateConstructor.cpp +++ b/Libraries/LibJS/Runtime/DateConstructor.cpp @@ -34,6 +34,7 @@ namespace JS { DateConstructor::DateConstructor() + : NativeFunction("Date") { put("prototype", interpreter().date_prototype()); put("length", Value(7)); diff --git a/Libraries/LibJS/Runtime/ErrorConstructor.cpp b/Libraries/LibJS/Runtime/ErrorConstructor.cpp index b62018fe02..082d3d3944 100644 --- a/Libraries/LibJS/Runtime/ErrorConstructor.cpp +++ b/Libraries/LibJS/Runtime/ErrorConstructor.cpp @@ -31,6 +31,7 @@ namespace JS { ErrorConstructor::ErrorConstructor() + : NativeFunction("Error") { put("prototype", interpreter().error_prototype()); put("length", Value(1)); diff --git a/Libraries/LibJS/Runtime/FunctionConstructor.cpp b/Libraries/LibJS/Runtime/FunctionConstructor.cpp index 6a005f8324..fc93e3d13c 100644 --- a/Libraries/LibJS/Runtime/FunctionConstructor.cpp +++ b/Libraries/LibJS/Runtime/FunctionConstructor.cpp @@ -35,6 +35,7 @@ namespace JS { FunctionConstructor::FunctionConstructor() + : NativeFunction("Function") { put("prototype", interpreter().function_prototype()); put("length", Value(1)); diff --git a/Libraries/LibJS/Runtime/FunctionPrototype.cpp b/Libraries/LibJS/Runtime/FunctionPrototype.cpp index 33f36335af..62e6c60068 100644 --- a/Libraries/LibJS/Runtime/FunctionPrototype.cpp +++ b/Libraries/LibJS/Runtime/FunctionPrototype.cpp @@ -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); } diff --git a/Libraries/LibJS/Runtime/NativeFunction.cpp b/Libraries/LibJS/Runtime/NativeFunction.cpp index e50988105c..aadf7d8060 100644 --- a/Libraries/LibJS/Runtime/NativeFunction.cpp +++ b/Libraries/LibJS/Runtime/NativeFunction.cpp @@ -36,6 +36,11 @@ NativeFunction::NativeFunction(const FlyString& name, AK::Function