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 {
|
|
|
|
|
2020-06-20 17:11:11 +02:00
|
|
|
BoundFunction::BoundFunction(GlobalObject& global_object, Function& target_function, Value bound_this, Vector<Value> arguments, i32 length, Object* constructor_prototype)
|
2021-06-19 23:21:08 +01:00
|
|
|
: Function::Function(bound_this, move(arguments), *global_object.function_prototype())
|
2020-04-19 15:03:02 -05:00
|
|
|
, m_target_function(&target_function)
|
|
|
|
, m_constructor_prototype(constructor_prototype)
|
2020-10-04 15:18:52 +01:00
|
|
|
, m_name(String::formatted("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();
|
2020-07-22 17:50:18 +02:00
|
|
|
Function::initialize(global_object);
|
2020-10-13 23:49:19 +02:00
|
|
|
define_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
|
|
|
{
|
2020-09-27 17:24:14 +02:00
|
|
|
return m_target_function->call();
|
2020-04-19 15:03:02 -05:00
|
|
|
}
|
|
|
|
|
2020-09-27 18:45:21 +02:00
|
|
|
Value BoundFunction::construct(Function& new_target)
|
2020-04-19 15:03:02 -05:00
|
|
|
{
|
2020-09-27 18:36:49 +02:00
|
|
|
if (auto this_value = vm().this_value(global_object()); m_constructor_prototype && this_value.is_object()) {
|
2020-04-19 15:03:02 -05:00
|
|
|
this_value.as_object().set_prototype(m_constructor_prototype);
|
2020-09-27 18:45:21 +02:00
|
|
|
if (vm().exception())
|
2020-06-07 10:53:14 -07:00
|
|
|
return {};
|
2020-04-19 15:03:02 -05:00
|
|
|
}
|
2020-09-27 18:45:21 +02:00
|
|
|
return m_target_function->construct(new_target);
|
2020-04-19 15:03:02 -05:00
|
|
|
}
|
|
|
|
|
2021-06-22 13:30:48 +02:00
|
|
|
FunctionEnvironmentRecord* BoundFunction::create_environment_record()
|
2020-04-19 15:03:02 -05:00
|
|
|
{
|
2021-06-21 23:17:24 +02:00
|
|
|
return m_target_function->create_environment_record();
|
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
|
|
|
{
|
2020-11-28 14:33:36 +01:00
|
|
|
Function::visit_edges(visitor);
|
2020-04-19 15:03:02 -05:00
|
|
|
visitor.visit(m_target_function);
|
|
|
|
visitor.visit(m_constructor_prototype);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|