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// Get the Debug object exposed from the debug context global object.
309a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comDebug = debug.Debug
319a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
329a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// Make sure that the backtrace command can be processed when the receiver is
339a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// undefined.
349a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comlistenerCalled = false;
359a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comexception = false;
369a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
373291210ab99f306b74430ebbc4b7d939629e699fager@chromium.orgfunction ParsedResponse(json) {
383291210ab99f306b74430ebbc4b7d939629e699fager@chromium.org  this.response_ = eval('(' + json + ')');
393291210ab99f306b74430ebbc4b7d939629e699fager@chromium.org  this.refs_ = [];
403291210ab99f306b74430ebbc4b7d939629e699fager@chromium.org  if (this.response_.refs) {
413291210ab99f306b74430ebbc4b7d939629e699fager@chromium.org    for (var i = 0; i < this.response_.refs.length; i++) {
423291210ab99f306b74430ebbc4b7d939629e699fager@chromium.org      this.refs_[this.response_.refs[i].handle] = this.response_.refs[i];
433291210ab99f306b74430ebbc4b7d939629e699fager@chromium.org    }
449a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  }
459a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com}
469a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
473291210ab99f306b74430ebbc4b7d939629e699fager@chromium.org
483291210ab99f306b74430ebbc4b7d939629e699fager@chromium.orgParsedResponse.prototype.response = function() {
493291210ab99f306b74430ebbc4b7d939629e699fager@chromium.org  return this.response_;
503291210ab99f306b74430ebbc4b7d939629e699fager@chromium.org}
513291210ab99f306b74430ebbc4b7d939629e699fager@chromium.org
523291210ab99f306b74430ebbc4b7d939629e699fager@chromium.org
533291210ab99f306b74430ebbc4b7d939629e699fager@chromium.orgParsedResponse.prototype.body = function() {
543291210ab99f306b74430ebbc4b7d939629e699fager@chromium.org  return this.response_.body;
553291210ab99f306b74430ebbc4b7d939629e699fager@chromium.org}
563291210ab99f306b74430ebbc4b7d939629e699fager@chromium.org
573291210ab99f306b74430ebbc4b7d939629e699fager@chromium.org
583291210ab99f306b74430ebbc4b7d939629e699fager@chromium.orgParsedResponse.prototype.lookup = function(handle) {
593291210ab99f306b74430ebbc4b7d939629e699fager@chromium.org  return this.refs_[handle];
603291210ab99f306b74430ebbc4b7d939629e699fager@chromium.org}
613291210ab99f306b74430ebbc4b7d939629e699fager@chromium.org
623291210ab99f306b74430ebbc4b7d939629e699fager@chromium.org
639a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comfunction listener(event, exec_state, event_data, data) {
649a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  try {
659a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  if (event == Debug.DebugEvent.Exception)
669a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  {
679a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    // The expected backtrace is
689a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    // 1: g
699a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    // 0: [anonymous]
701805e21b0aece8c05f4960a5c0751c4463557891fschneider@chromium.org
719a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    // Get the debug command processor.
729d58c2b1c27d8b2890b9bd46e57d3842b09e0292christian.plesner.hansen@gmail.com    var dcp = exec_state.debugCommandProcessor(false);
739a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
749a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    // Get the backtrace.
759a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    var json;
769a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    json = '{"seq":0,"type":"request","command":"backtrace"}'
773291210ab99f306b74430ebbc4b7d939629e699fager@chromium.org    var response = new ParsedResponse(dcp.processDebugJSONRequest(json));
783291210ab99f306b74430ebbc4b7d939629e699fager@chromium.org    var backtrace = response.body();
799a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    assertEquals(2, backtrace.totalFrames);
809a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    assertEquals(2, backtrace.frames.length);
819a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
823291210ab99f306b74430ebbc4b7d939629e699fager@chromium.org    assertEquals("g", response.lookup(backtrace.frames[0].func.ref).name);
833291210ab99f306b74430ebbc4b7d939629e699fager@chromium.org    assertEquals("", response.lookup(backtrace.frames[1].func.ref).name);
849a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
859a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    listenerCalled = true;
869a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  }
879a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  } catch (e) {
889a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    exception = e
899a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  };
909a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com};
919a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
929a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// Add the debug event listener.
93245aa859d34fd516161c48ef4c69d38d9b889284iposva@chromium.orgDebug.setListener(listener);
949a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
959a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// Call method on undefined.
969a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comfunction g() {
979a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  (void 0).f();
989a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com};
999a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
1009a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// Break on the exception to do a backtrace with undefined as receiver.
1019a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comDebug.setBreakOnException(true);
1029a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comtry {
1039a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  g();
1049a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com} catch(e) {
1059a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  // Ignore the exception "Cannot call method 'x' of undefined"
1069a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com}
1079a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
1089d58c2b1c27d8b2890b9bd46e57d3842b09e0292christian.plesner.hansen@gmail.comassertFalse(exception, "exception in listener", exception)
1099a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// Make sure that the debug event listener vas invoked.
1109a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comassertTrue(listenerCalled, "listener not called");
111