forked from Samsung/escargot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoundFunctionObject.cpp
More file actions
93 lines (82 loc) · 4.13 KB
/
BoundFunctionObject.cpp
File metadata and controls
93 lines (82 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
* 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 F’s [[BoundTargetFunction]] internal slot.
// Let boundArgs be the value of F’s [[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