blob: 32d720e24e5f35961d03c319ae5b61e1139af289 [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();
Ben Murdochda12d292016-06-02 14:46:10 +010042 return JSReceiver::GetProperty(isolate, isolate->global_object(), name)
43 .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();
Ben Murdochda12d292016-06-02 14:46:10 +010062 Handle<SharedFunctionInfo> shared = Compiler::GetSharedFunctionInfoForScript(
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(
Ben Murdochda12d292016-06-02 14:46:10 +010067 shared, 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 Murdochda12d292016-06-02 14:46:10 +0100230 Handle<Object> fun1 =
231 JSReceiver::GetProperty(isolate, isolate->global_object(), "foo")
232 .ToHandleChecked();
Steve Blocka7e24c12009-10-30 11:49:00 +0000233 CHECK(fun1->IsJSFunction());
234
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000235 Handle<Object> argv[] = {isolate->factory()->InternalizeOneByteString(
236 STATIC_CHAR_VECTOR("hello"))};
237 Execution::Call(isolate,
238 Handle<JSFunction>::cast(fun1),
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100239 global,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000240 arraysize(argv),
241 argv).Check();
Steve Blocka7e24c12009-10-30 11:49:00 +0000242}
243
244
245// Regression 236. Calling InitLineEnds on a Script with undefined
246// source resulted in crash.
247TEST(Regression236) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000248 CcTest::InitializeVM();
249 Isolate* isolate = CcTest::i_isolate();
250 Factory* factory = isolate->factory();
251 v8::HandleScope scope(CcTest::isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000252
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000253 Handle<Script> script = factory->NewScript(factory->empty_string());
254 script->set_source(CcTest::heap()->undefined_value());
255 CHECK_EQ(-1, Script::GetLineNumber(script, 0));
256 CHECK_EQ(-1, Script::GetLineNumber(script, 100));
257 CHECK_EQ(-1, Script::GetLineNumber(script, -1));
Steve Blocka7e24c12009-10-30 11:49:00 +0000258}
Andrei Popescu402d9372010-02-26 13:31:12 +0000259
260
261TEST(GetScriptLineNumber) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000262 LocalContext context;
263 v8::HandleScope scope(CcTest::isolate());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000264 v8::ScriptOrigin origin = v8::ScriptOrigin(v8_str("test"));
Andrei Popescu402d9372010-02-26 13:31:12 +0000265 const char function_f[] = "function f() {}";
266 const int max_rows = 1000;
267 const int buffer_size = max_rows + sizeof(function_f);
268 ScopedVector<char> buffer(buffer_size);
269 memset(buffer.start(), '\n', buffer_size - 1);
270 buffer[buffer_size - 1] = '\0';
271
272 for (int i = 0; i < max_rows; ++i) {
273 if (i > 0)
274 buffer[i - 1] = '\n';
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000275 MemCopy(&buffer[i], function_f, sizeof(function_f) - 1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000276 v8::Local<v8::String> script_body = v8_str(buffer.start());
277 v8::Script::Compile(context.local(), script_body, &origin)
278 .ToLocalChecked()
279 ->Run(context.local())
280 .ToLocalChecked();
281 v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast(
282 context->Global()->Get(context.local(), v8_str("f")).ToLocalChecked());
Andrei Popescu402d9372010-02-26 13:31:12 +0000283 CHECK_EQ(i, f->GetScriptLineNumber());
284 }
285}
Steve Block053d10c2011-06-13 19:13:29 +0100286
287
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000288TEST(FeedbackVectorPreservedAcrossRecompiles) {
289 if (i::FLAG_always_opt || !i::FLAG_crankshaft) return;
290 i::FLAG_allow_natives_syntax = true;
291 CcTest::InitializeVM();
292 if (!CcTest::i_isolate()->use_crankshaft()) return;
293 v8::HandleScope scope(CcTest::isolate());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000294 v8::Local<v8::Context> context = CcTest::isolate()->GetCurrentContext();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000295
296 // Make sure function f has a call that uses a type feedback slot.
297 CompileRun("function fun() {};"
298 "fun1 = fun;"
299 "function f(a) { a(); } f(fun1);");
300
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000301 Handle<JSFunction> f = Handle<JSFunction>::cast(
302 v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast(
303 CcTest::global()->Get(context, v8_str("f")).ToLocalChecked())));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000304
305 // We shouldn't have deoptimization support. We want to recompile and
306 // verify that our feedback vector preserves information.
307 CHECK(!f->shared()->has_deoptimization_support());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400308 Handle<TypeFeedbackVector> feedback_vector(f->shared()->feedback_vector());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000309
310 // Verify that we gathered feedback.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000311 CHECK(!feedback_vector->is_empty());
312 FeedbackVectorSlot slot_for_a(0);
313 Object* object = feedback_vector->Get(slot_for_a);
314 CHECK(object->IsWeakCell() &&
315 WeakCell::cast(object)->value()->IsJSFunction());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000316
317 CompileRun("%OptimizeFunctionOnNextCall(f); f(fun1);");
318
319 // Verify that the feedback is still "gathered" despite a recompilation
320 // of the full code.
321 CHECK(f->IsOptimized());
322 CHECK(f->shared()->has_deoptimization_support());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000323 object = f->shared()->feedback_vector()->Get(slot_for_a);
324 CHECK(object->IsWeakCell() &&
325 WeakCell::cast(object)->value()->IsJSFunction());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000326}
327
328
329TEST(FeedbackVectorUnaffectedByScopeChanges) {
Ben Murdochda12d292016-06-02 14:46:10 +0100330 if (i::FLAG_always_opt || !i::FLAG_lazy ||
331 (FLAG_ignition && FLAG_ignition_eager)) {
332 return;
333 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000334 CcTest::InitializeVM();
335 v8::HandleScope scope(CcTest::isolate());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000336 v8::Local<v8::Context> context = CcTest::isolate()->GetCurrentContext();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000337
338 CompileRun("function builder() {"
339 " call_target = function() { return 3; };"
340 " return (function() {"
341 " eval('');"
342 " return function() {"
343 " 'use strict';"
344 " call_target();"
345 " }"
346 " })();"
347 "}"
348 "morphing_call = builder();");
349
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000350 Handle<JSFunction> f = Handle<JSFunction>::cast(v8::Utils::OpenHandle(
351 *v8::Local<v8::Function>::Cast(CcTest::global()
352 ->Get(context, v8_str("morphing_call"))
353 .ToLocalChecked())));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000354
Ben Murdochda12d292016-06-02 14:46:10 +0100355 // If we are compiling lazily then it should not be compiled, and so no
356 // feedback vector allocated yet.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000357 CHECK(!f->shared()->is_compiled());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000358 CHECK(f->shared()->feedback_vector()->is_empty());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000359
360 CompileRun("morphing_call();");
361
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400362 // Now a feedback vector is allocated.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000363 CHECK(f->shared()->is_compiled());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000364 CHECK(!f->shared()->feedback_vector()->is_empty());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000365}
366
367
368// Test that optimized code for different closures is actually shared
369// immediately by the FastNewClosureStub when run in the same context.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000370TEST(OptimizedCodeSharing1) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000371 FLAG_stress_compaction = false;
372 FLAG_allow_natives_syntax = true;
373 CcTest::InitializeVM();
374 v8::HandleScope scope(CcTest::isolate());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000375 for (int i = 0; i < 3; i++) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000376 LocalContext env;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000377 env->Global()
378 ->Set(env.local(), v8_str("x"), v8::Integer::New(CcTest::isolate(), i))
379 .FromJust();
380 CompileRun(
381 "function MakeClosure() {"
382 " return function() { return x; };"
383 "}"
384 "var closure0 = MakeClosure();"
385 "%DebugPrint(closure0());"
386 "%OptimizeFunctionOnNextCall(closure0);"
387 "%DebugPrint(closure0());"
388 "var closure1 = MakeClosure();"
389 "var closure2 = MakeClosure();");
390 Handle<JSFunction> fun1 = Handle<JSFunction>::cast(
391 v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast(
392 env->Global()
393 ->Get(env.local(), v8_str("closure1"))
394 .ToLocalChecked())));
395 Handle<JSFunction> fun2 = Handle<JSFunction>::cast(
396 v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast(
397 env->Global()
398 ->Get(env.local(), v8_str("closure2"))
399 .ToLocalChecked())));
400 CHECK(fun1->IsOptimized() || !CcTest::i_isolate()->use_crankshaft());
401 CHECK(fun2->IsOptimized() || !CcTest::i_isolate()->use_crankshaft());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000402 CHECK_EQ(fun1->code(), fun2->code());
403 }
404}
405
406
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000407// Test that optimized code for different closures is actually shared
408// immediately by the FastNewClosureStub when run different contexts.
409TEST(OptimizedCodeSharing2) {
410 if (FLAG_stress_compaction) return;
411 FLAG_allow_natives_syntax = true;
412 FLAG_native_context_specialization = false;
413 FLAG_turbo_cache_shared_code = true;
414 const char* flag = "--turbo-filter=*";
415 FlagList::SetFlagsFromString(flag, StrLength(flag));
416 CcTest::InitializeVM();
417 v8::HandleScope scope(CcTest::isolate());
418 v8::Local<v8::Script> script = v8_compile(
419 "function MakeClosure() {"
420 " return function() { return x; };"
421 "}");
422 Handle<Code> reference_code;
423 {
424 LocalContext env;
425 env->Global()
426 ->Set(env.local(), v8_str("x"), v8::Integer::New(CcTest::isolate(), 23))
427 .FromJust();
428 script->GetUnboundScript()
429 ->BindToCurrentContext()
430 ->Run(env.local())
431 .ToLocalChecked();
432 CompileRun(
433 "var closure0 = MakeClosure();"
434 "%DebugPrint(closure0());"
435 "%OptimizeFunctionOnNextCall(closure0);"
436 "%DebugPrint(closure0());");
437 Handle<JSFunction> fun0 = Handle<JSFunction>::cast(
438 v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast(
439 env->Global()
440 ->Get(env.local(), v8_str("closure0"))
441 .ToLocalChecked())));
442 CHECK(fun0->IsOptimized() || !CcTest::i_isolate()->use_crankshaft());
443 reference_code = handle(fun0->code());
444 }
445 for (int i = 0; i < 3; i++) {
446 LocalContext env;
447 env->Global()
448 ->Set(env.local(), v8_str("x"), v8::Integer::New(CcTest::isolate(), i))
449 .FromJust();
450 script->GetUnboundScript()
451 ->BindToCurrentContext()
452 ->Run(env.local())
453 .ToLocalChecked();
454 CompileRun(
455 "var closure0 = MakeClosure();"
456 "%DebugPrint(closure0());"
457 "%OptimizeFunctionOnNextCall(closure0);"
458 "%DebugPrint(closure0());"
459 "var closure1 = MakeClosure();"
460 "var closure2 = MakeClosure();");
461 Handle<JSFunction> fun1 = Handle<JSFunction>::cast(
462 v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast(
463 env->Global()
464 ->Get(env.local(), v8_str("closure1"))
465 .ToLocalChecked())));
466 Handle<JSFunction> fun2 = Handle<JSFunction>::cast(
467 v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast(
468 env->Global()
469 ->Get(env.local(), v8_str("closure2"))
470 .ToLocalChecked())));
471 CHECK(fun1->IsOptimized() || !CcTest::i_isolate()->use_crankshaft());
472 CHECK(fun2->IsOptimized() || !CcTest::i_isolate()->use_crankshaft());
473 CHECK_EQ(*reference_code, fun1->code());
474 CHECK_EQ(*reference_code, fun2->code());
475 }
476}
477
478
479// Test that optimized code for different closures is actually shared
480// immediately by the FastNewClosureStub without context-dependent entries.
481TEST(OptimizedCodeSharing3) {
482 if (FLAG_stress_compaction) return;
483 FLAG_allow_natives_syntax = true;
484 FLAG_native_context_specialization = false;
485 FLAG_turbo_cache_shared_code = true;
486 const char* flag = "--turbo-filter=*";
487 FlagList::SetFlagsFromString(flag, StrLength(flag));
488 CcTest::InitializeVM();
489 v8::HandleScope scope(CcTest::isolate());
490 v8::Local<v8::Script> script = v8_compile(
491 "function MakeClosure() {"
492 " return function() { return x; };"
493 "}");
494 Handle<Code> reference_code;
495 {
496 LocalContext env;
497 env->Global()
498 ->Set(env.local(), v8_str("x"), v8::Integer::New(CcTest::isolate(), 23))
499 .FromJust();
500 script->GetUnboundScript()
501 ->BindToCurrentContext()
502 ->Run(env.local())
503 .ToLocalChecked();
504 CompileRun(
505 "var closure0 = MakeClosure();"
506 "%DebugPrint(closure0());"
507 "%OptimizeFunctionOnNextCall(closure0);"
508 "%DebugPrint(closure0());");
509 Handle<JSFunction> fun0 = Handle<JSFunction>::cast(
510 v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast(
511 env->Global()
512 ->Get(env.local(), v8_str("closure0"))
513 .ToLocalChecked())));
514 CHECK(fun0->IsOptimized() || !CcTest::i_isolate()->use_crankshaft());
515 reference_code = handle(fun0->code());
516 // Evict only the context-dependent entry from the optimized code map. This
517 // leaves it in a state where only the context-independent entry exists.
518 fun0->shared()->TrimOptimizedCodeMap(SharedFunctionInfo::kEntryLength);
519 }
520 for (int i = 0; i < 3; i++) {
521 LocalContext env;
522 env->Global()
523 ->Set(env.local(), v8_str("x"), v8::Integer::New(CcTest::isolate(), i))
524 .FromJust();
525 script->GetUnboundScript()
526 ->BindToCurrentContext()
527 ->Run(env.local())
528 .ToLocalChecked();
529 CompileRun(
530 "var closure0 = MakeClosure();"
531 "%DebugPrint(closure0());"
532 "%OptimizeFunctionOnNextCall(closure0);"
533 "%DebugPrint(closure0());"
534 "var closure1 = MakeClosure();"
535 "var closure2 = MakeClosure();");
536 Handle<JSFunction> fun1 = Handle<JSFunction>::cast(
537 v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast(
538 env->Global()
539 ->Get(env.local(), v8_str("closure1"))
540 .ToLocalChecked())));
541 Handle<JSFunction> fun2 = Handle<JSFunction>::cast(
542 v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast(
543 env->Global()
544 ->Get(env.local(), v8_str("closure2"))
545 .ToLocalChecked())));
546 CHECK(fun1->IsOptimized() || !CcTest::i_isolate()->use_crankshaft());
547 CHECK(fun2->IsOptimized() || !CcTest::i_isolate()->use_crankshaft());
548 CHECK_EQ(*reference_code, fun1->code());
549 CHECK_EQ(*reference_code, fun2->code());
550 }
551}
552
553
554TEST(CompileFunctionInContext) {
555 CcTest::InitializeVM();
556 v8::HandleScope scope(CcTest::isolate());
557 LocalContext env;
558 CompileRun("var r = 10;");
559 v8::Local<v8::Object> math = v8::Local<v8::Object>::Cast(
560 env->Global()->Get(env.local(), v8_str("Math")).ToLocalChecked());
561 v8::ScriptCompiler::Source script_source(v8_str(
562 "a = PI * r * r;"
563 "x = r * cos(PI);"
564 "y = r * sin(PI / 2);"));
Steve Block053d10c2011-06-13 19:13:29 +0100565 v8::Local<v8::Function> fun =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000566 v8::ScriptCompiler::CompileFunctionInContext(env.local(), &script_source,
567 0, NULL, 1, &math)
568 .ToLocalChecked();
569 CHECK(!fun.IsEmpty());
570 fun->Call(env.local(), env->Global(), 0, NULL).ToLocalChecked();
571 CHECK(env->Global()->Has(env.local(), v8_str("a")).FromJust());
572 v8::Local<v8::Value> a =
573 env->Global()->Get(env.local(), v8_str("a")).ToLocalChecked();
574 CHECK(a->IsNumber());
575 CHECK(env->Global()->Has(env.local(), v8_str("x")).FromJust());
576 v8::Local<v8::Value> x =
577 env->Global()->Get(env.local(), v8_str("x")).ToLocalChecked();
578 CHECK(x->IsNumber());
579 CHECK(env->Global()->Has(env.local(), v8_str("y")).FromJust());
580 v8::Local<v8::Value> y =
581 env->Global()->Get(env.local(), v8_str("y")).ToLocalChecked();
582 CHECK(y->IsNumber());
583 CHECK_EQ(314.1592653589793, a->NumberValue(env.local()).FromJust());
584 CHECK_EQ(-10.0, x->NumberValue(env.local()).FromJust());
585 CHECK_EQ(10.0, y->NumberValue(env.local()).FromJust());
586}
587
588
589TEST(CompileFunctionInContextComplex) {
590 CcTest::InitializeVM();
591 v8::HandleScope scope(CcTest::isolate());
592 LocalContext env;
593 CompileRun(
594 "var x = 1;"
595 "var y = 2;"
596 "var z = 4;"
597 "var a = {x: 8, y: 16};"
598 "var b = {x: 32};");
599 v8::Local<v8::Object> ext[2];
600 ext[0] = v8::Local<v8::Object>::Cast(
601 env->Global()->Get(env.local(), v8_str("a")).ToLocalChecked());
602 ext[1] = v8::Local<v8::Object>::Cast(
603 env->Global()->Get(env.local(), v8_str("b")).ToLocalChecked());
604 v8::ScriptCompiler::Source script_source(v8_str("result = x + y + z"));
605 v8::Local<v8::Function> fun =
606 v8::ScriptCompiler::CompileFunctionInContext(env.local(), &script_source,
607 0, NULL, 2, ext)
608 .ToLocalChecked();
609 CHECK(!fun.IsEmpty());
610 fun->Call(env.local(), env->Global(), 0, NULL).ToLocalChecked();
611 CHECK(env->Global()->Has(env.local(), v8_str("result")).FromJust());
612 v8::Local<v8::Value> result =
613 env->Global()->Get(env.local(), v8_str("result")).ToLocalChecked();
614 CHECK(result->IsNumber());
615 CHECK_EQ(52.0, result->NumberValue(env.local()).FromJust());
616}
617
618
619TEST(CompileFunctionInContextArgs) {
620 CcTest::InitializeVM();
621 v8::HandleScope scope(CcTest::isolate());
622 LocalContext env;
623 CompileRun("var a = {x: 23};");
624 v8::Local<v8::Object> ext[1];
625 ext[0] = v8::Local<v8::Object>::Cast(
626 env->Global()->Get(env.local(), v8_str("a")).ToLocalChecked());
627 v8::ScriptCompiler::Source script_source(v8_str("result = x + b"));
628 v8::Local<v8::String> arg = v8_str("b");
629 v8::Local<v8::Function> fun =
630 v8::ScriptCompiler::CompileFunctionInContext(env.local(), &script_source,
631 1, &arg, 1, ext)
632 .ToLocalChecked();
633 CHECK(!fun.IsEmpty());
634 v8::Local<v8::Value> b_value = v8::Number::New(CcTest::isolate(), 42.0);
635 fun->Call(env.local(), env->Global(), 1, &b_value).ToLocalChecked();
636 CHECK(env->Global()->Has(env.local(), v8_str("result")).FromJust());
637 v8::Local<v8::Value> result =
638 env->Global()->Get(env.local(), v8_str("result")).ToLocalChecked();
639 CHECK(result->IsNumber());
640 CHECK_EQ(65.0, result->NumberValue(env.local()).FromJust());
641}
642
643
644TEST(CompileFunctionInContextComments) {
645 CcTest::InitializeVM();
646 v8::HandleScope scope(CcTest::isolate());
647 LocalContext env;
648 CompileRun("var a = {x: 23, y: 1, z: 2};");
649 v8::Local<v8::Object> ext[1];
650 ext[0] = v8::Local<v8::Object>::Cast(
651 env->Global()->Get(env.local(), v8_str("a")).ToLocalChecked());
652 v8::ScriptCompiler::Source script_source(
653 v8_str("result = /* y + */ x + b // + z"));
654 v8::Local<v8::String> arg = v8_str("b");
655 v8::Local<v8::Function> fun =
656 v8::ScriptCompiler::CompileFunctionInContext(env.local(), &script_source,
657 1, &arg, 1, ext)
658 .ToLocalChecked();
659 CHECK(!fun.IsEmpty());
660 v8::Local<v8::Value> b_value = v8::Number::New(CcTest::isolate(), 42.0);
661 fun->Call(env.local(), env->Global(), 1, &b_value).ToLocalChecked();
662 CHECK(env->Global()->Has(env.local(), v8_str("result")).FromJust());
663 v8::Local<v8::Value> result =
664 env->Global()->Get(env.local(), v8_str("result")).ToLocalChecked();
665 CHECK(result->IsNumber());
666 CHECK_EQ(65.0, result->NumberValue(env.local()).FromJust());
667}
668
669
670TEST(CompileFunctionInContextNonIdentifierArgs) {
671 CcTest::InitializeVM();
672 v8::HandleScope scope(CcTest::isolate());
673 LocalContext env;
674 v8::ScriptCompiler::Source script_source(v8_str("result = 1"));
675 v8::Local<v8::String> arg = v8_str("b }");
676 CHECK(v8::ScriptCompiler::CompileFunctionInContext(
677 env.local(), &script_source, 1, &arg, 0, NULL)
678 .IsEmpty());
679}
680
681
682TEST(CompileFunctionInContextScriptOrigin) {
683 CcTest::InitializeVM();
684 v8::HandleScope scope(CcTest::isolate());
685 LocalContext env;
686 v8::ScriptOrigin origin(v8_str("test"),
687 v8::Integer::New(CcTest::isolate(), 22),
688 v8::Integer::New(CcTest::isolate(), 41));
689 v8::ScriptCompiler::Source script_source(v8_str("throw new Error()"), origin);
690 v8::Local<v8::Function> fun =
691 v8::ScriptCompiler::CompileFunctionInContext(env.local(), &script_source,
692 0, NULL, 0, NULL)
693 .ToLocalChecked();
694 CHECK(!fun.IsEmpty());
695 v8::TryCatch try_catch(CcTest::isolate());
696 CcTest::isolate()->SetCaptureStackTraceForUncaughtExceptions(true);
697 CHECK(fun->Call(env.local(), env->Global(), 0, NULL).IsEmpty());
698 CHECK(try_catch.HasCaught());
699 CHECK(!try_catch.Exception().IsEmpty());
700 v8::Local<v8::StackTrace> stack =
701 v8::Exception::GetStackTrace(try_catch.Exception());
702 CHECK(!stack.IsEmpty());
703 CHECK(stack->GetFrameCount() > 0);
704 v8::Local<v8::StackFrame> frame = stack->GetFrame(0);
705 CHECK_EQ(23, frame->GetLineNumber());
706 CHECK_EQ(42 + strlen("throw "), static_cast<unsigned>(frame->GetColumn()));
707}
708
709
710#ifdef ENABLE_DISASSEMBLER
711static Handle<JSFunction> GetJSFunction(v8::Local<v8::Object> obj,
712 const char* property_name) {
713 v8::Local<v8::Function> fun = v8::Local<v8::Function>::Cast(
714 obj->Get(CcTest::isolate()->GetCurrentContext(), v8_str(property_name))
715 .ToLocalChecked());
716 return Handle<JSFunction>::cast(v8::Utils::OpenHandle(*fun));
Steve Block053d10c2011-06-13 19:13:29 +0100717}
718
719
720static void CheckCodeForUnsafeLiteral(Handle<JSFunction> f) {
721 // Create a disassembler with default name lookup.
722 disasm::NameConverter name_converter;
723 disasm::Disassembler d(name_converter);
724
725 if (f->code()->kind() == Code::FUNCTION) {
726 Address pc = f->code()->instruction_start();
727 int decode_size =
728 Min(f->code()->instruction_size(),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000729 static_cast<int>(f->code()->back_edge_table_offset()));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000730 if (FLAG_enable_embedded_constant_pool) {
731 decode_size = Min(decode_size, f->code()->constant_pool_offset());
732 }
Steve Block053d10c2011-06-13 19:13:29 +0100733 Address end = pc + decode_size;
734
735 v8::internal::EmbeddedVector<char, 128> decode_buffer;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000736 v8::internal::EmbeddedVector<char, 128> smi_hex_buffer;
737 Smi* smi = Smi::FromInt(12345678);
738 SNPrintF(smi_hex_buffer, "0x%" V8PRIxPTR, reinterpret_cast<intptr_t>(smi));
Steve Block053d10c2011-06-13 19:13:29 +0100739 while (pc < end) {
Ben Murdoch42effa52011-08-19 16:40:31 +0100740 int num_const = d.ConstantPoolSizeAt(pc);
741 if (num_const >= 0) {
742 pc += (num_const + 1) * kPointerSize;
743 } else {
744 pc += d.InstructionDecode(decode_buffer, pc);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000745 CHECK(strstr(decode_buffer.start(), smi_hex_buffer.start()) == NULL);
Ben Murdoch42effa52011-08-19 16:40:31 +0100746 }
Steve Block053d10c2011-06-13 19:13:29 +0100747 }
748 }
749}
750
751
752TEST(SplitConstantsInFullCompiler) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000753 LocalContext context;
754 v8::HandleScope scope(CcTest::isolate());
Steve Block053d10c2011-06-13 19:13:29 +0100755
756 CompileRun("function f() { a = 12345678 }; f();");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000757 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f"));
Steve Block053d10c2011-06-13 19:13:29 +0100758 CompileRun("function f(x) { a = 12345678 + x}; f(1);");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000759 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f"));
Steve Block053d10c2011-06-13 19:13:29 +0100760 CompileRun("function f(x) { var arguments = 1; x += 12345678}; f(1);");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000761 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f"));
Steve Block053d10c2011-06-13 19:13:29 +0100762 CompileRun("function f(x) { var arguments = 1; x = 12345678}; f(1);");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000763 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f"));
Steve Block053d10c2011-06-13 19:13:29 +0100764}
765#endif