OLD | NEW |
1 // Copyright 2006-2012 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 (function(global, utils) { | 5 (function(global, utils) { |
6 "use strict"; | 6 "use strict"; |
7 | 7 |
8 // ---------------------------------------------------------------------------- | 8 // ---------------------------------------------------------------------------- |
9 // Imports | 9 // Imports |
10 | 10 |
11 var GlobalArray = global.Array; | 11 var GlobalArray = global.Array; |
12 var IsNaN = global.isNaN; | 12 var IsNaN = global.isNaN; |
13 var JSONStringify = global.JSON.stringify; | 13 var JSONStringify = global.JSON.stringify; |
| 14 var GlobalMap = global.Map; |
14 var MathMin = global.Math.min; | 15 var MathMin = global.Math.min; |
15 | 16 |
16 // ---------------------------------------------------------------------------- | 17 // ---------------------------------------------------------------------------- |
17 | 18 |
18 // Mirror hierarchy: | 19 // Mirror hierarchy: |
19 // - Mirror | 20 // - Mirror |
20 // - ValueMirror | 21 // - ValueMirror |
21 // - UndefinedMirror | 22 // - UndefinedMirror |
22 // - NullMirror | 23 // - NullMirror |
23 // - BooleanMirror | 24 // - BooleanMirror |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 ITERATOR_TYPE : 'iterator', | 67 ITERATOR_TYPE : 'iterator', |
67 GENERATOR_TYPE : 'generator', | 68 GENERATOR_TYPE : 'generator', |
68 } | 69 } |
69 | 70 |
70 | 71 |
71 // Handle id counters. | 72 // Handle id counters. |
72 var next_handle_ = 0; | 73 var next_handle_ = 0; |
73 var next_transient_handle_ = -1; | 74 var next_transient_handle_ = -1; |
74 | 75 |
75 // Mirror cache. | 76 // Mirror cache. |
76 var mirror_cache_ = []; | 77 var mirror_cache_ = new GlobalMap(); |
77 var mirror_cache_enabled_ = true; | 78 var mirror_cache_enabled_ = true; |
78 | 79 |
79 | 80 |
80 function MirrorCacheIsEmpty() { | 81 function MirrorCacheIsEmpty() { |
81 return next_handle_ == 0 && mirror_cache_.length == 0; | 82 return mirror_cache_.size === 0; |
82 } | 83 } |
83 | 84 |
84 | 85 |
85 function ToggleMirrorCache(value) { | 86 function ToggleMirrorCache(value) { |
86 mirror_cache_enabled_ = value; | 87 mirror_cache_enabled_ = value; |
87 ClearMirrorCache(); | 88 ClearMirrorCache(); |
88 } | 89 } |
89 | 90 |
90 | 91 |
91 function ClearMirrorCache(value) { | 92 function ClearMirrorCache(value) { |
92 next_handle_ = 0; | 93 next_handle_ = 0; |
93 mirror_cache_ = []; | 94 mirror_cache_.clear(); |
94 } | 95 } |
95 | 96 |
96 | 97 |
97 // Wrapper to check whether an object is a Promise. The call may not work | 98 // Wrapper to check whether an object is a Promise. The call may not work |
98 // if promises are not enabled. | 99 // if promises are not enabled. |
99 // TODO(yangguo): remove try-catch once promises are enabled by default. | 100 // TODO(yangguo): remove try-catch once promises are enabled by default. |
100 function ObjectIsPromise(value) { | 101 function ObjectIsPromise(value) { |
101 try { | 102 try { |
102 return IS_SPEC_OBJECT(value) && | 103 return IS_SPEC_OBJECT(value) && |
103 !IS_UNDEFINED(%DebugGetProperty(value, builtins.$promiseStatus)); | 104 !IS_UNDEFINED(%DebugGetProperty(value, builtins.$promiseStatus)); |
104 } catch (e) { | 105 } catch (e) { |
105 return false; | 106 return false; |
106 } | 107 } |
107 } | 108 } |
108 | 109 |
109 | 110 |
110 /** | 111 /** |
111 * Returns the mirror for a specified value or object. | 112 * Returns the mirror for a specified value or object. |
112 * | 113 * |
113 * @param {value or Object} value the value or object to retreive the mirror for | 114 * @param {value or Object} value the value or object to retreive the mirror for |
114 * @param {boolean} transient indicate whether this object is transient and | 115 * @param {boolean} transient indicate whether this object is transient and |
115 * should not be added to the mirror cache. The default is not transient. | 116 * should not be added to the mirror cache. The default is not transient. |
116 * @returns {Mirror} the mirror reflects the passed value or object | 117 * @returns {Mirror} the mirror reflects the passed value or object |
117 */ | 118 */ |
118 function MakeMirror(value, opt_transient) { | 119 function MakeMirror(value, opt_transient) { |
119 var mirror; | 120 var mirror; |
120 | 121 |
121 // Look for non transient mirrors in the mirror cache. | 122 // Look for non transient mirrors in the mirror cache. |
122 if (!opt_transient && mirror_cache_enabled_) { | 123 if (!opt_transient && mirror_cache_enabled_) { |
123 for (var id in mirror_cache_) { | 124 if (mirror_cache_.has(value)) return mirror_cache_.get(value); |
124 mirror = mirror_cache_[id]; | |
125 if (mirror.value() === value) { | |
126 return mirror; | |
127 } | |
128 // Special check for NaN as NaN == NaN is false. | |
129 if (mirror.isNumber() && IsNaN(mirror.value()) && | |
130 typeof value == 'number' && IsNaN(value)) { | |
131 return mirror; | |
132 } | |
133 } | |
134 } | 125 } |
135 | 126 |
136 if (IS_UNDEFINED(value)) { | 127 if (IS_UNDEFINED(value)) { |
137 mirror = new UndefinedMirror(); | 128 mirror = new UndefinedMirror(); |
138 } else if (IS_NULL(value)) { | 129 } else if (IS_NULL(value)) { |
139 mirror = new NullMirror(); | 130 mirror = new NullMirror(); |
140 } else if (IS_BOOLEAN(value)) { | 131 } else if (IS_BOOLEAN(value)) { |
141 mirror = new BooleanMirror(value); | 132 mirror = new BooleanMirror(value); |
142 } else if (IS_NUMBER(value)) { | 133 } else if (IS_NUMBER(value)) { |
143 mirror = new NumberMirror(value); | 134 mirror = new NumberMirror(value); |
(...skipping 20 matching lines...) Expand all Loading... |
164 } else if (IS_MAP_ITERATOR(value) || IS_SET_ITERATOR(value)) { | 155 } else if (IS_MAP_ITERATOR(value) || IS_SET_ITERATOR(value)) { |
165 mirror = new IteratorMirror(value); | 156 mirror = new IteratorMirror(value); |
166 } else if (ObjectIsPromise(value)) { | 157 } else if (ObjectIsPromise(value)) { |
167 mirror = new PromiseMirror(value); | 158 mirror = new PromiseMirror(value); |
168 } else if (IS_GENERATOR(value)) { | 159 } else if (IS_GENERATOR(value)) { |
169 mirror = new GeneratorMirror(value); | 160 mirror = new GeneratorMirror(value); |
170 } else { | 161 } else { |
171 mirror = new ObjectMirror(value, MirrorType.OBJECT_TYPE, opt_transient); | 162 mirror = new ObjectMirror(value, MirrorType.OBJECT_TYPE, opt_transient); |
172 } | 163 } |
173 | 164 |
174 if (mirror_cache_enabled_) mirror_cache_[mirror.handle()] = mirror; | 165 if (mirror_cache_enabled_) mirror_cache_.set(value, mirror); |
175 return mirror; | 166 return mirror; |
176 } | 167 } |
177 | 168 |
178 | 169 |
179 /** | 170 /** |
180 * Returns the mirror for a specified mirror handle. | 171 * Returns the mirror for a specified mirror handle. |
181 * | 172 * |
182 * @param {number} handle the handle to find the mirror for | 173 * @param {number} handle the handle to find the mirror for |
183 * @returns {Mirror or undefiend} the mirror with the requested handle or | 174 * @returns {Mirror or undefiend} the mirror with the requested handle or |
184 * undefined if no mirror with the requested handle was found | 175 * undefined if no mirror with the requested handle was found |
185 */ | 176 */ |
186 function LookupMirror(handle) { | 177 function LookupMirror(handle) { |
187 if (!mirror_cache_enabled_) { | 178 if (!mirror_cache_enabled_) { |
188 throw MakeError(kDebugger, "Mirror cache is disabled"); | 179 throw MakeError(kDebugger, "Mirror cache is disabled"); |
189 } | 180 } |
190 return mirror_cache_[handle]; | 181 for (var value of mirror_cache_.values()) { |
| 182 if (value.handle() == handle) return value; |
| 183 } |
| 184 return UNDEFINED; |
191 } | 185 } |
192 | 186 |
193 | 187 |
194 /** | 188 /** |
195 * Returns the mirror for the undefined value. | 189 * Returns the mirror for the undefined value. |
196 * | 190 * |
197 * @returns {Mirror} the mirror reflects the undefined value | 191 * @returns {Mirror} the mirror reflects the undefined value |
198 */ | 192 */ |
199 function GetUndefinedMirror() { | 193 function GetUndefinedMirror() { |
200 return MakeMirror(UNDEFINED); | 194 return MakeMirror(UNDEFINED); |
(...skipping 2897 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3098 // Functions needed by the debugger runtime. | 3092 // Functions needed by the debugger runtime. |
3099 utils.InstallFunctions(utils, DONT_ENUM, [ | 3093 utils.InstallFunctions(utils, DONT_ENUM, [ |
3100 "ClearMirrorCache", ClearMirrorCache | 3094 "ClearMirrorCache", ClearMirrorCache |
3101 ]); | 3095 ]); |
3102 | 3096 |
3103 // Export to debug.js | 3097 // Export to debug.js |
3104 utils.Export(function(to) { | 3098 utils.Export(function(to) { |
3105 to.MirrorType = MirrorType; | 3099 to.MirrorType = MirrorType; |
3106 }); | 3100 }); |
3107 }) | 3101 }) |
OLD | NEW |