blob: 18f00097cdedd10fc3835d2f063bb3b4c7114fb3 [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
Ben Murdochc5610432016-08-08 18:44:38 +0100367// Test that optimized code for different closures is actually shared.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000368TEST(OptimizedCodeSharing1) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000369 FLAG_stress_compaction = false;
370 FLAG_allow_natives_syntax = true;
371 CcTest::InitializeVM();
372 v8::HandleScope scope(CcTest::isolate());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000373 for (int i = 0; i < 3; i++) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000374 LocalContext env;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000375 env->Global()
376 ->Set(env.local(), v8_str("x"), v8::Integer::New(CcTest::isolate(), i))
377 .FromJust();
378 CompileRun(
379 "function MakeClosure() {"
380 " return function() { return x; };"
381 "}"
382 "var closure0 = MakeClosure();"
383 "%DebugPrint(closure0());"
384 "%OptimizeFunctionOnNextCall(closure0);"
385 "%DebugPrint(closure0());"
Ben Murdochc5610432016-08-08 18:44:38 +0100386 "var closure1 = MakeClosure(); closure1();"
387 "var closure2 = MakeClosure(); closure2();");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000388 Handle<JSFunction> fun1 = Handle<JSFunction>::cast(
389 v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast(
390 env->Global()
391 ->Get(env.local(), v8_str("closure1"))
392 .ToLocalChecked())));
393 Handle<JSFunction> fun2 = Handle<JSFunction>::cast(
394 v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast(
395 env->Global()
396 ->Get(env.local(), v8_str("closure2"))
397 .ToLocalChecked())));
398 CHECK(fun1->IsOptimized() || !CcTest::i_isolate()->use_crankshaft());
399 CHECK(fun2->IsOptimized() || !CcTest::i_isolate()->use_crankshaft());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000400 CHECK_EQ(fun1->code(), fun2->code());
401 }
402}
403
Ben Murdochc5610432016-08-08 18:44:38 +0100404// Test that optimized code for different closures is actually shared.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000405TEST(OptimizedCodeSharing2) {
406 if (FLAG_stress_compaction) return;
407 FLAG_allow_natives_syntax = true;
408 FLAG_native_context_specialization = false;
409 FLAG_turbo_cache_shared_code = true;
410 const char* flag = "--turbo-filter=*";
411 FlagList::SetFlagsFromString(flag, StrLength(flag));
412 CcTest::InitializeVM();
413 v8::HandleScope scope(CcTest::isolate());
414 v8::Local<v8::Script> script = v8_compile(
415 "function MakeClosure() {"
416 " return function() { return x; };"
417 "}");
418 Handle<Code> reference_code;
419 {
420 LocalContext env;
421 env->Global()
422 ->Set(env.local(), v8_str("x"), v8::Integer::New(CcTest::isolate(), 23))
423 .FromJust();
424 script->GetUnboundScript()
425 ->BindToCurrentContext()
426 ->Run(env.local())
427 .ToLocalChecked();
428 CompileRun(
429 "var closure0 = MakeClosure();"
430 "%DebugPrint(closure0());"
431 "%OptimizeFunctionOnNextCall(closure0);"
432 "%DebugPrint(closure0());");
433 Handle<JSFunction> fun0 = Handle<JSFunction>::cast(
434 v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast(
435 env->Global()
436 ->Get(env.local(), v8_str("closure0"))
437 .ToLocalChecked())));
438 CHECK(fun0->IsOptimized() || !CcTest::i_isolate()->use_crankshaft());
439 reference_code = handle(fun0->code());
440 }
441 for (int i = 0; i < 3; i++) {
442 LocalContext env;
443 env->Global()
444 ->Set(env.local(), v8_str("x"), v8::Integer::New(CcTest::isolate(), i))
445 .FromJust();
446 script->GetUnboundScript()
447 ->BindToCurrentContext()
448 ->Run(env.local())
449 .ToLocalChecked();
450 CompileRun(
451 "var closure0 = MakeClosure();"
452 "%DebugPrint(closure0());"
453 "%OptimizeFunctionOnNextCall(closure0);"
454 "%DebugPrint(closure0());"
Ben Murdochc5610432016-08-08 18:44:38 +0100455 "var closure1 = MakeClosure(); closure1();"
456 "var closure2 = MakeClosure(); closure2();");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000457 Handle<JSFunction> fun1 = Handle<JSFunction>::cast(
458 v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast(
459 env->Global()
460 ->Get(env.local(), v8_str("closure1"))
461 .ToLocalChecked())));
462 Handle<JSFunction> fun2 = Handle<JSFunction>::cast(
463 v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast(
464 env->Global()
465 ->Get(env.local(), v8_str("closure2"))
466 .ToLocalChecked())));
467 CHECK(fun1->IsOptimized() || !CcTest::i_isolate()->use_crankshaft());
468 CHECK(fun2->IsOptimized() || !CcTest::i_isolate()->use_crankshaft());
469 CHECK_EQ(*reference_code, fun1->code());
470 CHECK_EQ(*reference_code, fun2->code());
471 }
472}
473
Ben Murdochc5610432016-08-08 18:44:38 +0100474// Test that optimized code for different closures is actually shared.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000475TEST(OptimizedCodeSharing3) {
476 if (FLAG_stress_compaction) return;
477 FLAG_allow_natives_syntax = true;
478 FLAG_native_context_specialization = false;
479 FLAG_turbo_cache_shared_code = true;
480 const char* flag = "--turbo-filter=*";
481 FlagList::SetFlagsFromString(flag, StrLength(flag));
482 CcTest::InitializeVM();
483 v8::HandleScope scope(CcTest::isolate());
484 v8::Local<v8::Script> script = v8_compile(
485 "function MakeClosure() {"
486 " return function() { return x; };"
487 "}");
488 Handle<Code> reference_code;
489 {
490 LocalContext env;
491 env->Global()
492 ->Set(env.local(), v8_str("x"), v8::Integer::New(CcTest::isolate(), 23))
493 .FromJust();
494 script->GetUnboundScript()
495 ->BindToCurrentContext()
496 ->Run(env.local())
497 .ToLocalChecked();
498 CompileRun(
499 "var closure0 = MakeClosure();"
500 "%DebugPrint(closure0());"
501 "%OptimizeFunctionOnNextCall(closure0);"
502 "%DebugPrint(closure0());");
503 Handle<JSFunction> fun0 = Handle<JSFunction>::cast(
504 v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast(
505 env->Global()
506 ->Get(env.local(), v8_str("closure0"))
507 .ToLocalChecked())));
508 CHECK(fun0->IsOptimized() || !CcTest::i_isolate()->use_crankshaft());
509 reference_code = handle(fun0->code());
510 // Evict only the context-dependent entry from the optimized code map. This
511 // leaves it in a state where only the context-independent entry exists.
512 fun0->shared()->TrimOptimizedCodeMap(SharedFunctionInfo::kEntryLength);
513 }
514 for (int i = 0; i < 3; i++) {
515 LocalContext env;
516 env->Global()
517 ->Set(env.local(), v8_str("x"), v8::Integer::New(CcTest::isolate(), i))
518 .FromJust();
519 script->GetUnboundScript()
520 ->BindToCurrentContext()
521 ->Run(env.local())
522 .ToLocalChecked();
523 CompileRun(
524 "var closure0 = MakeClosure();"
525 "%DebugPrint(closure0());"
526 "%OptimizeFunctionOnNextCall(closure0);"
527 "%DebugPrint(closure0());"
Ben Murdochc5610432016-08-08 18:44:38 +0100528 "var closure1 = MakeClosure(); closure1();"
529 "var closure2 = MakeClosure(); closure2();");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000530 Handle<JSFunction> fun1 = Handle<JSFunction>::cast(
531 v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast(
532 env->Global()
533 ->Get(env.local(), v8_str("closure1"))
534 .ToLocalChecked())));
535 Handle<JSFunction> fun2 = Handle<JSFunction>::cast(
536 v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast(
537 env->Global()
538 ->Get(env.local(), v8_str("closure2"))
539 .ToLocalChecked())));
540 CHECK(fun1->IsOptimized() || !CcTest::i_isolate()->use_crankshaft());
541 CHECK(fun2->IsOptimized() || !CcTest::i_isolate()->use_crankshaft());
542 CHECK_EQ(*reference_code, fun1->code());
543 CHECK_EQ(*reference_code, fun2->code());
544 }
545}
546
547
548TEST(CompileFunctionInContext) {
549 CcTest::InitializeVM();
550 v8::HandleScope scope(CcTest::isolate());
551 LocalContext env;
552 CompileRun("var r = 10;");
553 v8::Local<v8::Object> math = v8::Local<v8::Object>::Cast(
554 env->Global()->Get(env.local(), v8_str("Math")).ToLocalChecked());
555 v8::ScriptCompiler::Source script_source(v8_str(
556 "a = PI * r * r;"
557 "x = r * cos(PI);"
558 "y = r * sin(PI / 2);"));
Steve Block053d10c2011-06-13 19:13:29 +0100559 v8::Local<v8::Function> fun =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000560 v8::ScriptCompiler::CompileFunctionInContext(env.local(), &script_source,
561 0, NULL, 1, &math)
562 .ToLocalChecked();
563 CHECK(!fun.IsEmpty());
564 fun->Call(env.local(), env->Global(), 0, NULL).ToLocalChecked();
565 CHECK(env->Global()->Has(env.local(), v8_str("a")).FromJust());
566 v8::Local<v8::Value> a =
567 env->Global()->Get(env.local(), v8_str("a")).ToLocalChecked();
568 CHECK(a->IsNumber());
569 CHECK(env->Global()->Has(env.local(), v8_str("x")).FromJust());
570 v8::Local<v8::Value> x =
571 env->Global()->Get(env.local(), v8_str("x")).ToLocalChecked();
572 CHECK(x->IsNumber());
573 CHECK(env->Global()->Has(env.local(), v8_str("y")).FromJust());
574 v8::Local<v8::Value> y =
575 env->Global()->Get(env.local(), v8_str("y")).ToLocalChecked();
576 CHECK(y->IsNumber());
577 CHECK_EQ(314.1592653589793, a->NumberValue(env.local()).FromJust());
578 CHECK_EQ(-10.0, x->NumberValue(env.local()).FromJust());
579 CHECK_EQ(10.0, y->NumberValue(env.local()).FromJust());
580}
581
582
583TEST(CompileFunctionInContextComplex) {
584 CcTest::InitializeVM();
585 v8::HandleScope scope(CcTest::isolate());
586 LocalContext env;
587 CompileRun(
588 "var x = 1;"
589 "var y = 2;"
590 "var z = 4;"
591 "var a = {x: 8, y: 16};"
592 "var b = {x: 32};");
593 v8::Local<v8::Object> ext[2];
594 ext[0] = v8::Local<v8::Object>::Cast(
595 env->Global()->Get(env.local(), v8_str("a")).ToLocalChecked());
596 ext[1] = v8::Local<v8::Object>::Cast(
597 env->Global()->Get(env.local(), v8_str("b")).ToLocalChecked());
598 v8::ScriptCompiler::Source script_source(v8_str("result = x + y + z"));
599 v8::Local<v8::Function> fun =
600 v8::ScriptCompiler::CompileFunctionInContext(env.local(), &script_source,
601 0, NULL, 2, ext)
602 .ToLocalChecked();
603 CHECK(!fun.IsEmpty());
604 fun->Call(env.local(), env->Global(), 0, NULL).ToLocalChecked();
605 CHECK(env->Global()->Has(env.local(), v8_str("result")).FromJust());
606 v8::Local<v8::Value> result =
607 env->Global()->Get(env.local(), v8_str("result")).ToLocalChecked();
608 CHECK(result->IsNumber());
609 CHECK_EQ(52.0, result->NumberValue(env.local()).FromJust());
610}
611
612
613TEST(CompileFunctionInContextArgs) {
614 CcTest::InitializeVM();
615 v8::HandleScope scope(CcTest::isolate());
616 LocalContext env;
617 CompileRun("var a = {x: 23};");
618 v8::Local<v8::Object> ext[1];
619 ext[0] = v8::Local<v8::Object>::Cast(
620 env->Global()->Get(env.local(), v8_str("a")).ToLocalChecked());
621 v8::ScriptCompiler::Source script_source(v8_str("result = x + b"));
622 v8::Local<v8::String> arg = v8_str("b");
623 v8::Local<v8::Function> fun =
624 v8::ScriptCompiler::CompileFunctionInContext(env.local(), &script_source,
625 1, &arg, 1, ext)
626 .ToLocalChecked();
627 CHECK(!fun.IsEmpty());
628 v8::Local<v8::Value> b_value = v8::Number::New(CcTest::isolate(), 42.0);
629 fun->Call(env.local(), env->Global(), 1, &b_value).ToLocalChecked();
630 CHECK(env->Global()->Has(env.local(), v8_str("result")).FromJust());
631 v8::Local<v8::Value> result =
632 env->Global()->Get(env.local(), v8_str("result")).ToLocalChecked();
633 CHECK(result->IsNumber());
634 CHECK_EQ(65.0, result->NumberValue(env.local()).FromJust());
635}
636
637
638TEST(CompileFunctionInContextComments) {
639 CcTest::InitializeVM();
640 v8::HandleScope scope(CcTest::isolate());
641 LocalContext env;
642 CompileRun("var a = {x: 23, y: 1, z: 2};");
643 v8::Local<v8::Object> ext[1];
644 ext[0] = v8::Local<v8::Object>::Cast(
645 env->Global()->Get(env.local(), v8_str("a")).ToLocalChecked());
646 v8::ScriptCompiler::Source script_source(
647 v8_str("result = /* y + */ x + b // + z"));
648 v8::Local<v8::String> arg = v8_str("b");
649 v8::Local<v8::Function> fun =
650 v8::ScriptCompiler::CompileFunctionInContext(env.local(), &script_source,
651 1, &arg, 1, ext)
652 .ToLocalChecked();
653 CHECK(!fun.IsEmpty());
654 v8::Local<v8::Value> b_value = v8::Number::New(CcTest::isolate(), 42.0);
655 fun->Call(env.local(), env->Global(), 1, &b_value).ToLocalChecked();
656 CHECK(env->Global()->Has(env.local(), v8_str("result")).FromJust());
657 v8::Local<v8::Value> result =
658 env->Global()->Get(env.local(), v8_str("result")).ToLocalChecked();
659 CHECK(result->IsNumber());
660 CHECK_EQ(65.0, result->NumberValue(env.local()).FromJust());
661}
662
663
664TEST(CompileFunctionInContextNonIdentifierArgs) {
665 CcTest::InitializeVM();
666 v8::HandleScope scope(CcTest::isolate());
667 LocalContext env;
668 v8::ScriptCompiler::Source script_source(v8_str("result = 1"));
669 v8::Local<v8::String> arg = v8_str("b }");
670 CHECK(v8::ScriptCompiler::CompileFunctionInContext(
671 env.local(), &script_source, 1, &arg, 0, NULL)
672 .IsEmpty());
673}
674
675
676TEST(CompileFunctionInContextScriptOrigin) {
677 CcTest::InitializeVM();
678 v8::HandleScope scope(CcTest::isolate());
679 LocalContext env;
680 v8::ScriptOrigin origin(v8_str("test"),
681 v8::Integer::New(CcTest::isolate(), 22),
682 v8::Integer::New(CcTest::isolate(), 41));
683 v8::ScriptCompiler::Source script_source(v8_str("throw new Error()"), origin);
684 v8::Local<v8::Function> fun =
685 v8::ScriptCompiler::CompileFunctionInContext(env.local(), &script_source,
686 0, NULL, 0, NULL)
687 .ToLocalChecked();
688 CHECK(!fun.IsEmpty());
689 v8::TryCatch try_catch(CcTest::isolate());
690 CcTest::isolate()->SetCaptureStackTraceForUncaughtExceptions(true);
691 CHECK(fun->Call(env.local(), env->Global(), 0, NULL).IsEmpty());
692 CHECK(try_catch.HasCaught());
693 CHECK(!try_catch.Exception().IsEmpty());
694 v8::Local<v8::StackTrace> stack =
695 v8::Exception::GetStackTrace(try_catch.Exception());
696 CHECK(!stack.IsEmpty());
697 CHECK(stack->GetFrameCount() > 0);
698 v8::Local<v8::StackFrame> frame = stack->GetFrame(0);
699 CHECK_EQ(23, frame->GetLineNumber());
700 CHECK_EQ(42 + strlen("throw "), static_cast<unsigned>(frame->GetColumn()));
701}
702
703
704#ifdef ENABLE_DISASSEMBLER
705static Handle<JSFunction> GetJSFunction(v8::Local<v8::Object> obj,
706 const char* property_name) {
707 v8::Local<v8::Function> fun = v8::Local<v8::Function>::Cast(
708 obj->Get(CcTest::isolate()->GetCurrentContext(), v8_str(property_name))
709 .ToLocalChecked());
710 return Handle<JSFunction>::cast(v8::Utils::OpenHandle(*fun));
Steve Block053d10c2011-06-13 19:13:29 +0100711}
712
713
714static void CheckCodeForUnsafeLiteral(Handle<JSFunction> f) {
715 // Create a disassembler with default name lookup.
716 disasm::NameConverter name_converter;
717 disasm::Disassembler d(name_converter);
718
719 if (f->code()->kind() == Code::FUNCTION) {
720 Address pc = f->code()->instruction_start();
721 int decode_size =
722 Min(f->code()->instruction_size(),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000723 static_cast<int>(f->code()->back_edge_table_offset()));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000724 if (FLAG_enable_embedded_constant_pool) {
725 decode_size = Min(decode_size, f->code()->constant_pool_offset());
726 }
Steve Block053d10c2011-06-13 19:13:29 +0100727 Address end = pc + decode_size;
728
729 v8::internal::EmbeddedVector<char, 128> decode_buffer;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000730 v8::internal::EmbeddedVector<char, 128> smi_hex_buffer;
731 Smi* smi = Smi::FromInt(12345678);
732 SNPrintF(smi_hex_buffer, "0x%" V8PRIxPTR, reinterpret_cast<intptr_t>(smi));
Steve Block053d10c2011-06-13 19:13:29 +0100733 while (pc < end) {
Ben Murdoch42effa52011-08-19 16:40:31 +0100734 int num_const = d.ConstantPoolSizeAt(pc);
735 if (num_const >= 0) {
736 pc += (num_const + 1) * kPointerSize;
737 } else {
738 pc += d.InstructionDecode(decode_buffer, pc);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000739 CHECK(strstr(decode_buffer.start(), smi_hex_buffer.start()) == NULL);
Ben Murdoch42effa52011-08-19 16:40:31 +0100740 }
Steve Block053d10c2011-06-13 19:13:29 +0100741 }
742 }
743}
744
745
746TEST(SplitConstantsInFullCompiler) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000747 LocalContext context;
748 v8::HandleScope scope(CcTest::isolate());
Steve Block053d10c2011-06-13 19:13:29 +0100749
750 CompileRun("function f() { a = 12345678 }; f();");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000751 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f"));
Steve Block053d10c2011-06-13 19:13:29 +0100752 CompileRun("function f(x) { a = 12345678 + x}; f(1);");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000753 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f"));
Steve Block053d10c2011-06-13 19:13:29 +0100754 CompileRun("function f(x) { var arguments = 1; x += 12345678}; f(1);");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000755 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f"));
Steve Block053d10c2011-06-13 19:13:29 +0100756 CompileRun("function f(x) { var arguments = 1; x = 12345678}; f(1);");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000757 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f"));
Steve Block053d10c2011-06-13 19:13:29 +0100758}
759#endif