forked from Samsung/escargot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctionObject.cpp
More file actions
249 lines (218 loc) · 10.9 KB
/
FunctionObject.cpp
File metadata and controls
249 lines (218 loc) · 10.9 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
238
239
240
241
242
243
244
245
246
247
248
249
/*
* Copyright (c) 2016-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 "FunctionObject.h"
#include "runtime/VMInstance.h"
#include "runtime/Context.h"
#include "runtime/ReloadableString.h"
#include "interpreter/ByteCode.h"
#include "parser/ast/ProgramNode.h"
#include "parser/ScriptParser.h"
#include "parser/esprima_cpp/esprima.h"
#include "runtime/Environment.h"
#include "runtime/EnvironmentRecord.h"
namespace Escargot {
void FunctionObject::initStructureAndValues(ExecutionState& state, bool isConstructor, bool isGenerator, bool isAsync)
{
if (isGenerator) {
// Unlike function instances, the object that is the value of the GeneratorFunction’s of AsyncGeneratorFunction prototype property
// does not have a constructor property whose value is the GeneratorFunction or the AsyncGeneratorFunction instance.
m_structure = state.context()->defaultStructureForFunctionObject();
ASSERT(ESCARGOT_OBJECT_BUILTIN_PROPERTY_NUMBER + 0 < m_structure->propertyCount());
ASSERT(ESCARGOT_OBJECT_BUILTIN_PROPERTY_NUMBER + 1 < m_structure->propertyCount());
ASSERT(ESCARGOT_OBJECT_BUILTIN_PROPERTY_NUMBER + 2 < m_structure->propertyCount());
m_values[ESCARGOT_OBJECT_BUILTIN_PROPERTY_NUMBER + 0] = ObjectPropertyValue::EmptyValue; // lazy init on VMInstance::functionPrototypeNativeGetter
m_values[ESCARGOT_OBJECT_BUILTIN_PROPERTY_NUMBER + 1] = (Value(m_codeBlock->functionLength()));
m_values[ESCARGOT_OBJECT_BUILTIN_PROPERTY_NUMBER + 2] = (Value(m_codeBlock->functionName().string()));
} else {
if (isConstructor) {
m_structure = state.context()->defaultStructureForFunctionObject();
ASSERT(ESCARGOT_OBJECT_BUILTIN_PROPERTY_NUMBER + 0 < m_structure->propertyCount());
ASSERT(ESCARGOT_OBJECT_BUILTIN_PROPERTY_NUMBER + 1 < m_structure->propertyCount());
ASSERT(ESCARGOT_OBJECT_BUILTIN_PROPERTY_NUMBER + 2 < m_structure->propertyCount());
m_values[ESCARGOT_OBJECT_BUILTIN_PROPERTY_NUMBER + 0] = ObjectPropertyValue::EmptyValue; // lazy init on VMInstance::functionPrototypeNativeGetter
m_values[ESCARGOT_OBJECT_BUILTIN_PROPERTY_NUMBER + 1] = (Value(m_codeBlock->functionLength()));
m_values[ESCARGOT_OBJECT_BUILTIN_PROPERTY_NUMBER + 2] = (Value(m_codeBlock->functionName().string()));
} else {
m_structure = state.context()->defaultStructureForNotConstructorFunctionObject();
ASSERT(ESCARGOT_OBJECT_BUILTIN_PROPERTY_NUMBER + 0 < m_structure->propertyCount());
ASSERT(ESCARGOT_OBJECT_BUILTIN_PROPERTY_NUMBER + 1 < m_structure->propertyCount());
m_values[ESCARGOT_OBJECT_BUILTIN_PROPERTY_NUMBER + 0] = (Value(m_codeBlock->functionLength()));
m_values[ESCARGOT_OBJECT_BUILTIN_PROPERTY_NUMBER + 1] = (Value(m_codeBlock->functionName().string()));
}
}
}
// function for derived classes. derived class MUST initlize member variable of FunctionObject.
FunctionObject::FunctionObject(ExecutionState& state, Object* proto, size_t defaultSpace)
: Object(state, proto, defaultSpace)
, m_codeBlock(nullptr)
{
}
FunctionObject::FunctionObject(ObjectStructure* structure, ObjectPropertyValueVector&& values, Object* proto)
: Object(structure, std::move(values), proto)
, m_codeBlock(nullptr)
{
}
FunctionObject::FunctionSource FunctionObject::createFunctionSourceFromScriptSource(ExecutionState& state, AtomicString functionName, size_t argCount, Value* argArray, Value bodyValue, bool useStrict, bool isGenerator, bool isAsync, bool allowSuperCall, bool isInternalSource)
{
StringBuilder src, parameters;
if (useStrict) {
src.appendString("'use strict'; ");
}
if (isGenerator && isAsync) {
src.appendString("async function* ");
} else if (isGenerator) {
src.appendString("function* ");
} else if (isAsync) {
src.appendString("async function ");
} else {
src.appendString("function ");
}
// function name
src.appendString(functionName.string());
{
// function parameters
parameters.appendString("(");
for (size_t i = 0; i < argCount; i++) {
String* p = argArray[i].toString(state);
parameters.appendString(p);
if (i != argCount - 1) {
parameters.appendString(",");
}
}
parameters.appendString("\n)");
}
String* parameterStr = parameters.finalize(&state);
String* originBodyStr = bodyValue.toString(state);
String* scriptSource = nullptr;
#if defined(ENABLE_RELOADABLE_STRING)
if (UNLIKELY(originBodyStr->isReloadableString())) {
src.appendString(parameterStr);
src.appendString(" {\n");
String* headStr = src.finalize(&state);
bool is8Bit = headStr->has8BitContent() && originBodyStr->has8BitContent();
const char tail[] = "\n}";
class ReloadableStringData : public gc {
public:
ReloadableStringData(String* head, ReloadableString* body)
: m_head(head)
, m_body(body)
, m_dest(nullptr)
{
}
String* m_head;
ReloadableString* m_body;
ReloadableString* m_dest;
};
ReloadableStringData* data = new ReloadableStringData(headStr, originBodyStr->asReloadableString());
scriptSource = new ReloadableString(state.context()->vmInstance(), is8Bit, headStr->length() + originBodyStr->length() + sizeof(tail) - 1,
data, [](void* callbackData) -> void* {
ReloadableStringData* data = reinterpret_cast<ReloadableStringData*>(callbackData);
bool is8Bit = data->m_dest->has8BitContent();
char* dest = reinterpret_cast<char*>(malloc((data->m_dest->length()) * (is8Bit ? 1 : 2)));
auto headAccessData = data->m_head->bufferAccessData();
auto bodyAccessData = data->m_body->bufferAccessData();
if (is8Bit) {
ASSERT(headAccessData.has8BitContent && bodyAccessData.has8BitContent);
char* ptr = dest;
memcpy(ptr, headAccessData.bufferAs8Bit, headAccessData.length);
ptr += headAccessData.length;
memcpy(ptr, bodyAccessData.bufferAs8Bit, bodyAccessData.length);
ptr += bodyAccessData.length;
ptr[0] = '\n';
ptr[1] = '}';
} else {
char16_t* ptr = reinterpret_cast<char16_t*>(dest);
if (headAccessData.has8BitContent) {
for (size_t i = 0; i < headAccessData.length; i++) {
ptr[i] = headAccessData.charAt(i);
}
} else {
memcpy(ptr, headAccessData.bufferAs16Bit, headAccessData.length * 2);
}
ptr += headAccessData.length;
if (bodyAccessData.has8BitContent) {
for (size_t i = 0; i < bodyAccessData.length; i++) {
ptr[i] = bodyAccessData.charAt(i);
}
} else {
memcpy(ptr, bodyAccessData.bufferAs16Bit, bodyAccessData.length * 2);
}
ptr += bodyAccessData.length;
ptr[0] = '\n';
ptr[1] = '}';
}
// unload original body source immediately
// set nullptr to unload body string
bodyAccessData.buffer = nullptr;
data->m_body->unload();
return dest; }, [](void* memoryPtr, void* callbackData) { free(memoryPtr); });
data->m_dest = scriptSource->asReloadableString();
} else
#endif
{
StringBuilder body;
body.appendString(" {\n");
body.appendString(originBodyStr);
body.appendString("\n}");
String* bodyStr = body.finalize(&state);
#if defined(ESCARGOT_ENABLE_TEST)
// simple syntax check for dynamic generated function except internal source
// to check rare erratic formats e.g. Function('){ function foo(', '}')
// this check is enabled only for test mode because it requires double-parsing which may affect the overall performance
if (!isInternalSource) {
GC_disable();
try {
esprima::simpleSyntaxCheckFunctionElements(state.context(), parameterStr, bodyStr, useStrict, isGenerator, isAsync);
// reset ASTAllocator
state.context()->astAllocator().reset();
GC_enable();
} catch (esprima::Error* syntaxError) {
String* errorMessage = syntaxError->message;
// reset ASTAllocator
state.context()->astAllocator().reset();
GC_enable();
delete syntaxError;
ErrorObject::throwBuiltinError(state, ErrorObject::SyntaxError, errorMessage);
}
}
#endif
src.appendString(parameterStr);
src.appendString(bodyStr);
scriptSource = src.finalize(&state);
}
ScriptParser parser(state.context());
String* srcName = String::emptyString;
// find srcName through outer script except internal source
if (!isInternalSource) {
auto script = state.resolveOuterScript();
if (script) {
srcName = script->srcName();
}
}
Script* script = parser.initializeScript(scriptSource, srcName, nullptr, false, false, false, false, false, allowSuperCall, false, true, false).scriptThrowsExceptionIfParseError(state);
InterpretedCodeBlock* cb = script->topCodeBlock()->childBlockAt(0);
LexicalEnvironment* globalEnvironment = new LexicalEnvironment(new GlobalEnvironmentRecord(state, script->topCodeBlock(), state.context()->globalObject(), state.context()->globalDeclarativeRecord(), state.context()->globalDeclarativeStorage()), nullptr);
FunctionObject::FunctionSource fs;
fs.script = script;
fs.codeBlock = cb;
fs.outerEnvironment = globalEnvironment;
return fs;
}
} // namespace Escargot