blob: 563cc4bf4dceb9e75812237bfe0f7cb0d6919757 [file] [log] [blame]
kasperl@chromium.orgd1e3e722009-04-14 13:38:25 +00001// 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
kasperl@chromium.org71affb52009-05-26 05:44:31 +000035using ::v8::internal::CStrVector;
36using ::v8::internal::Factory;
kasperl@chromium.orgd1e3e722009-04-14 13:38:25 +000037using ::v8::internal::Handle;
kasperl@chromium.org71affb52009-05-26 05:44:31 +000038using ::v8::internal::Heap;
kasperl@chromium.orgd1e3e722009-04-14 13:38:25 +000039using ::v8::internal::JSFunction;
40using ::v8::internal::Object;
kasperl@chromium.org71affb52009-05-26 05:44:31 +000041using ::v8::internal::Runtime;
kasperl@chromium.orgd1e3e722009-04-14 13:38:25 +000042using ::v8::internal::Script;
kasperl@chromium.org71affb52009-05-26 05:44:31 +000043using ::v8::internal::SmartPointer;
kasperl@chromium.orgd1e3e722009-04-14 13:38:25 +000044using ::v8::internal::SharedFunctionInfo;
45using ::v8::internal::String;
46
kasperl@chromium.orgd1e3e722009-04-14 13:38:25 +000047
48static v8::Persistent<v8::Context> env;
49
50
51static void InitializeVM() {
52 if (env.IsEmpty()) {
53 v8::HandleScope scope;
54 env = v8::Context::New();
55 }
56 v8::HandleScope scope;
57 env->Enter();
58}
59
60
61static void CheckFunctionName(v8::Handle<v8::Script> script,
62 const char* func_pos_src,
63 const char* ref_inferred_name) {
64 // Get script source.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000065 Handle<Object> obj = v8::Utils::OpenHandle(*script);
66 Handle<SharedFunctionInfo> shared_function;
67 if (obj->IsSharedFunctionInfo()) {
68 shared_function =
69 Handle<SharedFunctionInfo>(SharedFunctionInfo::cast(*obj));
70 } else {
71 shared_function =
72 Handle<SharedFunctionInfo>(JSFunction::cast(*obj)->shared());
73 }
74 Handle<Script> i_script(Script::cast(shared_function->script()));
kasperl@chromium.orgd1e3e722009-04-14 13:38:25 +000075 CHECK(i_script->source()->IsString());
76 Handle<String> script_src(String::cast(i_script->source()));
77
78 // Find the position of a given func source substring in the source.
79 Handle<String> func_pos_str =
kasperl@chromium.org71affb52009-05-26 05:44:31 +000080 Factory::NewStringFromAscii(CStrVector(func_pos_src));
81 int func_pos = Runtime::StringMatch(script_src, func_pos_str, 0);
kasperl@chromium.orgd1e3e722009-04-14 13:38:25 +000082 CHECK_NE(0, func_pos);
83
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +000084#ifdef ENABLE_DEBUGGER_SUPPORT
kasperl@chromium.orgd1e3e722009-04-14 13:38:25 +000085 // Obtain SharedFunctionInfo for the function.
86 Object* shared_func_info_ptr =
kasperl@chromium.org71affb52009-05-26 05:44:31 +000087 Runtime::FindSharedFunctionInfoInScript(i_script, func_pos);
88 CHECK(shared_func_info_ptr != Heap::undefined_value());
kasperl@chromium.orgd1e3e722009-04-14 13:38:25 +000089 Handle<SharedFunctionInfo> shared_func_info(
90 SharedFunctionInfo::cast(shared_func_info_ptr));
91
92 // Verify inferred function name.
kasperl@chromium.org71affb52009-05-26 05:44:31 +000093 SmartPointer<char> inferred_name =
kasperl@chromium.orgd1e3e722009-04-14 13:38:25 +000094 shared_func_info->inferred_name()->ToCString();
95 CHECK_EQ(ref_inferred_name, *inferred_name);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +000096#endif // ENABLE_DEBUGGER_SUPPORT
kasperl@chromium.orgd1e3e722009-04-14 13:38:25 +000097}
98
99
100static v8::Handle<v8::Script> Compile(const char* src) {
101 return v8::Script::Compile(v8::String::New(src));
102}
103
104
105TEST(GlobalProperty) {
106 InitializeVM();
107 v8::HandleScope scope;
108
109 v8::Handle<v8::Script> script = Compile(
110 "fun1 = function() { return 1; }\n"
111 "fun2 = function() { return 2; }\n");
112 CheckFunctionName(script, "return 1", "fun1");
113 CheckFunctionName(script, "return 2", "fun2");
114}
115
116
117TEST(GlobalVar) {
118 InitializeVM();
119 v8::HandleScope scope;
120
121 v8::Handle<v8::Script> script = Compile(
122 "var fun1 = function() { return 1; }\n"
123 "var fun2 = function() { return 2; }\n");
124 CheckFunctionName(script, "return 1", "fun1");
125 CheckFunctionName(script, "return 2", "fun2");
126}
127
128
129TEST(LocalVar) {
130 InitializeVM();
131 v8::HandleScope scope;
132
133 v8::Handle<v8::Script> script = Compile(
134 "function outer() {\n"
135 " var fun1 = function() { return 1; }\n"
136 " var fun2 = function() { return 2; }\n"
137 "}");
138 CheckFunctionName(script, "return 1", "fun1");
139 CheckFunctionName(script, "return 2", "fun2");
140}
141
142
143TEST(InConstructor) {
144 InitializeVM();
145 v8::HandleScope scope;
146
147 v8::Handle<v8::Script> script = Compile(
148 "function MyClass() {\n"
149 " this.method1 = function() { return 1; }\n"
150 " this.method2 = function() { return 2; }\n"
151 "}");
152 CheckFunctionName(script, "return 1", "MyClass.method1");
153 CheckFunctionName(script, "return 2", "MyClass.method2");
154}
155
156
157TEST(Factory) {
158 InitializeVM();
159 v8::HandleScope scope;
160
161 v8::Handle<v8::Script> script = Compile(
162 "function createMyObj() {\n"
163 " var obj = {};\n"
164 " obj.method1 = function() { return 1; }\n"
165 " obj.method2 = function() { return 2; }\n"
166 " return obj;\n"
167 "}");
168 CheckFunctionName(script, "return 1", "obj.method1");
169 CheckFunctionName(script, "return 2", "obj.method2");
170}
171
172
173TEST(Static) {
174 InitializeVM();
175 v8::HandleScope scope;
176
177 v8::Handle<v8::Script> script = Compile(
178 "function MyClass() {}\n"
179 "MyClass.static1 = function() { return 1; }\n"
180 "MyClass.static2 = function() { return 2; }\n"
181 "MyClass.MyInnerClass = {}\n"
182 "MyClass.MyInnerClass.static3 = function() { return 3; }\n"
183 "MyClass.MyInnerClass.static4 = function() { return 4; }");
184 CheckFunctionName(script, "return 1", "MyClass.static1");
185 CheckFunctionName(script, "return 2", "MyClass.static2");
186 CheckFunctionName(script, "return 3", "MyClass.MyInnerClass.static3");
187 CheckFunctionName(script, "return 4", "MyClass.MyInnerClass.static4");
188}
189
190
191TEST(Prototype) {
192 InitializeVM();
193 v8::HandleScope scope;
194
195 v8::Handle<v8::Script> script = Compile(
196 "function MyClass() {}\n"
197 "MyClass.prototype.method1 = function() { return 1; }\n"
198 "MyClass.prototype.method2 = function() { return 2; }\n"
199 "MyClass.MyInnerClass = function() {}\n"
200 "MyClass.MyInnerClass.prototype.method3 = function() { return 3; }\n"
201 "MyClass.MyInnerClass.prototype.method4 = function() { return 4; }");
202 CheckFunctionName(script, "return 1", "MyClass.method1");
203 CheckFunctionName(script, "return 2", "MyClass.method2");
204 CheckFunctionName(script, "return 3", "MyClass.MyInnerClass.method3");
205 CheckFunctionName(script, "return 4", "MyClass.MyInnerClass.method4");
206}
207
208
209TEST(ObjectLiteral) {
210 InitializeVM();
211 v8::HandleScope scope;
212
213 v8::Handle<v8::Script> script = Compile(
214 "function MyClass() {}\n"
215 "MyClass.prototype = {\n"
216 " method1: function() { return 1; },\n"
217 " method2: function() { return 2; } }");
218 CheckFunctionName(script, "return 1", "MyClass.method1");
219 CheckFunctionName(script, "return 2", "MyClass.method2");
220}
221
222
223TEST(AsParameter) {
224 InitializeVM();
225 v8::HandleScope scope;
226
227 v8::Handle<v8::Script> script = Compile(
228 "function f1(a) { return a(); }\n"
229 "function f2(a, b) { return a() + b(); }\n"
230 "var result1 = f1(function() { return 1; })\n"
231 "var result2 = f2(function() { return 2; }, function() { return 3; })");
232 // Can't infer names here.
233 CheckFunctionName(script, "return 1", "");
234 CheckFunctionName(script, "return 2", "");
235 CheckFunctionName(script, "return 3", "");
236}
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000237
238
239TEST(MultipleFuncsConditional) {
240 InitializeVM();
241 v8::HandleScope scope;
242
243 v8::Handle<v8::Script> script = Compile(
244 "fun1 = 0 ?\n"
245 " function() { return 1; } :\n"
246 " function() { return 2; }");
247 CheckFunctionName(script, "return 1", "fun1");
248 CheckFunctionName(script, "return 2", "fun1");
249}
250
251
252TEST(MultipleFuncsInLiteral) {
253 InitializeVM();
254 v8::HandleScope scope;
255
256 v8::Handle<v8::Script> script = Compile(
257 "function MyClass() {}\n"
258 "MyClass.prototype = {\n"
259 " method1: 0 ? function() { return 1; } :\n"
260 " function() { return 2; } }");
261 CheckFunctionName(script, "return 1", "MyClass.method1");
262 CheckFunctionName(script, "return 2", "MyClass.method1");
263}
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000264
265
266// See http://code.google.com/p/v8/issues/detail?id=380
267TEST(Issue380) {
268 InitializeVM();
269 v8::HandleScope scope;
270
271 v8::Handle<v8::Script> script = Compile(
272 "function a() {\n"
273 "var result = function(p,a,c,k,e,d)"
274 "{return p}(\"if blah blah\",62,1976,\'a|b\'.split(\'|\'),0,{})\n"
275 "}");
276 CheckFunctionName(script, "return p", "");
277}