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

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.
This commit is contained in:
Stephan Unverwerth 2020-04-13 01:07:31 +02:00 committed by Andreas Kling
parent 0d41e542b7
commit bbd592cb6c
13 changed files with 22 additions and 4 deletions

View file

@ -34,6 +34,7 @@
namespace JS { namespace JS {
ArrayConstructor::ArrayConstructor() ArrayConstructor::ArrayConstructor()
: NativeFunction("Array")
{ {
put("prototype", interpreter().array_prototype()); put("prototype", interpreter().array_prototype());
put("length", Value(1)); put("length", Value(1));

View file

@ -33,6 +33,7 @@
namespace JS { namespace JS {
BooleanConstructor::BooleanConstructor() BooleanConstructor::BooleanConstructor()
: NativeFunction("Boolean")
{ {
put("prototype", Value(interpreter().boolean_prototype())); put("prototype", Value(interpreter().boolean_prototype()));
put("length", Value(1)); put("length", Value(1));

View file

@ -34,6 +34,7 @@
namespace JS { namespace JS {
DateConstructor::DateConstructor() DateConstructor::DateConstructor()
: NativeFunction("Date")
{ {
put("prototype", interpreter().date_prototype()); put("prototype", interpreter().date_prototype());
put("length", Value(7)); put("length", Value(7));

View file

@ -31,6 +31,7 @@
namespace JS { namespace JS {
ErrorConstructor::ErrorConstructor() ErrorConstructor::ErrorConstructor()
: NativeFunction("Error")
{ {
put("prototype", interpreter().error_prototype()); put("prototype", interpreter().error_prototype());
put("length", Value(1)); put("length", Value(1));

View file

@ -35,6 +35,7 @@
namespace JS { namespace JS {
FunctionConstructor::FunctionConstructor() FunctionConstructor::FunctionConstructor()
: NativeFunction("Function")
{ {
put("prototype", interpreter().function_prototype()); put("prototype", interpreter().function_prototype());
put("length", Value(1)); put("length", Value(1));

View file

@ -122,7 +122,11 @@ Value FunctionPrototype::to_string(Interpreter& interpreter)
// function_body = body.to_source(); // function_body = body.to_source();
function_body = " ???"; 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); return js_string(interpreter, function_source);
} }

View file

@ -36,6 +36,11 @@ NativeFunction::NativeFunction(const FlyString& name, AK::Function<Value(Interpr
{ {
} }
NativeFunction::NativeFunction(const FlyString& name)
: m_name(name)
{
}
NativeFunction::~NativeFunction() NativeFunction::~NativeFunction()
{ {
} }

View file

@ -43,6 +43,7 @@ public:
virtual bool has_constructor() const { return false; } virtual bool has_constructor() const { return false; }
protected: protected:
NativeFunction(const FlyString& name);
NativeFunction() {} NativeFunction() {}
private: private:

View file

@ -37,6 +37,7 @@
namespace JS { namespace JS {
NumberConstructor::NumberConstructor() NumberConstructor::NumberConstructor()
: NativeFunction("Number")
{ {
put_native_function("isSafeInteger", is_safe_integer, 1); put_native_function("isSafeInteger", is_safe_integer, 1);

View file

@ -35,6 +35,7 @@
namespace JS { namespace JS {
ObjectConstructor::ObjectConstructor() ObjectConstructor::ObjectConstructor()
: NativeFunction("Object")
{ {
put("prototype", interpreter().object_prototype()); put("prototype", interpreter().object_prototype());

View file

@ -33,6 +33,7 @@
namespace JS { namespace JS {
StringConstructor::StringConstructor() StringConstructor::StringConstructor()
: NativeFunction("String")
{ {
put("prototype", interpreter().string_prototype()); put("prototype", interpreter().string_prototype());
put("length", Value(1)); put("length", Value(1));

View file

@ -10,8 +10,8 @@ try {
} }
return bar + 42; return bar + 42;
}).toString() === "function (foo, bar, baz) {\n ???\n}"); }).toString() === "function (foo, bar, baz) {\n ???\n}");
assert(console.log.toString() === "function () {\n [NativeFunction]\n}"); assert(console.log.toString() === "function log() {\n [NativeFunction]\n}");
assert(Function.toString() === "function () {\n [FunctionConstructor]\n}"); assert(Function.toString() === "function Function() {\n [FunctionConstructor]\n}");
console.log("PASS"); console.log("PASS");
} catch (e) { } catch (e) {

View file

@ -41,7 +41,7 @@ try {
new isNaN(); new isNaN();
} catch(e) { } catch(e) {
assert(e.name === "TypeError"); 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"); console.log("PASS");