The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.

Please select the desired protocol below to get the URL.

This URL has Read-Only access.

Statistics
| Branch: | Revision:

main_repo / deps / v8 / test / cctest / test-func-name-inference.cc @ 40c0f755

History | View | Annotate | Download (7.28 KB)

1
// Copyright 2007-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
#include "v8.h"
29

    
30
#include "api.h"
31
#include "runtime.h"
32
#include "cctest.h"
33

    
34

    
35
using ::v8::internal::Handle;
36
using ::v8::internal::JSFunction;
37
using ::v8::internal::Object;
38
using ::v8::internal::Script;
39
using ::v8::internal::SharedFunctionInfo;
40
using ::v8::internal::String;
41

    
42
namespace i = ::v8::internal;
43

    
44

    
45
static v8::Persistent<v8::Context> env;
46

    
47

    
48
static void InitializeVM() {
49
  if (env.IsEmpty()) {
50
    v8::HandleScope scope;
51
    env = v8::Context::New();
52
  }
53
  v8::HandleScope scope;
54
  env->Enter();
55
}
56

    
57

    
58
static void CheckFunctionName(v8::Handle<v8::Script> script,
59
                              const char* func_pos_src,
60
                              const char* ref_inferred_name) {
61
  // Get script source.
62
  Handle<JSFunction> fun = v8::Utils::OpenHandle(*script);
63
  Handle<Script> i_script(Script::cast(fun->shared()->script()));
64
  CHECK(i_script->source()->IsString());
65
  Handle<String> script_src(String::cast(i_script->source()));
66

    
67
  // Find the position of a given func source substring in the source.
68
  Handle<String> func_pos_str =
69
      i::Factory::NewStringFromAscii(i::CStrVector(func_pos_src));
70
  int func_pos = i::Runtime::StringMatch(script_src, func_pos_str, 0);
71
  CHECK_NE(0, func_pos);
72

    
73
  // Obtain SharedFunctionInfo for the function.
74
  Object* shared_func_info_ptr =
75
      i::Runtime::FindSharedFunctionInfoInScript(i_script, func_pos);
76
  CHECK(shared_func_info_ptr != i::Heap::undefined_value());
77
  Handle<SharedFunctionInfo> shared_func_info(
78
      SharedFunctionInfo::cast(shared_func_info_ptr));
79

    
80
  // Verify inferred function name.
81
  i::SmartPointer<char> inferred_name =
82
      shared_func_info->inferred_name()->ToCString();
83
  CHECK_EQ(ref_inferred_name, *inferred_name);
84
}
85

    
86

    
87
static v8::Handle<v8::Script> Compile(const char* src) {
88
  return v8::Script::Compile(v8::String::New(src));
89
}
90

    
91

    
92
TEST(GlobalProperty) {
93
  InitializeVM();
94
  v8::HandleScope scope;
95

    
96
  v8::Handle<v8::Script> script = Compile(
97
      "fun1 = function() { return 1; }\n"
98
      "fun2 = function() { return 2; }\n");
99
  CheckFunctionName(script, "return 1", "fun1");
100
  CheckFunctionName(script, "return 2", "fun2");
101
}
102

    
103

    
104
TEST(GlobalVar) {
105
  InitializeVM();
106
  v8::HandleScope scope;
107

    
108
  v8::Handle<v8::Script> script = Compile(
109
      "var fun1 = function() { return 1; }\n"
110
      "var fun2 = function() { return 2; }\n");
111
  CheckFunctionName(script, "return 1", "fun1");
112
  CheckFunctionName(script, "return 2", "fun2");
113
}
114

    
115

    
116
TEST(LocalVar) {
117
  InitializeVM();
118
  v8::HandleScope scope;
119

    
120
  v8::Handle<v8::Script> script = Compile(
121
      "function outer() {\n"
122
      "  var fun1 = function() { return 1; }\n"
123
      "  var fun2 = function() { return 2; }\n"
124
      "}");
125
  CheckFunctionName(script, "return 1", "fun1");
126
  CheckFunctionName(script, "return 2", "fun2");
127
}
128

    
129

    
130
TEST(InConstructor) {
131
  InitializeVM();
132
  v8::HandleScope scope;
133

    
134
  v8::Handle<v8::Script> script = Compile(
135
      "function MyClass() {\n"
136
      "  this.method1 = function() { return 1; }\n"
137
      "  this.method2 = function() { return 2; }\n"
138
      "}");
139
  CheckFunctionName(script, "return 1", "MyClass.method1");
140
  CheckFunctionName(script, "return 2", "MyClass.method2");
141
}
142

    
143

    
144
TEST(Factory) {
145
  InitializeVM();
146
  v8::HandleScope scope;
147

    
148
  v8::Handle<v8::Script> script = Compile(
149
      "function createMyObj() {\n"
150
      "  var obj = {};\n"
151
      "  obj.method1 = function() { return 1; }\n"
152
      "  obj.method2 = function() { return 2; }\n"
153
      "  return obj;\n"
154
      "}");
155
  CheckFunctionName(script, "return 1", "obj.method1");
156
  CheckFunctionName(script, "return 2", "obj.method2");
157
}
158

    
159

    
160
TEST(Static) {
161
  InitializeVM();
162
  v8::HandleScope scope;
163

    
164
  v8::Handle<v8::Script> script = Compile(
165
      "function MyClass() {}\n"
166
      "MyClass.static1 = function() { return 1; }\n"
167
      "MyClass.static2 = function() { return 2; }\n"
168
      "MyClass.MyInnerClass = {}\n"
169
      "MyClass.MyInnerClass.static3 = function() { return 3; }\n"
170
      "MyClass.MyInnerClass.static4 = function() { return 4; }");
171
  CheckFunctionName(script, "return 1", "MyClass.static1");
172
  CheckFunctionName(script, "return 2", "MyClass.static2");
173
  CheckFunctionName(script, "return 3", "MyClass.MyInnerClass.static3");
174
  CheckFunctionName(script, "return 4", "MyClass.MyInnerClass.static4");
175
}
176

    
177

    
178
TEST(Prototype) {
179
  InitializeVM();
180
  v8::HandleScope scope;
181

    
182
  v8::Handle<v8::Script> script = Compile(
183
      "function MyClass() {}\n"
184
      "MyClass.prototype.method1 = function() { return 1; }\n"
185
      "MyClass.prototype.method2 = function() { return 2; }\n"
186
      "MyClass.MyInnerClass = function() {}\n"
187
      "MyClass.MyInnerClass.prototype.method3 = function() { return 3; }\n"
188
      "MyClass.MyInnerClass.prototype.method4 = function() { return 4; }");
189
  CheckFunctionName(script, "return 1", "MyClass.method1");
190
  CheckFunctionName(script, "return 2", "MyClass.method2");
191
  CheckFunctionName(script, "return 3", "MyClass.MyInnerClass.method3");
192
  CheckFunctionName(script, "return 4", "MyClass.MyInnerClass.method4");
193
}
194

    
195

    
196
TEST(ObjectLiteral) {
197
  InitializeVM();
198
  v8::HandleScope scope;
199

    
200
  v8::Handle<v8::Script> script = Compile(
201
      "function MyClass() {}\n"
202
      "MyClass.prototype = {\n"
203
      "  method1: function() { return 1; },\n"
204
      "  method2: function() { return 2; } }");
205
  CheckFunctionName(script, "return 1", "MyClass.method1");
206
  CheckFunctionName(script, "return 2", "MyClass.method2");
207
}
208

    
209

    
210
TEST(AsParameter) {
211
  InitializeVM();
212
  v8::HandleScope scope;
213

    
214
  v8::Handle<v8::Script> script = Compile(
215
      "function f1(a) { return a(); }\n"
216
      "function f2(a, b) { return a() + b(); }\n"
217
      "var result1 = f1(function() { return 1; })\n"
218
      "var result2 = f2(function() { return 2; }, function() { return 3; })");
219
  // Can't infer names here.
220
  CheckFunctionName(script, "return 1", "");
221
  CheckFunctionName(script, "return 2", "");
222
  CheckFunctionName(script, "return 3", "");
223
}