OLD | NEW |
---|---|
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 22 matching lines...) Expand all Loading... | |
33 | 33 |
34 namespace v8 { | 34 namespace v8 { |
35 namespace internal { | 35 namespace internal { |
36 | 36 |
37 class CompilationInfo; | 37 class CompilationInfo; |
38 | 38 |
39 | 39 |
40 // A hash map to support fast variable declaration and lookup. | 40 // A hash map to support fast variable declaration and lookup. |
41 class VariableMap: public ZoneHashMap { | 41 class VariableMap: public ZoneHashMap { |
42 public: | 42 public: |
43 VariableMap(); | 43 explicit VariableMap(Zone* zone); |
44 | 44 |
45 virtual ~VariableMap(); | 45 virtual ~VariableMap(); |
46 | 46 |
47 Variable* Declare(Scope* scope, | 47 Variable* Declare(Scope* scope, |
48 Handle<String> name, | 48 Handle<String> name, |
49 VariableMode mode, | 49 VariableMode mode, |
50 bool is_valid_lhs, | 50 bool is_valid_lhs, |
51 Variable::Kind kind, | 51 Variable::Kind kind, |
52 InitializationFlag initialization_flag, | 52 InitializationFlag initialization_flag, |
53 Interface* interface = Interface::NewValue()); | 53 Interface* interface = Interface::NewValue()); |
54 | 54 |
55 Variable* Lookup(Handle<String> name); | 55 Variable* Lookup(Handle<String> name); |
56 | |
57 Zone* zone() const { return zone_; } | |
58 | |
59 private: | |
60 Zone* zone_; | |
56 }; | 61 }; |
57 | 62 |
58 | 63 |
59 // The dynamic scope part holds hash maps for the variables that will | 64 // The dynamic scope part holds hash maps for the variables that will |
60 // be looked up dynamically from within eval and with scopes. The objects | 65 // be looked up dynamically from within eval and with scopes. The objects |
61 // are allocated on-demand from Scope::NonLocal to avoid wasting memory | 66 // are allocated on-demand from Scope::NonLocal to avoid wasting memory |
62 // and setup time for scopes that don't need them. | 67 // and setup time for scopes that don't need them. |
63 class DynamicScopePart : public ZoneObject { | 68 class DynamicScopePart : public ZoneObject { |
64 public: | 69 public: |
70 explicit DynamicScopePart(Zone* zone) { | |
71 for (int i = 0; i < 3; i++) | |
72 maps_[i] = new VariableMap(zone); | |
danno
2012/06/11 12:11:45
Where does this allocation come from? It looks lik
sanjoy
2012/06/11 12:37:56
Changed to allocate from the zone. VariableMap is
| |
73 } | |
74 | |
65 VariableMap* GetMap(VariableMode mode) { | 75 VariableMap* GetMap(VariableMode mode) { |
66 int index = mode - DYNAMIC; | 76 int index = mode - DYNAMIC; |
67 ASSERT(index >= 0 && index < 3); | 77 ASSERT(index >= 0 && index < 3); |
68 return &maps_[index]; | 78 return maps_[index]; |
79 } | |
80 | |
81 ~DynamicScopePart() { | |
82 for (int i = 0; i < 3; i++) | |
83 delete maps_[i]; | |
69 } | 84 } |
70 | 85 |
71 private: | 86 private: |
72 VariableMap maps_[3]; | 87 VariableMap *maps_[3]; |
73 }; | 88 }; |
74 | 89 |
75 | 90 |
76 // Global invariants after AST construction: Each reference (i.e. identifier) | 91 // Global invariants after AST construction: Each reference (i.e. identifier) |
77 // to a JavaScript variable (including global properties) is represented by a | 92 // to a JavaScript variable (including global properties) is represented by a |
78 // VariableProxy node. Immediately after AST construction and before variable | 93 // VariableProxy node. Immediately after AST construction and before variable |
79 // allocation, most VariableProxy nodes are "unresolved", i.e. not bound to a | 94 // allocation, most VariableProxy nodes are "unresolved", i.e. not bound to a |
80 // corresponding variable (though some are bound during parse time). Variable | 95 // corresponding variable (though some are bound during parse time). Variable |
81 // allocation binds each unresolved VariableProxy to one Variable and assigns | 96 // allocation binds each unresolved VariableProxy to one Variable and assigns |
82 // a location. Note that many VariableProxy nodes may refer to the same Java- | 97 // a location. Note that many VariableProxy nodes may refer to the same Java- |
83 // Script variable. | 98 // Script variable. |
84 | 99 |
85 class Scope: public ZoneObject { | 100 class Scope: public ZoneObject { |
86 public: | 101 public: |
87 // --------------------------------------------------------------------------- | 102 // --------------------------------------------------------------------------- |
88 // Construction | 103 // Construction |
89 | 104 |
90 Scope(Scope* outer_scope, ScopeType type); | 105 Scope(Scope* outer_scope, ScopeType type, Zone* zone); |
91 | 106 |
92 // Compute top scope and allocate variables. For lazy compilation the top | 107 // Compute top scope and allocate variables. For lazy compilation the top |
93 // scope only contains the single lazily compiled function, so this | 108 // scope only contains the single lazily compiled function, so this |
94 // doesn't re-allocate variables repeatedly. | 109 // doesn't re-allocate variables repeatedly. |
95 static bool Analyze(CompilationInfo* info); | 110 static bool Analyze(CompilationInfo* info); |
96 | 111 |
97 static Scope* DeserializeScopeChain(Context* context, Scope* global_scope); | 112 static Scope* DeserializeScopeChain(Context* context, Scope* global_scope, |
113 Zone* zone); | |
98 | 114 |
99 // The scope name is only used for printing/debugging. | 115 // The scope name is only used for printing/debugging. |
100 void SetScopeName(Handle<String> scope_name) { scope_name_ = scope_name; } | 116 void SetScopeName(Handle<String> scope_name) { scope_name_ = scope_name; } |
101 | 117 |
102 void Initialize(); | 118 void Initialize(); |
103 | 119 |
104 // Checks if the block scope is redundant, i.e. it does not contain any | 120 // Checks if the block scope is redundant, i.e. it does not contain any |
105 // block scoped declarations. In that case it is removed from the scope | 121 // block scoped declarations. In that case it is removed from the scope |
106 // tree and its children are reparented. | 122 // tree and its children are reparented. |
107 Scope* FinalizeBlockScope(); | 123 Scope* FinalizeBlockScope(); |
108 | 124 |
125 Zone* zone() const { return zone_; } | |
126 | |
109 // --------------------------------------------------------------------------- | 127 // --------------------------------------------------------------------------- |
110 // Declarations | 128 // Declarations |
111 | 129 |
112 // Lookup a variable in this scope. Returns the variable or NULL if not found. | 130 // Lookup a variable in this scope. Returns the variable or NULL if not found. |
113 Variable* LocalLookup(Handle<String> name); | 131 Variable* LocalLookup(Handle<String> name); |
114 | 132 |
115 // This lookup corresponds to a lookup in the "intermediate" scope sitting | 133 // This lookup corresponds to a lookup in the "intermediate" scope sitting |
116 // between this scope and the outer scope. (ECMA-262, 3rd., requires that | 134 // between this scope and the outer scope. (ECMA-262, 3rd., requires that |
117 // the name of named function literal is kept in an intermediate scope | 135 // the name of named function literal is kept in an intermediate scope |
118 // in between this scope and the next outer scope.) | 136 // in between this scope and the next outer scope.) |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
154 VariableProxy* NewUnresolved(AstNodeFactory<Visitor>* factory, | 172 VariableProxy* NewUnresolved(AstNodeFactory<Visitor>* factory, |
155 Handle<String> name, | 173 Handle<String> name, |
156 int position = RelocInfo::kNoPosition, | 174 int position = RelocInfo::kNoPosition, |
157 Interface* interface = Interface::NewValue()) { | 175 Interface* interface = Interface::NewValue()) { |
158 // Note that we must not share the unresolved variables with | 176 // Note that we must not share the unresolved variables with |
159 // the same name because they may be removed selectively via | 177 // the same name because they may be removed selectively via |
160 // RemoveUnresolved(). | 178 // RemoveUnresolved(). |
161 ASSERT(!already_resolved()); | 179 ASSERT(!already_resolved()); |
162 VariableProxy* proxy = | 180 VariableProxy* proxy = |
163 factory->NewVariableProxy(name, false, position, interface); | 181 factory->NewVariableProxy(name, false, position, interface); |
164 unresolved_.Add(proxy); | 182 unresolved_.Add(proxy, zone_); |
165 return proxy; | 183 return proxy; |
166 } | 184 } |
167 | 185 |
168 // Remove a unresolved variable. During parsing, an unresolved variable | 186 // Remove a unresolved variable. During parsing, an unresolved variable |
169 // may have been added optimistically, but then only the variable name | 187 // may have been added optimistically, but then only the variable name |
170 // was used (typically for labels). If the variable was not declared, the | 188 // was used (typically for labels). If the variable was not declared, the |
171 // addition introduced a new unresolved variable which may end up being | 189 // addition introduced a new unresolved variable which may end up being |
172 // allocated globally as a "ghost" variable. RemoveUnresolved removes | 190 // allocated globally as a "ghost" variable. RemoveUnresolved removes |
173 // such a variable again if it was added; otherwise this is a no-op. | 191 // such a variable again if it was added; otherwise this is a no-op. |
174 void RemoveUnresolved(VariableProxy* var); | 192 void RemoveUnresolved(VariableProxy* var); |
(...skipping 399 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
574 // | 592 // |
575 // In the case of code compiled and run using 'eval', the context | 593 // In the case of code compiled and run using 'eval', the context |
576 // parameter is the context in which eval was called. In all other | 594 // parameter is the context in which eval was called. In all other |
577 // cases the context parameter is an empty handle. | 595 // cases the context parameter is an empty handle. |
578 MUST_USE_RESULT | 596 MUST_USE_RESULT |
579 bool AllocateVariables(CompilationInfo* info, | 597 bool AllocateVariables(CompilationInfo* info, |
580 AstNodeFactory<AstNullVisitor>* factory); | 598 AstNodeFactory<AstNullVisitor>* factory); |
581 | 599 |
582 private: | 600 private: |
583 // Construct a scope based on the scope info. | 601 // Construct a scope based on the scope info. |
584 Scope(Scope* inner_scope, ScopeType type, Handle<ScopeInfo> scope_info); | 602 Scope(Scope* inner_scope, ScopeType type, Handle<ScopeInfo> scope_info, |
603 Zone* zone); | |
585 | 604 |
586 // Construct a catch scope with a binding for the name. | 605 // Construct a catch scope with a binding for the name. |
587 Scope(Scope* inner_scope, Handle<String> catch_variable_name); | 606 Scope(Scope* inner_scope, Handle<String> catch_variable_name, Zone* zone); |
588 | 607 |
589 void AddInnerScope(Scope* inner_scope) { | 608 void AddInnerScope(Scope* inner_scope) { |
590 if (inner_scope != NULL) { | 609 if (inner_scope != NULL) { |
591 inner_scopes_.Add(inner_scope); | 610 inner_scopes_.Add(inner_scope, zone_); |
592 inner_scope->outer_scope_ = this; | 611 inner_scope->outer_scope_ = this; |
593 } | 612 } |
594 } | 613 } |
595 | 614 |
596 void SetDefaults(ScopeType type, | 615 void SetDefaults(ScopeType type, |
597 Scope* outer_scope, | 616 Scope* outer_scope, |
598 Handle<ScopeInfo> scope_info); | 617 Handle<ScopeInfo> scope_info); |
618 | |
619 Zone* zone_; | |
599 }; | 620 }; |
600 | 621 |
601 } } // namespace v8::internal | 622 } } // namespace v8::internal |
602 | 623 |
603 #endif // V8_SCOPES_H_ | 624 #endif // V8_SCOPES_H_ |
OLD | NEW |