forked from Samsung/escargot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayExpressionNode.h
More file actions
159 lines (136 loc) · 6.91 KB
/
ArrayExpressionNode.h
File metadata and controls
159 lines (136 loc) · 6.91 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
/*
* 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
*/
#ifndef ArrayExpressionNode_h
#define ArrayExpressionNode_h
#include "ExpressionNode.h"
#include "ArrayPatternNode.h"
#include "runtime/Context.h"
namespace Escargot {
class ArrayExpressionNode : public ExpressionNode {
public:
ArrayExpressionNode(const NodeList& elements, AtomicString additionalPropertyName = AtomicString(), Node* additionalPropertyExpression = nullptr, bool hasSpreadElement = false, bool isTaggedTemplateExpression = false)
: ExpressionNode()
, m_elements(elements)
, m_additionalPropertyName(additionalPropertyName)
, m_additionalPropertyExpression(additionalPropertyExpression)
, m_hasSpreadElement(hasSpreadElement)
, m_isTaggedTemplateExpression(isTaggedTemplateExpression)
{
}
NodeList& elements()
{
return m_elements;
}
virtual ASTNodeType type() override { return ASTNodeType::ArrayExpression; }
virtual void generateExpressionByteCode(ByteCodeBlock* codeBlock, ByteCodeGenerateContext* context, ByteCodeRegisterIndex dstRegister) override
{
size_t arrayIndex = codeBlock->currentCodeSize();
size_t arrLen = 0;
codeBlock->pushCode(CreateArray(ByteCodeLOC(m_loc.index), dstRegister), context, this);
size_t objIndex = dstRegister;
size_t baseIndex = 0;
SentinelNode* element = m_elements.begin();
while (element != m_elements.end()) {
size_t fillCount = 0;
size_t regCount = 0;
size_t regIndex = 0;
ByteCodeRegisterIndex regs[ARRAY_DEFINE_OPERATION_MERGE_COUNT];
while (regIndex < ARRAY_DEFINE_OPERATION_MERGE_COUNT && element != m_elements.end()) {
ByteCodeRegisterIndex valueIndex = REGISTER_LIMIT;
if (element->astNode()) {
valueIndex = element->astNode()->getRegister(codeBlock, context);
element->astNode()->generateExpressionByteCode(codeBlock, context, valueIndex);
regCount++;
}
regs[regIndex] = valueIndex;
fillCount++;
regIndex++;
element = element->next();
}
if (m_hasSpreadElement) {
codeBlock->pushCode(ArrayDefineOwnPropertyBySpreadElementOperation(ByteCodeLOC(m_loc.index), objIndex, fillCount), context, this);
memcpy(codeBlock->peekCode<ArrayDefineOwnPropertyBySpreadElementOperation>(codeBlock->lastCodePosition<ArrayDefineOwnPropertyBySpreadElementOperation>())->m_loadRegisterIndexs,
regs, sizeof(regs));
} else {
codeBlock->pushCode(ArrayDefineOwnPropertyOperation(ByteCodeLOC(m_loc.index), objIndex, baseIndex, fillCount), context, this);
memcpy(codeBlock->peekCode<ArrayDefineOwnPropertyOperation>(codeBlock->lastCodePosition<ArrayDefineOwnPropertyOperation>())->m_loadRegisterIndexs,
regs, sizeof(regs));
}
for (size_t i = 0; i < regCount; i++) {
// drop value register
context->giveUpRegister();
}
baseIndex += fillCount;
arrLen += fillCount;
}
if (!m_hasSpreadElement) {
codeBlock->peekCode<CreateArray>(arrayIndex)->m_length = arrLen;
}
codeBlock->m_shouldClearStack = true;
if (m_additionalPropertyExpression) {
ByteCodeRegisterIndex additionalPropertyExpressionRegsiter = m_additionalPropertyExpression->getRegister(codeBlock, context);
m_additionalPropertyExpression->generateExpressionByteCode(codeBlock, context, additionalPropertyExpressionRegsiter);
codeBlock->pushCode(ObjectDefineOwnPropertyWithNameOperation(ByteCodeLOC(m_loc.index), dstRegister, m_additionalPropertyName, additionalPropertyExpressionRegsiter, ObjectPropertyDescriptor::NotPresent), context, this);
if (m_isTaggedTemplateExpression) {
auto freezeFunctionRegister = context->getRegister();
codeBlock->pushCode(LoadLiteral(ByteCodeLOC(m_loc.index), freezeFunctionRegister, Value(codeBlock->m_codeBlock->context()->globalObject()->objectFreeze())), context, this);
codeBlock->pushCode(CallFunction(ByteCodeLOC(m_loc.index), freezeFunctionRegister, additionalPropertyExpressionRegsiter, freezeFunctionRegister, 1), context, this);
context->giveUpRegister();
}
context->giveUpRegister();
}
if (m_isTaggedTemplateExpression) {
auto freezeFunctionRegister = context->getRegister();
codeBlock->pushCode(LoadLiteral(ByteCodeLOC(m_loc.index), freezeFunctionRegister, Value(codeBlock->m_codeBlock->context()->globalObject()->objectFreeze())), context, this);
codeBlock->pushCode(CallFunction(ByteCodeLOC(m_loc.index), freezeFunctionRegister, dstRegister, freezeFunctionRegister, 1), context, this);
context->giveUpRegister();
}
}
virtual void iterateChildrenIdentifier(const std::function<void(AtomicString name, bool isAssignment)>& fn) override
{
for (SentinelNode* element = m_elements.begin(); element != m_elements.end(); element = element->next()) {
if (element->astNode()) {
element->astNode()->iterateChildrenIdentifier(fn);
}
}
if (m_additionalPropertyExpression) {
m_additionalPropertyExpression->iterateChildrenIdentifier(fn);
}
}
virtual void iterateChildren(const std::function<void(Node* node)>& fn) override
{
fn(this);
for (SentinelNode* element = m_elements.begin(); element != m_elements.end(); element = element->next()) {
if (element->astNode()) {
element->astNode()->iterateChildren(fn);
}
}
if (m_additionalPropertyExpression) {
m_additionalPropertyExpression->iterateChildren(fn);
}
}
private:
NodeList m_elements;
AtomicString m_additionalPropertyName;
Node* m_additionalPropertyExpression;
bool m_hasSpreadElement;
bool m_isTaggedTemplateExpression;
};
} // namespace Escargot
#endif