escargot/src/runtime/BoundFunctionObject.cpp
HyukWoo Park 8fa6031fec Add DerivedObject for sub classes of Object
* check index property set in DerivedObject::defineOwnProperty method only
* index property check is removed for normal Object because PrototypeObject would check it instead

Signed-off-by: HyukWoo Park <hyukwoo.park@samsung.com>
2022-05-11 15:59:43 +09:00

93 lines
4.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Copyright (c) 2019-present Samsung Electronics Co., Ltd
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*/
#include "Escargot.h"
#include "Object.h"
#include "runtime/Context.h"
#include "runtime/FunctionObject.h"
#include "BoundFunctionObject.h"
#include "EncodedValue.h"
namespace Escargot {
BoundFunctionObject::BoundFunctionObject(ExecutionState& state, Object* targetFunction, Value& boundThis, size_t boundArgc, Value* boundArgv, const Value& length, const Value& name)
: DerivedObject(state, state.context()->globalObject()->objectPrototype(), ESCARGOT_OBJECT_BUILTIN_PROPERTY_NUMBER + 2)
, m_boundTargetFunction(targetFunction)
, m_boundThis(boundThis)
{
m_structure = state.context()->defaultStructureForBoundFunctionObject();
m_values[ESCARGOT_OBJECT_BUILTIN_PROPERTY_NUMBER + 0] = length;
m_values[ESCARGOT_OBJECT_BUILTIN_PROPERTY_NUMBER + 1] = name;
Value proto = m_boundTargetFunction->getPrototype(state);
Object::setPrototype(state, proto);
if (boundArgc > 0) {
m_boundArguments.resizeWithUninitializedValues(boundArgc);
for (size_t i = 0; i < boundArgc; i++) {
m_boundArguments[i] = boundArgv[i];
}
}
}
// https://www.ecma-international.org/ecma-262/6.0/#sec-bound-function-exotic-objects-call-thisargument-argumentslist
Value BoundFunctionObject::call(ExecutionState& state, const Value& thisValue, const size_t calledArgc, Value* calledArgv)
{
// Let args be a new list containing the same values as the list boundArgs in the same order followed by the same values as the list argumentsList in the same order.
size_t boundArgc = m_boundArguments.size();
size_t mergedArgc = boundArgc + calledArgc;
Value* mergedArgv = ALLOCA(mergedArgc * sizeof(Value), Value, state);
for (size_t i = 0; i < boundArgc; i++) {
mergedArgv[i] = m_boundArguments[i];
}
if (calledArgc > 0) {
memcpy(mergedArgv + boundArgc, calledArgv, sizeof(Value) * calledArgc);
}
// Return Call(target, boundThis, args).
return Object::call(state, m_boundTargetFunction, m_boundThis, mergedArgc, mergedArgv);
}
// https://www.ecma-international.org/ecma-262/6.0/#sec-bound-function-exotic-objects-construct-argumentslist-newtarget
Value BoundFunctionObject::construct(ExecutionState& state, const size_t calledArgc, Value* calledArgv, Object* newTarget)
{
ASSERT(m_boundTargetFunction && m_boundTargetFunction->isConstructor());
// Let target be the value of Fs [[BoundTargetFunction]] internal slot.
// Let boundArgs be the value of Fs [[BoundArguments]] internal slot.
// Let args be a new list containing the same values as the list boundArgs in the same order followed by the same values as the list argumentsList in the same order.
size_t boundArgc = m_boundArguments.size();
size_t mergedArgc = boundArgc + calledArgc;
Value* mergedArgv = ALLOCA(mergedArgc * sizeof(Value), Value, state);
for (size_t i = 0; i < boundArgc; i++) {
mergedArgv[i] = m_boundArguments[i];
}
if (calledArgc > 0) {
memcpy(mergedArgv + boundArgc, calledArgv, sizeof(Value) * calledArgc);
}
// If SameValue(F, newTarget) is true, let newTarget be target.
if (this == newTarget) {
newTarget = Value(m_boundTargetFunction).asObject();
}
// Return Construct(target, args, newTarget).
return Object::construct(state, m_boundTargetFunction, mergedArgc, mergedArgv, newTarget).toObject(state);
}
} // namespace Escargot