Home | History | Annotate | Download | only in mjsunit
      1 // Copyright 2008 the V8 project authors. All rights reserved.
      2 // 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 
     28 // Flags: --expose-debug-as debug
     29 // Get the Debug object exposed from the debug context global object.
     30 Debug = debug.Debug
     31 
     32 // Simple function which stores the last debug event.
     33 var listenerComplete = false;
     34 var exception = false;
     35 var f_script_id = 0;
     36 var g_script_id = 0;
     37 var h_script_id = 0;
     38 var f_line = 0;
     39 var g_line = 0;
     40 
     41 var base_request = '"seq":0,"type":"request","command":"setbreakpoint"'
     42 
     43 function safeEval(code) {
     44   try {
     45     return eval('(' + code + ')');
     46   } catch (e) {
     47     assertEquals(void 0, e);
     48     return undefined;
     49   }
     50 }
     51 
     52 function testArguments(dcp, arguments, success, is_script) {
     53   var request = '{' + base_request + ',"arguments":' + arguments + '}'
     54   var json_response = dcp.processDebugJSONRequest(request);
     55   var response = safeEval(json_response);
     56   if (success) {
     57     assertTrue(response.success, request + ' -> ' + json_response);
     58     if (is_script) {
     59       assertEquals('scriptName', response.body.type, request + ' -> ' + json_response);
     60     } else {
     61       assertEquals('scriptId', response.body.type, request + ' -> ' + json_response);
     62     }
     63   } else {
     64     assertFalse(response.success, request + ' -> ' + json_response);
     65   }
     66 }
     67 
     68 function listener(event, exec_state, event_data, data) {
     69   try {
     70   if (event == Debug.DebugEvent.Break) {
     71     // Get the debug command processor.
     72     var dcp = exec_state.debugCommandProcessor("unspecified_running_state");
     73 
     74     // Test some illegal setbreakpoint requests.
     75     var request = '{' + base_request + '}'
     76     var response = safeEval(dcp.processDebugJSONRequest(request));
     77     assertFalse(response.success);
     78     
     79     var mirror;
     80 
     81     testArguments(dcp, '{}', false);
     82     testArguments(dcp, '{"type":"xx"}', false);
     83     testArguments(dcp, '{"type":"function"}', false);
     84     testArguments(dcp, '{"type":"script"}', false);
     85     testArguments(dcp, '{"target":"f"}', false);
     86     testArguments(dcp, '{"type":"xx","target":"xx"}', false);
     87     testArguments(dcp, '{"type":"function","target":1}', false);
     88     testArguments(dcp, '{"type":"function","target":"f","line":-1}', false);
     89     testArguments(dcp, '{"type":"function","target":"f","column":-1}', false);
     90     testArguments(dcp, '{"type":"function","target":"f","ignoreCount":-1}', false);
     91     testArguments(dcp, '{"type":"handle","target":"-1"}', false);
     92     mirror = debug.MakeMirror(o);
     93     testArguments(dcp, '{"type":"handle","target":' + mirror.handle() + '}', false);
     94 
     95     // Test some legal setbreakpoint requests.
     96     testArguments(dcp, '{"type":"function","target":"f"}', true, false);
     97     testArguments(dcp, '{"type":"function","target":"h"}', true, false);
     98     testArguments(dcp, '{"type":"function","target":"f","line":1}', true, false);
     99     testArguments(dcp, '{"type":"function","target":"f","position":1}', true, false);
    100     testArguments(dcp, '{"type":"function","target":"f","condition":"i == 1"}', true, false);
    101     testArguments(dcp, '{"type":"function","target":"f","enabled":true}', true, false);
    102     testArguments(dcp, '{"type":"function","target":"f","enabled":false}', true, false);
    103     testArguments(dcp, '{"type":"function","target":"f","ignoreCount":7}', true, false);
    104 
    105     testArguments(dcp, '{"type":"script","target":"test"}', true, true);
    106     testArguments(dcp, '{"type":"script","target":"test"}', true, true);
    107     testArguments(dcp, '{"type":"script","target":"test","line":1}', true, true);
    108     testArguments(dcp, '{"type":"script","target":"test","column":1}', true, true);
    109 
    110     testArguments(dcp, '{"type":"scriptId","target":' + f_script_id + ',"line":' + f_line + '}', true, false);
    111     testArguments(dcp, '{"type":"scriptId","target":' + g_script_id + ',"line":' + g_line + '}', true, false);
    112     testArguments(dcp, '{"type":"scriptId","target":' + h_script_id + ',"line":' + h_line + '}', true, false);
    113 
    114     mirror = debug.MakeMirror(f);
    115     testArguments(dcp, '{"type":"handle","target":' + mirror.handle() + '}', true, false);
    116     mirror = debug.MakeMirror(o.a);
    117     testArguments(dcp, '{"type":"handle","target":' + mirror.handle() + '}', true, false);
    118 
    119     // Indicate that all was processed.
    120     listenerComplete = true;
    121   }
    122   } catch (e) {
    123     exception = e
    124   };
    125 };
    126 
    127 // Add the debug event listener.
    128 Debug.setListener(listener);
    129 
    130 function f() {
    131   a=1
    132 };
    133 
    134 function g() {
    135   f();
    136 };
    137 
    138 eval('function h(){}');
    139 
    140 o = {a:function(){},b:function(){}}
    141 
    142 // Check the script ids for the test functions.
    143 f_script_id = Debug.findScript(f).id;
    144 g_script_id = Debug.findScript(g).id;
    145 h_script_id = Debug.findScript(h).id;
    146 assertTrue(f_script_id > 0, "invalid script id for f");
    147 assertTrue(g_script_id > 0, "invalid script id for g");
    148 assertTrue(h_script_id > 0, "invalid script id for h");
    149 assertEquals(f_script_id, g_script_id);
    150 
    151 // Get the source line for the test functions.
    152 f_line = Debug.findFunctionSourceLocation(f).line;
    153 g_line = Debug.findFunctionSourceLocation(g).line;
    154 h_line = Debug.findFunctionSourceLocation(h).line;
    155 assertTrue(f_line > 0, "invalid line for f");
    156 assertTrue(g_line > 0, "invalid line for g");
    157 assertTrue(f_line < g_line);
    158 assertEquals(h_line, 0, "invalid line for h");
    159 
    160 // Set a break point and call to invoke the debug event listener.
    161 Debug.setBreakPoint(g, 0, 0);
    162 g();
    163 
    164 // Make sure that the debug event listener vas invoked.
    165 assertTrue(listenerComplete, "listener did not run to completion: " + exception);
    166