forked from Samsung/escargot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctionTemplate.cpp
More file actions
237 lines (210 loc) · 10.6 KB
/
FunctionTemplate.cpp
File metadata and controls
237 lines (210 loc) · 10.6 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
* Copyright (c) 2020-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 "FunctionTemplate.h"
#include "runtime/ObjectTemplate.h"
#include "runtime/Context.h"
#include "runtime/ExtendedNativeFunctionObject.h"
#include "runtime/SandBox.h"
#include "api/internal/ValueAdapter.h"
#include "runtime/FunctionObjectInlines.h"
namespace Escargot {
void* FunctionTemplate::operator new(size_t size)
{
static MAY_THREAD_LOCAL bool typeInited = false;
static MAY_THREAD_LOCAL GC_descr descr;
if (!typeInited) {
GC_word objBitmap[GC_BITMAP_SIZE(FunctionTemplate)] = { 0 };
Template::fillGCDescriptor(objBitmap);
GC_set_bit(objBitmap, GC_WORD_OFFSET(FunctionTemplate, m_nativeFunctionData));
GC_set_bit(objBitmap, GC_WORD_OFFSET(FunctionTemplate, m_prototypeTemplate));
GC_set_bit(objBitmap, GC_WORD_OFFSET(FunctionTemplate, m_instanceTemplate));
GC_set_bit(objBitmap, GC_WORD_OFFSET(FunctionTemplate, m_parent));
descr = GC_make_descriptor(objBitmap, GC_WORD_LEN(FunctionTemplate));
typeInited = true;
}
return GC_MALLOC_EXPLICITLY_TYPED(size, descr);
}
class FunctionTemplateNativeFunctionObject : public ExtendedNativeFunctionObjectImpl<1> {
public:
FunctionTemplateNativeFunctionObject(Context* context, ObjectStructure* structure, ObjectPropertyValueVector&& values, const NativeFunctionInfo& info)
: ExtendedNativeFunctionObjectImpl<1>(context, structure, std::move(values), info)
{
}
virtual Value construct(ExecutionState& state, const size_t argc, Value* argv, Object* newTarget) override
{
return processNativeFunctionCall<true, false>(state, Value(), argc, argv, newTarget);
}
};
class CallTemplateFunctionData : public gc {
public:
CallTemplateFunctionData(FunctionTemplate* functionTemplate, NativeFunctionPointer fn, FunctionTemplateRef::NativeFunctionPointer callback)
: m_functionTemplate(functionTemplate)
, m_nativeFunction(fn)
, m_publicCallback(callback)
{
}
void* operator new(size_t size)
{
static MAY_THREAD_LOCAL bool typeInited = false;
static MAY_THREAD_LOCAL GC_descr descr;
if (!typeInited) {
GC_word obj_bitmap[GC_BITMAP_SIZE(CallTemplateFunctionData)] = { 0 };
GC_set_bit(obj_bitmap, GC_WORD_OFFSET(CallTemplateFunctionData, m_functionTemplate));
descr = GC_make_descriptor(obj_bitmap, GC_WORD_LEN(CallTemplateFunctionData));
typeInited = true;
}
return GC_MALLOC_EXPLICITLY_TYPED(size, descr);
}
void* operator new[](size_t size) = delete;
FunctionTemplate* m_functionTemplate;
NativeFunctionPointer m_nativeFunction;
FunctionTemplateRef::NativeFunctionPointer m_publicCallback;
};
FunctionTemplate::FunctionTemplate(AtomicString name, size_t argumentCount, bool isStrict, bool isConstructor,
FunctionTemplateRef::NativeFunctionPointer fn)
: m_name(name)
, m_argumentCount(argumentCount)
, m_isStrict(isStrict)
, m_isConstructor(isConstructor)
, m_prototypeTemplate(new ObjectTemplate())
, m_instanceTemplate(new ObjectTemplate(this))
{
auto fnData = new CallTemplateFunctionData(this, [](ExecutionState& state, Value thisValue, size_t argc, Value* argv, Optional<Object*> newTarget) -> Value {
ExtendedNativeFunctionObject* activeFunction = state.resolveCallee()->asExtendedNativeFunctionObject();
CallTemplateFunctionData* data = activeFunction->internalSlotAsPointer<CallTemplateFunctionData>(FunctionTemplate::BuiltinFunctionSlot::CallTemplateFunctionDataIndex);
ValueRef** newArgv = ALLOCA(sizeof(ValueRef*) * argc, ValueRef*, state);
for (size_t i = 0; i < argc; i++) {
newArgv[i] = toRef(argv[i]);
}
if (newTarget) {
Value proto = newTarget.value()->get(state, ObjectPropertyName(state.context()->staticStrings().prototype)).value(state, newTarget.value());
if (!proto.isObject()) {
proto = activeFunction->getFunctionPrototype(state);
}
Object* newThisValue = data->m_functionTemplate->instanceTemplate()->instantiate(state.context());
newThisValue->setPrototype(state, proto);
return toImpl(data->m_publicCallback((ExecutionStateRef*)(&state), toRef(newThisValue), argc, newArgv, OptionalRef<ObjectRef>(toRef(newTarget.value()))));
} else {
return toImpl(data->m_publicCallback((ExecutionStateRef*)(&state), toRef(thisValue), argc, newArgv, nullptr));
}
},
fn);
m_nativeFunctionData = fnData;
}
void FunctionTemplate::updateCallbackFunction(FunctionTemplateRef::NativeFunctionPointer fn)
{
m_nativeFunctionData->m_publicCallback = fn;
}
void FunctionTemplate::setName(AtomicString name)
{
ASSERT(m_cachedObjectStructure.m_objectStructure == nullptr);
m_name = name;
}
void FunctionTemplate::setLength(size_t length)
{
ASSERT(m_cachedObjectStructure.m_objectStructure == nullptr);
m_argumentCount = length;
}
Object* FunctionTemplate::instantiate(Context* ctx)
{
auto& instantiatedFunctionObjects = ctx->instantiatedFunctionObjects();
for (size_t i = 0; i < instantiatedFunctionObjects.size(); i++) {
if (instantiatedFunctionObjects[i].first == this) {
return instantiatedFunctionObjects[i].second;
}
}
const size_t functionDefaultPropertyCount = m_isConstructor ? ctx->defaultStructureForFunctionObject()->propertyCount() : ctx->defaultStructureForNotConstructorFunctionObject()->propertyCount();
if (!m_cachedObjectStructure.m_objectStructure) {
if (m_isConstructor) {
// [prototype, name, length]
ASSERT(functionDefaultPropertyCount == 3);
ObjectStructureItem structureItemVector[3] = {
ObjectStructureItem(ctx->staticStrings().prototype,
ObjectStructurePropertyDescriptor::createDataDescriptor(ObjectStructurePropertyDescriptor::WritablePresent)),
ObjectStructureItem(ctx->staticStrings().name,
ObjectStructurePropertyDescriptor::createDataDescriptor(ObjectStructurePropertyDescriptor::ConfigurablePresent)),
ObjectStructureItem(ctx->staticStrings().length,
ObjectStructurePropertyDescriptor::createDataDescriptor(ObjectStructurePropertyDescriptor::ConfigurablePresent))
};
m_cachedObjectStructure = constructObjectStructure(ctx, structureItemVector, 3);
} else {
// [name, length]
ASSERT(functionDefaultPropertyCount == 2);
ObjectStructureItem structureItemVector[2] = {
ObjectStructureItem(ctx->staticStrings().name,
ObjectStructurePropertyDescriptor::createDataDescriptor(ObjectStructurePropertyDescriptor::ConfigurablePresent)),
ObjectStructureItem(ctx->staticStrings().length,
ObjectStructurePropertyDescriptor::createDataDescriptor(ObjectStructurePropertyDescriptor::ConfigurablePresent))
};
m_cachedObjectStructure = constructObjectStructure(ctx, structureItemVector, 2);
}
}
ObjectPropertyValueVector objectPropertyValues;
Object* functionPrototype = nullptr;
if (m_isConstructor) {
// [prototype, name, length]
if (!m_prototypeTemplate->has(ctx->staticStrings().constructor.string())) {
m_prototypeTemplate->set(ctx->staticStrings().constructor.string(), Value(), true, false, true);
}
ObjectPropertyValue baseValues[3];
baseValues[0] = functionPrototype = m_prototypeTemplate->instantiate(ctx);
baseValues[1] = m_name.string();
baseValues[2] = Value(m_argumentCount);
constructObjectPropertyValues(ctx, baseValues, 3, objectPropertyValues);
} else {
// [name, length]
ObjectPropertyValue baseValues[2];
baseValues[0] = m_name.string();
baseValues[1] = Value(m_argumentCount);
constructObjectPropertyValues(ctx, baseValues, 2, objectPropertyValues);
}
int flags = 0;
flags |= m_isStrict ? NativeFunctionInfo::Strict : 0;
flags |= m_isConstructor ? NativeFunctionInfo::Constructor : 0;
FunctionTemplateNativeFunctionObject* result = new FunctionTemplateNativeFunctionObject(ctx,
m_cachedObjectStructure.m_objectStructure, std::move(objectPropertyValues), NativeFunctionInfo(m_name, m_nativeFunctionData->m_nativeFunction, m_argumentCount, flags));
result->setInternalSlotAsPointer(FunctionTemplate::BuiltinFunctionSlot::CallTemplateFunctionDataIndex, m_nativeFunctionData);
if (m_isConstructor) {
// set `constructor` property in prototype object
auto idx = m_prototypeTemplate->cachedObjectStructure()->findProperty(ctx->staticStrings().constructor).first;
functionPrototype->uncheckedSetOwnDataProperty(idx, result);
struct Sender {
FunctionTemplate* functionTemplate;
Object* prototype;
};
Sender d = { this, functionPrototype };
SandBox sb(ctx);
sb.run([](ExecutionState& state, void* data) -> Value {
Sender* s = (Sender*)data;
s->prototype->markAsPrototypeObject(state);
if (s->functionTemplate->parent()) {
// for FunctionTemplate::inherit
// set parent's prototype on prototype.__proto__
s->prototype->setPrototype(state, s->functionTemplate->parent()->instantiate(state.context())->asFunctionObject()->getFunctionPrototype(state));
}
return Value();
},
&d);
}
instantiatedFunctionObjects.pushBack(std::make_pair(this, result));
postProcessing(result);
return result;
}
} // namespace Escargot