blob: db2ccb29af6e12938913b30c24d7a6b1ff1aef37 [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +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>
Ben Murdoch589d6972011-11-30 16:04:58 +000029#include <wchar.h>
Steve Blocka7e24c12009-10-30 11:49:00 +000030
Ben Murdochb8a8cc12014-11-26 15:28:44 +000031#include "src/v8.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000032
Ben Murdochb8a8cc12014-11-26 15:28:44 +000033#include "src/compiler.h"
34#include "src/disasm.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000035#include "src/parsing/parser.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000036#include "test/cctest/cctest.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000037
38using namespace v8::internal;
39
Ben Murdochb8a8cc12014-11-26 15:28:44 +000040static Handle<Object> GetGlobalProperty(const char* name) {
41 Isolate* isolate = CcTest::i_isolate();
42 return Object::GetProperty(
43 isolate, isolate->global_object(), name).ToHandleChecked();
Steve Blocka7e24c12009-10-30 11:49:00 +000044}
45
46
47static void SetGlobalProperty(const char* name, Object* value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000048 Isolate* isolate = CcTest::i_isolate();
49 Handle<Object> object(value, isolate);
50 Handle<String> internalized_name =
51 isolate->factory()->InternalizeUtf8String(name);
52 Handle<JSObject> global(isolate->context()->global_object());
53 Runtime::SetObjectProperty(isolate, global, internalized_name, object,
54 SLOPPY).Check();
Steve Blocka7e24c12009-10-30 11:49:00 +000055}
56
57
58static Handle<JSFunction> Compile(const char* source) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000059 Isolate* isolate = CcTest::i_isolate();
60 Handle<String> source_code = isolate->factory()->NewStringFromUtf8(
61 CStrVector(source)).ToHandleChecked();
62 Handle<SharedFunctionInfo> shared_function = Compiler::CompileScript(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000063 source_code, Handle<String>(), 0, 0, v8::ScriptOriginOptions(),
64 Handle<Object>(), Handle<Context>(isolate->native_context()), NULL, NULL,
65 v8::ScriptCompiler::kNoCompileOptions, NOT_NATIVES_CODE, false);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000066 return isolate->factory()->NewFunctionFromSharedFunctionInfo(
67 shared_function, isolate->native_context());
Steve Blocka7e24c12009-10-30 11:49:00 +000068}
69
70
Ben Murdochb8a8cc12014-11-26 15:28:44 +000071static double Inc(Isolate* isolate, int x) {
Steve Blocka7e24c12009-10-30 11:49:00 +000072 const char* source = "result = %d + 1;";
73 EmbeddedVector<char, 512> buffer;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000074 SNPrintF(buffer, source, x);
Steve Blocka7e24c12009-10-30 11:49:00 +000075
76 Handle<JSFunction> fun = Compile(buffer.start());
77 if (fun.is_null()) return -1;
78
Ben Murdochb8a8cc12014-11-26 15:28:44 +000079 Handle<JSObject> global(isolate->context()->global_object());
80 Execution::Call(isolate, fun, global, 0, NULL).Check();
81 return GetGlobalProperty("result")->Number();
Steve Blocka7e24c12009-10-30 11:49:00 +000082}
83
84
85TEST(Inc) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000086 CcTest::InitializeVM();
87 v8::HandleScope scope(CcTest::isolate());
88 CHECK_EQ(4.0, Inc(CcTest::i_isolate(), 3));
Steve Blocka7e24c12009-10-30 11:49:00 +000089}
90
91
Ben Murdochb8a8cc12014-11-26 15:28:44 +000092static double Add(Isolate* isolate, int x, int y) {
Steve Blocka7e24c12009-10-30 11:49:00 +000093 Handle<JSFunction> fun = Compile("result = x + y;");
94 if (fun.is_null()) return -1;
95
96 SetGlobalProperty("x", Smi::FromInt(x));
97 SetGlobalProperty("y", Smi::FromInt(y));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000098 Handle<JSObject> global(isolate->context()->global_object());
99 Execution::Call(isolate, fun, global, 0, NULL).Check();
100 return GetGlobalProperty("result")->Number();
Steve Blocka7e24c12009-10-30 11:49:00 +0000101}
102
103
104TEST(Add) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000105 CcTest::InitializeVM();
106 v8::HandleScope scope(CcTest::isolate());
107 CHECK_EQ(5.0, Add(CcTest::i_isolate(), 2, 3));
Steve Blocka7e24c12009-10-30 11:49:00 +0000108}
109
110
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000111static double Abs(Isolate* isolate, int x) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000112 Handle<JSFunction> fun = Compile("if (x < 0) result = -x; else result = x;");
113 if (fun.is_null()) return -1;
114
115 SetGlobalProperty("x", Smi::FromInt(x));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000116 Handle<JSObject> global(isolate->context()->global_object());
117 Execution::Call(isolate, fun, global, 0, NULL).Check();
118 return GetGlobalProperty("result")->Number();
Steve Blocka7e24c12009-10-30 11:49:00 +0000119}
120
121
122TEST(Abs) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000123 CcTest::InitializeVM();
124 v8::HandleScope scope(CcTest::isolate());
125 CHECK_EQ(3.0, Abs(CcTest::i_isolate(), -3));
Steve Blocka7e24c12009-10-30 11:49:00 +0000126}
127
128
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000129static double Sum(Isolate* isolate, int n) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000130 Handle<JSFunction> fun =
131 Compile("s = 0; while (n > 0) { s += n; n -= 1; }; result = s;");
132 if (fun.is_null()) return -1;
133
134 SetGlobalProperty("n", Smi::FromInt(n));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000135 Handle<JSObject> global(isolate->context()->global_object());
136 Execution::Call(isolate, fun, global, 0, NULL).Check();
137 return GetGlobalProperty("result")->Number();
Steve Blocka7e24c12009-10-30 11:49:00 +0000138}
139
140
141TEST(Sum) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000142 CcTest::InitializeVM();
143 v8::HandleScope scope(CcTest::isolate());
144 CHECK_EQ(5050.0, Sum(CcTest::i_isolate(), 100));
Steve Blocka7e24c12009-10-30 11:49:00 +0000145}
146
147
148TEST(Print) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000149 v8::HandleScope scope(CcTest::isolate());
150 v8::Local<v8::Context> context = CcTest::NewContext(PRINT_EXTENSION);
151 v8::Context::Scope context_scope(context);
Steve Blocka7e24c12009-10-30 11:49:00 +0000152 const char* source = "for (n = 0; n < 100; ++n) print(n, 1, 2);";
153 Handle<JSFunction> fun = Compile(source);
154 if (fun.is_null()) return;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000155 Handle<JSObject> global(CcTest::i_isolate()->context()->global_object());
156 Execution::Call(CcTest::i_isolate(), fun, global, 0, NULL).Check();
Steve Blocka7e24c12009-10-30 11:49:00 +0000157}
158
159
160// The following test method stems from my coding efforts today. It
161// tests all the functionality I have added to the compiler today
162TEST(Stuff) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000163 CcTest::InitializeVM();
164 v8::HandleScope scope(CcTest::isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000165 const char* source =
166 "r = 0;\n"
167 "a = new Object;\n"
168 "if (a == a) r+=1;\n" // 1
169 "if (a != new Object()) r+=2;\n" // 2
170 "a.x = 42;\n"
171 "if (a.x == 42) r+=4;\n" // 4
172 "function foo() { var x = 87; return x; }\n"
173 "if (foo() == 87) r+=8;\n" // 8
174 "function bar() { var x; x = 99; return x; }\n"
175 "if (bar() == 99) r+=16;\n" // 16
176 "function baz() { var x = 1, y, z = 2; y = 3; return x + y + z; }\n"
177 "if (baz() == 6) r+=32;\n" // 32
178 "function Cons0() { this.x = 42; this.y = 87; }\n"
179 "if (new Cons0().x == 42) r+=64;\n" // 64
180 "if (new Cons0().y == 87) r+=128;\n" // 128
181 "function Cons2(x, y) { this.sum = x + y; }\n"
182 "if (new Cons2(3,4).sum == 7) r+=256;"; // 256
183
184 Handle<JSFunction> fun = Compile(source);
185 CHECK(!fun.is_null());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000186 Handle<JSObject> global(CcTest::i_isolate()->context()->global_object());
187 Execution::Call(
188 CcTest::i_isolate(), fun, global, 0, NULL).Check();
189 CHECK_EQ(511.0, GetGlobalProperty("r")->Number());
Steve Blocka7e24c12009-10-30 11:49:00 +0000190}
191
192
193TEST(UncaughtThrow) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000194 CcTest::InitializeVM();
195 v8::HandleScope scope(CcTest::isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000196
197 const char* source = "throw 42;";
198 Handle<JSFunction> fun = Compile(source);
199 CHECK(!fun.is_null());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000200 Isolate* isolate = fun->GetIsolate();
201 Handle<JSObject> global(isolate->context()->global_object());
202 CHECK(Execution::Call(isolate, fun, global, 0, NULL).is_null());
203 CHECK_EQ(42.0, isolate->pending_exception()->Number());
Steve Blocka7e24c12009-10-30 11:49:00 +0000204}
205
206
207// Tests calling a builtin function from C/C++ code, and the builtin function
208// performs GC. It creates a stack frame looks like following:
209// | C (PerformGC) |
210// | JS-to-C |
211// | JS |
212// | C-to-JS |
213TEST(C2JSFrames) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000214 FLAG_expose_gc = true;
215 v8::HandleScope scope(CcTest::isolate());
216 v8::Local<v8::Context> context =
217 CcTest::NewContext(PRINT_EXTENSION | GC_EXTENSION);
218 v8::Context::Scope context_scope(context);
Steve Blocka7e24c12009-10-30 11:49:00 +0000219
220 const char* source = "function foo(a) { gc(), print(a); }";
221
222 Handle<JSFunction> fun0 = Compile(source);
223 CHECK(!fun0.is_null());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000224 Isolate* isolate = fun0->GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +0000225
226 // Run the generated code to populate the global object with 'foo'.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000227 Handle<JSObject> global(isolate->context()->global_object());
228 Execution::Call(isolate, fun0, global, 0, NULL).Check();
Steve Blocka7e24c12009-10-30 11:49:00 +0000229
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000230 Handle<String> foo_string =
231 isolate->factory()->InternalizeOneByteString(STATIC_CHAR_VECTOR("foo"));
232 Handle<Object> fun1 = Object::GetProperty(
233 isolate->global_object(), foo_string).ToHandleChecked();
Steve Blocka7e24c12009-10-30 11:49:00 +0000234 CHECK(fun1->IsJSFunction());
235
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000236 Handle<Object> argv[] = {isolate->factory()->InternalizeOneByteString(
237 STATIC_CHAR_VECTOR("hello"))};
238 Execution::Call(isolate,
239 Handle<JSFunction>::cast(fun1),
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100240 global,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000241 arraysize(argv),
242 argv).Check();
Steve Blocka7e24c12009-10-30 11:49:00 +0000243}
244
245
246// Regression 236. Calling InitLineEnds on a Script with undefined
247// source resulted in crash.
248TEST(Regression236) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000249 CcTest::InitializeVM();
250 Isolate* isolate = CcTest::i_isolate();
251 Factory* factory = isolate->factory();
252 v8::HandleScope scope(CcTest::isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000253
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000254 Handle<Script> script = factory->NewScript(factory->empty_string());
255 script->set_source(CcTest::heap()->undefined_value());
256 CHECK_EQ(-1, Script::GetLineNumber(script, 0));
257 CHECK_EQ(-1, Script::GetLineNumber(script, 100));
258 CHECK_EQ(-1, Script::GetLineNumber(script, -1));
Steve Blocka7e24c12009-10-30 11:49:00 +0000259}
Andrei Popescu402d9372010-02-26 13:31:12 +0000260
261
262TEST(GetScriptLineNumber) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000263 LocalContext context;
264 v8::HandleScope scope(CcTest::isolate());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000265 v8::ScriptOrigin origin = v8::ScriptOrigin(v8_str("test"));
Andrei Popescu402d9372010-02-26 13:31:12 +0000266 const char function_f[] = "function f() {}";
267 const int max_rows = 1000;
268 const int buffer_size = max_rows + sizeof(function_f);
269 ScopedVector<char> buffer(buffer_size);
270 memset(buffer.start(), '\n', buffer_size - 1);
271 buffer[buffer_size - 1] = '\0';
272
273 for (int i = 0; i < max_rows; ++i) {
274 if (i > 0)
275 buffer[i - 1] = '\n';
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000276 MemCopy(&buffer[i], function_f, sizeof(function_f) - 1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000277 v8::Local<v8::String> script_body = v8_str(buffer.start());
278 v8::Script::Compile(context.local(), script_body, &origin)
279 .ToLocalChecked()
280 ->Run(context.local())
281 .ToLocalChecked();
282 v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast(
283 context->Global()->Get(context.local(), v8_str("f")).ToLocalChecked());
Andrei Popescu402d9372010-02-26 13:31:12 +0000284 CHECK_EQ(i, f->GetScriptLineNumber());
285 }
286}
Steve Block053d10c2011-06-13 19:13:29 +0100287
288
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000289TEST(FeedbackVectorPreservedAcrossRecompiles) {
290 if (i::FLAG_always_opt || !i::FLAG_crankshaft) return;
291 i::FLAG_allow_natives_syntax = true;
292 CcTest::InitializeVM();
293 if (!CcTest::i_isolate()->use_crankshaft()) return;
294 v8::HandleScope scope(CcTest::isolate());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000295 v8::Local<v8::Context> context = CcTest::isolate()->GetCurrentContext();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000296
297 // Make sure function f has a call that uses a type feedback slot.
298 CompileRun("function fun() {};"
299 "fun1 = fun;"
300 "function f(a) { a(); } f(fun1);");
301
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000302 Handle<JSFunction> f = Handle<JSFunction>::cast(
303 v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast(
304 CcTest::global()->Get(context, v8_str("f")).ToLocalChecked())));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000305
306 // We shouldn't have deoptimization support. We want to recompile and
307 // verify that our feedback vector preserves information.
308 CHECK(!f->shared()->has_deoptimization_support());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400309 Handle<TypeFeedbackVector> feedback_vector(f->shared()->feedback_vector());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000310
311 // Verify that we gathered feedback.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000312 CHECK(!feedback_vector->is_empty());
313 FeedbackVectorSlot slot_for_a(0);
314 Object* object = feedback_vector->Get(slot_for_a);
315 CHECK(object->IsWeakCell() &&
316 WeakCell::cast(object)->value()->IsJSFunction());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000317
318 CompileRun("%OptimizeFunctionOnNextCall(f); f(fun1);");
319
320 // Verify that the feedback is still "gathered" despite a recompilation
321 // of the full code.
322 CHECK(f->IsOptimized());
323 CHECK(f->shared()->has_deoptimization_support());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000324 object = f->shared()->feedback_vector()->Get(slot_for_a);
325 CHECK(object->IsWeakCell() &&
326 WeakCell::cast(object)->value()->IsJSFunction());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000327}
328
329
330TEST(FeedbackVectorUnaffectedByScopeChanges) {
331 if (i::FLAG_always_opt || !i::FLAG_lazy) return;
332 CcTest::InitializeVM();
333 v8::HandleScope scope(CcTest::isolate());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000334 v8::Local<v8::Context> context = CcTest::isolate()->GetCurrentContext();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000335
336 CompileRun("function builder() {"
337 " call_target = function() { return 3; };"
338 " return (function() {"
339 " eval('');"
340 " return function() {"
341 " 'use strict';"
342 " call_target();"
343 " }"
344 " })();"
345 "}"
346 "morphing_call = builder();");
347
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000348 Handle<JSFunction> f = Handle<JSFunction>::cast(v8::Utils::OpenHandle(
349 *v8::Local<v8::Function>::Cast(CcTest::global()
350 ->Get(context, v8_str("morphing_call"))
351 .ToLocalChecked())));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000352
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400353 // Not compiled, and so no feedback vector allocated yet.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000354 CHECK(!f->shared()->is_compiled());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000355 CHECK(f->shared()->feedback_vector()->is_empty());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000356
357 CompileRun("morphing_call();");
358
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400359 // Now a feedback vector is allocated.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000360 CHECK(f->shared()->is_compiled());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000361 CHECK(!f->shared()->feedback_vector()->is_empty());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000362}
363
364
365// Test that optimized code for different closures is actually shared
366// immediately by the FastNewClosureStub when run in the same context.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000367TEST(OptimizedCodeSharing1) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000368 FLAG_stress_compaction = false;
369 FLAG_allow_natives_syntax = true;
370 CcTest::InitializeVM();
371 v8::HandleScope scope(CcTest::isolate());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000372 for (int i = 0; i < 3; i++) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000373 LocalContext env;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000374 env->Global()
375 ->Set(env.local(), v8_str("x"), v8::Integer::New(CcTest::isolate(), i))
376 .FromJust();
377 CompileRun(
378 "function MakeClosure() {"
379 " return function() { return x; };"
380 "}"
381 "var closure0 = MakeClosure();"
382 "%DebugPrint(closure0());"
383 "%OptimizeFunctionOnNextCall(closure0);"
384 "%DebugPrint(closure0());"
385 "var closure1 = MakeClosure();"
386 "var closure2 = MakeClosure();");
387 Handle<JSFunction> fun1 = Handle<JSFunction>::cast(
388 v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast(
389 env->Global()
390 ->Get(env.local(), v8_str("closure1"))
391 .ToLocalChecked())));
392 Handle<JSFunction> fun2 = Handle<JSFunction>::cast(
393 v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast(
394 env->Global()
395 ->Get(env.local(), v8_str("closure2"))
396 .ToLocalChecked())));
397 CHECK(fun1->IsOptimized() || !CcTest::i_isolate()->use_crankshaft());
398 CHECK(fun2->IsOptimized() || !CcTest::i_isolate()->use_crankshaft());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000399 CHECK_EQ(fun1->code(), fun2->code());
400 }
401}
402
403
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000404// Test that optimized code for different closures is actually shared
405// immediately by the FastNewClosureStub when run different contexts.
406TEST(OptimizedCodeSharing2) {
407 if (FLAG_stress_compaction) return;
408 FLAG_allow_natives_syntax = true;
409 FLAG_native_context_specialization = false;
410 FLAG_turbo_cache_shared_code = true;
411 const char* flag = "--turbo-filter=*";
412 FlagList::SetFlagsFromString(flag, StrLength(flag));
413 CcTest::InitializeVM();
414 v8::HandleScope scope(CcTest::isolate());
415 v8::Local<v8::Script> script = v8_compile(
416 "function MakeClosure() {"
417 " return function() { return x; };"
418 "}");
419 Handle<Code> reference_code;
420 {
421 LocalContext env;
422 env->Global()
423 ->Set(env.local(), v8_str("x"), v8::Integer::New(CcTest::isolate(), 23))
424 .FromJust();
425 script->GetUnboundScript()
426 ->BindToCurrentContext()
427 ->Run(env.local())
428 .ToLocalChecked();
429 CompileRun(
430 "var closure0 = MakeClosure();"
431 "%DebugPrint(closure0());"
432 "%OptimizeFunctionOnNextCall(closure0);"
433 "%DebugPrint(closure0());");
434 Handle<JSFunction> fun0 = Handle<JSFunction>::cast(
435 v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast(
436 env->Global()
437 ->Get(env.local(), v8_str("closure0"))
438 .ToLocalChecked())));
439 CHECK(fun0->IsOptimized() || !CcTest::i_isolate()->use_crankshaft());
440 reference_code = handle(fun0->code());
441 }
442 for (int i = 0; i < 3; i++) {
443 LocalContext env;
444 env->Global()
445 ->Set(env.local(), v8_str("x"), v8::Integer::New(CcTest::isolate(), i))
446 .FromJust();
447 script->GetUnboundScript()
448 ->BindToCurrentContext()
449 ->Run(env.local())
450 .ToLocalChecked();
451 CompileRun(
452 "var closure0 = MakeClosure();"
453 "%DebugPrint(closure0());"
454 "%OptimizeFunctionOnNextCall(closure0);"
455 "%DebugPrint(closure0());"
456 "var closure1 = MakeClosure();"
457 "var closure2 = MakeClosure();");
458 Handle<JSFunction> fun1 = Handle<JSFunction>::cast(
459 v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast(
460 env->Global()
461 ->Get(env.local(), v8_str("closure1"))
462 .ToLocalChecked())));
463 Handle<JSFunction> fun2 = Handle<JSFunction>::cast(
464 v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast(
465 env->Global()
466 ->Get(env.local(), v8_str("closure2"))
467 .ToLocalChecked())));
468 CHECK(fun1->IsOptimized() || !CcTest::i_isolate()->use_crankshaft());
469 CHECK(fun2->IsOptimized() || !CcTest::i_isolate()->use_crankshaft());
470 CHECK_EQ(*reference_code, fun1->code());
471 CHECK_EQ(*reference_code, fun2->code());
472 }
473}
474
475
476// Test that optimized code for different closures is actually shared
477// immediately by the FastNewClosureStub without context-dependent entries.
478TEST(OptimizedCodeSharing3) {
479 if (FLAG_stress_compaction) return;
480 FLAG_allow_natives_syntax = true;
481 FLAG_native_context_specialization = false;
482 FLAG_turbo_cache_shared_code = true;
483 const char* flag = "--turbo-filter=*";
484 FlagList::SetFlagsFromString(flag, StrLength(flag));
485 CcTest::InitializeVM();
486 v8::HandleScope scope(CcTest::isolate());
487 v8::Local<v8::Script> script = v8_compile(
488 "function MakeClosure() {"
489 " return function() { return x; };"
490 "}");
491 Handle<Code> reference_code;
492 {
493 LocalContext env;
494 env->Global()
495 ->Set(env.local(), v8_str("x"), v8::Integer::New(CcTest::isolate(), 23))
496 .FromJust();
497 script->GetUnboundScript()
498 ->BindToCurrentContext()
499 ->Run(env.local())
500 .ToLocalChecked();
501 CompileRun(
502 "var closure0 = MakeClosure();"
503 "%DebugPrint(closure0());"
504 "%OptimizeFunctionOnNextCall(closure0);"
505 "%DebugPrint(closure0());");
506 Handle<JSFunction> fun0 = Handle<JSFunction>::cast(
507 v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast(
508 env->Global()
509 ->Get(env.local(), v8_str("closure0"))
510 .ToLocalChecked())));
511 CHECK(fun0->IsOptimized() || !CcTest::i_isolate()->use_crankshaft());
512 reference_code = handle(fun0->code());
513 // Evict only the context-dependent entry from the optimized code map. This
514 // leaves it in a state where only the context-independent entry exists.
515 fun0->shared()->TrimOptimizedCodeMap(SharedFunctionInfo::kEntryLength);
516 }
517 for (int i = 0; i < 3; i++) {
518 LocalContext env;
519 env->Global()
520 ->Set(env.local(), v8_str("x"), v8::Integer::New(CcTest::isolate(), i))
521 .FromJust();
522 script->GetUnboundScript()
523 ->BindToCurrentContext()
524 ->Run(env.local())
525 .ToLocalChecked();
526 CompileRun(
527 "var closure0 = MakeClosure();"
528 "%DebugPrint(closure0());"
529 "%OptimizeFunctionOnNextCall(closure0);"
530 "%DebugPrint(closure0());"
531 "var closure1 = MakeClosure();"
532 "var closure2 = MakeClosure();");
533 Handle<JSFunction> fun1 = Handle<JSFunction>::cast(
534 v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast(
535 env->Global()
536 ->Get(env.local(), v8_str("closure1"))
537 .ToLocalChecked())));
538 Handle<JSFunction> fun2 = Handle<JSFunction>::cast(
539 v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast(
540 env->Global()
541 ->Get(env.local(), v8_str("closure2"))
542 .ToLocalChecked())));
543 CHECK(fun1->IsOptimized() || !CcTest::i_isolate()->use_crankshaft());
544 CHECK(fun2->IsOptimized() || !CcTest::i_isolate()->use_crankshaft());
545 CHECK_EQ(*reference_code, fun1->code());
546 CHECK_EQ(*reference_code, fun2->code());
547 }
548}
549
550
551TEST(CompileFunctionInContext) {
552 CcTest::InitializeVM();
553 v8::HandleScope scope(CcTest::isolate());
554 LocalContext env;
555 CompileRun("var r = 10;");
556 v8::Local<v8::Object> math = v8::Local<v8::Object>::Cast(
557 env->Global()->Get(env.local(), v8_str("Math")).ToLocalChecked());
558 v8::ScriptCompiler::Source script_source(v8_str(
559 "a = PI * r * r;"
560 "x = r * cos(PI);"
561 "y = r * sin(PI / 2);"));
Steve Block053d10c2011-06-13 19:13:29 +0100562 v8::Local<v8::Function> fun =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000563 v8::ScriptCompiler::CompileFunctionInContext(env.local(), &script_source,
564 0, NULL, 1, &math)
565 .ToLocalChecked();
566 CHECK(!fun.IsEmpty());
567 fun->Call(env.local(), env->Global(), 0, NULL).ToLocalChecked();
568 CHECK(env->Global()->Has(env.local(), v8_str("a")).FromJust());
569 v8::Local<v8::Value> a =
570 env->Global()->Get(env.local(), v8_str("a")).ToLocalChecked();
571 CHECK(a->IsNumber());
572 CHECK(env->Global()->Has(env.local(), v8_str("x")).FromJust());
573 v8::Local<v8::Value> x =
574 env->Global()->Get(env.local(), v8_str("x")).ToLocalChecked();
575 CHECK(x->IsNumber());
576 CHECK(env->Global()->Has(env.local(), v8_str("y")).FromJust());
577 v8::Local<v8::Value> y =
578 env->Global()->Get(env.local(), v8_str("y")).ToLocalChecked();
579 CHECK(y->IsNumber());
580 CHECK_EQ(314.1592653589793, a->NumberValue(env.local()).FromJust());
581 CHECK_EQ(-10.0, x->NumberValue(env.local()).FromJust());
582 CHECK_EQ(10.0, y->NumberValue(env.local()).FromJust());
583}
584
585
586TEST(CompileFunctionInContextComplex) {
587 CcTest::InitializeVM();
588 v8::HandleScope scope(CcTest::isolate());
589 LocalContext env;
590 CompileRun(
591 "var x = 1;"
592 "var y = 2;"
593 "var z = 4;"
594 "var a = {x: 8, y: 16};"
595 "var b = {x: 32};");
596 v8::Local<v8::Object> ext[2];
597 ext[0] = v8::Local<v8::Object>::Cast(
598 env->Global()->Get(env.local(), v8_str("a")).ToLocalChecked());
599 ext[1] = v8::Local<v8::Object>::Cast(
600 env->Global()->Get(env.local(), v8_str("b")).ToLocalChecked());
601 v8::ScriptCompiler::Source script_source(v8_str("result = x + y + z"));
602 v8::Local<v8::Function> fun =
603 v8::ScriptCompiler::CompileFunctionInContext(env.local(), &script_source,
604 0, NULL, 2, ext)
605 .ToLocalChecked();
606 CHECK(!fun.IsEmpty());
607 fun->Call(env.local(), env->Global(), 0, NULL).ToLocalChecked();
608 CHECK(env->Global()->Has(env.local(), v8_str("result")).FromJust());
609 v8::Local<v8::Value> result =
610 env->Global()->Get(env.local(), v8_str("result")).ToLocalChecked();
611 CHECK(result->IsNumber());
612 CHECK_EQ(52.0, result->NumberValue(env.local()).FromJust());
613}
614
615
616TEST(CompileFunctionInContextArgs) {
617 CcTest::InitializeVM();
618 v8::HandleScope scope(CcTest::isolate());
619 LocalContext env;
620 CompileRun("var a = {x: 23};");
621 v8::Local<v8::Object> ext[1];
622 ext[0] = v8::Local<v8::Object>::Cast(
623 env->Global()->Get(env.local(), v8_str("a")).ToLocalChecked());
624 v8::ScriptCompiler::Source script_source(v8_str("result = x + b"));
625 v8::Local<v8::String> arg = v8_str("b");
626 v8::Local<v8::Function> fun =
627 v8::ScriptCompiler::CompileFunctionInContext(env.local(), &script_source,
628 1, &arg, 1, ext)
629 .ToLocalChecked();
630 CHECK(!fun.IsEmpty());
631 v8::Local<v8::Value> b_value = v8::Number::New(CcTest::isolate(), 42.0);
632 fun->Call(env.local(), env->Global(), 1, &b_value).ToLocalChecked();
633 CHECK(env->Global()->Has(env.local(), v8_str("result")).FromJust());
634 v8::Local<v8::Value> result =
635 env->Global()->Get(env.local(), v8_str("result")).ToLocalChecked();
636 CHECK(result->IsNumber());
637 CHECK_EQ(65.0, result->NumberValue(env.local()).FromJust());
638}
639
640
641TEST(CompileFunctionInContextComments) {
642 CcTest::InitializeVM();
643 v8::HandleScope scope(CcTest::isolate());
644 LocalContext env;
645 CompileRun("var a = {x: 23, y: 1, z: 2};");
646 v8::Local<v8::Object> ext[1];
647 ext[0] = v8::Local<v8::Object>::Cast(
648 env->Global()->Get(env.local(), v8_str("a")).ToLocalChecked());
649 v8::ScriptCompiler::Source script_source(
650 v8_str("result = /* y + */ x + b // + z"));
651 v8::Local<v8::String> arg = v8_str("b");
652 v8::Local<v8::Function> fun =
653 v8::ScriptCompiler::CompileFunctionInContext(env.local(), &script_source,
654 1, &arg, 1, ext)
655 .ToLocalChecked();
656 CHECK(!fun.IsEmpty());
657 v8::Local<v8::Value> b_value = v8::Number::New(CcTest::isolate(), 42.0);
658 fun->Call(env.local(), env->Global(), 1, &b_value).ToLocalChecked();
659 CHECK(env->Global()->Has(env.local(), v8_str("result")).FromJust());
660 v8::Local<v8::Value> result =
661 env->Global()->Get(env.local(), v8_str("result")).ToLocalChecked();
662 CHECK(result->IsNumber());
663 CHECK_EQ(65.0, result->NumberValue(env.local()).FromJust());
664}
665
666
667TEST(CompileFunctionInContextNonIdentifierArgs) {
668 CcTest::InitializeVM();
669 v8::HandleScope scope(CcTest::isolate());
670 LocalContext env;
671 v8::ScriptCompiler::Source script_source(v8_str("result = 1"));
672 v8::Local<v8::String> arg = v8_str("b }");
673 CHECK(v8::ScriptCompiler::CompileFunctionInContext(
674 env.local(), &script_source, 1, &arg, 0, NULL)
675 .IsEmpty());
676}
677
678
679TEST(CompileFunctionInContextScriptOrigin) {
680 CcTest::InitializeVM();
681 v8::HandleScope scope(CcTest::isolate());
682 LocalContext env;
683 v8::ScriptOrigin origin(v8_str("test"),
684 v8::Integer::New(CcTest::isolate(), 22),
685 v8::Integer::New(CcTest::isolate(), 41));
686 v8::ScriptCompiler::Source script_source(v8_str("throw new Error()"), origin);
687 v8::Local<v8::Function> fun =
688 v8::ScriptCompiler::CompileFunctionInContext(env.local(), &script_source,
689 0, NULL, 0, NULL)
690 .ToLocalChecked();
691 CHECK(!fun.IsEmpty());
692 v8::TryCatch try_catch(CcTest::isolate());
693 CcTest::isolate()->SetCaptureStackTraceForUncaughtExceptions(true);
694 CHECK(fun->Call(env.local(), env->Global(), 0, NULL).IsEmpty());
695 CHECK(try_catch.HasCaught());
696 CHECK(!try_catch.Exception().IsEmpty());
697 v8::Local<v8::StackTrace> stack =
698 v8::Exception::GetStackTrace(try_catch.Exception());
699 CHECK(!stack.IsEmpty());
700 CHECK(stack->GetFrameCount() > 0);
701 v8::Local<v8::StackFrame> frame = stack->GetFrame(0);
702 CHECK_EQ(23, frame->GetLineNumber());
703 CHECK_EQ(42 + strlen("throw "), static_cast<unsigned>(frame->GetColumn()));
704}
705
706
707#ifdef ENABLE_DISASSEMBLER
708static Handle<JSFunction> GetJSFunction(v8::Local<v8::Object> obj,
709 const char* property_name) {
710 v8::Local<v8::Function> fun = v8::Local<v8::Function>::Cast(
711 obj->Get(CcTest::isolate()->GetCurrentContext(), v8_str(property_name))
712 .ToLocalChecked());
713 return Handle<JSFunction>::cast(v8::Utils::OpenHandle(*fun));
Steve Block053d10c2011-06-13 19:13:29 +0100714}
715
716
717static void CheckCodeForUnsafeLiteral(Handle<JSFunction> f) {
718 // Create a disassembler with default name lookup.
719 disasm::NameConverter name_converter;
720 disasm::Disassembler d(name_converter);
721
722 if (f->code()->kind() == Code::FUNCTION) {
723 Address pc = f->code()->instruction_start();
724 int decode_size =
725 Min(f->code()->instruction_size(),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000726 static_cast<int>(f->code()->back_edge_table_offset()));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000727 if (FLAG_enable_embedded_constant_pool) {
728 decode_size = Min(decode_size, f->code()->constant_pool_offset());
729 }
Steve Block053d10c2011-06-13 19:13:29 +0100730 Address end = pc + decode_size;
731
732 v8::internal::EmbeddedVector<char, 128> decode_buffer;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000733 v8::internal::EmbeddedVector<char, 128> smi_hex_buffer;
734 Smi* smi = Smi::FromInt(12345678);
735 SNPrintF(smi_hex_buffer, "0x%" V8PRIxPTR, reinterpret_cast<intptr_t>(smi));
Steve Block053d10c2011-06-13 19:13:29 +0100736 while (pc < end) {
Ben Murdoch42effa52011-08-19 16:40:31 +0100737 int num_const = d.ConstantPoolSizeAt(pc);
738 if (num_const >= 0) {
739 pc += (num_const + 1) * kPointerSize;
740 } else {
741 pc += d.InstructionDecode(decode_buffer, pc);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000742 CHECK(strstr(decode_buffer.start(), smi_hex_buffer.start()) == NULL);
Ben Murdoch42effa52011-08-19 16:40:31 +0100743 }
Steve Block053d10c2011-06-13 19:13:29 +0100744 }
745 }
746}
747
748
749TEST(SplitConstantsInFullCompiler) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000750 LocalContext context;
751 v8::HandleScope scope(CcTest::isolate());
Steve Block053d10c2011-06-13 19:13:29 +0100752
753 CompileRun("function f() { a = 12345678 }; f();");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000754 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f"));
Steve Block053d10c2011-06-13 19:13:29 +0100755 CompileRun("function f(x) { a = 12345678 + x}; f(1);");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000756 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f"));
Steve Block053d10c2011-06-13 19:13:29 +0100757 CompileRun("function f(x) { var arguments = 1; x += 12345678}; f(1);");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000758 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f"));
Steve Block053d10c2011-06-13 19:13:29 +0100759 CompileRun("function f(x) { var arguments = 1; x = 12345678}; f(1);");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000760 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f"));
Steve Block053d10c2011-06-13 19:13:29 +0100761}
762#endif