blob: 0cca0c4fb2a5b52044e43633b050fdead2ec28f8 [file] [log] [blame]
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001// Copyright 2012 the V8 project authors. All rights reserved.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002// 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 <stdlib.h>
jkummerow@chromium.org486075a2011-09-07 12:44:28 +000029#include <wchar.h>
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000030
31#include "v8.h"
32
33#include "compiler.h"
vegorov@chromium.org7304bca2011-05-16 12:14:13 +000034#include "disasm.h"
35#include "disassembler.h"
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000036#include "execution.h"
37#include "factory.h"
38#include "platform.h"
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000039#include "cctest.h"
40
41using namespace v8::internal;
42
43static v8::Persistent<v8::Context> env;
44
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000045// --- P r i n t E x t e n s i o n ---
46
47class PrintExtension : public v8::Extension {
48 public:
49 PrintExtension() : v8::Extension("v8/print", kSource) { }
50 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction(
51 v8::Handle<v8::String> name);
52 static v8::Handle<v8::Value> Print(const v8::Arguments& args);
53 private:
54 static const char* kSource;
55};
56
57
58const char* PrintExtension::kSource = "native function print();";
59
60
61v8::Handle<v8::FunctionTemplate> PrintExtension::GetNativeFunction(
62 v8::Handle<v8::String> str) {
63 return v8::FunctionTemplate::New(PrintExtension::Print);
64}
65
66
67v8::Handle<v8::Value> PrintExtension::Print(const v8::Arguments& args) {
68 for (int i = 0; i < args.Length(); i++) {
69 if (i != 0) printf(" ");
70 v8::HandleScope scope;
ulan@chromium.org56c14af2012-09-20 12:51:09 +000071 v8::String::Utf8Value str(args[i]);
72 if (*str == NULL) return v8::Undefined();
73 printf("%s", *str);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000074 }
75 printf("\n");
76 return v8::Undefined();
77}
78
79
80static PrintExtension kPrintExtension;
81v8::DeclareExtension kPrintExtensionDeclaration(&kPrintExtension);
82
83
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000084static void InitializeVM() {
85 if (env.IsEmpty()) {
86 v8::HandleScope scope;
87 const char* extensions[] = { "v8/print", "v8/gc" };
88 v8::ExtensionConfiguration config(2, extensions);
89 env = v8::Context::New(&config);
90 }
91 v8::HandleScope scope;
92 env->Enter();
93}
94
95
lrn@chromium.org303ada72010-10-27 09:33:13 +000096static MaybeObject* GetGlobalProperty(const char* name) {
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +000097 Handle<String> symbol = FACTORY->LookupUtf8Symbol(name);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +000098 return Isolate::Current()->context()->global_object()->GetProperty(*symbol);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000099}
100
101
102static void SetGlobalProperty(const char* name, Object* value) {
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000103 Isolate* isolate = Isolate::Current();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000104 Handle<Object> object(value);
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000105 Handle<String> symbol = FACTORY->LookupUtf8Symbol(name);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000106 Handle<JSObject> global(Isolate::Current()->context()->global_object());
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000107 SetProperty(isolate, global, symbol, object, NONE, kNonStrictMode);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000108}
109
110
111static Handle<JSFunction> Compile(const char* source) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000112 Handle<String> source_code(FACTORY->NewStringFromUtf8(CStrVector(source)));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000113 Handle<SharedFunctionInfo> shared_function =
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000114 Compiler::Compile(source_code,
115 Handle<String>(),
116 0,
117 0,
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000118 Handle<Context>(Isolate::Current()->native_context()),
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000119 NULL,
120 NULL,
121 Handle<String>::null(),
122 NOT_NATIVES_CODE);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000123 return FACTORY->NewFunctionFromSharedFunctionInfo(shared_function,
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000124 Isolate::Current()->native_context());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000125}
126
127
128static double Inc(int x) {
129 const char* source = "result = %d + 1;";
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000130 EmbeddedVector<char, 512> buffer;
131 OS::SNPrintF(buffer, source, x);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000132
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000133 Handle<JSFunction> fun = Compile(buffer.start());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000134 if (fun.is_null()) return -1;
135
136 bool has_pending_exception;
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000137 Handle<JSObject> global(Isolate::Current()->context()->global_object());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000138 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
139 CHECK(!has_pending_exception);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000140 return GetGlobalProperty("result")->ToObjectChecked()->Number();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000141}
142
143
144TEST(Inc) {
145 InitializeVM();
146 v8::HandleScope scope;
147 CHECK_EQ(4.0, Inc(3));
148}
149
150
151static double Add(int x, int y) {
152 Handle<JSFunction> fun = Compile("result = x + y;");
153 if (fun.is_null()) return -1;
154
155 SetGlobalProperty("x", Smi::FromInt(x));
156 SetGlobalProperty("y", Smi::FromInt(y));
157 bool has_pending_exception;
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000158 Handle<JSObject> global(Isolate::Current()->context()->global_object());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000159 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
160 CHECK(!has_pending_exception);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000161 return GetGlobalProperty("result")->ToObjectChecked()->Number();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000162}
163
164
165TEST(Add) {
166 InitializeVM();
167 v8::HandleScope scope;
168 CHECK_EQ(5.0, Add(2, 3));
169}
170
171
172static double Abs(int x) {
173 Handle<JSFunction> fun = Compile("if (x < 0) result = -x; else result = x;");
174 if (fun.is_null()) return -1;
175
176 SetGlobalProperty("x", Smi::FromInt(x));
177 bool has_pending_exception;
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000178 Handle<JSObject> global(Isolate::Current()->context()->global_object());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000179 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
180 CHECK(!has_pending_exception);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000181 return GetGlobalProperty("result")->ToObjectChecked()->Number();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000182}
183
184
185TEST(Abs) {
186 InitializeVM();
187 v8::HandleScope scope;
188 CHECK_EQ(3.0, Abs(-3));
189}
190
191
192static double Sum(int n) {
193 Handle<JSFunction> fun =
194 Compile("s = 0; while (n > 0) { s += n; n -= 1; }; result = s;");
195 if (fun.is_null()) return -1;
196
197 SetGlobalProperty("n", Smi::FromInt(n));
198 bool has_pending_exception;
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000199 Handle<JSObject> global(Isolate::Current()->context()->global_object());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000200 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
201 CHECK(!has_pending_exception);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000202 return GetGlobalProperty("result")->ToObjectChecked()->Number();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000203}
204
205
206TEST(Sum) {
207 InitializeVM();
208 v8::HandleScope scope;
209 CHECK_EQ(5050.0, Sum(100));
210}
211
212
213TEST(Print) {
214 InitializeVM();
215 v8::HandleScope scope;
216 const char* source = "for (n = 0; n < 100; ++n) print(n, 1, 2);";
217 Handle<JSFunction> fun = Compile(source);
218 if (fun.is_null()) return;
219 bool has_pending_exception;
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000220 Handle<JSObject> global(Isolate::Current()->context()->global_object());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000221 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
222 CHECK(!has_pending_exception);
223}
224
225
226// The following test method stems from my coding efforts today. It
227// tests all the functionality I have added to the compiler today
228TEST(Stuff) {
229 InitializeVM();
230 v8::HandleScope scope;
231 const char* source =
232 "r = 0;\n"
233 "a = new Object;\n"
234 "if (a == a) r+=1;\n" // 1
235 "if (a != new Object()) r+=2;\n" // 2
236 "a.x = 42;\n"
237 "if (a.x == 42) r+=4;\n" // 4
238 "function foo() { var x = 87; return x; }\n"
239 "if (foo() == 87) r+=8;\n" // 8
240 "function bar() { var x; x = 99; return x; }\n"
241 "if (bar() == 99) r+=16;\n" // 16
242 "function baz() { var x = 1, y, z = 2; y = 3; return x + y + z; }\n"
243 "if (baz() == 6) r+=32;\n" // 32
244 "function Cons0() { this.x = 42; this.y = 87; }\n"
245 "if (new Cons0().x == 42) r+=64;\n" // 64
246 "if (new Cons0().y == 87) r+=128;\n" // 128
247 "function Cons2(x, y) { this.sum = x + y; }\n"
248 "if (new Cons2(3,4).sum == 7) r+=256;"; // 256
249
250 Handle<JSFunction> fun = Compile(source);
251 CHECK(!fun.is_null());
252 bool has_pending_exception;
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000253 Handle<JSObject> global(Isolate::Current()->context()->global_object());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000254 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
255 CHECK(!has_pending_exception);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000256 CHECK_EQ(511.0, GetGlobalProperty("r")->ToObjectChecked()->Number());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000257}
258
259
260TEST(UncaughtThrow) {
261 InitializeVM();
262 v8::HandleScope scope;
263
264 const char* source = "throw 42;";
265 Handle<JSFunction> fun = Compile(source);
266 CHECK(!fun.is_null());
267 bool has_pending_exception;
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000268 Handle<JSObject> global(Isolate::Current()->context()->global_object());
yangguo@chromium.org659ceec2012-01-26 07:37:54 +0000269 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000270 CHECK(has_pending_exception);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000271 CHECK_EQ(42.0, Isolate::Current()->pending_exception()->
272 ToObjectChecked()->Number());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000273}
274
275
276// Tests calling a builtin function from C/C++ code, and the builtin function
277// performs GC. It creates a stack frame looks like following:
278// | C (PerformGC) |
279// | JS-to-C |
280// | JS |
281// | C-to-JS |
282TEST(C2JSFrames) {
283 InitializeVM();
284 v8::HandleScope scope;
285
286 const char* source = "function foo(a) { gc(), print(a); }";
287
288 Handle<JSFunction> fun0 = Compile(source);
289 CHECK(!fun0.is_null());
290
291 // Run the generated code to populate the global object with 'foo'.
292 bool has_pending_exception;
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000293 Handle<JSObject> global(Isolate::Current()->context()->global_object());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000294 Execution::Call(fun0, global, 0, NULL, &has_pending_exception);
295 CHECK(!has_pending_exception);
296
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000297 Object* foo_symbol =
298 FACTORY->LookupOneByteSymbol(STATIC_ASCII_VECTOR("foo"))->
299 ToObjectChecked();
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000300 MaybeObject* fun1_object = Isolate::Current()->context()->global_object()->
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000301 GetProperty(String::cast(foo_symbol));
lrn@chromium.org303ada72010-10-27 09:33:13 +0000302 Handle<Object> fun1(fun1_object->ToObjectChecked());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000303 CHECK(fun1->IsJSFunction());
304
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000305 Handle<Object> argv[] =
306 { FACTORY->LookupOneByteSymbol(STATIC_ASCII_VECTOR("hello")) };
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000307 Execution::Call(Handle<JSFunction>::cast(fun1),
308 global,
309 ARRAY_SIZE(argv),
310 argv,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000311 &has_pending_exception);
312 CHECK(!has_pending_exception);
313}
ager@chromium.org381abbb2009-02-25 13:23:22 +0000314
315
316// Regression 236. Calling InitLineEnds on a Script with undefined
317// source resulted in crash.
318TEST(Regression236) {
319 InitializeVM();
320 v8::HandleScope scope;
321
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000322 Handle<Script> script = FACTORY->NewScript(FACTORY->empty_string());
323 script->set_source(HEAP->undefined_value());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000324 CHECK_EQ(-1, GetScriptLineNumber(script, 0));
325 CHECK_EQ(-1, GetScriptLineNumber(script, 100));
326 CHECK_EQ(-1, GetScriptLineNumber(script, -1));
ager@chromium.org381abbb2009-02-25 13:23:22 +0000327}
ager@chromium.org5c838252010-02-19 08:53:10 +0000328
329
330TEST(GetScriptLineNumber) {
331 LocalContext env;
332 v8::HandleScope scope;
333 v8::ScriptOrigin origin = v8::ScriptOrigin(v8::String::New("test"));
334 const char function_f[] = "function f() {}";
335 const int max_rows = 1000;
336 const int buffer_size = max_rows + sizeof(function_f);
337 ScopedVector<char> buffer(buffer_size);
338 memset(buffer.start(), '\n', buffer_size - 1);
339 buffer[buffer_size - 1] = '\0';
340
341 for (int i = 0; i < max_rows; ++i) {
342 if (i > 0)
343 buffer[i - 1] = '\n';
344 memcpy(&buffer[i], function_f, sizeof(function_f) - 1);
345 v8::Handle<v8::String> script_body = v8::String::New(buffer.start());
346 v8::Script::Compile(script_body, &origin)->Run();
347 v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast(
348 env->Global()->Get(v8::String::New("f")));
349 CHECK_EQ(i, f->GetScriptLineNumber());
350 }
351}
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000352
353
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000354// Test that optimized code for different closures is actually shared
355// immediately by the FastNewClosureStub when run in the same context.
356TEST(OptimizedCodeSharing) {
yangguo@chromium.orgde0db002012-06-22 13:44:28 +0000357 // Skip test if --cache-optimized-code is not activated by default because
358 // FastNewClosureStub that is baked into the snapshot is incorrect.
359 if (!FLAG_cache_optimized_code) return;
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000360 FLAG_allow_natives_syntax = true;
361 InitializeVM();
362 v8::HandleScope scope;
363 for (int i = 0; i < 10; i++) {
364 LocalContext env;
365 env->Global()->Set(v8::String::New("x"), v8::Integer::New(i));
366 CompileRun("function MakeClosure() {"
367 " return function() { return x; };"
368 "}"
369 "var closure0 = MakeClosure();"
370 "%DebugPrint(closure0());"
371 "%OptimizeFunctionOnNextCall(closure0);"
372 "%DebugPrint(closure0());"
373 "var closure1 = MakeClosure();"
374 "var closure2 = MakeClosure();");
375 Handle<JSFunction> fun1 = v8::Utils::OpenHandle(
376 *v8::Local<v8::Function>::Cast(env->Global()->Get(v8_str("closure1"))));
377 Handle<JSFunction> fun2 = v8::Utils::OpenHandle(
378 *v8::Local<v8::Function>::Cast(env->Global()->Get(v8_str("closure2"))));
379 CHECK(fun1->IsOptimized() || !fun1->IsOptimizable());
380 CHECK(fun2->IsOptimized() || !fun2->IsOptimizable());
381 CHECK_EQ(fun1->code(), fun2->code());
382 }
383}
384
385
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000386#ifdef ENABLE_DISASSEMBLER
387static Handle<JSFunction> GetJSFunction(v8::Handle<v8::Object> obj,
388 const char* property_name) {
389 v8::Local<v8::Function> fun =
390 v8::Local<v8::Function>::Cast(obj->Get(v8_str(property_name)));
391 return v8::Utils::OpenHandle(*fun);
392}
393
394
395static void CheckCodeForUnsafeLiteral(Handle<JSFunction> f) {
396 // Create a disassembler with default name lookup.
397 disasm::NameConverter name_converter;
398 disasm::Disassembler d(name_converter);
399
400 if (f->code()->kind() == Code::FUNCTION) {
401 Address pc = f->code()->instruction_start();
402 int decode_size =
403 Min(f->code()->instruction_size(),
404 static_cast<int>(f->code()->stack_check_table_offset()));
405 Address end = pc + decode_size;
406
407 v8::internal::EmbeddedVector<char, 128> decode_buffer;
jkummerow@chromium.org7a6fc812012-06-27 11:12:38 +0000408 v8::internal::EmbeddedVector<char, 128> smi_hex_buffer;
409 Smi* smi = Smi::FromInt(12345678);
410 OS::SNPrintF(smi_hex_buffer, "0x%lx", reinterpret_cast<intptr_t>(smi));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000411 while (pc < end) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000412 int num_const = d.ConstantPoolSizeAt(pc);
413 if (num_const >= 0) {
ricow@chromium.org9fa09672011-07-25 11:05:35 +0000414 pc += (num_const + 1) * kPointerSize;
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000415 } else {
416 pc += d.InstructionDecode(decode_buffer, pc);
jkummerow@chromium.org7a6fc812012-06-27 11:12:38 +0000417 CHECK(strstr(decode_buffer.start(), smi_hex_buffer.start()) == NULL);
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000418 }
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000419 }
420 }
421}
422
423
424TEST(SplitConstantsInFullCompiler) {
425 v8::HandleScope scope;
426 LocalContext env;
427
428 CompileRun("function f() { a = 12345678 }; f();");
429 CheckCodeForUnsafeLiteral(GetJSFunction(env->Global(), "f"));
430 CompileRun("function f(x) { a = 12345678 + x}; f(1);");
431 CheckCodeForUnsafeLiteral(GetJSFunction(env->Global(), "f"));
432 CompileRun("function f(x) { var arguments = 1; x += 12345678}; f(1);");
433 CheckCodeForUnsafeLiteral(GetJSFunction(env->Global(), "f"));
434 CompileRun("function f(x) { var arguments = 1; x = 12345678}; f(1);");
435 CheckCodeForUnsafeLiteral(GetJSFunction(env->Global(), "f"));
436}
437#endif