forked from Samsung/escargot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectPatternNode.h
More file actions
135 lines (118 loc) · 6.09 KB
/
ObjectPatternNode.h
File metadata and controls
135 lines (118 loc) · 6.09 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
/*
* 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
*/
#ifndef ObjectPatternNode_h
#define ObjectPatternNode_h
#include "Node.h"
#include "PropertyNode.h"
namespace Escargot {
class ObjectPatternNode : public Node {
public:
ObjectPatternNode(const NodeList& properties, bool hasRestElement)
: m_properties(properties)
, m_hasRestElement(hasRestElement)
{
#ifndef NDEBUG
for (SentinelNode* property = m_properties.begin(); property != m_properties.end(); property = property->next()) {
if (m_hasRestElement) {
ASSERT(property->astNode()->isProperty() || property->astNode()->type() == ASTNodeType::RestElement);
} else {
ASSERT(property->astNode()->isProperty());
}
}
#endif
}
ObjectPatternNode(const NodeList& properties, NodeLOC& loc)
: m_properties(properties)
, m_hasRestElement(false)
{
m_loc = loc;
for (SentinelNode* property = m_properties.begin(); property != m_properties.end(); property = property->next()) {
ASSERT(property->astNode()->isProperty() || property->astNode()->type() == ASTNodeType::RestElement);
if (property->astNode()->type() == ASTNodeType::RestElement) {
m_hasRestElement = true;
}
}
}
virtual ASTNodeType type() override { return ASTNodeType::ObjectPattern; }
virtual void generateStoreByteCode(ByteCodeBlock* codeBlock, ByteCodeGenerateContext* context, ByteCodeRegisterIndex srcRegister, bool needToReferenceSelf) override
{
bool isLexicallyDeclaredBindingInitialization = context->m_isLexicallyDeclaredBindingInitialization;
// set m_isLexicallyDeclaredBindingInitialization to false for the case of empty object pattern
context->m_isLexicallyDeclaredBindingInitialization = false;
if (m_properties.size() > 0) {
if (m_hasRestElement) {
context->m_inObjectDestruction = true;
ByteCodeRegisterIndex enumDataIndex = context->getRegister();
codeBlock->pushCode(CreateEnumerateObject(ByteCodeLOC(m_loc.index), srcRegister, enumDataIndex, true), context, this);
for (SentinelNode* property = m_properties.begin(); property != m_properties.end(); property = property->next()) {
context->m_isLexicallyDeclaredBindingInitialization = isLexicallyDeclaredBindingInitialization;
if (LIKELY(property->astNode()->isProperty())) {
property->astNode()->generateStoreByteCode(codeBlock, context, srcRegister, needToReferenceSelf);
} else {
ASSERT(property->astNode()->type() == ASTNodeType::RestElement);
property->astNode()->generateStoreByteCode(codeBlock, context, enumDataIndex, needToReferenceSelf);
}
}
context->giveUpRegister(); // for drop enumDataIndex
context->m_inObjectDestruction = false;
} else {
for (SentinelNode* property = m_properties.begin(); property != m_properties.end(); property = property->next()) {
ASSERT(property->astNode()->isProperty());
context->m_isLexicallyDeclaredBindingInitialization = isLexicallyDeclaredBindingInitialization;
property->astNode()->generateStoreByteCode(codeBlock, context, srcRegister, needToReferenceSelf);
}
}
} else {
// ObjectAssignmentPattern without AssignmentPropertyList requires object-coercible
// check if srcRegister is undefined or null
codeBlock->pushCode<JumpIfUndefinedOrNull>(JumpIfUndefinedOrNull(ByteCodeLOC(m_loc.index), true, srcRegister), context, this);
size_t pos = codeBlock->lastCodePosition<JumpIfUndefinedOrNull>();
codeBlock->pushCode(ThrowStaticErrorOperation(ByteCodeLOC(m_loc.index), ErrorObject::TypeError, ErrorObject::Messages::Can_Not_Be_Destructed), context, this);
codeBlock->peekCode<JumpIfUndefinedOrNull>(pos)->m_jumpPosition = codeBlock->currentCodeSize();
}
ASSERT(!context->m_isLexicallyDeclaredBindingInitialization);
codeBlock->m_shouldClearStack = true;
}
// FIXME implement iterateChildrenIdentifier in PropertyNode itself
virtual void iterateChildrenIdentifier(const std::function<void(AtomicString name, bool isAssignment)>& fn) override
{
for (SentinelNode* property = m_properties.begin(); property != m_properties.end(); property = property->next()) {
if (property->astNode()->isProperty()) {
PropertyNode* p = property->astNode()->asProperty();
if (!(p->key()->isIdentifier() && !p->computed())) {
p->key()->iterateChildrenIdentifier(fn);
}
} else {
property->astNode()->iterateChildrenIdentifier(fn);
}
}
}
virtual void iterateChildren(const std::function<void(Node* node)>& fn) override
{
fn(this);
for (SentinelNode* property = m_properties.begin(); property != m_properties.end(); property = property->next()) {
property->astNode()->iterateChildren(fn);
}
}
private:
NodeList m_properties;
bool m_hasRestElement : 1;
};
} // namespace Escargot
#endif