blob: 071183bf6e4f2de12fa86bea5d1379fce7e4fb08 [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;
35var current_source = ''; // Current source being compiled.
36var source_count = 0; // Total number of scources compiled.
37var host_compilations = 0; // Number of scources compiled through the API.
38var eval_compilations = 0; // Number of scources compiled through eval.
39var json_compilations = 0; // Number of scources compiled through JSON.parse.
40
41
42function compileSource(source) {
43 current_source = source;
44 eval(current_source);
45 source_count++;
46}
47
48
49function listener(event, exec_state, event_data, data) {
50 try {
51 if (event == Debug.DebugEvent.BeforeCompile ||
52 event == Debug.DebugEvent.AfterCompile) {
53 // Count the events.
54 if (event == Debug.DebugEvent.BeforeCompile) {
55 before_compile_count++;
56 } else {
57 after_compile_count++;
58 switch (event_data.script().compilationType()) {
59 case Debug.ScriptCompilationType.Host:
60 host_compilations++;
61 break;
62 case Debug.ScriptCompilationType.Eval:
63 eval_compilations++;
64 break;
65 case Debug.ScriptCompilationType.JSON:
66 json_compilations++;
67 break;
68 }
69 }
70
71 // If the compiled source contains 'eval' there will be additional compile
72 // events for the source inside eval.
73 if (current_source.indexOf('eval') == 0) {
74 // For source with 'eval' there will be compile events with substrings
75 // as well as with with the exact source.
76 assertTrue(current_source.indexOf(event_data.script().source()) >= 0);
77 } else if (current_source.indexOf('JSON.parse') == 0) {
78 // For JSON the JSON source will be in parentheses.
79 var s = event_data.script().source();
80 if (s[0] == '(') {
81 s = s.substring(1, s.length - 2);
82 }
83 assertTrue(current_source.indexOf(s) >= 0);
84 } else {
85 // For source without 'eval' there will be a compile events with the
86 // exact source.
87 assertEquals(current_source, event_data.script().source());
88 }
89 // Check that script context is included into the event message.
90 var json = event_data.toJSONProtocol();
91 var msg = eval('(' + json + ')');
92 assertTrue('context' in msg.body.script);
93 }
94 } catch (e) {
95 exception = e
96 }
97};
98
99
100// Add the debug event listener.
101Debug.setListener(listener);
102
103// Compile different sources.
104compileSource('a=1');
105compileSource('(function(){})');
106compileSource('eval("a=2")');
107source_count++; // Using eval causes additional compilation event.
108compileSource('eval("eval(\'(function(){return a;})\')")');
109source_count += 2; // Using eval causes additional compilation event.
Leon Clarke4515c472010-02-03 11:58:03 +0000110compileSource('JSON.parse(\'{"a":1,"b":2}\')');
Steve Blocka7e24c12009-10-30 11:49:00 +0000111source_count++; // Using JSON.parse causes additional compilation event.
112
113// Make sure that the debug event listener was invoked.
114assertFalse(exception, "exception in listener")
115
116// Number of before and after compile events should be the same.
117assertEquals(before_compile_count, after_compile_count);
118
119// Check the actual number of events (no compilation through the API as all
120// source compiled through eval except for one JSON.parse call).
121assertEquals(source_count, after_compile_count);
122assertEquals(0, host_compilations);
123assertEquals(source_count - 1, eval_compilations);
124assertEquals(1, json_compilations);
125
126Debug.setListener(null);