2020-04-19 15:03:02 -05:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Jack Karamanian <karamanian.jack@gmail.com>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-04-19 15:03:02 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <LibJS/Runtime/BoundFunction.h>
|
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2021-09-25 00:38:23 +02:00
|
|
|
BoundFunction::BoundFunction(GlobalObject& global_object, FunctionObject& bound_target_function, Value bound_this, Vector<Value> bound_arguments, i32 length, Object* constructor_prototype)
|
|
|
|
: FunctionObject(*global_object.function_prototype())
|
2021-09-25 00:16:39 +02:00
|
|
|
, m_bound_target_function(&bound_target_function)
|
2021-09-25 00:38:23 +02:00
|
|
|
, m_bound_this(bound_this)
|
|
|
|
, m_bound_arguments(move(bound_arguments))
|
2020-04-19 15:03:02 -05:00
|
|
|
, m_constructor_prototype(constructor_prototype)
|
2021-09-25 00:16:39 +02:00
|
|
|
, m_name(String::formatted("bound {}", bound_target_function.name()))
|
2020-06-20 17:11:11 +02:00
|
|
|
, m_length(length)
|
2020-04-19 15:03:02 -05:00
|
|
|
{
|
2020-06-20 17:11:11 +02:00
|
|
|
}
|
|
|
|
|
2020-07-22 17:50:18 +02:00
|
|
|
void BoundFunction::initialize(GlobalObject& global_object)
|
2020-06-20 17:11:11 +02:00
|
|
|
{
|
2020-10-13 23:49:19 +02:00
|
|
|
auto& vm = this->vm();
|
2021-06-27 21:48:34 +02:00
|
|
|
Base::initialize(global_object);
|
2021-07-06 02:15:08 +03:00
|
|
|
define_direct_property(vm.names.length, Value(m_length), Attribute::Configurable);
|
2020-04-19 15:03:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
BoundFunction::~BoundFunction()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-09-27 17:24:14 +02:00
|
|
|
Value BoundFunction::call()
|
2020-04-19 15:03:02 -05:00
|
|
|
{
|
2021-09-25 00:16:39 +02:00
|
|
|
return m_bound_target_function->call();
|
2020-04-19 15:03:02 -05:00
|
|
|
}
|
|
|
|
|
2021-06-27 21:48:34 +02:00
|
|
|
Value BoundFunction::construct(FunctionObject& new_target)
|
2020-04-19 15:03:02 -05:00
|
|
|
{
|
2021-09-28 23:54:42 +01:00
|
|
|
if (auto this_value = vm().this_value(global_object()); m_constructor_prototype && this_value.is_object())
|
|
|
|
TRY_OR_DISCARD(this_value.as_object().internal_set_prototype_of(m_constructor_prototype));
|
2021-09-25 00:16:39 +02:00
|
|
|
return m_bound_target_function->construct(new_target);
|
2020-04-19 15:03:02 -05:00
|
|
|
}
|
|
|
|
|
2021-09-22 12:44:56 +02:00
|
|
|
FunctionEnvironment* BoundFunction::new_function_environment(Object* new_target)
|
2020-04-19 15:03:02 -05:00
|
|
|
{
|
2021-09-22 12:44:56 +02:00
|
|
|
return m_bound_target_function->new_function_environment(new_target);
|
2020-04-19 15:03:02 -05:00
|
|
|
}
|
|
|
|
|
2020-11-28 14:33:36 +01:00
|
|
|
void BoundFunction::visit_edges(Visitor& visitor)
|
2020-04-19 15:03:02 -05:00
|
|
|
{
|
2021-06-27 21:48:34 +02:00
|
|
|
Base::visit_edges(visitor);
|
2021-09-25 00:38:23 +02:00
|
|
|
|
2021-09-25 00:16:39 +02:00
|
|
|
visitor.visit(m_bound_target_function);
|
2021-09-25 00:38:23 +02:00
|
|
|
visitor.visit(m_bound_this);
|
|
|
|
for (auto argument : m_bound_arguments)
|
|
|
|
visitor.visit(argument);
|
|
|
|
|
2020-04-19 15:03:02 -05:00
|
|
|
visitor.visit(m_constructor_prototype);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|