1a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Copyright 2008 the V8 project authors. All rights reserved.
2a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Redistribution and use in source and binary forms, with or without
3a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// modification, are permitted provided that the following conditions are
4a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// met:
5a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//
6a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//     * Redistributions of source code must retain the above copyright
7a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       notice, this list of conditions and the following disclaimer.
8a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//     * Redistributions in binary form must reproduce the above
9a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       copyright notice, this list of conditions and the following
10a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       disclaimer in the documentation and/or other materials provided
11a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       with the distribution.
12a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//     * Neither the name of Google Inc. nor the names of its
13a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       contributors may be used to endorse or promote products derived
14a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       from this software without specific prior written permission.
15a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//
16a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
28a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Flags: --expose-debug-as debug --allow-natives-syntax
29a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Test the mirror object for scripts.
30a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
31a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockfunction testScriptMirror(f, file_name, file_lines, type, compilation_type,
32a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block                          source, eval_from_line) {
33a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Create mirror and JSON representation.
34a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  var mirror = debug.MakeMirror(f).script();
35a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  var serializer = debug.MakeMirrorSerializer();
36a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  var json = JSON.stringify(serializer.serializeValue(mirror));
37a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
38a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Check the mirror hierachy.
39a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  assertTrue(mirror instanceof debug.Mirror);
40a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  assertFalse(mirror instanceof debug.ValueMirror);
41a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  assertTrue(mirror instanceof debug.ScriptMirror);
42a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
43a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Check the mirror properties.
44a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  assertTrue(mirror.isScript());
45a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  assertEquals('script', mirror.type());
46a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  var name = mirror.name();
47a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  if (name) {
48a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    assertEquals(file_name, name.substring(name.length - file_name.length));
49a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  } else {
50a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    assertTrue(file_name === null);
51a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  }
52a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  assertEquals(0, mirror.lineOffset());
53a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  assertEquals(0, mirror.columnOffset());
54a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  if (file_lines > 0) {
55a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    assertEquals(file_lines, mirror.lineCount());
56a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  }
57a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  assertEquals(type, mirror.scriptType());
58a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  assertEquals(compilation_type, mirror.compilationType(), "compilation type");
59a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  if (source) {
60a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    assertEquals(source, mirror.source());
61a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  }
62a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  if (eval_from_line) {
63a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    assertEquals(eval_from_line,  mirror.evalFromLocation().line);
64a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  }
65589d6979ff2ef66fca2d8fa51404c369ca5e9250Ben Murdoch
66a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Parse JSON representation and check.
67a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  var fromJSON = JSON.parse(json);
68a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  assertEquals('script', fromJSON.type);
69a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  name = fromJSON.name;
70a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  if (name) {
71a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    assertEquals(file_name, name.substring(name.length - file_name.length));
72a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  } else {
73a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    assertTrue(file_name === null);
74a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  }
75a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  assertEquals(0, fromJSON.lineOffset);
76a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  assertEquals(0, fromJSON.columnOffset);
77a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  if (file_lines > 0) {
78a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    assertEquals(file_lines, fromJSON.lineCount);
79a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  }
80a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  assertEquals(type, fromJSON.scriptType);
81a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  assertEquals(compilation_type, fromJSON.compilationType);
82a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block}
83a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
84a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
85a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Test the script mirror for different functions.
863e5fa29ddb82551500b118e9bf37af3966277b70Teng-Hui ZhutestScriptMirror(function(){}, 'mirror-script.js', 98, 2, 0);
87a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlocktestScriptMirror(Math.sin, 'native math.js', -1, 0, 0);
88a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlocktestScriptMirror(eval('(function(){})'), null, 1, 2, 1, '(function(){})', 87);
89a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlocktestScriptMirror(eval('(function(){\n  })'), null, 2, 2, 1, '(function(){\n  })', 88);
90a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
91a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Test taking slices of source.
92a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvar mirror = debug.MakeMirror(eval('(function(){\n  1;\n})')).script();
93a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals('(function(){\n', mirror.sourceSlice(0, 1).sourceText());
94a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals('  1;\n', mirror.sourceSlice(1, 2).sourceText());
95a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals('})', mirror.sourceSlice(2, 3).sourceText());
96a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals('(function(){\n  1;\n', mirror.sourceSlice(0, 2).sourceText());
97a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals('  1;\n})', mirror.sourceSlice(1, 3).sourceText());
98a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals('(function(){\n  1;\n})', mirror.sourceSlice(0, 3).sourceText());
99