PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/deps/v8/test/mjsunit/debug-compile-event.js

https://github.com/kevinxx/node_doc_zh_CN
JavaScript | 132 lines | 73 code | 15 blank | 44 comment | 15 complexity | 6a66243ed52f84a2997668bcf9f55c5a MD5 | raw file
Possible License(s): JSON, BSD-2-Clause, BSD-3-Clause
  1. // 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. // Flags: --expose-debug-as debug
  28. // Get the Debug object exposed from the debug context global object.
  29. Debug = debug.Debug
  30. var exception = false; // Exception in debug event listener.
  31. var before_compile_count = 0;
  32. var after_compile_count = 0;
  33. var current_source = ''; // Current source being compiled.
  34. var source_count = 0; // Total number of scources compiled.
  35. var host_compilations = 0; // Number of scources compiled through the API.
  36. var eval_compilations = 0; // Number of scources compiled through eval.
  37. var json_compilations = 0; // Number of scources compiled through JSON.parse.
  38. function compileSource(source) {
  39. current_source = source;
  40. eval(current_source);
  41. source_count++;
  42. }
  43. function listener(event, exec_state, event_data, data) {
  44. try {
  45. if (event == Debug.DebugEvent.BeforeCompile ||
  46. event == Debug.DebugEvent.AfterCompile) {
  47. // Count the events.
  48. if (event == Debug.DebugEvent.BeforeCompile) {
  49. before_compile_count++;
  50. } else {
  51. after_compile_count++;
  52. switch (event_data.script().compilationType()) {
  53. case Debug.ScriptCompilationType.Host:
  54. host_compilations++;
  55. break;
  56. case Debug.ScriptCompilationType.Eval:
  57. eval_compilations++;
  58. break;
  59. case Debug.ScriptCompilationType.JSON:
  60. json_compilations++;
  61. break;
  62. }
  63. }
  64. // If the compiled source contains 'eval' there will be additional compile
  65. // events for the source inside eval.
  66. if (current_source.indexOf('eval') == 0) {
  67. // For source with 'eval' there will be compile events with substrings
  68. // as well as with with the exact source.
  69. assertTrue(current_source.indexOf(event_data.script().source()) >= 0);
  70. } else if (current_source.indexOf('JSON.parse') == 0) {
  71. // For JSON the JSON source will be in parentheses.
  72. var s = event_data.script().source();
  73. if (s[0] == '(') {
  74. s = s.substring(1, s.length - 2);
  75. }
  76. assertTrue(current_source.indexOf(s) >= 0);
  77. } else {
  78. // For source without 'eval' there will be a compile events with the
  79. // exact source.
  80. assertEquals(current_source, event_data.script().source());
  81. }
  82. // Check that script context is included into the event message.
  83. var json = event_data.toJSONProtocol();
  84. var msg = eval('(' + json + ')');
  85. assertTrue('context' in msg.body.script);
  86. // Check that we pick script name from //@ sourceURL, iff present
  87. assertEquals(current_source.indexOf('sourceURL') >= 0 ?
  88. 'myscript.js' : undefined,
  89. event_data.script().name());
  90. }
  91. } catch (e) {
  92. exception = e
  93. }
  94. };
  95. // Add the debug event listener.
  96. Debug.setListener(listener);
  97. // Compile different sources.
  98. compileSource('a=1');
  99. compileSource('(function(){})');
  100. compileSource('eval("a=2")');
  101. source_count++; // Using eval causes additional compilation event.
  102. compileSource('eval("eval(\'(function(){return a;})\')")');
  103. source_count += 2; // Using eval causes additional compilation event.
  104. compileSource('JSON.parse(\'{"a":1,"b":2}\')');
  105. source_count++; // Using JSON.parse causes additional compilation event.
  106. compileSource('x=1; //@ sourceURL=myscript.js');
  107. // Make sure that the debug event listener was invoked.
  108. assertFalse(exception, "exception in listener")
  109. // Number of before and after compile events should be the same.
  110. assertEquals(before_compile_count, after_compile_count);
  111. // Check the actual number of events (no compilation through the API as all
  112. // source compiled through eval except for one JSON.parse call).
  113. assertEquals(source_count, after_compile_count);
  114. assertEquals(0, host_compilations);
  115. assertEquals(source_count - 1, eval_compilations);
  116. assertEquals(1, json_compilations);
  117. Debug.setListener(null);