blob: 862340602b34c6b145d872bdc64efa643d1592e1 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2009 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.
30Debug = debug.Debug
31
32var exception = false; // Exception in debug event listener.
33var before_compile_count = 0;
34var after_compile_count = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000035var compile_error_count = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +000036var current_source = ''; // Current source being compiled.
37var source_count = 0; // Total number of scources compiled.
38var host_compilations = 0; // Number of scources compiled through the API.
39var eval_compilations = 0; // Number of scources compiled through eval.
Emily Bernierd0a1eb72015-03-24 16:35:39 -040040var mute_listener = false;
Steve Blocka7e24c12009-10-30 11:49:00 +000041
42function compileSource(source) {
43 current_source = source;
44 eval(current_source);
45 source_count++;
46}
47
Emily Bernierd0a1eb72015-03-24 16:35:39 -040048function safeEval(code) {
49 try {
50 mute_listener = true;
51 return eval('(' + code + ')');
52 } catch (e) {
53 assertEquals(void 0, e);
54 return undefined;
55 } finally {
56 mute_listener = false;
57 }
58}
Steve Blocka7e24c12009-10-30 11:49:00 +000059
60function listener(event, exec_state, event_data, data) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -040061 if (mute_listener) return;
Steve Blocka7e24c12009-10-30 11:49:00 +000062 try {
63 if (event == Debug.DebugEvent.BeforeCompile ||
Ben Murdochb8a8cc12014-11-26 15:28:44 +000064 event == Debug.DebugEvent.AfterCompile ||
65 event == Debug.DebugEvent.CompileError) {
Steve Blocka7e24c12009-10-30 11:49:00 +000066 // Count the events.
67 if (event == Debug.DebugEvent.BeforeCompile) {
68 before_compile_count++;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000069 } else if (event == Debug.DebugEvent.AfterCompile) {
Steve Blocka7e24c12009-10-30 11:49:00 +000070 after_compile_count++;
71 switch (event_data.script().compilationType()) {
72 case Debug.ScriptCompilationType.Host:
73 host_compilations++;
74 break;
75 case Debug.ScriptCompilationType.Eval:
76 eval_compilations++;
77 break;
Steve Blocka7e24c12009-10-30 11:49:00 +000078 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000079 } else {
80 compile_error_count++;
Steve Blocka7e24c12009-10-30 11:49:00 +000081 }
82
83 // If the compiled source contains 'eval' there will be additional compile
84 // events for the source inside eval.
85 if (current_source.indexOf('eval') == 0) {
86 // For source with 'eval' there will be compile events with substrings
87 // as well as with with the exact source.
88 assertTrue(current_source.indexOf(event_data.script().source()) >= 0);
Steve Blocka7e24c12009-10-30 11:49:00 +000089 } else {
90 // For source without 'eval' there will be a compile events with the
91 // exact source.
92 assertEquals(current_source, event_data.script().source());
93 }
94 // Check that script context is included into the event message.
95 var json = event_data.toJSONProtocol();
Emily Bernierd0a1eb72015-03-24 16:35:39 -040096 var msg = safeEval(json);
Steve Blocka7e24c12009-10-30 11:49:00 +000097 assertTrue('context' in msg.body.script);
Andrei Popescu402d9372010-02-26 13:31:12 +000098
Ben Murdochb8a8cc12014-11-26 15:28:44 +000099 // Check that we pick script name from //# sourceURL, iff present
100 if (event == Debug.DebugEvent.AfterCompile) {
101 assertEquals(current_source.indexOf('sourceURL') >= 0 ?
102 'myscript.js' : undefined,
103 event_data.script().name());
104 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000105 }
106 } catch (e) {
107 exception = e
108 }
109};
110
111
112// Add the debug event listener.
113Debug.setListener(listener);
114
115// Compile different sources.
116compileSource('a=1');
117compileSource('(function(){})');
118compileSource('eval("a=2")');
119source_count++; // Using eval causes additional compilation event.
120compileSource('eval("eval(\'(function(){return a;})\')")');
121source_count += 2; // Using eval causes additional compilation event.
Leon Clarke4515c472010-02-03 11:58:03 +0000122compileSource('JSON.parse(\'{"a":1,"b":2}\')');
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800123// Using JSON.parse does not causes additional compilation events.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000124compileSource('x=1; //# sourceURL=myscript.js');
125
126try {
127 compileSource('}');
128} catch(e) {
129}
Steve Blocka7e24c12009-10-30 11:49:00 +0000130
131// Make sure that the debug event listener was invoked.
132assertFalse(exception, "exception in listener")
133
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000134// Number of before and after + error events should be the same.
135assertEquals(before_compile_count, after_compile_count + compile_error_count);
136assertEquals(compile_error_count, 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000137
138// Check the actual number of events (no compilation through the API as all
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800139// source compiled through eval).
Steve Blocka7e24c12009-10-30 11:49:00 +0000140assertEquals(source_count, after_compile_count);
141assertEquals(0, host_compilations);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800142assertEquals(source_count, eval_compilations);
Steve Blocka7e24c12009-10-30 11:49:00 +0000143
144Debug.setListener(null);