mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 22:17:42 +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:
parent
0d41e542b7
commit
bbd592cb6c
13 changed files with 22 additions and 4 deletions
|
@ -34,6 +34,7 @@
|
|||
namespace JS {
|
||||
|
||||
ArrayConstructor::ArrayConstructor()
|
||||
: NativeFunction("Array")
|
||||
{
|
||||
put("prototype", interpreter().array_prototype());
|
||||
put("length", Value(1));
|
||||
|
|
|
@ -33,6 +33,7 @@
|
|||
namespace JS {
|
||||
|
||||
BooleanConstructor::BooleanConstructor()
|
||||
: NativeFunction("Boolean")
|
||||
{
|
||||
put("prototype", Value(interpreter().boolean_prototype()));
|
||||
put("length", Value(1));
|
||||
|
|
|
@ -34,6 +34,7 @@
|
|||
namespace JS {
|
||||
|
||||
DateConstructor::DateConstructor()
|
||||
: NativeFunction("Date")
|
||||
{
|
||||
put("prototype", interpreter().date_prototype());
|
||||
put("length", Value(7));
|
||||
|
|
|
@ -31,6 +31,7 @@
|
|||
namespace JS {
|
||||
|
||||
ErrorConstructor::ErrorConstructor()
|
||||
: NativeFunction("Error")
|
||||
{
|
||||
put("prototype", interpreter().error_prototype());
|
||||
put("length", Value(1));
|
||||
|
|
|
@ -35,6 +35,7 @@
|
|||
namespace JS {
|
||||
|
||||
FunctionConstructor::FunctionConstructor()
|
||||
: NativeFunction("Function")
|
||||
{
|
||||
put("prototype", interpreter().function_prototype());
|
||||
put("length", Value(1));
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -36,6 +36,11 @@ NativeFunction::NativeFunction(const FlyString& name, AK::Function<Value(Interpr
|
|||
{
|
||||
}
|
||||
|
||||
NativeFunction::NativeFunction(const FlyString& name)
|
||||
: m_name(name)
|
||||
{
|
||||
}
|
||||
|
||||
NativeFunction::~NativeFunction()
|
||||
{
|
||||
}
|
||||
|
|
|
@ -43,6 +43,7 @@ public:
|
|||
virtual bool has_constructor() const { return false; }
|
||||
|
||||
protected:
|
||||
NativeFunction(const FlyString& name);
|
||||
NativeFunction() {}
|
||||
|
||||
private:
|
||||
|
|
|
@ -37,6 +37,7 @@
|
|||
namespace JS {
|
||||
|
||||
NumberConstructor::NumberConstructor()
|
||||
: NativeFunction("Number")
|
||||
{
|
||||
put_native_function("isSafeInteger", is_safe_integer, 1);
|
||||
|
||||
|
|
|
@ -35,6 +35,7 @@
|
|||
namespace JS {
|
||||
|
||||
ObjectConstructor::ObjectConstructor()
|
||||
: NativeFunction("Object")
|
||||
{
|
||||
put("prototype", interpreter().object_prototype());
|
||||
|
||||
|
|
|
@ -33,6 +33,7 @@
|
|||
namespace JS {
|
||||
|
||||
StringConstructor::StringConstructor()
|
||||
: NativeFunction("String")
|
||||
{
|
||||
put("prototype", interpreter().string_prototype());
|
||||
put("length", Value(1));
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue