OLD | NEW |
1 // Copyright 2009 the V8 project authors. All rights reserved. | 1 // Copyright 2009 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 18 matching lines...) Expand all Loading... |
29 // Get the Debug object exposed from the debug context global object. | 29 // Get the Debug object exposed from the debug context global object. |
30 Debug = debug.Debug | 30 Debug = debug.Debug |
31 | 31 |
32 var exception = false; // Exception in debug event listener. | 32 var exception = false; // Exception in debug event listener. |
33 var before_compile_count = 0; | 33 var before_compile_count = 0; |
34 var after_compile_count = 0; | 34 var after_compile_count = 0; |
35 var current_source = ''; // Current source being compiled. | 35 var current_source = ''; // Current source being compiled. |
36 var source_count = 0; // Total number of scources compiled. | 36 var source_count = 0; // Total number of scources compiled. |
37 var host_compilations = 0; // Number of scources compiled through the API. | 37 var host_compilations = 0; // Number of scources compiled through the API. |
38 var eval_compilations = 0; // Number of scources compiled through eval. | 38 var eval_compilations = 0; // Number of scources compiled through eval. |
39 var json_compilations = 0; // Number of scources compiled through JSON.parse. | |
40 | 39 |
41 | 40 |
42 function compileSource(source) { | 41 function compileSource(source) { |
43 current_source = source; | 42 current_source = source; |
44 eval(current_source); | 43 eval(current_source); |
45 source_count++; | 44 source_count++; |
46 } | 45 } |
47 | 46 |
48 | 47 |
49 function listener(event, exec_state, event_data, data) { | 48 function listener(event, exec_state, event_data, data) { |
50 try { | 49 try { |
51 if (event == Debug.DebugEvent.BeforeCompile || | 50 if (event == Debug.DebugEvent.BeforeCompile || |
52 event == Debug.DebugEvent.AfterCompile) { | 51 event == Debug.DebugEvent.AfterCompile) { |
53 // Count the events. | 52 // Count the events. |
54 if (event == Debug.DebugEvent.BeforeCompile) { | 53 if (event == Debug.DebugEvent.BeforeCompile) { |
55 before_compile_count++; | 54 before_compile_count++; |
56 } else { | 55 } else { |
57 after_compile_count++; | 56 after_compile_count++; |
58 switch (event_data.script().compilationType()) { | 57 switch (event_data.script().compilationType()) { |
59 case Debug.ScriptCompilationType.Host: | 58 case Debug.ScriptCompilationType.Host: |
60 host_compilations++; | 59 host_compilations++; |
61 break; | 60 break; |
62 case Debug.ScriptCompilationType.Eval: | 61 case Debug.ScriptCompilationType.Eval: |
63 eval_compilations++; | 62 eval_compilations++; |
64 break; | 63 break; |
65 case Debug.ScriptCompilationType.JSON: | |
66 json_compilations++; | |
67 break; | |
68 } | 64 } |
69 } | 65 } |
70 | 66 |
71 // If the compiled source contains 'eval' there will be additional compile | 67 // If the compiled source contains 'eval' there will be additional compile |
72 // events for the source inside eval. | 68 // events for the source inside eval. |
73 if (current_source.indexOf('eval') == 0) { | 69 if (current_source.indexOf('eval') == 0) { |
74 // For source with 'eval' there will be compile events with substrings | 70 // For source with 'eval' there will be compile events with substrings |
75 // as well as with with the exact source. | 71 // as well as with with the exact source. |
76 assertTrue(current_source.indexOf(event_data.script().source()) >= 0); | 72 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 { | 73 } else { |
85 // For source without 'eval' there will be a compile events with the | 74 // For source without 'eval' there will be a compile events with the |
86 // exact source. | 75 // exact source. |
87 assertEquals(current_source, event_data.script().source()); | 76 assertEquals(current_source, event_data.script().source()); |
88 } | 77 } |
89 // Check that script context is included into the event message. | 78 // Check that script context is included into the event message. |
90 var json = event_data.toJSONProtocol(); | 79 var json = event_data.toJSONProtocol(); |
91 var msg = eval('(' + json + ')'); | 80 var msg = eval('(' + json + ')'); |
92 assertTrue('context' in msg.body.script); | 81 assertTrue('context' in msg.body.script); |
93 | 82 |
(...skipping 12 matching lines...) Expand all Loading... |
106 Debug.setListener(listener); | 95 Debug.setListener(listener); |
107 | 96 |
108 // Compile different sources. | 97 // Compile different sources. |
109 compileSource('a=1'); | 98 compileSource('a=1'); |
110 compileSource('(function(){})'); | 99 compileSource('(function(){})'); |
111 compileSource('eval("a=2")'); | 100 compileSource('eval("a=2")'); |
112 source_count++; // Using eval causes additional compilation event. | 101 source_count++; // Using eval causes additional compilation event. |
113 compileSource('eval("eval(\'(function(){return a;})\')")'); | 102 compileSource('eval("eval(\'(function(){return a;})\')")'); |
114 source_count += 2; // Using eval causes additional compilation event. | 103 source_count += 2; // Using eval causes additional compilation event. |
115 compileSource('JSON.parse(\'{"a":1,"b":2}\')'); | 104 compileSource('JSON.parse(\'{"a":1,"b":2}\')'); |
116 source_count++; // Using JSON.parse causes additional compilation event. | 105 // Using JSON.parse does not causes additional compilation events. |
117 compileSource('x=1; //@ sourceURL=myscript.js'); | 106 compileSource('x=1; //@ sourceURL=myscript.js'); |
118 | 107 |
119 // Make sure that the debug event listener was invoked. | 108 // Make sure that the debug event listener was invoked. |
120 assertFalse(exception, "exception in listener") | 109 assertFalse(exception, "exception in listener") |
121 | 110 |
122 // Number of before and after compile events should be the same. | 111 // Number of before and after compile events should be the same. |
123 assertEquals(before_compile_count, after_compile_count); | 112 assertEquals(before_compile_count, after_compile_count); |
124 | 113 |
125 // Check the actual number of events (no compilation through the API as all | 114 // Check the actual number of events (no compilation through the API as all |
126 // source compiled through eval except for one JSON.parse call). | 115 // source compiled through eval). |
127 assertEquals(source_count, after_compile_count); | 116 assertEquals(source_count, after_compile_count); |
128 assertEquals(0, host_compilations); | 117 assertEquals(0, host_compilations); |
129 assertEquals(source_count - 1, eval_compilations); | 118 assertEquals(source_count, eval_compilations); |
130 assertEquals(1, json_compilations); | |
131 | 119 |
132 Debug.setListener(null); | 120 Debug.setListener(null); |
OLD | NEW |