19258b6bc66e09368ada54001f619d53b4fc976d5ager@chromium.org// Copyright 2008 the V8 project authors. All rights reserved.
29a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// Redistribution and use in source and binary forms, with or without
39a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// modification, are permitted provided that the following conditions are
49a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// met:
59a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com//
69a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com//     * Redistributions of source code must retain the above copyright
79a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com//       notice, this list of conditions and the following disclaimer.
89a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com//     * Redistributions in binary form must reproduce the above
99a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com//       copyright notice, this list of conditions and the following
109a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com//       disclaimer in the documentation and/or other materials provided
119a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com//       with the distribution.
129a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com//     * Neither the name of Google Inc. nor the names of its
139a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com//       contributors may be used to endorse or promote products derived
149a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com//       from this software without specific prior written permission.
159a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com//
169a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
179a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
189a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
199a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
209a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
219a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
229a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
239a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
249a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
259a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
269a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
279a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
289a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// Flags: --expose-debug-as debug
299a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// Test the mirror object for regular error objects
309a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
313291210ab99f306b74430ebbc4b7d939629e699fager@chromium.orgfunction MirrorRefCache(json_refs) {
323291210ab99f306b74430ebbc4b7d939629e699fager@chromium.org  var tmp = eval('(' + json_refs + ')');
333291210ab99f306b74430ebbc4b7d939629e699fager@chromium.org  this.refs_ = [];
343291210ab99f306b74430ebbc4b7d939629e699fager@chromium.org  for (var i = 0; i < tmp.length; i++) {
353291210ab99f306b74430ebbc4b7d939629e699fager@chromium.org    this.refs_[tmp[i].handle] = tmp[i];
363291210ab99f306b74430ebbc4b7d939629e699fager@chromium.org  }
373291210ab99f306b74430ebbc4b7d939629e699fager@chromium.org}
383291210ab99f306b74430ebbc4b7d939629e699fager@chromium.org
393291210ab99f306b74430ebbc4b7d939629e699fager@chromium.orgMirrorRefCache.prototype.lookup = function(handle) {
403291210ab99f306b74430ebbc4b7d939629e699fager@chromium.org  return this.refs_[handle];
413291210ab99f306b74430ebbc4b7d939629e699fager@chromium.org}
423291210ab99f306b74430ebbc4b7d939629e699fager@chromium.org
439a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comfunction testErrorMirror(e) {
449a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  // Create mirror and JSON representation.
459a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  var mirror = debug.MakeMirror(e);
463291210ab99f306b74430ebbc4b7d939629e699fager@chromium.org  var serializer = debug.MakeMirrorSerializer();
47b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org  var json = JSON.stringify(serializer.serializeValue(mirror));
48b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org  var refs = new MirrorRefCache(
49b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org      JSON.stringify(serializer.serializeReferencedObjects()));
509a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
519a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  // Check the mirror hierachy.
529a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  assertTrue(mirror instanceof debug.Mirror);
539a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  assertTrue(mirror instanceof debug.ValueMirror);
549a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  assertTrue(mirror instanceof debug.ObjectMirror);
559a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  assertTrue(mirror instanceof debug.ErrorMirror);
569a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
579a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  // Check the mirror properties.
589a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  assertTrue(mirror.isError());
599a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  assertEquals('error', mirror.type());
609a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  assertFalse(mirror.isPrimitive());
619a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  assertEquals(mirror.message(), e.message, 'source');
629a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
639a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  // Parse JSON representation and check.
649a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  var fromJSON = eval('(' + json + ')');
659a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  assertEquals('error', fromJSON.type);
669a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  assertEquals('Error', fromJSON.className);
673291210ab99f306b74430ebbc4b7d939629e699fager@chromium.org  if (e.message) {
683291210ab99f306b74430ebbc4b7d939629e699fager@chromium.org    var found_message = false;
693291210ab99f306b74430ebbc4b7d939629e699fager@chromium.org    for (var i in fromJSON.properties) {
703291210ab99f306b74430ebbc4b7d939629e699fager@chromium.org      var p = fromJSON.properties[i];
713291210ab99f306b74430ebbc4b7d939629e699fager@chromium.org      print(p.name);
723291210ab99f306b74430ebbc4b7d939629e699fager@chromium.org      if (p.name == 'message') {
733291210ab99f306b74430ebbc4b7d939629e699fager@chromium.org        assertEquals(e.message, refs.lookup(p.ref).value);
743291210ab99f306b74430ebbc4b7d939629e699fager@chromium.org        found_message = true;
753291210ab99f306b74430ebbc4b7d939629e699fager@chromium.org      }
763291210ab99f306b74430ebbc4b7d939629e699fager@chromium.org    }
773291210ab99f306b74430ebbc4b7d939629e699fager@chromium.org    assertTrue(found_message, 'Property message not found');
783291210ab99f306b74430ebbc4b7d939629e699fager@chromium.org  }
7931b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
809a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  // Check the formatted text (regress 1231579).
819a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  assertEquals(fromJSON.text, e.toString(), 'toString');
829a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com}
839a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
849a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
859a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// Test Date values.
869a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comtestErrorMirror(new Error());
879a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comtestErrorMirror(new Error('This does not work'));
889a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comtestErrorMirror(new Error(123+456));
899a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comtestErrorMirror(new EvalError('EvalError'));
909a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comtestErrorMirror(new RangeError('RangeError'));
919a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comtestErrorMirror(new ReferenceError('ReferenceError'));
929a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comtestErrorMirror(new SyntaxError('SyntaxError'));
939a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comtestErrorMirror(new TypeError('TypeError'));
949a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comtestErrorMirror(new URIError('URIError'));
95