blob: 00f29d446d1ceb7c805e9108ae7807ac879751d2 [file] [log] [blame]
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001// Copyright 2012 the V8 project authors. All rights reserved.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include <stdlib.h>
jkummerow@chromium.org486075a2011-09-07 12:44:28 +000029#include <wchar.h>
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000030
31#include "v8.h"
32
33#include "compiler.h"
vegorov@chromium.org7304bca2011-05-16 12:14:13 +000034#include "disasm.h"
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000035#include "cctest.h"
36
37using namespace v8::internal;
38
lrn@chromium.org303ada72010-10-27 09:33:13 +000039static MaybeObject* GetGlobalProperty(const char* name) {
machenbach@chromium.org528ce022013-09-23 14:09:36 +000040 Isolate* isolate = CcTest::i_isolate();
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +000041 Handle<String> internalized_name =
42 isolate->factory()->InternalizeUtf8String(name);
43 return isolate->context()->global_object()->GetProperty(*internalized_name);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000044}
45
46
47static void SetGlobalProperty(const char* name, Object* value) {
machenbach@chromium.org528ce022013-09-23 14:09:36 +000048 Isolate* isolate = CcTest::i_isolate();
ulan@chromium.org09d7ab52013-02-25 15:50:35 +000049 Handle<Object> object(value, isolate);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +000050 Handle<String> internalized_name =
51 isolate->factory()->InternalizeUtf8String(name);
ulan@chromium.org09d7ab52013-02-25 15:50:35 +000052 Handle<JSObject> global(isolate->context()->global_object());
machenbach@chromium.orge8412be2013-11-08 10:23:52 +000053 Runtime::SetObjectProperty(isolate, global, internalized_name, object, NONE,
dslomov@chromium.org486536d2014-03-12 13:09:18 +000054 SLOPPY);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000055}
56
57
58static Handle<JSFunction> Compile(const char* source) {
machenbach@chromium.org528ce022013-09-23 14:09:36 +000059 Isolate* isolate = CcTest::i_isolate();
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +000060 Handle<String> source_code(
61 isolate->factory()->NewStringFromUtf8(CStrVector(source)));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000062 Handle<SharedFunctionInfo> shared_function =
yangguo@chromium.org49546742013-12-23 16:17:49 +000063 Compiler::CompileScript(source_code,
64 Handle<String>(),
65 0,
66 0,
67 false,
68 Handle<Context>(isolate->native_context()),
69 NULL, NULL,
yangguo@chromium.org49546742013-12-23 16:17:49 +000070 NOT_NATIVES_CODE);
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +000071 return isolate->factory()->NewFunctionFromSharedFunctionInfo(
72 shared_function, isolate->native_context());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000073}
74
75
jkummerow@chromium.org2c9426b2013-09-05 16:31:13 +000076static double Inc(Isolate* isolate, int x) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000077 const char* source = "result = %d + 1;";
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000078 EmbeddedVector<char, 512> buffer;
79 OS::SNPrintF(buffer, source, x);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000080
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000081 Handle<JSFunction> fun = Compile(buffer.start());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000082 if (fun.is_null()) return -1;
83
84 bool has_pending_exception;
jkummerow@chromium.org2c9426b2013-09-05 16:31:13 +000085 Handle<JSObject> global(isolate->context()->global_object());
86 Execution::Call(isolate, fun, global, 0, NULL, &has_pending_exception);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000087 CHECK(!has_pending_exception);
lrn@chromium.org303ada72010-10-27 09:33:13 +000088 return GetGlobalProperty("result")->ToObjectChecked()->Number();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000089}
90
91
92TEST(Inc) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +000093 CcTest::InitializeVM();
94 v8::HandleScope scope(CcTest::isolate());
jkummerow@chromium.org2c9426b2013-09-05 16:31:13 +000095 CHECK_EQ(4.0, Inc(CcTest::i_isolate(), 3));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000096}
97
98
jkummerow@chromium.org2c9426b2013-09-05 16:31:13 +000099static double Add(Isolate* isolate, int x, int y) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000100 Handle<JSFunction> fun = Compile("result = x + y;");
101 if (fun.is_null()) return -1;
102
103 SetGlobalProperty("x", Smi::FromInt(x));
104 SetGlobalProperty("y", Smi::FromInt(y));
105 bool has_pending_exception;
jkummerow@chromium.org2c9426b2013-09-05 16:31:13 +0000106 Handle<JSObject> global(isolate->context()->global_object());
107 Execution::Call(isolate, fun, global, 0, NULL, &has_pending_exception);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000108 CHECK(!has_pending_exception);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000109 return GetGlobalProperty("result")->ToObjectChecked()->Number();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000110}
111
112
113TEST(Add) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000114 CcTest::InitializeVM();
115 v8::HandleScope scope(CcTest::isolate());
jkummerow@chromium.org2c9426b2013-09-05 16:31:13 +0000116 CHECK_EQ(5.0, Add(CcTest::i_isolate(), 2, 3));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000117}
118
119
jkummerow@chromium.org2c9426b2013-09-05 16:31:13 +0000120static double Abs(Isolate* isolate, int x) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000121 Handle<JSFunction> fun = Compile("if (x < 0) result = -x; else result = x;");
122 if (fun.is_null()) return -1;
123
124 SetGlobalProperty("x", Smi::FromInt(x));
125 bool has_pending_exception;
jkummerow@chromium.org2c9426b2013-09-05 16:31:13 +0000126 Handle<JSObject> global(isolate->context()->global_object());
127 Execution::Call(isolate, fun, global, 0, NULL, &has_pending_exception);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000128 CHECK(!has_pending_exception);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000129 return GetGlobalProperty("result")->ToObjectChecked()->Number();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000130}
131
132
133TEST(Abs) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000134 CcTest::InitializeVM();
135 v8::HandleScope scope(CcTest::isolate());
jkummerow@chromium.org2c9426b2013-09-05 16:31:13 +0000136 CHECK_EQ(3.0, Abs(CcTest::i_isolate(), -3));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000137}
138
139
jkummerow@chromium.org2c9426b2013-09-05 16:31:13 +0000140static double Sum(Isolate* isolate, int n) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000141 Handle<JSFunction> fun =
142 Compile("s = 0; while (n > 0) { s += n; n -= 1; }; result = s;");
143 if (fun.is_null()) return -1;
144
145 SetGlobalProperty("n", Smi::FromInt(n));
146 bool has_pending_exception;
jkummerow@chromium.org2c9426b2013-09-05 16:31:13 +0000147 Handle<JSObject> global(isolate->context()->global_object());
148 Execution::Call(isolate, fun, global, 0, NULL, &has_pending_exception);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000149 CHECK(!has_pending_exception);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000150 return GetGlobalProperty("result")->ToObjectChecked()->Number();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000151}
152
153
154TEST(Sum) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000155 CcTest::InitializeVM();
156 v8::HandleScope scope(CcTest::isolate());
jkummerow@chromium.org2c9426b2013-09-05 16:31:13 +0000157 CHECK_EQ(5050.0, Sum(CcTest::i_isolate(), 100));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000158}
159
160
161TEST(Print) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000162 v8::HandleScope scope(CcTest::isolate());
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000163 v8::Local<v8::Context> context = CcTest::NewContext(PRINT_EXTENSION);
164 v8::Context::Scope context_scope(context);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000165 const char* source = "for (n = 0; n < 100; ++n) print(n, 1, 2);";
166 Handle<JSFunction> fun = Compile(source);
167 if (fun.is_null()) return;
168 bool has_pending_exception;
jkummerow@chromium.org2c9426b2013-09-05 16:31:13 +0000169 Handle<JSObject> global(CcTest::i_isolate()->context()->global_object());
170 Execution::Call(
171 CcTest::i_isolate(), fun, global, 0, NULL, &has_pending_exception);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000172 CHECK(!has_pending_exception);
173}
174
175
176// The following test method stems from my coding efforts today. It
177// tests all the functionality I have added to the compiler today
178TEST(Stuff) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000179 CcTest::InitializeVM();
180 v8::HandleScope scope(CcTest::isolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000181 const char* source =
182 "r = 0;\n"
183 "a = new Object;\n"
184 "if (a == a) r+=1;\n" // 1
185 "if (a != new Object()) r+=2;\n" // 2
186 "a.x = 42;\n"
187 "if (a.x == 42) r+=4;\n" // 4
188 "function foo() { var x = 87; return x; }\n"
189 "if (foo() == 87) r+=8;\n" // 8
190 "function bar() { var x; x = 99; return x; }\n"
191 "if (bar() == 99) r+=16;\n" // 16
192 "function baz() { var x = 1, y, z = 2; y = 3; return x + y + z; }\n"
193 "if (baz() == 6) r+=32;\n" // 32
194 "function Cons0() { this.x = 42; this.y = 87; }\n"
195 "if (new Cons0().x == 42) r+=64;\n" // 64
196 "if (new Cons0().y == 87) r+=128;\n" // 128
197 "function Cons2(x, y) { this.sum = x + y; }\n"
198 "if (new Cons2(3,4).sum == 7) r+=256;"; // 256
199
200 Handle<JSFunction> fun = Compile(source);
201 CHECK(!fun.is_null());
202 bool has_pending_exception;
jkummerow@chromium.org2c9426b2013-09-05 16:31:13 +0000203 Handle<JSObject> global(CcTest::i_isolate()->context()->global_object());
204 Execution::Call(
205 CcTest::i_isolate(), fun, global, 0, NULL, &has_pending_exception);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000206 CHECK(!has_pending_exception);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000207 CHECK_EQ(511.0, GetGlobalProperty("r")->ToObjectChecked()->Number());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000208}
209
210
211TEST(UncaughtThrow) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000212 CcTest::InitializeVM();
213 v8::HandleScope scope(CcTest::isolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000214
215 const char* source = "throw 42;";
216 Handle<JSFunction> fun = Compile(source);
217 CHECK(!fun.is_null());
218 bool has_pending_exception;
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000219 Isolate* isolate = fun->GetIsolate();
220 Handle<JSObject> global(isolate->context()->global_object());
jkummerow@chromium.org2c9426b2013-09-05 16:31:13 +0000221 Execution::Call(isolate, fun, global, 0, NULL, &has_pending_exception);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000222 CHECK(has_pending_exception);
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000223 CHECK_EQ(42.0, isolate->pending_exception()->ToObjectChecked()->Number());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000224}
225
226
227// Tests calling a builtin function from C/C++ code, and the builtin function
228// performs GC. It creates a stack frame looks like following:
229// | C (PerformGC) |
230// | JS-to-C |
231// | JS |
232// | C-to-JS |
233TEST(C2JSFrames) {
machenbach@chromium.orgc8cbc432014-01-21 09:01:57 +0000234 FLAG_expose_gc = true;
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000235 v8::HandleScope scope(CcTest::isolate());
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000236 v8::Local<v8::Context> context =
237 CcTest::NewContext(PRINT_EXTENSION | GC_EXTENSION);
238 v8::Context::Scope context_scope(context);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000239
240 const char* source = "function foo(a) { gc(), print(a); }";
241
242 Handle<JSFunction> fun0 = Compile(source);
243 CHECK(!fun0.is_null());
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000244 Isolate* isolate = fun0->GetIsolate();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000245
246 // Run the generated code to populate the global object with 'foo'.
247 bool has_pending_exception;
jkummerow@chromium.org2c9426b2013-09-05 16:31:13 +0000248 Handle<JSObject> global(isolate->context()->global_object());
249 Execution::Call(
250 isolate, fun0, global, 0, NULL, &has_pending_exception);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000251 CHECK(!has_pending_exception);
252
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +0000253 Object* foo_string = isolate->factory()->InternalizeOneByteString(
254 STATIC_ASCII_VECTOR("foo"))->ToObjectChecked();
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000255 MaybeObject* fun1_object = isolate->context()->global_object()->
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000256 GetProperty(String::cast(foo_string));
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000257 Handle<Object> fun1(fun1_object->ToObjectChecked(), isolate);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000258 CHECK(fun1->IsJSFunction());
259
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +0000260 Handle<Object> argv[] = { isolate->factory()->InternalizeOneByteString(
261 STATIC_ASCII_VECTOR("hello")) };
jkummerow@chromium.org2c9426b2013-09-05 16:31:13 +0000262 Execution::Call(isolate,
263 Handle<JSFunction>::cast(fun1),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000264 global,
265 ARRAY_SIZE(argv),
266 argv,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000267 &has_pending_exception);
268 CHECK(!has_pending_exception);
269}
ager@chromium.org381abbb2009-02-25 13:23:22 +0000270
271
272// Regression 236. Calling InitLineEnds on a Script with undefined
273// source resulted in crash.
274TEST(Regression236) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000275 CcTest::InitializeVM();
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000276 Isolate* isolate = CcTest::i_isolate();
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +0000277 Factory* factory = isolate->factory();
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000278 v8::HandleScope scope(CcTest::isolate());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000279
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +0000280 Handle<Script> script = factory->NewScript(factory->empty_string());
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000281 script->set_source(CcTest::heap()->undefined_value());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000282 CHECK_EQ(-1, GetScriptLineNumber(script, 0));
283 CHECK_EQ(-1, GetScriptLineNumber(script, 100));
284 CHECK_EQ(-1, GetScriptLineNumber(script, -1));
ager@chromium.org381abbb2009-02-25 13:23:22 +0000285}
ager@chromium.org5c838252010-02-19 08:53:10 +0000286
287
288TEST(GetScriptLineNumber) {
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000289 LocalContext context;
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000290 v8::HandleScope scope(CcTest::isolate());
machenbach@chromium.orgf9841892013-11-25 12:01:13 +0000291 v8::ScriptOrigin origin =
292 v8::ScriptOrigin(v8::String::NewFromUtf8(CcTest::isolate(), "test"));
ager@chromium.org5c838252010-02-19 08:53:10 +0000293 const char function_f[] = "function f() {}";
294 const int max_rows = 1000;
295 const int buffer_size = max_rows + sizeof(function_f);
296 ScopedVector<char> buffer(buffer_size);
297 memset(buffer.start(), '\n', buffer_size - 1);
298 buffer[buffer_size - 1] = '\0';
299
300 for (int i = 0; i < max_rows; ++i) {
301 if (i > 0)
302 buffer[i - 1] = '\n';
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000303 OS::MemCopy(&buffer[i], function_f, sizeof(function_f) - 1);
machenbach@chromium.orgf9841892013-11-25 12:01:13 +0000304 v8::Handle<v8::String> script_body =
305 v8::String::NewFromUtf8(CcTest::isolate(), buffer.start());
ager@chromium.org5c838252010-02-19 08:53:10 +0000306 v8::Script::Compile(script_body, &origin)->Run();
machenbach@chromium.orgf9841892013-11-25 12:01:13 +0000307 v8::Local<v8::Function> f =
308 v8::Local<v8::Function>::Cast(context->Global()->Get(
309 v8::String::NewFromUtf8(CcTest::isolate(), "f")));
ager@chromium.org5c838252010-02-19 08:53:10 +0000310 CHECK_EQ(i, f->GetScriptLineNumber());
311 }
312}
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000313
314
dslomov@chromium.org6b6df382014-03-14 16:14:53 +0000315TEST(FeedbackVectorRecreatedOnScopeChanges) {
316 if (i::FLAG_always_opt || !i::FLAG_lazy) return;
317 CcTest::InitializeVM();
318 v8::HandleScope scope(CcTest::isolate());
319
320 CompileRun("function builder() {"
321 " call_target = function() { return 3; };"
322 " return (function() {"
323 " eval('');"
324 " return function() {"
325 " 'use strict';"
326 " call_target();"
327 " }"
328 " })();"
329 "}"
330 "morphing_call = builder();");
331
332 Handle<JSFunction> f =
333 v8::Utils::OpenHandle(
334 *v8::Handle<v8::Function>::Cast(
335 CcTest::global()->Get(v8_str("morphing_call"))));
336
337 // morphing_call should have one feedback vector slot for the call to
338 // call_target(), scoping analysis having been performed.
339 CHECK_EQ(1, f->shared()->feedback_vector()->length());
340 // And yet it's not compiled.
341 CHECK(!f->shared()->is_compiled());
342
343 CompileRun("morphing_call();");
344
345 // On scoping analysis after lazy compile, the call is now a global
346 // call which needs no feedback vector slot.
347 CHECK_EQ(0, f->shared()->feedback_vector()->length());
348 CHECK(f->shared()->is_compiled());
349}
350
351
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000352// Test that optimized code for different closures is actually shared
353// immediately by the FastNewClosureStub when run in the same context.
354TEST(OptimizedCodeSharing) {
yangguo@chromium.orgde0db002012-06-22 13:44:28 +0000355 // Skip test if --cache-optimized-code is not activated by default because
356 // FastNewClosureStub that is baked into the snapshot is incorrect.
357 if (!FLAG_cache_optimized_code) return;
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000358 FLAG_stress_compaction = false;
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000359 FLAG_allow_natives_syntax = true;
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000360 CcTest::InitializeVM();
361 v8::HandleScope scope(CcTest::isolate());
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000362 for (int i = 0; i < 10; i++) {
363 LocalContext env;
machenbach@chromium.orgf9841892013-11-25 12:01:13 +0000364 env->Global()->Set(v8::String::NewFromUtf8(CcTest::isolate(), "x"),
ulan@chromium.org0f13e742014-01-03 15:51:11 +0000365 v8::Integer::New(CcTest::isolate(), i));
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000366 CompileRun("function MakeClosure() {"
367 " return function() { return x; };"
368 "}"
369 "var closure0 = MakeClosure();"
370 "%DebugPrint(closure0());"
371 "%OptimizeFunctionOnNextCall(closure0);"
372 "%DebugPrint(closure0());"
373 "var closure1 = MakeClosure();"
374 "var closure2 = MakeClosure();");
375 Handle<JSFunction> fun1 = v8::Utils::OpenHandle(
376 *v8::Local<v8::Function>::Cast(env->Global()->Get(v8_str("closure1"))));
377 Handle<JSFunction> fun2 = v8::Utils::OpenHandle(
378 *v8::Local<v8::Function>::Cast(env->Global()->Get(v8_str("closure2"))));
bmeurer@chromium.orgc9913f02013-10-24 06:31:36 +0000379 CHECK(fun1->IsOptimized()
380 || !CcTest::i_isolate()->use_crankshaft() || !fun1->IsOptimizable());
381 CHECK(fun2->IsOptimized()
382 || !CcTest::i_isolate()->use_crankshaft() || !fun2->IsOptimizable());
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000383 CHECK_EQ(fun1->code(), fun2->code());
384 }
385}
386
387
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000388#ifdef ENABLE_DISASSEMBLER
389static Handle<JSFunction> GetJSFunction(v8::Handle<v8::Object> obj,
390 const char* property_name) {
391 v8::Local<v8::Function> fun =
392 v8::Local<v8::Function>::Cast(obj->Get(v8_str(property_name)));
393 return v8::Utils::OpenHandle(*fun);
394}
395
396
397static void CheckCodeForUnsafeLiteral(Handle<JSFunction> f) {
398 // Create a disassembler with default name lookup.
399 disasm::NameConverter name_converter;
400 disasm::Disassembler d(name_converter);
401
402 if (f->code()->kind() == Code::FUNCTION) {
403 Address pc = f->code()->instruction_start();
404 int decode_size =
405 Min(f->code()->instruction_size(),
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000406 static_cast<int>(f->code()->back_edge_table_offset()));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000407 Address end = pc + decode_size;
408
409 v8::internal::EmbeddedVector<char, 128> decode_buffer;
jkummerow@chromium.org7a6fc812012-06-27 11:12:38 +0000410 v8::internal::EmbeddedVector<char, 128> smi_hex_buffer;
411 Smi* smi = Smi::FromInt(12345678);
412 OS::SNPrintF(smi_hex_buffer, "0x%lx", reinterpret_cast<intptr_t>(smi));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000413 while (pc < end) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000414 int num_const = d.ConstantPoolSizeAt(pc);
415 if (num_const >= 0) {
ricow@chromium.org9fa09672011-07-25 11:05:35 +0000416 pc += (num_const + 1) * kPointerSize;
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000417 } else {
418 pc += d.InstructionDecode(decode_buffer, pc);
jkummerow@chromium.org7a6fc812012-06-27 11:12:38 +0000419 CHECK(strstr(decode_buffer.start(), smi_hex_buffer.start()) == NULL);
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000420 }
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000421 }
422 }
423}
424
425
426TEST(SplitConstantsInFullCompiler) {
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000427 LocalContext context;
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000428 v8::HandleScope scope(CcTest::isolate());
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000429
430 CompileRun("function f() { a = 12345678 }; f();");
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000431 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f"));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000432 CompileRun("function f(x) { a = 12345678 + x}; f(1);");
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000433 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f"));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000434 CompileRun("function f(x) { var arguments = 1; x += 12345678}; f(1);");
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000435 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f"));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000436 CompileRun("function f(x) { var arguments = 1; x = 12345678}; f(1);");
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000437 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f"));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000438}
439#endif