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 const& // 4. If F has an [[InitialName]] internal slot, then if (is(this)) { // a. Set F.[[InitialName]] to name. - // TODO: Remove FunctionObject::name(), implement NativeFunction::initial_name(), and then do this. + static_cast(*this).set_initial_name({}, name); } // 5. If prefix is present, then @@ -72,7 +72,7 @@ void FunctionObject::set_function_name(Variant const& // b. If F has an [[InitialName]] internal slot, then if (is(this)) { // i. Optionally, set F.[[InitialName]] to name. - // TODO: See above. + static_cast(*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(function)) - return js_string(vm, String::formatted("function {}() {{ [native code] }}", static_cast(function).name())); + if (is(function)) { + // NOTE: once we remove name(), the fallback here can simply be an empty string. + auto const& native_function = static_cast(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 + * Copyright (c) 2021-2022, Linus Groh * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once +#include #include +#include #include #include @@ -35,6 +38,9 @@ public: virtual bool has_constructor() const override { return false; } virtual Realm* realm() const override { return m_realm; } + Optional const& initial_name() const { return m_initial_name; } + void set_initial_name(Badge, 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 m_initial_name; // [[InitialName]] Function(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