mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 23:50:19 +00:00
LibJS: Move has_constructor() from NativeFunction to FunctionObject
At a later point this will indicate whether some FunctionObject "has a [[Construct]] internal method" (separate from the current FunctionObject call() / construct()), to help with a more spec-compliant implementation of [[Call]] and [[Construct]]. This means that it is no longer relevant to just NativeFunction.
This commit is contained in:
parent
e14f420a44
commit
38157a6093
Notes:
sideshowbarker
2024-07-18 03:27:42 +09:00
Author: https://github.com/linusg Commit: https://github.com/SerenityOS/serenity/commit/38157a60936 Pull-request: https://github.com/SerenityOS/serenity/pull/10203 Reviewed-by: https://github.com/IdanHo ✅ Reviewed-by: https://github.com/davidot
7 changed files with 16 additions and 11 deletions
|
@ -181,7 +181,7 @@ Value NewExpression::execute(Interpreter& interpreter, GlobalObject& global_obje
|
|||
if (vm.exception())
|
||||
return {};
|
||||
|
||||
if (!callee_value.is_function() || (is<NativeFunction>(callee_value.as_object()) && !static_cast<NativeFunction&>(callee_value.as_object()).has_constructor())) {
|
||||
if (!callee_value.is_function() || !callee_value.as_function().has_constructor()) {
|
||||
throw_type_error_for_callee(interpreter, global_object, callee_value, "constructor"sv);
|
||||
return {};
|
||||
}
|
||||
|
|
|
@ -23,6 +23,7 @@ public:
|
|||
virtual FunctionEnvironment* create_environment(FunctionObject&) override;
|
||||
virtual const FlyString& name() const override { return m_name; }
|
||||
virtual bool is_strict_mode() const override { return m_bound_target_function->is_strict_mode(); }
|
||||
virtual bool has_constructor() const override { return true; }
|
||||
|
||||
FunctionObject& bound_target_function() const { return *m_bound_target_function; }
|
||||
Value bound_this() const { return m_bound_this; }
|
||||
|
|
|
@ -71,6 +71,8 @@ public:
|
|||
// This is for IsSimpleParameterList (static semantics)
|
||||
bool has_simple_parameter_list() const { return m_has_simple_parameter_list; }
|
||||
|
||||
virtual bool has_constructor() const override { return true; }
|
||||
|
||||
protected:
|
||||
virtual bool is_strict_mode() const final { return m_strict; }
|
||||
|
||||
|
|
|
@ -27,6 +27,8 @@ public:
|
|||
|
||||
virtual bool is_strict_mode() const { return false; }
|
||||
|
||||
virtual bool has_constructor() const { return false; }
|
||||
|
||||
// [[Realm]]
|
||||
virtual Realm* realm() const { return nullptr; }
|
||||
|
||||
|
|
|
@ -23,12 +23,9 @@ public:
|
|||
|
||||
virtual Value call() override;
|
||||
virtual Value construct(FunctionObject& new_target) override;
|
||||
|
||||
virtual const FlyString& name() const override { return m_name; };
|
||||
virtual bool has_constructor() const { return false; }
|
||||
|
||||
virtual bool is_strict_mode() const override;
|
||||
|
||||
virtual bool has_constructor() const override { return false; }
|
||||
virtual Realm* realm() const override { return m_realm; }
|
||||
|
||||
protected:
|
||||
|
|
|
@ -24,6 +24,7 @@ public:
|
|||
virtual Value construct(FunctionObject& new_target) override;
|
||||
virtual const FlyString& name() const override;
|
||||
virtual FunctionEnvironment* create_environment(FunctionObject&) override;
|
||||
virtual bool has_constructor() const override { return true; }
|
||||
|
||||
const Object& target() const { return m_target; }
|
||||
const Object& handler() const { return m_handler; }
|
||||
|
|
|
@ -237,14 +237,16 @@ FunctionObject const& Value::as_function() const
|
|||
// 7.2.4 IsConstructor ( argument ), https://tc39.es/ecma262/#sec-isconstructor
|
||||
bool Value::is_constructor() const
|
||||
{
|
||||
// 1. If Type(argument) is not Object, return false.
|
||||
if (!is_function())
|
||||
return false;
|
||||
if (is<NativeFunction>(as_object()))
|
||||
return static_cast<const NativeFunction&>(as_object()).has_constructor();
|
||||
if (is<BoundFunction>(as_object()))
|
||||
return Value(&static_cast<const BoundFunction&>(as_object()).bound_target_function()).is_constructor();
|
||||
// ECMAScriptFunctionObject
|
||||
return true;
|
||||
|
||||
// 2. If argument has a [[Construct]] internal method, return true.
|
||||
if (as_function().has_constructor())
|
||||
return true;
|
||||
|
||||
// 3. Return false.
|
||||
return false;
|
||||
}
|
||||
|
||||
// 7.2.8 IsRegExp ( argument ), https://tc39.es/ecma262/#sec-isregexp
|
||||
|
|
Loading…
Reference in a new issue