mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 00:07:34 +00:00
LibJS: Add [[InitialName]] and use it in Function.prototype.toString()
This commit is contained in:
parent
0be67ef12c
commit
e657e88ed6
4 changed files with 18 additions and 4 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 };
|
||||
};
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue