summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2022-02-20 17:13:16 +0000
committerLinus Groh <mail@linusgroh.de>2022-02-20 23:21:40 +0000
commite657e88ed662db962dbb1d6b825cd96be221ad3b (patch)
tree9c4c0d671dacfdf3b77cc0649e576c133520ed99
parent0be67ef12cda977e1ffbe92ac3f95a5e2d1e8b9e (diff)
downloadserenity-e657e88ed662db962dbb1d6b825cd96be221ad3b.zip
LibJS: Add [[InitialName]] and use it in Function.prototype.toString()
-rw-r--r--Userland/Libraries/LibJS/Runtime/FunctionObject.cpp4
-rw-r--r--Userland/Libraries/LibJS/Runtime/FunctionPrototype.cpp8
-rw-r--r--Userland/Libraries/LibJS/Runtime/NativeFunction.h7
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Function/Function.prototype.toString.js3
4 files changed, 18 insertions, 4 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/FunctionObject.cpp b/Userland/Libraries/LibJS/Runtime/FunctionObject.cpp
index 4997c1cee7..5bcde28169 100644
--- a/Userland/Libraries/LibJS/Runtime/FunctionObject.cpp
+++ b/Userland/Libraries/LibJS/Runtime/FunctionObject.cpp
@@ -61,7 +61,7 @@ void FunctionObject::set_function_name(Variant<PropertyKey, PrivateName> const&
// 4. If F has an [[InitialName]] internal slot, then
if (is<NativeFunction>(this)) {
// a. Set F.[[InitialName]] to name.
- // TODO: Remove FunctionObject::name(), implement NativeFunction::initial_name(), and then do this.
+ static_cast<NativeFunction&>(*this).set_initial_name({}, name);
}
// 5. If prefix is present, then
@@ -72,7 +72,7 @@ void FunctionObject::set_function_name(Variant<PropertyKey, PrivateName> const&
// b. If F has an [[InitialName]] internal slot, then
if (is<NativeFunction>(this)) {
// i. Optionally, set F.[[InitialName]] to name.
- // TODO: See above.
+ static_cast<NativeFunction&>(*this).set_initial_name({}, name);
}
}
diff --git a/Userland/Libraries/LibJS/Runtime/FunctionPrototype.cpp b/Userland/Libraries/LibJS/Runtime/FunctionPrototype.cpp
index 3796bd8fa3..12f7047186 100644
--- a/Userland/Libraries/LibJS/Runtime/FunctionPrototype.cpp
+++ b/Userland/Libraries/LibJS/Runtime/FunctionPrototype.cpp
@@ -155,8 +155,12 @@ JS_DEFINE_NATIVE_FUNCTION(FunctionPrototype::to_string)
}
// 3. If func is a built-in function object, return an implementation-defined String source code representation of func. The representation must have the syntax of a NativeFunction. Additionally, if func has an [[InitialName]] internal slot and func.[[InitialName]] is a String, the portion of the returned String that would be matched by NativeFunctionAccessor[opt] PropertyName must be the value of func.[[InitialName]].
- if (is<NativeFunction>(function))
- return js_string(vm, String::formatted("function {}() {{ [native code] }}", static_cast<NativeFunction&>(function).name()));
+ if (is<NativeFunction>(function)) {
+ // NOTE: once we remove name(), the fallback here can simply be an empty string.
+ auto const& native_function = static_cast<NativeFunction&>(function);
+ auto const name = native_function.initial_name().value_or(native_function.name());
+ return js_string(vm, String::formatted("function {}() {{ [native code] }}", name));
+ }
// 4. If Type(func) is Object and IsCallable(func) is true, return an implementation-defined String source code representation of func. The representation must have the syntax of a NativeFunction.
// NOTE: ProxyObject, BoundFunction, WrappedFunction
diff --git a/Userland/Libraries/LibJS/Runtime/NativeFunction.h b/Userland/Libraries/LibJS/Runtime/NativeFunction.h
index e1e4b09509..a13a471cf2 100644
--- a/Userland/Libraries/LibJS/Runtime/NativeFunction.h
+++ b/Userland/Libraries/LibJS/Runtime/NativeFunction.h
@@ -1,12 +1,15 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
+ * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
+#include <AK/Badge.h>
#include <AK/Function.h>
+#include <AK/Optional.h>
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/FunctionObject.h>
@@ -35,6 +38,9 @@ public:
virtual bool has_constructor() const override { return false; }
virtual Realm* realm() const override { return m_realm; }
+ Optional<FlyString> const& initial_name() const { return m_initial_name; }
+ void set_initial_name(Badge<FunctionObject>, FlyString initial_name) { m_initial_name = move(initial_name); }
+
protected:
NativeFunction(FlyString name, Object& prototype);
explicit NativeFunction(Object& prototype);
@@ -43,6 +49,7 @@ private:
virtual bool is_native_function() const final { return true; }
FlyString m_name;
+ Optional<FlyString> m_initial_name; // [[InitialName]]
Function<ThrowCompletionOr<Value>(VM&, GlobalObject&)> m_native_function;
Realm* m_realm { nullptr };
};
diff --git a/Userland/Libraries/LibJS/Tests/builtins/Function/Function.prototype.toString.js b/Userland/Libraries/LibJS/Tests/builtins/Function/Function.prototype.toString.js
index fabeecd5ab..d4238d3e89 100644
--- a/Userland/Libraries/LibJS/Tests/builtins/Function/Function.prototype.toString.js
+++ b/Userland/Libraries/LibJS/Tests/builtins/Function/Function.prototype.toString.js
@@ -127,6 +127,9 @@ describe("correct behavior", () => {
// Built-in functions
expect(console.debug.toString()).toBe("function debug() { [native code] }");
expect(Function.toString()).toBe("function Function() { [native code] }");
+ expect(
+ Object.getOwnPropertyDescriptor(Temporal.TimeZone.prototype, "id").get.toString()
+ ).toBe("function get id() { [native code] }");
const values = [
// Callable Proxy