blob: 7f1a05aed9479e8b8cb0d12cf7777bcea1eae73c [file] [log] [blame]
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001// Copyright 2006-2012 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
ager@chromium.orgeadaf222009-06-16 09:43:10 +000028// Handle id counters.
ager@chromium.org32912102009-01-16 10:38:43 +000029var next_handle_ = 0;
ager@chromium.orgeadaf222009-06-16 09:43:10 +000030var next_transient_handle_ = -1;
31
32// Mirror cache.
ager@chromium.org32912102009-01-16 10:38:43 +000033var mirror_cache_ = [];
34
ager@chromium.orgeadaf222009-06-16 09:43:10 +000035
ager@chromium.org32912102009-01-16 10:38:43 +000036/**
37 * Clear the mirror handle cache.
38 */
39function ClearMirrorCache() {
40 next_handle_ = 0;
41 mirror_cache_ = [];
42}
43
44
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +000045/**
46 * Returns the mirror for a specified value or object.
47 *
48 * @param {value or Object} value the value or object to retreive the mirror for
ager@chromium.orgeadaf222009-06-16 09:43:10 +000049 * @param {boolean} transient indicate whether this object is transient and
50 * should not be added to the mirror cache. The default is not transient.
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +000051 * @returns {Mirror} the mirror reflects the passed value or object
52 */
ager@chromium.orgeadaf222009-06-16 09:43:10 +000053function MakeMirror(value, opt_transient) {
ager@chromium.org32912102009-01-16 10:38:43 +000054 var mirror;
ager@chromium.orgeadaf222009-06-16 09:43:10 +000055
56 // Look for non transient mirrors in the mirror cache.
57 if (!opt_transient) {
58 for (id in mirror_cache_) {
59 mirror = mirror_cache_[id];
60 if (mirror.value() === value) {
61 return mirror;
62 }
63 // Special check for NaN as NaN == NaN is false.
64 if (mirror.isNumber() && isNaN(mirror.value()) &&
65 typeof value == 'number' && isNaN(value)) {
66 return mirror;
67 }
ager@chromium.orgddb913d2009-01-27 10:01:48 +000068 }
ager@chromium.org32912102009-01-16 10:38:43 +000069 }
lrn@chromium.org25156de2010-04-06 13:10:27 +000070
ager@chromium.org32912102009-01-16 10:38:43 +000071 if (IS_UNDEFINED(value)) {
72 mirror = new UndefinedMirror();
73 } else if (IS_NULL(value)) {
74 mirror = new NullMirror();
75 } else if (IS_BOOLEAN(value)) {
76 mirror = new BooleanMirror(value);
77 } else if (IS_NUMBER(value)) {
78 mirror = new NumberMirror(value);
79 } else if (IS_STRING(value)) {
80 mirror = new StringMirror(value);
81 } else if (IS_ARRAY(value)) {
82 mirror = new ArrayMirror(value);
83 } else if (IS_DATE(value)) {
84 mirror = new DateMirror(value);
85 } else if (IS_FUNCTION(value)) {
86 mirror = new FunctionMirror(value);
87 } else if (IS_REGEXP(value)) {
88 mirror = new RegExpMirror(value);
89 } else if (IS_ERROR(value)) {
90 mirror = new ErrorMirror(value);
iposva@chromium.org245aa852009-02-10 00:49:54 +000091 } else if (IS_SCRIPT(value)) {
92 mirror = new ScriptMirror(value);
ager@chromium.org32912102009-01-16 10:38:43 +000093 } else {
ager@chromium.orgeadaf222009-06-16 09:43:10 +000094 mirror = new ObjectMirror(value, OBJECT_TYPE, opt_transient);
ager@chromium.org32912102009-01-16 10:38:43 +000095 }
96
97 mirror_cache_[mirror.handle()] = mirror;
98 return mirror;
99}
100
101
102/**
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000103 * Returns the mirror for a specified mirror handle.
104 *
105 * @param {number} handle the handle to find the mirror for
106 * @returns {Mirror or undefiend} the mirror with the requested handle or
107 * undefined if no mirror with the requested handle was found
108 */
109function LookupMirror(handle) {
110 return mirror_cache_[handle];
111}
112
lrn@chromium.org25156de2010-04-06 13:10:27 +0000113
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000114/**
ager@chromium.org32912102009-01-16 10:38:43 +0000115 * Returns the mirror for the undefined value.
116 *
117 * @returns {Mirror} the mirror reflects the undefined value
118 */
119function GetUndefinedMirror() {
120 return MakeMirror(void 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000121}
122
123
124/**
125 * Inherit the prototype methods from one constructor into another.
126 *
127 * The Function.prototype.inherits from lang.js rewritten as a standalone
128 * function (not on Function.prototype). NOTE: If this file is to be loaded
129 * during bootstrapping this function needs to be revritten using some native
130 * functions as prototype setup using normal JavaScript does not work as
131 * expected during bootstrapping (see mirror.js in r114903).
132 *
133 * @param {function} ctor Constructor function which needs to inherit the
134 * prototype
135 * @param {function} superCtor Constructor function to inherit prototype from
136 */
137function inherits(ctor, superCtor) {
138 var tempCtor = function(){};
139 tempCtor.prototype = superCtor.prototype;
140 ctor.super_ = superCtor.prototype;
141 ctor.prototype = new tempCtor();
142 ctor.prototype.constructor = ctor;
143}
144
145
146// Type names of the different mirrors.
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000147var UNDEFINED_TYPE = 'undefined';
148var NULL_TYPE = 'null';
149var BOOLEAN_TYPE = 'boolean';
150var NUMBER_TYPE = 'number';
151var STRING_TYPE = 'string';
152var OBJECT_TYPE = 'object';
153var FUNCTION_TYPE = 'function';
154var REGEXP_TYPE = 'regexp';
155var ERROR_TYPE = 'error';
156var PROPERTY_TYPE = 'property';
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +0000157var INTERNAL_PROPERTY_TYPE = 'internalProperty';
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000158var FRAME_TYPE = 'frame';
159var SCRIPT_TYPE = 'script';
160var CONTEXT_TYPE = 'context';
161var SCOPE_TYPE = 'scope';
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000162
163// Maximum length when sending strings through the JSON protocol.
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000164var kMaxProtocolStringLength = 80;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000165
166// Different kind of properties.
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000167var PropertyKind = {};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000168PropertyKind.Named = 1;
169PropertyKind.Indexed = 2;
170
171
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000172// A copy of the PropertyType enum from global.h
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000173var PropertyType = {};
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000174PropertyType.Normal = 0;
175PropertyType.Field = 1;
176PropertyType.ConstantFunction = 2;
177PropertyType.Callbacks = 3;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000178PropertyType.Handler = 4;
179PropertyType.Interceptor = 5;
yangguo@chromium.org99aa4902012-07-06 16:21:55 +0000180PropertyType.Transition = 6;
181PropertyType.Nonexistent = 7;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000182
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000183
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000184// Different attributes for a property.
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000185var PropertyAttribute = {};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000186PropertyAttribute.None = NONE;
187PropertyAttribute.ReadOnly = READ_ONLY;
188PropertyAttribute.DontEnum = DONT_ENUM;
189PropertyAttribute.DontDelete = DONT_DELETE;
190
191
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000192// A copy of the scope types from runtime.cc.
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000193var ScopeType = { Global: 0,
194 Local: 1,
195 With: 2,
196 Closure: 3,
197 Catch: 4,
198 Block: 5 };
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000199
200
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000201// Mirror hierarchy:
202// - Mirror
203// - ValueMirror
204// - UndefinedMirror
205// - NullMirror
206// - NumberMirror
207// - StringMirror
208// - ObjectMirror
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +0000209// - FunctionMirror
210// - UnresolvedFunctionMirror
211// - ArrayMirror
212// - DateMirror
213// - RegExpMirror
214// - ErrorMirror
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000215// - PropertyMirror
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +0000216// - InternalPropertyMirror
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000217// - FrameMirror
218// - ScriptMirror
219
220
221/**
222 * Base class for all mirror objects.
223 * @param {string} type The type of the mirror
224 * @constructor
225 */
226function Mirror(type) {
227 this.type_ = type;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000228}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000229
230
231Mirror.prototype.type = function() {
232 return this.type_;
233};
234
235
236/**
ager@chromium.org32912102009-01-16 10:38:43 +0000237 * Check whether the mirror reflects a value.
238 * @returns {boolean} True if the mirror reflects a value.
239 */
240Mirror.prototype.isValue = function() {
241 return this instanceof ValueMirror;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000242};
ager@chromium.org32912102009-01-16 10:38:43 +0000243
244
245/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000246 * Check whether the mirror reflects the undefined value.
247 * @returns {boolean} True if the mirror reflects the undefined value.
248 */
249Mirror.prototype.isUndefined = function() {
250 return this instanceof UndefinedMirror;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000251};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000252
253
254/**
255 * Check whether the mirror reflects the null value.
256 * @returns {boolean} True if the mirror reflects the null value
257 */
258Mirror.prototype.isNull = function() {
259 return this instanceof NullMirror;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000260};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000261
262
263/**
264 * Check whether the mirror reflects a boolean value.
265 * @returns {boolean} True if the mirror reflects a boolean value
266 */
267Mirror.prototype.isBoolean = function() {
268 return this instanceof BooleanMirror;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000269};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000270
271
272/**
273 * Check whether the mirror reflects a number value.
274 * @returns {boolean} True if the mirror reflects a number value
275 */
276Mirror.prototype.isNumber = function() {
277 return this instanceof NumberMirror;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000278};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000279
280
281/**
282 * Check whether the mirror reflects a string value.
283 * @returns {boolean} True if the mirror reflects a string value
284 */
285Mirror.prototype.isString = function() {
286 return this instanceof StringMirror;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000287};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000288
289
290/**
291 * Check whether the mirror reflects an object.
292 * @returns {boolean} True if the mirror reflects an object
293 */
294Mirror.prototype.isObject = function() {
295 return this instanceof ObjectMirror;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000296};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000297
298
299/**
300 * Check whether the mirror reflects a function.
301 * @returns {boolean} True if the mirror reflects a function
302 */
303Mirror.prototype.isFunction = function() {
304 return this instanceof FunctionMirror;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000305};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000306
307
308/**
309 * Check whether the mirror reflects an unresolved function.
310 * @returns {boolean} True if the mirror reflects an unresolved function
311 */
312Mirror.prototype.isUnresolvedFunction = function() {
313 return this instanceof UnresolvedFunctionMirror;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000314};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000315
316
317/**
318 * Check whether the mirror reflects an array.
319 * @returns {boolean} True if the mirror reflects an array
320 */
321Mirror.prototype.isArray = function() {
322 return this instanceof ArrayMirror;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000323};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000324
325
326/**
327 * Check whether the mirror reflects a date.
328 * @returns {boolean} True if the mirror reflects a date
329 */
330Mirror.prototype.isDate = function() {
331 return this instanceof DateMirror;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000332};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000333
334
335/**
336 * Check whether the mirror reflects a regular expression.
337 * @returns {boolean} True if the mirror reflects a regular expression
338 */
339Mirror.prototype.isRegExp = function() {
340 return this instanceof RegExpMirror;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000341};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000342
343
344/**
345 * Check whether the mirror reflects an error.
346 * @returns {boolean} True if the mirror reflects an error
347 */
348Mirror.prototype.isError = function() {
349 return this instanceof ErrorMirror;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000350};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000351
352
353/**
354 * Check whether the mirror reflects a property.
355 * @returns {boolean} True if the mirror reflects a property
356 */
357Mirror.prototype.isProperty = function() {
358 return this instanceof PropertyMirror;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000359};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000360
361
362/**
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +0000363 * Check whether the mirror reflects an internal property.
364 * @returns {boolean} True if the mirror reflects an internal property
365 */
366Mirror.prototype.isInternalProperty = function() {
367 return this instanceof InternalPropertyMirror;
368};
369
370
371/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000372 * Check whether the mirror reflects a stack frame.
373 * @returns {boolean} True if the mirror reflects a stack frame
374 */
375Mirror.prototype.isFrame = function() {
376 return this instanceof FrameMirror;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000377};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000378
379
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +0000380/**
381 * Check whether the mirror reflects a script.
382 * @returns {boolean} True if the mirror reflects a script
383 */
384Mirror.prototype.isScript = function() {
385 return this instanceof ScriptMirror;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000386};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000387
388
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000389/**
ager@chromium.org9085a012009-05-11 19:22:57 +0000390 * Check whether the mirror reflects a context.
391 * @returns {boolean} True if the mirror reflects a context
392 */
393Mirror.prototype.isContext = function() {
394 return this instanceof ContextMirror;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000395};
ager@chromium.org9085a012009-05-11 19:22:57 +0000396
397
398/**
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000399 * Check whether the mirror reflects a scope.
400 * @returns {boolean} True if the mirror reflects a scope
401 */
402Mirror.prototype.isScope = function() {
403 return this instanceof ScopeMirror;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000404};
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000405
406
407/**
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000408 * Allocate a handle id for this object.
409 */
410Mirror.prototype.allocateHandle_ = function() {
411 this.handle_ = next_handle_++;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000412};
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000413
414
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000415/**
416 * Allocate a transient handle id for this object. Transient handles are
417 * negative.
418 */
419Mirror.prototype.allocateTransientHandle_ = function() {
420 this.handle_ = next_transient_handle_--;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000421};
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000422
423
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000424Mirror.prototype.toText = function() {
425 // Simpel to text which is used when on specialization in subclass.
whesse@chromium.org7a392b32011-01-31 11:30:36 +0000426 return "#<" + this.constructor.name + ">";
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000427};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000428
429
430/**
431 * Base class for all value mirror objects.
432 * @param {string} type The type of the mirror
433 * @param {value} value The value reflected by this mirror
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000434 * @param {boolean} transient indicate whether this object is transient with a
435 * transient handle
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000436 * @constructor
437 * @extends Mirror
438 */
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000439function ValueMirror(type, value, transient) {
whesse@chromium.org7a392b32011-01-31 11:30:36 +0000440 %_CallFunction(this, type, Mirror);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000441 this.value_ = value;
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000442 if (!transient) {
443 this.allocateHandle_();
444 } else {
445 this.allocateTransientHandle_();
446 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000447}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000448inherits(ValueMirror, Mirror);
449
450
ager@chromium.org32912102009-01-16 10:38:43 +0000451Mirror.prototype.handle = function() {
452 return this.handle_;
453};
454
455
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000456/**
457 * Check whether this is a primitive value.
458 * @return {boolean} True if the mirror reflects a primitive value
459 */
460ValueMirror.prototype.isPrimitive = function() {
461 var type = this.type();
462 return type === 'undefined' ||
463 type === 'null' ||
464 type === 'boolean' ||
465 type === 'number' ||
466 type === 'string';
467};
468
469
ager@chromium.org32912102009-01-16 10:38:43 +0000470/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000471 * Get the actual value reflected by this mirror.
472 * @return {value} The value reflected by this mirror
473 */
474ValueMirror.prototype.value = function() {
475 return this.value_;
476};
477
478
479/**
480 * Mirror object for Undefined.
481 * @constructor
482 * @extends ValueMirror
483 */
484function UndefinedMirror() {
whesse@chromium.org7a392b32011-01-31 11:30:36 +0000485 %_CallFunction(this, UNDEFINED_TYPE, void 0, ValueMirror);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000486}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000487inherits(UndefinedMirror, ValueMirror);
488
489
490UndefinedMirror.prototype.toText = function() {
491 return 'undefined';
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000492};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000493
494
495/**
496 * Mirror object for null.
497 * @constructor
498 * @extends ValueMirror
499 */
500function NullMirror() {
whesse@chromium.org7a392b32011-01-31 11:30:36 +0000501 %_CallFunction(this, NULL_TYPE, null, ValueMirror);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000502}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000503inherits(NullMirror, ValueMirror);
504
505
506NullMirror.prototype.toText = function() {
507 return 'null';
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000508};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000509
510
511/**
512 * Mirror object for boolean values.
513 * @param {boolean} value The boolean value reflected by this mirror
514 * @constructor
515 * @extends ValueMirror
516 */
517function BooleanMirror(value) {
whesse@chromium.org7a392b32011-01-31 11:30:36 +0000518 %_CallFunction(this, BOOLEAN_TYPE, value, ValueMirror);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000519}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000520inherits(BooleanMirror, ValueMirror);
521
522
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000523BooleanMirror.prototype.toText = function() {
524 return this.value_ ? 'true' : 'false';
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000525};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000526
527
528/**
529 * Mirror object for number values.
530 * @param {number} value The number value reflected by this mirror
531 * @constructor
532 * @extends ValueMirror
533 */
534function NumberMirror(value) {
whesse@chromium.org7a392b32011-01-31 11:30:36 +0000535 %_CallFunction(this, NUMBER_TYPE, value, ValueMirror);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000536}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000537inherits(NumberMirror, ValueMirror);
538
539
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000540NumberMirror.prototype.toText = function() {
541 return %NumberToString(this.value_);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000542};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000543
544
545/**
546 * Mirror object for string values.
547 * @param {string} value The string value reflected by this mirror
548 * @constructor
549 * @extends ValueMirror
550 */
551function StringMirror(value) {
whesse@chromium.org7a392b32011-01-31 11:30:36 +0000552 %_CallFunction(this, STRING_TYPE, value, ValueMirror);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000553}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000554inherits(StringMirror, ValueMirror);
555
556
557StringMirror.prototype.length = function() {
558 return this.value_.length;
559};
560
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +0000561StringMirror.prototype.getTruncatedValue = function(maxLength) {
562 if (maxLength != -1 && this.length() > maxLength) {
563 return this.value_.substring(0, maxLength) +
564 '... (length: ' + this.length() + ')';
565 }
566 return this.value_;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000567};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000568
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000569StringMirror.prototype.toText = function() {
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +0000570 return this.getTruncatedValue(kMaxProtocolStringLength);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000571};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000572
573
574/**
575 * Mirror object for objects.
576 * @param {object} value The object reflected by this mirror
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000577 * @param {boolean} transient indicate whether this object is transient with a
578 * transient handle
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000579 * @constructor
580 * @extends ValueMirror
581 */
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000582function ObjectMirror(value, type, transient) {
whesse@chromium.org7a392b32011-01-31 11:30:36 +0000583 %_CallFunction(this, type || OBJECT_TYPE, value, transient, ValueMirror);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000584}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000585inherits(ObjectMirror, ValueMirror);
586
587
588ObjectMirror.prototype.className = function() {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000589 return %_ClassOf(this.value_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000590};
591
592
593ObjectMirror.prototype.constructorFunction = function() {
594 return MakeMirror(%DebugGetProperty(this.value_, 'constructor'));
595};
596
597
598ObjectMirror.prototype.prototypeObject = function() {
599 return MakeMirror(%DebugGetProperty(this.value_, 'prototype'));
600};
601
602
603ObjectMirror.prototype.protoObject = function() {
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000604 return MakeMirror(%DebugGetPrototype(this.value_));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000605};
606
607
608ObjectMirror.prototype.hasNamedInterceptor = function() {
609 // Get information on interceptors for this object.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000610 var x = %GetInterceptorInfo(this.value_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000611 return (x & 2) != 0;
612};
613
614
615ObjectMirror.prototype.hasIndexedInterceptor = function() {
616 // Get information on interceptors for this object.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000617 var x = %GetInterceptorInfo(this.value_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000618 return (x & 1) != 0;
619};
620
621
622/**
623 * Return the property names for this object.
624 * @param {number} kind Indicate whether named, indexed or both kinds of
625 * properties are requested
626 * @param {number} limit Limit the number of names returend to the specified
627 value
628 * @return {Array} Property names for this object
629 */
630ObjectMirror.prototype.propertyNames = function(kind, limit) {
631 // Find kind and limit and allocate array for the result
632 kind = kind || PropertyKind.Named | PropertyKind.Indexed;
633
634 var propertyNames;
635 var elementNames;
636 var total = 0;
lrn@chromium.org25156de2010-04-06 13:10:27 +0000637
ager@chromium.org32912102009-01-16 10:38:43 +0000638 // Find all the named properties.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000639 if (kind & PropertyKind.Named) {
ager@chromium.org32912102009-01-16 10:38:43 +0000640 // Get the local property names.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000641 propertyNames = %GetLocalPropertyNames(this.value_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000642 total += propertyNames.length;
ager@chromium.org32912102009-01-16 10:38:43 +0000643
644 // Get names for named interceptor properties if any.
645 if (this.hasNamedInterceptor() && (kind & PropertyKind.Named)) {
646 var namedInterceptorNames =
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000647 %GetNamedInterceptorPropertyNames(this.value_);
ager@chromium.org32912102009-01-16 10:38:43 +0000648 if (namedInterceptorNames) {
649 propertyNames = propertyNames.concat(namedInterceptorNames);
650 total += namedInterceptorNames.length;
651 }
652 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000653 }
ager@chromium.org32912102009-01-16 10:38:43 +0000654
655 // Find all the indexed properties.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000656 if (kind & PropertyKind.Indexed) {
ager@chromium.org32912102009-01-16 10:38:43 +0000657 // Get the local element names.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000658 elementNames = %GetLocalElementNames(this.value_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000659 total += elementNames.length;
ager@chromium.org32912102009-01-16 10:38:43 +0000660
661 // Get names for indexed interceptor properties.
662 if (this.hasIndexedInterceptor() && (kind & PropertyKind.Indexed)) {
663 var indexedInterceptorNames =
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000664 %GetIndexedInterceptorElementNames(this.value_);
ager@chromium.org32912102009-01-16 10:38:43 +0000665 if (indexedInterceptorNames) {
666 elementNames = elementNames.concat(indexedInterceptorNames);
667 total += indexedInterceptorNames.length;
668 }
669 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000670 }
671 limit = Math.min(limit || total, total);
672
673 var names = new Array(limit);
674 var index = 0;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000675
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000676 // Copy names for named properties.
677 if (kind & PropertyKind.Named) {
678 for (var i = 0; index < limit && i < propertyNames.length; i++) {
679 names[index++] = propertyNames[i];
680 }
681 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000682
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000683 // Copy names for indexed properties.
684 if (kind & PropertyKind.Indexed) {
685 for (var i = 0; index < limit && i < elementNames.length; i++) {
686 names[index++] = elementNames[i];
687 }
688 }
689
690 return names;
691};
692
693
694/**
695 * Return the properties for this object as an array of PropertyMirror objects.
696 * @param {number} kind Indicate whether named, indexed or both kinds of
697 * properties are requested
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +0000698 * @param {number} limit Limit the number of properties returned to the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000699 specified value
700 * @return {Array} Property mirrors for this object
701 */
702ObjectMirror.prototype.properties = function(kind, limit) {
703 var names = this.propertyNames(kind, limit);
704 var properties = new Array(names.length);
705 for (var i = 0; i < names.length; i++) {
706 properties[i] = this.property(names[i]);
707 }
708
709 return properties;
710};
711
712
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +0000713/**
714 * Return the internal properties for this object as an array of
715 * InternalPropertyMirror objects.
716 * @return {Array} Property mirrors for this object
717 */
718ObjectMirror.prototype.internalProperties = function() {
719 return ObjectMirror.GetInternalProperties(this.value_);
720}
721
722
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000723ObjectMirror.prototype.property = function(name) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000724 var details = %DebugGetPropertyDetails(this.value_, %ToString(name));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000725 if (details) {
ager@chromium.org32912102009-01-16 10:38:43 +0000726 return new PropertyMirror(this, name, details);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000727 }
728
729 // Nothing found.
ager@chromium.org32912102009-01-16 10:38:43 +0000730 return GetUndefinedMirror();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000731};
732
733
734
735/**
736 * Try to find a property from its value.
737 * @param {Mirror} value The property value to look for
738 * @return {PropertyMirror} The property with the specified value. If no
739 * property was found with the specified value UndefinedMirror is returned
740 */
741ObjectMirror.prototype.lookupProperty = function(value) {
742 var properties = this.properties();
743
744 // Look for property value in properties.
745 for (var i = 0; i < properties.length; i++) {
746
747 // Skip properties which are defined through assessors.
748 var property = properties[i];
749 if (property.propertyType() != PropertyType.Callbacks) {
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000750 if (%_ObjectEquals(property.value_, value.value_)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000751 return property;
752 }
753 }
754 }
755
756 // Nothing found.
ager@chromium.org32912102009-01-16 10:38:43 +0000757 return GetUndefinedMirror();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000758};
759
760
761/**
762 * Returns objects which has direct references to this object
iposva@chromium.org245aa852009-02-10 00:49:54 +0000763 * @param {number} opt_max_objects Optional parameter specifying the maximum
764 * number of referencing objects to return.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000765 * @return {Array} The objects which has direct references to this object.
766 */
iposva@chromium.org245aa852009-02-10 00:49:54 +0000767ObjectMirror.prototype.referencedBy = function(opt_max_objects) {
768 // Find all objects with direct references to this object.
769 var result = %DebugReferencedBy(this.value_,
770 Mirror.prototype, opt_max_objects || 0);
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000771
iposva@chromium.org245aa852009-02-10 00:49:54 +0000772 // Make mirrors for all the references found.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000773 for (var i = 0; i < result.length; i++) {
774 result[i] = MakeMirror(result[i]);
775 }
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000776
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000777 return result;
778};
779
780
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000781ObjectMirror.prototype.toText = function() {
782 var name;
783 var ctor = this.constructorFunction();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000784 if (!ctor.isFunction()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000785 name = this.className();
786 } else {
787 name = ctor.name();
788 if (!name) {
789 name = this.className();
790 }
791 }
whesse@chromium.org7a392b32011-01-31 11:30:36 +0000792 return '#<' + name + '>';
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000793};
794
795
796/**
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +0000797 * Return the internal properties of the value, such as [[PrimitiveValue]] of
798 * scalar wrapper objects and properties of the bound function.
799 * This method is done static to be accessible from Debug API with the bare
800 * values without mirrors.
801 * @return {Array} array (possibly empty) of InternalProperty instances
802 */
803ObjectMirror.GetInternalProperties = function(value) {
804 if (IS_STRING_WRAPPER(value) || IS_NUMBER_WRAPPER(value) ||
805 IS_BOOLEAN_WRAPPER(value)) {
806 var primitiveValue = %_ValueOf(value);
807 return [new InternalPropertyMirror("[[PrimitiveValue]]", primitiveValue)];
808 } else if (IS_FUNCTION(value)) {
809 var bindings = %BoundFunctionGetBindings(value);
810 var result = [];
811 if (bindings && IS_ARRAY(bindings)) {
812 result.push(new InternalPropertyMirror("[[TargetFunction]]",
813 bindings[0]));
814 result.push(new InternalPropertyMirror("[[BoundThis]]", bindings[1]));
815 var boundArgs = [];
816 for (var i = 2; i < bindings.length; i++) {
817 boundArgs.push(bindings[i]);
818 }
819 result.push(new InternalPropertyMirror("[[BoundArgs]]", boundArgs));
820 }
821 return result;
822 }
823 return [];
824}
825
826
827/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000828 * Mirror object for functions.
829 * @param {function} value The function object reflected by this mirror.
830 * @constructor
831 * @extends ObjectMirror
832 */
833function FunctionMirror(value) {
whesse@chromium.org7a392b32011-01-31 11:30:36 +0000834 %_CallFunction(this, value, FUNCTION_TYPE, ObjectMirror);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000835 this.resolved_ = true;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000836}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000837inherits(FunctionMirror, ObjectMirror);
838
839
840/**
841 * Returns whether the function is resolved.
842 * @return {boolean} True if the function is resolved. Unresolved functions can
843 * only originate as functions from stack frames
844 */
845FunctionMirror.prototype.resolved = function() {
846 return this.resolved_;
847};
848
849
850/**
851 * Returns the name of the function.
852 * @return {string} Name of the function
853 */
854FunctionMirror.prototype.name = function() {
855 return %FunctionGetName(this.value_);
856};
857
858
859/**
ager@chromium.org9085a012009-05-11 19:22:57 +0000860 * Returns the inferred name of the function.
861 * @return {string} Name of the function
862 */
863FunctionMirror.prototype.inferredName = function() {
864 return %FunctionGetInferredName(this.value_);
865};
866
867
868/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000869 * Returns the source code for the function.
870 * @return {string or undefined} The source code for the function. If the
871 * function is not resolved undefined will be returned.
872 */
873FunctionMirror.prototype.source = function() {
874 // Return source if function is resolved. Otherwise just fall through to
875 // return undefined.
876 if (this.resolved()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000877 return builtins.FunctionSourceString(this.value_);
878 }
879};
880
881
882/**
883 * Returns the script object for the function.
884 * @return {ScriptMirror or undefined} Script object for the function or
885 * undefined if the function has no script
886 */
887FunctionMirror.prototype.script = function() {
888 // Return script if function is resolved. Otherwise just fall through
889 // to return undefined.
890 if (this.resolved()) {
891 var script = %FunctionGetScript(this.value_);
892 if (script) {
iposva@chromium.org245aa852009-02-10 00:49:54 +0000893 return MakeMirror(script);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000894 }
895 }
896};
897
898
899/**
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000900 * Returns the script source position for the function. Only makes sense
901 * for functions which has a script defined.
902 * @return {Number or undefined} in-script position for the function
903 */
904FunctionMirror.prototype.sourcePosition_ = function() {
905 // Return script if function is resolved. Otherwise just fall through
906 // to return undefined.
907 if (this.resolved()) {
908 return %FunctionGetScriptSourcePosition(this.value_);
909 }
910};
911
912
913/**
914 * Returns the script source location object for the function. Only makes sense
915 * for functions which has a script defined.
916 * @return {Location or undefined} in-script location for the function begin
917 */
918FunctionMirror.prototype.sourceLocation = function() {
919 if (this.resolved() && this.script()) {
920 return this.script().locationFromPosition(this.sourcePosition_(),
921 true);
922 }
923};
924
925
926/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000927 * Returns objects constructed by this function.
928 * @param {number} opt_max_instances Optional parameter specifying the maximum
929 * number of instances to return.
930 * @return {Array or undefined} The objects constructed by this function.
931 */
932FunctionMirror.prototype.constructedBy = function(opt_max_instances) {
933 if (this.resolved()) {
934 // Find all objects constructed from this function.
935 var result = %DebugConstructedBy(this.value_, opt_max_instances || 0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000936
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000937 // Make mirrors for all the instances found.
938 for (var i = 0; i < result.length; i++) {
939 result[i] = MakeMirror(result[i]);
940 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000941
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000942 return result;
943 } else {
944 return [];
945 }
946};
947
948
danno@chromium.org1044a4d2012-04-30 12:34:39 +0000949FunctionMirror.prototype.scopeCount = function() {
950 if (this.resolved()) {
951 return %GetFunctionScopeCount(this.value());
952 } else {
953 return 0;
954 }
955};
956
957
958FunctionMirror.prototype.scope = function(index) {
959 if (this.resolved()) {
960 return new ScopeMirror(void 0, this, index);
961 }
962};
963
964
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000965FunctionMirror.prototype.toText = function() {
966 return this.source();
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000967};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000968
969
970/**
971 * Mirror object for unresolved functions.
972 * @param {string} value The name for the unresolved function reflected by this
973 * mirror.
974 * @constructor
975 * @extends ObjectMirror
976 */
977function UnresolvedFunctionMirror(value) {
978 // Construct this using the ValueMirror as an unresolved function is not a
979 // real object but just a string.
whesse@chromium.org7a392b32011-01-31 11:30:36 +0000980 %_CallFunction(this, FUNCTION_TYPE, value, ValueMirror);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000981 this.propertyCount_ = 0;
982 this.elementCount_ = 0;
983 this.resolved_ = false;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000984}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000985inherits(UnresolvedFunctionMirror, FunctionMirror);
986
987
988UnresolvedFunctionMirror.prototype.className = function() {
989 return 'Function';
990};
991
992
993UnresolvedFunctionMirror.prototype.constructorFunction = function() {
ager@chromium.org32912102009-01-16 10:38:43 +0000994 return GetUndefinedMirror();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000995};
996
997
998UnresolvedFunctionMirror.prototype.prototypeObject = function() {
ager@chromium.org32912102009-01-16 10:38:43 +0000999 return GetUndefinedMirror();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001000};
1001
1002
1003UnresolvedFunctionMirror.prototype.protoObject = function() {
ager@chromium.org32912102009-01-16 10:38:43 +00001004 return GetUndefinedMirror();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001005};
1006
1007
1008UnresolvedFunctionMirror.prototype.name = function() {
1009 return this.value_;
1010};
1011
1012
ager@chromium.org9085a012009-05-11 19:22:57 +00001013UnresolvedFunctionMirror.prototype.inferredName = function() {
1014 return undefined;
1015};
1016
1017
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001018UnresolvedFunctionMirror.prototype.propertyNames = function(kind, limit) {
1019 return [];
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001020};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001021
1022
1023/**
1024 * Mirror object for arrays.
1025 * @param {Array} value The Array object reflected by this mirror
1026 * @constructor
1027 * @extends ObjectMirror
1028 */
1029function ArrayMirror(value) {
whesse@chromium.org7a392b32011-01-31 11:30:36 +00001030 %_CallFunction(this, value, ObjectMirror);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001031}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001032inherits(ArrayMirror, ObjectMirror);
1033
1034
1035ArrayMirror.prototype.length = function() {
1036 return this.value_.length;
1037};
1038
1039
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001040ArrayMirror.prototype.indexedPropertiesFromRange = function(opt_from_index,
1041 opt_to_index) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001042 var from_index = opt_from_index || 0;
1043 var to_index = opt_to_index || this.length() - 1;
1044 if (from_index > to_index) return new Array();
1045 var values = new Array(to_index - from_index + 1);
1046 for (var i = from_index; i <= to_index; i++) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001047 var details = %DebugGetPropertyDetails(this.value_, %ToString(i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001048 var value;
1049 if (details) {
ager@chromium.org32912102009-01-16 10:38:43 +00001050 value = new PropertyMirror(this, i, details);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001051 } else {
ager@chromium.org32912102009-01-16 10:38:43 +00001052 value = GetUndefinedMirror();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001053 }
1054 values[i - from_index] = value;
1055 }
1056 return values;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001057};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001058
1059
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001060/**
1061 * Mirror object for dates.
1062 * @param {Date} value The Date object reflected by this mirror
1063 * @constructor
1064 * @extends ObjectMirror
1065 */
1066function DateMirror(value) {
whesse@chromium.org7a392b32011-01-31 11:30:36 +00001067 %_CallFunction(this, value, ObjectMirror);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001068}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001069inherits(DateMirror, ObjectMirror);
1070
1071
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001072DateMirror.prototype.toText = function() {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00001073 var s = JSON.stringify(this.value_);
1074 return s.substring(1, s.length - 1); // cut quotes
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001075};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001076
1077
1078/**
1079 * Mirror object for regular expressions.
1080 * @param {RegExp} value The RegExp object reflected by this mirror
1081 * @constructor
1082 * @extends ObjectMirror
1083 */
1084function RegExpMirror(value) {
whesse@chromium.org7a392b32011-01-31 11:30:36 +00001085 %_CallFunction(this, value, REGEXP_TYPE, ObjectMirror);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001086}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001087inherits(RegExpMirror, ObjectMirror);
1088
1089
1090/**
1091 * Returns the source to the regular expression.
1092 * @return {string or undefined} The source to the regular expression
1093 */
1094RegExpMirror.prototype.source = function() {
1095 return this.value_.source;
1096};
1097
1098
1099/**
1100 * Returns whether this regular expression has the global (g) flag set.
1101 * @return {boolean} Value of the global flag
1102 */
1103RegExpMirror.prototype.global = function() {
1104 return this.value_.global;
1105};
1106
1107
1108/**
1109 * Returns whether this regular expression has the ignore case (i) flag set.
1110 * @return {boolean} Value of the ignore case flag
1111 */
1112RegExpMirror.prototype.ignoreCase = function() {
1113 return this.value_.ignoreCase;
1114};
1115
1116
1117/**
1118 * Returns whether this regular expression has the multiline (m) flag set.
1119 * @return {boolean} Value of the multiline flag
1120 */
1121RegExpMirror.prototype.multiline = function() {
1122 return this.value_.multiline;
1123};
1124
1125
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001126RegExpMirror.prototype.toText = function() {
1127 // Simpel to text which is used when on specialization in subclass.
1128 return "/" + this.source() + "/";
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001129};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001130
1131
1132/**
1133 * Mirror object for error objects.
1134 * @param {Error} value The error object reflected by this mirror
1135 * @constructor
1136 * @extends ObjectMirror
1137 */
1138function ErrorMirror(value) {
whesse@chromium.org7a392b32011-01-31 11:30:36 +00001139 %_CallFunction(this, value, ERROR_TYPE, ObjectMirror);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001140}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001141inherits(ErrorMirror, ObjectMirror);
1142
1143
1144/**
1145 * Returns the message for this eror object.
1146 * @return {string or undefined} The message for this eror object
1147 */
1148ErrorMirror.prototype.message = function() {
1149 return this.value_.message;
1150};
1151
1152
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001153ErrorMirror.prototype.toText = function() {
1154 // Use the same text representation as in messages.js.
1155 var text;
1156 try {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001157 str = %_CallFunction(this.value_, builtins.ErrorToString);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001158 } catch (e) {
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00001159 str = '#<Error>';
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001160 }
1161 return str;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001162};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001163
1164
1165/**
1166 * Base mirror object for properties.
1167 * @param {ObjectMirror} mirror The mirror object having this property
1168 * @param {string} name The name of the property
ager@chromium.org32912102009-01-16 10:38:43 +00001169 * @param {Array} details Details about the property
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001170 * @constructor
1171 * @extends Mirror
1172 */
ager@chromium.org32912102009-01-16 10:38:43 +00001173function PropertyMirror(mirror, name, details) {
whesse@chromium.org7a392b32011-01-31 11:30:36 +00001174 %_CallFunction(this, PROPERTY_TYPE, Mirror);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001175 this.mirror_ = mirror;
1176 this.name_ = name;
ager@chromium.org32912102009-01-16 10:38:43 +00001177 this.value_ = details[0];
1178 this.details_ = details[1];
1179 if (details.length > 2) {
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001180 this.exception_ = details[2];
ager@chromium.org32912102009-01-16 10:38:43 +00001181 this.getter_ = details[3];
1182 this.setter_ = details[4];
1183 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001184}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001185inherits(PropertyMirror, Mirror);
1186
1187
1188PropertyMirror.prototype.isReadOnly = function() {
1189 return (this.attributes() & PropertyAttribute.ReadOnly) != 0;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001190};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001191
1192
1193PropertyMirror.prototype.isEnum = function() {
1194 return (this.attributes() & PropertyAttribute.DontEnum) == 0;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001195};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001196
1197
1198PropertyMirror.prototype.canDelete = function() {
1199 return (this.attributes() & PropertyAttribute.DontDelete) == 0;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001200};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001201
1202
1203PropertyMirror.prototype.name = function() {
1204 return this.name_;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001205};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001206
1207
1208PropertyMirror.prototype.isIndexed = function() {
1209 for (var i = 0; i < this.name_.length; i++) {
1210 if (this.name_[i] < '0' || '9' < this.name_[i]) {
1211 return false;
1212 }
1213 }
1214 return true;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001215};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001216
1217
1218PropertyMirror.prototype.value = function() {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001219 return MakeMirror(this.value_, false);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001220};
ager@chromium.org32912102009-01-16 10:38:43 +00001221
1222
1223/**
1224 * Returns whether this property value is an exception.
1225 * @return {booolean} True if this property value is an exception
1226 */
1227PropertyMirror.prototype.isException = function() {
1228 return this.exception_ ? true : false;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001229};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001230
1231
1232PropertyMirror.prototype.attributes = function() {
1233 return %DebugPropertyAttributesFromDetails(this.details_);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001234};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001235
1236
1237PropertyMirror.prototype.propertyType = function() {
1238 return %DebugPropertyTypeFromDetails(this.details_);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001239};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001240
1241
1242PropertyMirror.prototype.insertionIndex = function() {
1243 return %DebugPropertyIndexFromDetails(this.details_);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001244};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001245
1246
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001247/**
ager@chromium.org32912102009-01-16 10:38:43 +00001248 * Returns whether this property has a getter defined through __defineGetter__.
1249 * @return {booolean} True if this property has a getter
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001250 */
ager@chromium.org32912102009-01-16 10:38:43 +00001251PropertyMirror.prototype.hasGetter = function() {
1252 return this.getter_ ? true : false;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001253};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001254
1255
1256/**
ager@chromium.org32912102009-01-16 10:38:43 +00001257 * Returns whether this property has a setter defined through __defineSetter__.
1258 * @return {booolean} True if this property has a setter
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001259 */
ager@chromium.org32912102009-01-16 10:38:43 +00001260PropertyMirror.prototype.hasSetter = function() {
1261 return this.setter_ ? true : false;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001262};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001263
1264
1265/**
ager@chromium.org32912102009-01-16 10:38:43 +00001266 * Returns the getter for this property defined through __defineGetter__.
1267 * @return {Mirror} FunctionMirror reflecting the getter function or
1268 * UndefinedMirror if there is no getter for this property
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001269 */
ager@chromium.org32912102009-01-16 10:38:43 +00001270PropertyMirror.prototype.getter = function() {
1271 if (this.hasGetter()) {
1272 return MakeMirror(this.getter_);
1273 } else {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001274 return GetUndefinedMirror();
ager@chromium.org32912102009-01-16 10:38:43 +00001275 }
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001276};
ager@chromium.org32912102009-01-16 10:38:43 +00001277
1278
1279/**
1280 * Returns the setter for this property defined through __defineSetter__.
1281 * @return {Mirror} FunctionMirror reflecting the setter function or
1282 * UndefinedMirror if there is no setter for this property
1283 */
1284PropertyMirror.prototype.setter = function() {
1285 if (this.hasSetter()) {
1286 return MakeMirror(this.setter_);
1287 } else {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001288 return GetUndefinedMirror();
ager@chromium.org32912102009-01-16 10:38:43 +00001289 }
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001290};
ager@chromium.org32912102009-01-16 10:38:43 +00001291
1292
1293/**
1294 * Returns whether this property is natively implemented by the host or a set
1295 * through JavaScript code.
lrn@chromium.org25156de2010-04-06 13:10:27 +00001296 * @return {boolean} True if the property is
ager@chromium.org32912102009-01-16 10:38:43 +00001297 * UndefinedMirror if there is no setter for this property
1298 */
1299PropertyMirror.prototype.isNative = function() {
1300 return (this.propertyType() == PropertyType.Interceptor) ||
1301 ((this.propertyType() == PropertyType.Callbacks) &&
1302 !this.hasGetter() && !this.hasSetter());
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001303};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001304
1305
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00001306/**
1307 * Mirror object for internal properties. Internal property reflects properties
1308 * not accessible from user code such as [[BoundThis]] in bound function.
1309 * Their names are merely symbolic.
1310 * @param {string} name The name of the property
1311 * @param {value} property value
1312 * @constructor
1313 * @extends Mirror
1314 */
1315function InternalPropertyMirror(name, value) {
1316 %_CallFunction(this, INTERNAL_PROPERTY_TYPE, Mirror);
1317 this.name_ = name;
1318 this.value_ = value;
1319}
1320inherits(InternalPropertyMirror, Mirror);
1321
1322
1323InternalPropertyMirror.prototype.name = function() {
1324 return this.name_;
1325};
1326
1327
1328InternalPropertyMirror.prototype.value = function() {
1329 return MakeMirror(this.value_, false);
1330};
1331
1332
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00001333var kFrameDetailsFrameIdIndex = 0;
1334var kFrameDetailsReceiverIndex = 1;
1335var kFrameDetailsFunctionIndex = 2;
1336var kFrameDetailsArgumentCountIndex = 3;
1337var kFrameDetailsLocalCountIndex = 4;
1338var kFrameDetailsSourcePositionIndex = 5;
1339var kFrameDetailsConstructCallIndex = 6;
1340var kFrameDetailsAtReturnIndex = 7;
1341var kFrameDetailsFlagsIndex = 8;
1342var kFrameDetailsFirstDynamicIndex = 9;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001343
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00001344var kFrameDetailsNameIndex = 0;
1345var kFrameDetailsValueIndex = 1;
1346var kFrameDetailsNameValueSize = 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001347
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00001348var kFrameDetailsFlagDebuggerFrameMask = 1 << 0;
1349var kFrameDetailsFlagOptimizedFrameMask = 1 << 1;
1350var kFrameDetailsFlagInlinedFrameIndexMask = 7 << 2;
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001351
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001352/**
1353 * Wrapper for the frame details information retreived from the VM. The frame
1354 * details from the VM is an array with the following content. See runtime.cc
1355 * Runtime_GetFrameDetails.
1356 * 0: Id
1357 * 1: Receiver
1358 * 2: Function
1359 * 3: Argument count
1360 * 4: Local count
1361 * 5: Source position
1362 * 6: Construct call
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001363 * 7: Is at return
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001364 * 8: Flags (debugger frame, optimized frame, inlined frame index)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001365 * Arguments name, value
1366 * Locals name, value
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001367 * Return value if any
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001368 * @param {number} break_id Current break id
1369 * @param {number} index Frame number
1370 * @constructor
1371 */
1372function FrameDetails(break_id, index) {
1373 this.break_id_ = break_id;
1374 this.details_ = %GetFrameDetails(break_id, index);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001375}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001376
1377
1378FrameDetails.prototype.frameId = function() {
1379 %CheckExecutionState(this.break_id_);
1380 return this.details_[kFrameDetailsFrameIdIndex];
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001381};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001382
1383
1384FrameDetails.prototype.receiver = function() {
1385 %CheckExecutionState(this.break_id_);
1386 return this.details_[kFrameDetailsReceiverIndex];
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001387};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001388
1389
1390FrameDetails.prototype.func = function() {
1391 %CheckExecutionState(this.break_id_);
1392 return this.details_[kFrameDetailsFunctionIndex];
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001393};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001394
1395
1396FrameDetails.prototype.isConstructCall = function() {
1397 %CheckExecutionState(this.break_id_);
1398 return this.details_[kFrameDetailsConstructCallIndex];
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001399};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001400
1401
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001402FrameDetails.prototype.isAtReturn = function() {
1403 %CheckExecutionState(this.break_id_);
1404 return this.details_[kFrameDetailsAtReturnIndex];
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001405};
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001406
1407
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001408FrameDetails.prototype.isDebuggerFrame = function() {
1409 %CheckExecutionState(this.break_id_);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001410 var f = kFrameDetailsFlagDebuggerFrameMask;
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001411 return (this.details_[kFrameDetailsFlagsIndex] & f) == f;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001412};
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001413
1414
1415FrameDetails.prototype.isOptimizedFrame = function() {
1416 %CheckExecutionState(this.break_id_);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001417 var f = kFrameDetailsFlagOptimizedFrameMask;
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001418 return (this.details_[kFrameDetailsFlagsIndex] & f) == f;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001419};
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001420
1421
1422FrameDetails.prototype.isInlinedFrame = function() {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001423 return this.inlinedFrameIndex() > 0;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001424};
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001425
1426
1427FrameDetails.prototype.inlinedFrameIndex = function() {
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001428 %CheckExecutionState(this.break_id_);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001429 var f = kFrameDetailsFlagInlinedFrameIndexMask;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001430 return (this.details_[kFrameDetailsFlagsIndex] & f) >> 2;
1431};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001432
1433
1434FrameDetails.prototype.argumentCount = function() {
1435 %CheckExecutionState(this.break_id_);
1436 return this.details_[kFrameDetailsArgumentCountIndex];
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001437};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001438
1439
1440FrameDetails.prototype.argumentName = function(index) {
1441 %CheckExecutionState(this.break_id_);
1442 if (index >= 0 && index < this.argumentCount()) {
1443 return this.details_[kFrameDetailsFirstDynamicIndex +
1444 index * kFrameDetailsNameValueSize +
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001445 kFrameDetailsNameIndex];
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001446 }
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001447};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001448
1449
1450FrameDetails.prototype.argumentValue = function(index) {
1451 %CheckExecutionState(this.break_id_);
1452 if (index >= 0 && index < this.argumentCount()) {
1453 return this.details_[kFrameDetailsFirstDynamicIndex +
1454 index * kFrameDetailsNameValueSize +
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001455 kFrameDetailsValueIndex];
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001456 }
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001457};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001458
1459
1460FrameDetails.prototype.localCount = function() {
1461 %CheckExecutionState(this.break_id_);
1462 return this.details_[kFrameDetailsLocalCountIndex];
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001463};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001464
1465
1466FrameDetails.prototype.sourcePosition = function() {
1467 %CheckExecutionState(this.break_id_);
1468 return this.details_[kFrameDetailsSourcePositionIndex];
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001469};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001470
1471
1472FrameDetails.prototype.localName = function(index) {
1473 %CheckExecutionState(this.break_id_);
1474 if (index >= 0 && index < this.localCount()) {
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001475 var locals_offset = kFrameDetailsFirstDynamicIndex +
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001476 this.argumentCount() * kFrameDetailsNameValueSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001477 return this.details_[locals_offset +
1478 index * kFrameDetailsNameValueSize +
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001479 kFrameDetailsNameIndex];
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001480 }
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001481};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001482
1483
1484FrameDetails.prototype.localValue = function(index) {
1485 %CheckExecutionState(this.break_id_);
1486 if (index >= 0 && index < this.localCount()) {
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001487 var locals_offset = kFrameDetailsFirstDynamicIndex +
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001488 this.argumentCount() * kFrameDetailsNameValueSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001489 return this.details_[locals_offset +
1490 index * kFrameDetailsNameValueSize +
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001491 kFrameDetailsValueIndex];
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001492 }
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001493};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001494
1495
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001496FrameDetails.prototype.returnValue = function() {
1497 %CheckExecutionState(this.break_id_);
1498 var return_value_offset =
1499 kFrameDetailsFirstDynamicIndex +
1500 (this.argumentCount() + this.localCount()) * kFrameDetailsNameValueSize;
1501 if (this.details_[kFrameDetailsAtReturnIndex]) {
1502 return this.details_[return_value_offset];
1503 }
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001504};
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001505
1506
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001507FrameDetails.prototype.scopeCount = function() {
1508 return %GetScopeCount(this.break_id_, this.frameId());
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001509};
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001510
1511
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001512/**
1513 * Mirror object for stack frames.
1514 * @param {number} break_id The break id in the VM for which this frame is
1515 valid
1516 * @param {number} index The frame index (top frame is index 0)
1517 * @constructor
1518 * @extends Mirror
1519 */
1520function FrameMirror(break_id, index) {
whesse@chromium.org7a392b32011-01-31 11:30:36 +00001521 %_CallFunction(this, FRAME_TYPE, Mirror);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001522 this.break_id_ = break_id;
1523 this.index_ = index;
1524 this.details_ = new FrameDetails(break_id, index);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001525}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001526inherits(FrameMirror, Mirror);
1527
1528
1529FrameMirror.prototype.index = function() {
1530 return this.index_;
1531};
1532
1533
1534FrameMirror.prototype.func = function() {
1535 // Get the function for this frame from the VM.
1536 var f = this.details_.func();
lrn@chromium.org25156de2010-04-06 13:10:27 +00001537
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001538 // Create a function mirror. NOTE: MakeMirror cannot be used here as the
1539 // value returned from the VM might be a string if the function for the
1540 // frame is unresolved.
1541 if (IS_FUNCTION(f)) {
ager@chromium.org32912102009-01-16 10:38:43 +00001542 return MakeMirror(f);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001543 } else {
1544 return new UnresolvedFunctionMirror(f);
1545 }
1546};
1547
1548
1549FrameMirror.prototype.receiver = function() {
1550 return MakeMirror(this.details_.receiver());
1551};
1552
1553
1554FrameMirror.prototype.isConstructCall = function() {
1555 return this.details_.isConstructCall();
1556};
1557
1558
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001559FrameMirror.prototype.isAtReturn = function() {
1560 return this.details_.isAtReturn();
1561};
1562
1563
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001564FrameMirror.prototype.isDebuggerFrame = function() {
1565 return this.details_.isDebuggerFrame();
1566};
1567
1568
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001569FrameMirror.prototype.isOptimizedFrame = function() {
1570 return this.details_.isOptimizedFrame();
1571};
1572
1573
1574FrameMirror.prototype.isInlinedFrame = function() {
1575 return this.details_.isInlinedFrame();
1576};
1577
1578
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001579FrameMirror.prototype.inlinedFrameIndex = function() {
1580 return this.details_.inlinedFrameIndex();
1581};
1582
1583
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001584FrameMirror.prototype.argumentCount = function() {
1585 return this.details_.argumentCount();
1586};
1587
1588
1589FrameMirror.prototype.argumentName = function(index) {
1590 return this.details_.argumentName(index);
1591};
1592
1593
1594FrameMirror.prototype.argumentValue = function(index) {
1595 return MakeMirror(this.details_.argumentValue(index));
1596};
1597
1598
1599FrameMirror.prototype.localCount = function() {
1600 return this.details_.localCount();
1601};
1602
1603
1604FrameMirror.prototype.localName = function(index) {
1605 return this.details_.localName(index);
1606};
1607
1608
1609FrameMirror.prototype.localValue = function(index) {
1610 return MakeMirror(this.details_.localValue(index));
1611};
1612
1613
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001614FrameMirror.prototype.returnValue = function() {
1615 return MakeMirror(this.details_.returnValue());
1616};
1617
1618
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001619FrameMirror.prototype.sourcePosition = function() {
1620 return this.details_.sourcePosition();
1621};
1622
1623
1624FrameMirror.prototype.sourceLocation = function() {
1625 if (this.func().resolved() && this.func().script()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001626 return this.func().script().locationFromPosition(this.sourcePosition(),
1627 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001628 }
1629};
1630
1631
1632FrameMirror.prototype.sourceLine = function() {
1633 if (this.func().resolved()) {
1634 var location = this.sourceLocation();
1635 if (location) {
1636 return location.line;
1637 }
1638 }
1639};
1640
1641
1642FrameMirror.prototype.sourceColumn = function() {
1643 if (this.func().resolved()) {
1644 var location = this.sourceLocation();
1645 if (location) {
1646 return location.column;
1647 }
1648 }
1649};
1650
1651
1652FrameMirror.prototype.sourceLineText = function() {
1653 if (this.func().resolved()) {
1654 var location = this.sourceLocation();
1655 if (location) {
1656 return location.sourceText();
1657 }
1658 }
1659};
1660
1661
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001662FrameMirror.prototype.scopeCount = function() {
1663 return this.details_.scopeCount();
1664};
1665
1666
1667FrameMirror.prototype.scope = function(index) {
danno@chromium.org1044a4d2012-04-30 12:34:39 +00001668 return new ScopeMirror(this, void 0, index);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001669};
1670
1671
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001672FrameMirror.prototype.evaluate = function(source, disable_break,
1673 opt_context_object) {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001674 var result = %DebugEvaluate(this.break_id_,
1675 this.details_.frameId(),
1676 this.details_.inlinedFrameIndex(),
1677 source,
1678 Boolean(disable_break),
1679 opt_context_object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001680 return MakeMirror(result);
1681};
1682
1683
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001684FrameMirror.prototype.invocationText = function() {
1685 // Format frame invoaction (receiver, function and arguments).
1686 var result = '';
1687 var func = this.func();
1688 var receiver = this.receiver();
1689 if (this.isConstructCall()) {
1690 // For constructor frames display new followed by the function name.
1691 result += 'new ';
1692 result += func.name() ? func.name() : '[anonymous]';
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001693 } else if (this.isDebuggerFrame()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001694 result += '[debugger]';
1695 } else {
1696 // If the receiver has a className which is 'global' don't display it.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001697 var display_receiver =
1698 !receiver.className || (receiver.className() != 'global');
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001699 if (display_receiver) {
1700 result += receiver.toText();
1701 }
1702 // Try to find the function as a property in the receiver. Include the
1703 // prototype chain in the lookup.
ager@chromium.org32912102009-01-16 10:38:43 +00001704 var property = GetUndefinedMirror();
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001705 if (receiver.isObject()) {
1706 for (var r = receiver;
1707 !r.isNull() && property.isUndefined();
1708 r = r.protoObject()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001709 property = r.lookupProperty(func);
1710 }
1711 }
1712 if (!property.isUndefined()) {
1713 // The function invoked was found on the receiver. Use the property name
1714 // for the backtrace.
1715 if (!property.isIndexed()) {
1716 if (display_receiver) {
1717 result += '.';
1718 }
1719 result += property.name();
1720 } else {
1721 result += '[';
1722 result += property.name();
1723 result += ']';
1724 }
1725 // Also known as - if the name in the function doesn't match the name
1726 // under which it was looked up.
1727 if (func.name() && func.name() != property.name()) {
1728 result += '(aka ' + func.name() + ')';
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001729 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001730 } else {
1731 // The function invoked was not found on the receiver. Use the function
1732 // name if available for the backtrace.
1733 if (display_receiver) {
1734 result += '.';
1735 }
1736 result += func.name() ? func.name() : '[anonymous]';
1737 }
1738 }
1739
1740 // Render arguments for normal frames.
1741 if (!this.isDebuggerFrame()) {
1742 result += '(';
1743 for (var i = 0; i < this.argumentCount(); i++) {
1744 if (i != 0) result += ', ';
1745 if (this.argumentName(i)) {
1746 result += this.argumentName(i);
1747 result += '=';
1748 }
1749 result += this.argumentValue(i).toText();
1750 }
1751 result += ')';
1752 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001753
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001754 if (this.isAtReturn()) {
1755 result += ' returning ';
1756 result += this.returnValue().toText();
1757 }
vegorov@chromium.org42841962010-10-18 11:18:59 +00001758
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001759 return result;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001760};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001761
1762
1763FrameMirror.prototype.sourceAndPositionText = function() {
1764 // Format source and position.
1765 var result = '';
1766 var func = this.func();
1767 if (func.resolved()) {
1768 if (func.script()) {
1769 if (func.script().name()) {
1770 result += func.script().name();
1771 } else {
1772 result += '[unnamed]';
1773 }
1774 if (!this.isDebuggerFrame()) {
1775 var location = this.sourceLocation();
1776 result += ' line ';
1777 result += !IS_UNDEFINED(location) ? (location.line + 1) : '?';
1778 result += ' column ';
1779 result += !IS_UNDEFINED(location) ? (location.column + 1) : '?';
1780 if (!IS_UNDEFINED(this.sourcePosition())) {
1781 result += ' (position ' + (this.sourcePosition() + 1) + ')';
1782 }
1783 }
1784 } else {
1785 result += '[no source]';
1786 }
1787 } else {
1788 result += '[unresolved]';
1789 }
1790
1791 return result;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001792};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001793
1794
1795FrameMirror.prototype.localsText = function() {
1796 // Format local variables.
1797 var result = '';
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001798 var locals_count = this.localCount();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001799 if (locals_count > 0) {
1800 for (var i = 0; i < locals_count; ++i) {
1801 result += ' var ';
1802 result += this.localName(i);
1803 result += ' = ';
1804 result += this.localValue(i).toText();
1805 if (i < locals_count - 1) result += '\n';
1806 }
1807 }
1808
1809 return result;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001810};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001811
1812
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001813FrameMirror.prototype.restart = function() {
1814 var result = %LiveEditRestartFrame(this.break_id_, this.index_);
1815 if (IS_UNDEFINED(result)) {
1816 result = "Failed to find requested frame";
1817 }
1818 return result;
1819};
1820
1821
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001822FrameMirror.prototype.toText = function(opt_locals) {
1823 var result = '';
1824 result += '#' + (this.index() <= 9 ? '0' : '') + this.index();
1825 result += ' ';
1826 result += this.invocationText();
1827 result += ' ';
1828 result += this.sourceAndPositionText();
1829 if (opt_locals) {
1830 result += '\n';
1831 result += this.localsText();
1832 }
1833 return result;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001834};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001835
1836
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00001837var kScopeDetailsTypeIndex = 0;
1838var kScopeDetailsObjectIndex = 1;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001839
danno@chromium.org1044a4d2012-04-30 12:34:39 +00001840function ScopeDetails(frame, fun, index) {
1841 if (frame) {
1842 this.break_id_ = frame.break_id_;
1843 this.details_ = %GetScopeDetails(frame.break_id_,
1844 frame.details_.frameId(),
1845 frame.details_.inlinedFrameIndex(),
1846 index);
mmassi@chromium.org49a44672012-12-04 13:52:03 +00001847 this.frame_id_ = frame.details_.frameId();
1848 this.inlined_frame_id_ = frame.details_.inlinedFrameIndex();
danno@chromium.org1044a4d2012-04-30 12:34:39 +00001849 } else {
1850 this.details_ = %GetFunctionScopeDetails(fun.value(), index);
mmassi@chromium.org49a44672012-12-04 13:52:03 +00001851 this.fun_value_ = fun.value();
danno@chromium.org1044a4d2012-04-30 12:34:39 +00001852 this.break_id_ = undefined;
1853 }
mmassi@chromium.org49a44672012-12-04 13:52:03 +00001854 this.index_ = index;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001855}
1856
1857
1858ScopeDetails.prototype.type = function() {
danno@chromium.org1044a4d2012-04-30 12:34:39 +00001859 if (!IS_UNDEFINED(this.break_id_)) {
1860 %CheckExecutionState(this.break_id_);
1861 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001862 return this.details_[kScopeDetailsTypeIndex];
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001863};
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001864
1865
1866ScopeDetails.prototype.object = function() {
danno@chromium.org1044a4d2012-04-30 12:34:39 +00001867 if (!IS_UNDEFINED(this.break_id_)) {
1868 %CheckExecutionState(this.break_id_);
1869 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001870 return this.details_[kScopeDetailsObjectIndex];
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001871};
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001872
1873
mmassi@chromium.org49a44672012-12-04 13:52:03 +00001874ScopeDetails.prototype.setVariableValueImpl = function(name, new_value) {
1875 var raw_res;
1876 if (!IS_UNDEFINED(this.break_id_)) {
1877 %CheckExecutionState(this.break_id_);
1878 raw_res = %SetScopeVariableValue(this.break_id_, this.frame_id_,
1879 this.inlined_frame_id_, this.index_, name, new_value);
1880 } else {
1881 raw_res = %SetScopeVariableValue(this.fun_value_, null, null, this.index_,
1882 name, new_value);
1883 }
1884 if (!raw_res) {
1885 throw new Error("Failed to set variable value");
1886 }
1887};
1888
1889
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001890/**
danno@chromium.org1044a4d2012-04-30 12:34:39 +00001891 * Mirror object for scope of frame or function. Either frame or function must
1892 * be specified.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001893 * @param {FrameMirror} frame The frame this scope is a part of
danno@chromium.org1044a4d2012-04-30 12:34:39 +00001894 * @param {FunctionMirror} function The function this scope is a part of
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001895 * @param {number} index The scope index in the frame
1896 * @constructor
1897 * @extends Mirror
1898 */
danno@chromium.org1044a4d2012-04-30 12:34:39 +00001899function ScopeMirror(frame, function, index) {
whesse@chromium.org7a392b32011-01-31 11:30:36 +00001900 %_CallFunction(this, SCOPE_TYPE, Mirror);
danno@chromium.org1044a4d2012-04-30 12:34:39 +00001901 if (frame) {
1902 this.frame_index_ = frame.index_;
1903 } else {
1904 this.frame_index_ = undefined;
1905 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001906 this.scope_index_ = index;
danno@chromium.org1044a4d2012-04-30 12:34:39 +00001907 this.details_ = new ScopeDetails(frame, function, index);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001908}
1909inherits(ScopeMirror, Mirror);
1910
1911
1912ScopeMirror.prototype.frameIndex = function() {
1913 return this.frame_index_;
1914};
1915
1916
1917ScopeMirror.prototype.scopeIndex = function() {
1918 return this.scope_index_;
1919};
1920
1921
1922ScopeMirror.prototype.scopeType = function() {
1923 return this.details_.type();
1924};
1925
1926
1927ScopeMirror.prototype.scopeObject = function() {
1928 // For local and closure scopes create a transient mirror as these objects are
1929 // created on the fly materializing the local or closure scopes and
1930 // therefore will not preserve identity.
1931 var transient = this.scopeType() == ScopeType.Local ||
1932 this.scopeType() == ScopeType.Closure;
1933 return MakeMirror(this.details_.object(), transient);
1934};
1935
1936
mmassi@chromium.org49a44672012-12-04 13:52:03 +00001937ScopeMirror.prototype.setVariableValue = function(name, new_value) {
1938 this.details_.setVariableValueImpl(name, new_value);
1939};
1940
1941
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001942/**
1943 * Mirror object for script source.
1944 * @param {Script} script The script object
1945 * @constructor
1946 * @extends Mirror
1947 */
1948function ScriptMirror(script) {
whesse@chromium.org7a392b32011-01-31 11:30:36 +00001949 %_CallFunction(this, SCRIPT_TYPE, Mirror);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001950 this.script_ = script;
ager@chromium.org9085a012009-05-11 19:22:57 +00001951 this.context_ = new ContextMirror(script.context_data);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001952 this.allocateHandle_();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001953}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001954inherits(ScriptMirror, Mirror);
1955
1956
iposva@chromium.org245aa852009-02-10 00:49:54 +00001957ScriptMirror.prototype.value = function() {
1958 return this.script_;
1959};
1960
1961
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001962ScriptMirror.prototype.name = function() {
lrn@chromium.org25156de2010-04-06 13:10:27 +00001963 return this.script_.name || this.script_.nameOrSourceURL();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001964};
1965
1966
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001967ScriptMirror.prototype.id = function() {
1968 return this.script_.id;
1969};
1970
1971
iposva@chromium.org245aa852009-02-10 00:49:54 +00001972ScriptMirror.prototype.source = function() {
1973 return this.script_.source;
1974};
1975
1976
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001977ScriptMirror.prototype.setSource = function(source) {
1978 %DebugSetScriptSource(this.script_, source);
1979};
1980
1981
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001982ScriptMirror.prototype.lineOffset = function() {
1983 return this.script_.line_offset;
1984};
1985
1986
1987ScriptMirror.prototype.columnOffset = function() {
1988 return this.script_.column_offset;
1989};
1990
1991
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001992ScriptMirror.prototype.data = function() {
1993 return this.script_.data;
1994};
1995
1996
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001997ScriptMirror.prototype.scriptType = function() {
1998 return this.script_.type;
1999};
2000
2001
ager@chromium.orge2902be2009-06-08 12:21:35 +00002002ScriptMirror.prototype.compilationType = function() {
2003 return this.script_.compilation_type;
2004};
2005
2006
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002007ScriptMirror.prototype.lineCount = function() {
2008 return this.script_.lineCount();
2009};
2010
2011
ager@chromium.org3a6061e2009-03-12 14:24:36 +00002012ScriptMirror.prototype.locationFromPosition = function(
2013 position, include_resource_offset) {
2014 return this.script_.locationFromPosition(position, include_resource_offset);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002015};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002016
2017
2018ScriptMirror.prototype.sourceSlice = function (opt_from_line, opt_to_line) {
2019 return this.script_.sourceSlice(opt_from_line, opt_to_line);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002020};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002021
2022
ager@chromium.org9085a012009-05-11 19:22:57 +00002023ScriptMirror.prototype.context = function() {
2024 return this.context_;
2025};
2026
2027
sgjesse@chromium.org98180592009-12-02 08:17:28 +00002028ScriptMirror.prototype.evalFromScript = function() {
2029 return MakeMirror(this.script_.eval_from_script);
2030};
2031
2032
2033ScriptMirror.prototype.evalFromFunctionName = function() {
2034 return MakeMirror(this.script_.eval_from_function_name);
ager@chromium.orge2902be2009-06-08 12:21:35 +00002035};
2036
2037
2038ScriptMirror.prototype.evalFromLocation = function() {
sgjesse@chromium.org98180592009-12-02 08:17:28 +00002039 var eval_from_script = this.evalFromScript();
2040 if (!eval_from_script.isUndefined()) {
2041 var position = this.script_.eval_from_script_position;
2042 return eval_from_script.locationFromPosition(position, true);
ager@chromium.orge2902be2009-06-08 12:21:35 +00002043 }
2044};
2045
2046
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002047ScriptMirror.prototype.toText = function() {
2048 var result = '';
2049 result += this.name();
2050 result += ' (lines: ';
2051 if (this.lineOffset() > 0) {
2052 result += this.lineOffset();
2053 result += '-';
2054 result += this.lineOffset() + this.lineCount() - 1;
2055 } else {
2056 result += this.lineCount();
2057 }
2058 result += ')';
2059 return result;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002060};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002061
2062
ager@chromium.org32912102009-01-16 10:38:43 +00002063/**
ager@chromium.org9085a012009-05-11 19:22:57 +00002064 * Mirror object for context.
2065 * @param {Object} data The context data
2066 * @constructor
2067 * @extends Mirror
2068 */
2069function ContextMirror(data) {
whesse@chromium.org7a392b32011-01-31 11:30:36 +00002070 %_CallFunction(this, CONTEXT_TYPE, Mirror);
ager@chromium.org9085a012009-05-11 19:22:57 +00002071 this.data_ = data;
2072 this.allocateHandle_();
2073}
2074inherits(ContextMirror, Mirror);
2075
2076
2077ContextMirror.prototype.data = function() {
2078 return this.data_;
2079};
2080
2081
2082/**
ager@chromium.org32912102009-01-16 10:38:43 +00002083 * Returns a mirror serializer
2084 *
2085 * @param {boolean} details Set to true to include details
ager@chromium.org9085a012009-05-11 19:22:57 +00002086 * @param {Object} options Options comtrolling the serialization
2087 * The following options can be set:
2088 * includeSource: include ths full source of scripts
ager@chromium.org32912102009-01-16 10:38:43 +00002089 * @returns {MirrorSerializer} mirror serializer
2090 */
ager@chromium.org9085a012009-05-11 19:22:57 +00002091function MakeMirrorSerializer(details, options) {
2092 return new JSONProtocolSerializer(details, options);
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00002093}
2094
2095
ager@chromium.org32912102009-01-16 10:38:43 +00002096/**
2097 * Object for serializing a mirror objects and its direct references.
2098 * @param {boolean} details Indicates whether to include details for the mirror
2099 * serialized
2100 * @constructor
2101 */
ager@chromium.org9085a012009-05-11 19:22:57 +00002102function JSONProtocolSerializer(details, options) {
ager@chromium.org32912102009-01-16 10:38:43 +00002103 this.details_ = details;
ager@chromium.org9085a012009-05-11 19:22:57 +00002104 this.options_ = options;
ager@chromium.org32912102009-01-16 10:38:43 +00002105 this.mirrors_ = [ ];
2106}
2107
2108
2109/**
2110 * Returns a serialization of an object reference. The referenced object are
2111 * added to the serialization state.
2112 *
2113 * @param {Mirror} mirror The mirror to serialize
2114 * @returns {String} JSON serialization
2115 */
2116JSONProtocolSerializer.prototype.serializeReference = function(mirror) {
2117 return this.serialize_(mirror, true, true);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002118};
ager@chromium.org32912102009-01-16 10:38:43 +00002119
2120
2121/**
2122 * Returns a serialization of an object value. The referenced objects are
2123 * added to the serialization state.
2124 *
2125 * @param {Mirror} mirror The mirror to serialize
2126 * @returns {String} JSON serialization
2127 */
2128JSONProtocolSerializer.prototype.serializeValue = function(mirror) {
2129 var json = this.serialize_(mirror, false, true);
2130 return json;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002131};
ager@chromium.org32912102009-01-16 10:38:43 +00002132
2133
2134/**
2135 * Returns a serialization of all the objects referenced.
2136 *
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002137 * @param {Mirror} mirror The mirror to serialize.
2138 * @returns {Array.<Object>} Array of the referenced objects converted to
2139 * protcol objects.
ager@chromium.org32912102009-01-16 10:38:43 +00002140 */
2141JSONProtocolSerializer.prototype.serializeReferencedObjects = function() {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002142 // Collect the protocol representation of the referenced objects in an array.
2143 var content = [];
lrn@chromium.org25156de2010-04-06 13:10:27 +00002144
ager@chromium.org32912102009-01-16 10:38:43 +00002145 // Get the number of referenced objects.
2146 var count = this.mirrors_.length;
lrn@chromium.org25156de2010-04-06 13:10:27 +00002147
ager@chromium.org32912102009-01-16 10:38:43 +00002148 for (var i = 0; i < count; i++) {
2149 content.push(this.serialize_(this.mirrors_[i], false, false));
2150 }
2151
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002152 return content;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002153};
ager@chromium.org32912102009-01-16 10:38:43 +00002154
2155
ager@chromium.org9085a012009-05-11 19:22:57 +00002156JSONProtocolSerializer.prototype.includeSource_ = function() {
2157 return this.options_ && this.options_.includeSource;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002158};
ager@chromium.org9085a012009-05-11 19:22:57 +00002159
2160
ager@chromium.org3e875802009-06-29 08:26:34 +00002161JSONProtocolSerializer.prototype.inlineRefs_ = function() {
2162 return this.options_ && this.options_.inlineRefs;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002163};
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002164
2165
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00002166JSONProtocolSerializer.prototype.maxStringLength_ = function() {
2167 if (IS_UNDEFINED(this.options_) ||
2168 IS_UNDEFINED(this.options_.maxStringLength)) {
2169 return kMaxProtocolStringLength;
2170 }
2171 return this.options_.maxStringLength;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002172};
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00002173
2174
ager@chromium.org32912102009-01-16 10:38:43 +00002175JSONProtocolSerializer.prototype.add_ = function(mirror) {
2176 // If this mirror is already in the list just return.
2177 for (var i = 0; i < this.mirrors_.length; i++) {
2178 if (this.mirrors_[i] === mirror) {
2179 return;
2180 }
2181 }
lrn@chromium.org25156de2010-04-06 13:10:27 +00002182
ager@chromium.org32912102009-01-16 10:38:43 +00002183 // Add the mirror to the list of mirrors to be serialized.
2184 this.mirrors_.push(mirror);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002185};
ager@chromium.org32912102009-01-16 10:38:43 +00002186
2187
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002188/**
2189 * Formats mirror object to protocol reference object with some data that can
2190 * be used to display the value in debugger.
2191 * @param {Mirror} mirror Mirror to serialize.
2192 * @return {Object} Protocol reference object.
2193 */
lrn@chromium.org25156de2010-04-06 13:10:27 +00002194JSONProtocolSerializer.prototype.serializeReferenceWithDisplayData_ =
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002195 function(mirror) {
2196 var o = {};
2197 o.ref = mirror.handle();
2198 o.type = mirror.type();
2199 switch (mirror.type()) {
2200 case UNDEFINED_TYPE:
2201 case NULL_TYPE:
2202 case BOOLEAN_TYPE:
2203 case NUMBER_TYPE:
2204 o.value = mirror.value();
2205 break;
2206 case STRING_TYPE:
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00002207 o.value = mirror.getTruncatedValue(this.maxStringLength_());
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002208 break;
2209 case FUNCTION_TYPE:
2210 o.name = mirror.name();
2211 o.inferredName = mirror.inferredName();
2212 if (mirror.script()) {
2213 o.scriptId = mirror.script().id();
2214 }
2215 break;
2216 case ERROR_TYPE:
2217 case REGEXP_TYPE:
2218 o.value = mirror.toText();
2219 break;
2220 case OBJECT_TYPE:
2221 o.className = mirror.className();
2222 break;
2223 }
2224 return o;
2225};
2226
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002227
ager@chromium.org32912102009-01-16 10:38:43 +00002228JSONProtocolSerializer.prototype.serialize_ = function(mirror, reference,
2229 details) {
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002230 // If serializing a reference to a mirror just return the reference and add
2231 // the mirror to the referenced mirrors.
2232 if (reference &&
ager@chromium.org9085a012009-05-11 19:22:57 +00002233 (mirror.isValue() || mirror.isScript() || mirror.isContext())) {
ager@chromium.org3e875802009-06-29 08:26:34 +00002234 if (this.inlineRefs_() && mirror.isValue()) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002235 return this.serializeReferenceWithDisplayData_(mirror);
2236 } else {
2237 this.add_(mirror);
2238 return {'ref' : mirror.handle()};
2239 }
ager@chromium.org32912102009-01-16 10:38:43 +00002240 }
lrn@chromium.org25156de2010-04-06 13:10:27 +00002241
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002242 // Collect the JSON property/value pairs.
2243 var content = {};
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00002244
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002245 // Add the mirror handle.
ager@chromium.org9085a012009-05-11 19:22:57 +00002246 if (mirror.isValue() || mirror.isScript() || mirror.isContext()) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002247 content.handle = mirror.handle();
ager@chromium.org32912102009-01-16 10:38:43 +00002248 }
2249
2250 // Always add the type.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002251 content.type = mirror.type();
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00002252
2253 switch (mirror.type()) {
2254 case UNDEFINED_TYPE:
2255 case NULL_TYPE:
2256 // Undefined and null are represented just by their type.
2257 break;
2258
2259 case BOOLEAN_TYPE:
2260 // Boolean values are simply represented by their value.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002261 content.value = mirror.value();
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00002262 break;
2263
2264 case NUMBER_TYPE:
2265 // Number values are simply represented by their value.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002266 content.value = NumberToJSON_(mirror.value());
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00002267 break;
2268
2269 case STRING_TYPE:
2270 // String values might have their value cropped to keep down size.
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00002271 if (this.maxStringLength_() != -1 &&
2272 mirror.length() > this.maxStringLength_()) {
2273 var str = mirror.getTruncatedValue(this.maxStringLength_());
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002274 content.value = str;
2275 content.fromIndex = 0;
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00002276 content.toIndex = this.maxStringLength_();
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00002277 } else {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002278 content.value = mirror.value();
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00002279 }
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002280 content.length = mirror.length();
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00002281 break;
2282
2283 case OBJECT_TYPE:
2284 case FUNCTION_TYPE:
2285 case ERROR_TYPE:
2286 case REGEXP_TYPE:
2287 // Add object representation.
ager@chromium.org32912102009-01-16 10:38:43 +00002288 this.serializeObject_(mirror, content, details);
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00002289 break;
2290
2291 case PROPERTY_TYPE:
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00002292 case INTERNAL_PROPERTY_TYPE:
2293 throw new Error('PropertyMirror cannot be serialized independently');
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00002294 break;
2295
2296 case FRAME_TYPE:
2297 // Add object representation.
2298 this.serializeFrame_(mirror, content);
2299 break;
2300
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002301 case SCOPE_TYPE:
2302 // Add object representation.
2303 this.serializeScope_(mirror, content);
2304 break;
2305
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00002306 case SCRIPT_TYPE:
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002307 // Script is represented by id, name and source attributes.
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00002308 if (mirror.name()) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002309 content.name = mirror.name();
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00002310 }
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002311 content.id = mirror.id();
2312 content.lineOffset = mirror.lineOffset();
2313 content.columnOffset = mirror.columnOffset();
2314 content.lineCount = mirror.lineCount();
ager@chromium.org9085a012009-05-11 19:22:57 +00002315 if (mirror.data()) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002316 content.data = mirror.data();
ager@chromium.org9085a012009-05-11 19:22:57 +00002317 }
2318 if (this.includeSource_()) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002319 content.source = mirror.source();
ager@chromium.org9085a012009-05-11 19:22:57 +00002320 } else {
2321 var sourceStart = mirror.source().substring(0, 80);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002322 content.sourceStart = sourceStart;
ager@chromium.org9085a012009-05-11 19:22:57 +00002323 }
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002324 content.sourceLength = mirror.source().length;
2325 content.scriptType = mirror.scriptType();
ager@chromium.orge2902be2009-06-08 12:21:35 +00002326 content.compilationType = mirror.compilationType();
sgjesse@chromium.org636edf42009-06-18 14:43:07 +00002327 // For compilation type eval emit information on the script from which
2328 // eval was called if a script is present.
2329 if (mirror.compilationType() == 1 &&
sgjesse@chromium.org98180592009-12-02 08:17:28 +00002330 mirror.evalFromScript()) {
ager@chromium.orge2902be2009-06-08 12:21:35 +00002331 content.evalFromScript =
sgjesse@chromium.org98180592009-12-02 08:17:28 +00002332 this.serializeReference(mirror.evalFromScript());
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002333 var evalFromLocation = mirror.evalFromLocation();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002334 if (evalFromLocation) {
2335 content.evalFromLocation = { line: evalFromLocation.line,
2336 column: evalFromLocation.column };
2337 }
sgjesse@chromium.org98180592009-12-02 08:17:28 +00002338 if (mirror.evalFromFunctionName()) {
2339 content.evalFromFunctionName = mirror.evalFromFunctionName();
2340 }
ager@chromium.orge2902be2009-06-08 12:21:35 +00002341 }
ager@chromium.org9085a012009-05-11 19:22:57 +00002342 if (mirror.context()) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002343 content.context = this.serializeReference(mirror.context());
ager@chromium.org9085a012009-05-11 19:22:57 +00002344 }
2345 break;
2346
2347 case CONTEXT_TYPE:
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002348 content.data = mirror.data();
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00002349 break;
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00002350 }
2351
2352 // Always add the text representation.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002353 content.text = mirror.toText();
lrn@chromium.org25156de2010-04-06 13:10:27 +00002354
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00002355 // Create and return the JSON string.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002356 return content;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002357};
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00002358
2359
ager@chromium.org32912102009-01-16 10:38:43 +00002360/**
2361 * Serialize object information to the following JSON format.
2362 *
2363 * {"className":"<class name>",
2364 * "constructorFunction":{"ref":<number>},
2365 * "protoObject":{"ref":<number>},
2366 * "prototypeObject":{"ref":<number>},
2367 * "namedInterceptor":<boolean>,
2368 * "indexedInterceptor":<boolean>,
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00002369 * "properties":[<properties>],
2370 * "internalProperties":[<internal properties>]}
ager@chromium.org32912102009-01-16 10:38:43 +00002371 */
2372JSONProtocolSerializer.prototype.serializeObject_ = function(mirror, content,
2373 details) {
2374 // Add general object properties.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002375 content.className = mirror.className();
2376 content.constructorFunction =
2377 this.serializeReference(mirror.constructorFunction());
2378 content.protoObject = this.serializeReference(mirror.protoObject());
2379 content.prototypeObject = this.serializeReference(mirror.prototypeObject());
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00002380
ager@chromium.org32912102009-01-16 10:38:43 +00002381 // Add flags to indicate whether there are interceptors.
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00002382 if (mirror.hasNamedInterceptor()) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002383 content.namedInterceptor = true;
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00002384 }
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00002385 if (mirror.hasIndexedInterceptor()) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002386 content.indexedInterceptor = true;
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00002387 }
lrn@chromium.org25156de2010-04-06 13:10:27 +00002388
ager@chromium.org32912102009-01-16 10:38:43 +00002389 // Add function specific properties.
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00002390 if (mirror.isFunction()) {
2391 // Add function specific properties.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002392 content.name = mirror.name();
ager@chromium.org9085a012009-05-11 19:22:57 +00002393 if (!IS_UNDEFINED(mirror.inferredName())) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002394 content.inferredName = mirror.inferredName();
ager@chromium.org9085a012009-05-11 19:22:57 +00002395 }
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002396 content.resolved = mirror.resolved();
ager@chromium.org32912102009-01-16 10:38:43 +00002397 if (mirror.resolved()) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002398 content.source = mirror.source();
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00002399 }
2400 if (mirror.script()) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002401 content.script = this.serializeReference(mirror.script());
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002402 content.scriptId = mirror.script().id();
lrn@chromium.org25156de2010-04-06 13:10:27 +00002403
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002404 serializeLocationFields(mirror.sourceLocation(), content);
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00002405 }
danno@chromium.org1044a4d2012-04-30 12:34:39 +00002406
2407 content.scopes = [];
2408 for (var i = 0; i < mirror.scopeCount(); i++) {
2409 var scope = mirror.scope(i);
2410 content.scopes.push({
2411 type: scope.scopeType(),
2412 index: i
2413 });
2414 }
ager@chromium.org32912102009-01-16 10:38:43 +00002415 }
2416
2417 // Add date specific properties.
2418 if (mirror.isDate()) {
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00002419 // Add date specific properties.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002420 content.value = mirror.value();
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00002421 }
ager@chromium.org32912102009-01-16 10:38:43 +00002422
2423 // Add actual properties - named properties followed by indexed properties.
2424 var propertyNames = mirror.propertyNames(PropertyKind.Named);
2425 var propertyIndexes = mirror.propertyNames(PropertyKind.Indexed);
2426 var p = new Array(propertyNames.length + propertyIndexes.length);
2427 for (var i = 0; i < propertyNames.length; i++) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002428 var propertyMirror = mirror.property(propertyNames[i]);
2429 p[i] = this.serializeProperty_(propertyMirror);
ager@chromium.org32912102009-01-16 10:38:43 +00002430 if (details) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002431 this.add_(propertyMirror.value());
ager@chromium.org32912102009-01-16 10:38:43 +00002432 }
2433 }
2434 for (var i = 0; i < propertyIndexes.length; i++) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002435 var propertyMirror = mirror.property(propertyIndexes[i]);
2436 p[propertyNames.length + i] = this.serializeProperty_(propertyMirror);
ager@chromium.org32912102009-01-16 10:38:43 +00002437 if (details) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002438 this.add_(propertyMirror.value());
ager@chromium.org32912102009-01-16 10:38:43 +00002439 }
2440 }
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002441 content.properties = p;
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00002442
2443 var internalProperties = mirror.internalProperties();
2444 if (internalProperties.length > 0) {
2445 var ip = [];
2446 for (var i = 0; i < internalProperties.length; i++) {
2447 ip.push(this.serializeInternalProperty_(internalProperties[i]));
2448 }
2449 content.internalProperties = ip;
2450 }
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002451};
ager@chromium.org32912102009-01-16 10:38:43 +00002452
2453
2454/**
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002455 * Serialize location information to the following JSON format:
2456 *
2457 * "position":"<position>",
2458 * "line":"<line>",
2459 * "column":"<column>",
lrn@chromium.org25156de2010-04-06 13:10:27 +00002460 *
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002461 * @param {SourceLocation} location The location to serialize, may be undefined.
2462 */
2463function serializeLocationFields (location, content) {
2464 if (!location) {
2465 return;
lrn@chromium.org25156de2010-04-06 13:10:27 +00002466 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002467 content.position = location.position;
2468 var line = location.line;
2469 if (!IS_UNDEFINED(line)) {
2470 content.line = line;
2471 }
2472 var column = location.column;
2473 if (!IS_UNDEFINED(column)) {
2474 content.column = column;
2475 }
2476}
2477
2478
2479/**
ager@chromium.org32912102009-01-16 10:38:43 +00002480 * Serialize property information to the following JSON format for building the
2481 * array of properties.
2482 *
2483 * {"name":"<property name>",
2484 * "attributes":<number>,
2485 * "propertyType":<number>,
2486 * "ref":<number>}
2487 *
2488 * If the attribute for the property is PropertyAttribute.None it is not added.
2489 * If the propertyType for the property is PropertyType.Normal it is not added.
2490 * Here are a couple of examples.
2491 *
2492 * {"name":"hello","ref":1}
2493 * {"name":"length","attributes":7,"propertyType":3,"ref":2}
2494 *
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002495 * @param {PropertyMirror} propertyMirror The property to serialize.
2496 * @returns {Object} Protocol object representing the property.
ager@chromium.org32912102009-01-16 10:38:43 +00002497 */
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002498JSONProtocolSerializer.prototype.serializeProperty_ = function(propertyMirror) {
2499 var result = {};
lrn@chromium.org25156de2010-04-06 13:10:27 +00002500
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002501 result.name = propertyMirror.name();
2502 var propertyValue = propertyMirror.value();
ager@chromium.org3e875802009-06-29 08:26:34 +00002503 if (this.inlineRefs_() && propertyValue.isValue()) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002504 result.value = this.serializeReferenceWithDisplayData_(propertyValue);
2505 } else {
2506 if (propertyMirror.attributes() != PropertyAttribute.None) {
2507 result.attributes = propertyMirror.attributes();
2508 }
2509 if (propertyMirror.propertyType() != PropertyType.Normal) {
2510 result.propertyType = propertyMirror.propertyType();
2511 }
2512 result.ref = propertyValue.handle();
ager@chromium.org32912102009-01-16 10:38:43 +00002513 }
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002514 return result;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002515};
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00002516
2517
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00002518/**
2519 * Serialize internal property information to the following JSON format for
2520 * building the array of properties.
2521 *
2522 * {"name":"<property name>",
2523 * "ref":<number>}
2524 *
2525 * {"name":"[[BoundThis]]","ref":117}
2526 *
2527 * @param {InternalPropertyMirror} propertyMirror The property to serialize.
2528 * @returns {Object} Protocol object representing the property.
2529 */
2530JSONProtocolSerializer.prototype.serializeInternalProperty_ =
2531 function(propertyMirror) {
2532 var result = {};
2533
2534 result.name = propertyMirror.name();
2535 var propertyValue = propertyMirror.value();
2536 if (this.inlineRefs_() && propertyValue.isValue()) {
2537 result.value = this.serializeReferenceWithDisplayData_(propertyValue);
2538 } else {
2539 result.ref = propertyValue.handle();
2540 }
2541 return result;
2542};
2543
2544
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00002545JSONProtocolSerializer.prototype.serializeFrame_ = function(mirror, content) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002546 content.index = mirror.index();
2547 content.receiver = this.serializeReference(mirror.receiver());
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002548 var func = mirror.func();
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002549 content.func = this.serializeReference(func);
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002550 if (func.script()) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002551 content.script = this.serializeReference(func.script());
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002552 }
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002553 content.constructCall = mirror.isConstructCall();
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002554 content.atReturn = mirror.isAtReturn();
2555 if (mirror.isAtReturn()) {
2556 content.returnValue = this.serializeReference(mirror.returnValue());
2557 }
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002558 content.debuggerFrame = mirror.isDebuggerFrame();
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00002559 var x = new Array(mirror.argumentCount());
2560 for (var i = 0; i < mirror.argumentCount(); i++) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002561 var arg = {};
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002562 var argument_name = mirror.argumentName(i);
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00002563 if (argument_name) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002564 arg.name = argument_name;
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00002565 }
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002566 arg.value = this.serializeReference(mirror.argumentValue(i));
2567 x[i] = arg;
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00002568 }
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002569 content.arguments = x;
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00002570 var x = new Array(mirror.localCount());
2571 for (var i = 0; i < mirror.localCount(); i++) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002572 var local = {};
2573 local.name = mirror.localName(i);
2574 local.value = this.serializeReference(mirror.localValue(i));
2575 x[i] = local;
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00002576 }
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002577 content.locals = x;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002578 serializeLocationFields(mirror.sourceLocation(), content);
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00002579 var source_line_text = mirror.sourceLineText();
2580 if (!IS_UNDEFINED(source_line_text)) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002581 content.sourceLineText = source_line_text;
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00002582 }
lrn@chromium.org25156de2010-04-06 13:10:27 +00002583
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002584 content.scopes = [];
2585 for (var i = 0; i < mirror.scopeCount(); i++) {
2586 var scope = mirror.scope(i);
2587 content.scopes.push({
2588 type: scope.scopeType(),
2589 index: i
2590 });
2591 }
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002592};
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00002593
2594
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002595JSONProtocolSerializer.prototype.serializeScope_ = function(mirror, content) {
2596 content.index = mirror.scopeIndex();
2597 content.frameIndex = mirror.frameIndex();
2598 content.type = mirror.scopeType();
ager@chromium.org3e875802009-06-29 08:26:34 +00002599 content.object = this.inlineRefs_() ?
2600 this.serializeValue(mirror.scopeObject()) :
2601 this.serializeReference(mirror.scopeObject());
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002602};
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002603
2604
sgjesse@chromium.org715915b2009-01-19 16:08:47 +00002605/**
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002606 * Convert a number to a protocol value. For all finite numbers the number
2607 * itself is returned. For non finite numbers NaN, Infinite and
sgjesse@chromium.org715915b2009-01-19 16:08:47 +00002608 * -Infinite the string representation "NaN", "Infinite" or "-Infinite"
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002609 * (not including the quotes) is returned.
sgjesse@chromium.org715915b2009-01-19 16:08:47 +00002610 *
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002611 * @param {number} value The number value to convert to a protocol value.
2612 * @returns {number|string} Protocol value.
sgjesse@chromium.org715915b2009-01-19 16:08:47 +00002613 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002614function NumberToJSON_(value) {
sgjesse@chromium.org715915b2009-01-19 16:08:47 +00002615 if (isNaN(value)) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002616 return 'NaN';
sgjesse@chromium.org715915b2009-01-19 16:08:47 +00002617 }
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +00002618 if (!NUMBER_IS_FINITE(value)) {
sgjesse@chromium.org715915b2009-01-19 16:08:47 +00002619 if (value > 0) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002620 return 'Infinity';
sgjesse@chromium.org715915b2009-01-19 16:08:47 +00002621 } else {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00002622 return '-Infinity';
sgjesse@chromium.org715915b2009-01-19 16:08:47 +00002623 }
2624 }
lrn@chromium.org25156de2010-04-06 13:10:27 +00002625 return value;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002626}