blob: f200435489c48b97686e3daf53e3a6becbf534a1 [file] [log] [blame]
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001// Copyright 2012 the V8 project authors. All rights reserved.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include <stdlib.h>
jkummerow@chromium.org486075a2011-09-07 12:44:28 +000029#include <wchar.h>
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000030
31#include "v8.h"
32
33#include "compiler.h"
vegorov@chromium.org7304bca2011-05-16 12:14:13 +000034#include "disasm.h"
35#include "disassembler.h"
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000036#include "execution.h"
37#include "factory.h"
38#include "platform.h"
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000039#include "cctest.h"
40
41using namespace v8::internal;
42
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000043// --- P r i n t E x t e n s i o n ---
44
45class PrintExtension : public v8::Extension {
46 public:
47 PrintExtension() : v8::Extension("v8/print", kSource) { }
48 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction(
49 v8::Handle<v8::String> name);
50 static v8::Handle<v8::Value> Print(const v8::Arguments& args);
51 private:
52 static const char* kSource;
53};
54
55
56const char* PrintExtension::kSource = "native function print();";
57
58
59v8::Handle<v8::FunctionTemplate> PrintExtension::GetNativeFunction(
60 v8::Handle<v8::String> str) {
61 return v8::FunctionTemplate::New(PrintExtension::Print);
62}
63
64
65v8::Handle<v8::Value> PrintExtension::Print(const v8::Arguments& args) {
66 for (int i = 0; i < args.Length(); i++) {
67 if (i != 0) printf(" ");
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +000068 v8::HandleScope scope(args.GetIsolate());
ulan@chromium.org56c14af2012-09-20 12:51:09 +000069 v8::String::Utf8Value str(args[i]);
70 if (*str == NULL) return v8::Undefined();
71 printf("%s", *str);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000072 }
73 printf("\n");
74 return v8::Undefined();
75}
76
77
78static PrintExtension kPrintExtension;
79v8::DeclareExtension kPrintExtensionDeclaration(&kPrintExtension);
80
81
lrn@chromium.org303ada72010-10-27 09:33:13 +000082static MaybeObject* GetGlobalProperty(const char* name) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +000083 Handle<String> internalized_name = FACTORY->InternalizeUtf8String(name);
84 return Isolate::Current()->context()->global_object()->GetProperty(
85 *internalized_name);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000086}
87
88
89static void SetGlobalProperty(const char* name, Object* value) {
ulan@chromium.org8e8d8822012-11-23 14:36:46 +000090 Isolate* isolate = Isolate::Current();
ulan@chromium.org09d7ab52013-02-25 15:50:35 +000091 Handle<Object> object(value, isolate);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +000092 Handle<String> internalized_name =
93 isolate->factory()->InternalizeUtf8String(name);
ulan@chromium.org09d7ab52013-02-25 15:50:35 +000094 Handle<JSObject> global(isolate->context()->global_object());
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +000095 SetProperty(isolate, global, internalized_name, object, NONE, kNonStrictMode);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000096}
97
98
99static Handle<JSFunction> Compile(const char* source) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000100 Handle<String> source_code(FACTORY->NewStringFromUtf8(CStrVector(source)));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000101 Handle<SharedFunctionInfo> shared_function =
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000102 Compiler::Compile(source_code,
103 Handle<String>(),
104 0,
105 0,
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000106 Handle<Context>(Isolate::Current()->native_context()),
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000107 NULL,
108 NULL,
109 Handle<String>::null(),
110 NOT_NATIVES_CODE);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000111 return FACTORY->NewFunctionFromSharedFunctionInfo(shared_function,
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000112 Isolate::Current()->native_context());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000113}
114
115
116static double Inc(int x) {
117 const char* source = "result = %d + 1;";
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000118 EmbeddedVector<char, 512> buffer;
119 OS::SNPrintF(buffer, source, x);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000120
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000121 Handle<JSFunction> fun = Compile(buffer.start());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000122 if (fun.is_null()) return -1;
123
124 bool has_pending_exception;
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000125 Handle<JSObject> global(Isolate::Current()->context()->global_object());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000126 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
127 CHECK(!has_pending_exception);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000128 return GetGlobalProperty("result")->ToObjectChecked()->Number();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000129}
130
131
132TEST(Inc) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000133 CcTest::InitializeVM();
134 v8::HandleScope scope(CcTest::isolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000135 CHECK_EQ(4.0, Inc(3));
136}
137
138
139static double Add(int x, int y) {
140 Handle<JSFunction> fun = Compile("result = x + y;");
141 if (fun.is_null()) return -1;
142
143 SetGlobalProperty("x", Smi::FromInt(x));
144 SetGlobalProperty("y", Smi::FromInt(y));
145 bool has_pending_exception;
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000146 Handle<JSObject> global(Isolate::Current()->context()->global_object());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000147 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
148 CHECK(!has_pending_exception);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000149 return GetGlobalProperty("result")->ToObjectChecked()->Number();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000150}
151
152
153TEST(Add) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000154 CcTest::InitializeVM();
155 v8::HandleScope scope(CcTest::isolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000156 CHECK_EQ(5.0, Add(2, 3));
157}
158
159
160static double Abs(int x) {
161 Handle<JSFunction> fun = Compile("if (x < 0) result = -x; else result = x;");
162 if (fun.is_null()) return -1;
163
164 SetGlobalProperty("x", Smi::FromInt(x));
165 bool has_pending_exception;
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000166 Handle<JSObject> global(Isolate::Current()->context()->global_object());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000167 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
168 CHECK(!has_pending_exception);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000169 return GetGlobalProperty("result")->ToObjectChecked()->Number();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000170}
171
172
173TEST(Abs) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000174 CcTest::InitializeVM();
175 v8::HandleScope scope(CcTest::isolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000176 CHECK_EQ(3.0, Abs(-3));
177}
178
179
180static double Sum(int n) {
181 Handle<JSFunction> fun =
182 Compile("s = 0; while (n > 0) { s += n; n -= 1; }; result = s;");
183 if (fun.is_null()) return -1;
184
185 SetGlobalProperty("n", Smi::FromInt(n));
186 bool has_pending_exception;
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000187 Handle<JSObject> global(Isolate::Current()->context()->global_object());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000188 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
189 CHECK(!has_pending_exception);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000190 return GetGlobalProperty("result")->ToObjectChecked()->Number();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000191}
192
193
194TEST(Sum) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000195 CcTest::InitializeVM();
196 v8::HandleScope scope(CcTest::isolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000197 CHECK_EQ(5050.0, Sum(100));
198}
199
200
201TEST(Print) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000202 CcTest::InitializeVM(PRINT_EXTENSION);
203 v8::HandleScope scope(CcTest::isolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000204 const char* source = "for (n = 0; n < 100; ++n) print(n, 1, 2);";
205 Handle<JSFunction> fun = Compile(source);
206 if (fun.is_null()) return;
207 bool has_pending_exception;
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000208 Handle<JSObject> global(Isolate::Current()->context()->global_object());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000209 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
210 CHECK(!has_pending_exception);
211}
212
213
214// The following test method stems from my coding efforts today. It
215// tests all the functionality I have added to the compiler today
216TEST(Stuff) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000217 CcTest::InitializeVM();
218 v8::HandleScope scope(CcTest::isolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000219 const char* source =
220 "r = 0;\n"
221 "a = new Object;\n"
222 "if (a == a) r+=1;\n" // 1
223 "if (a != new Object()) r+=2;\n" // 2
224 "a.x = 42;\n"
225 "if (a.x == 42) r+=4;\n" // 4
226 "function foo() { var x = 87; return x; }\n"
227 "if (foo() == 87) r+=8;\n" // 8
228 "function bar() { var x; x = 99; return x; }\n"
229 "if (bar() == 99) r+=16;\n" // 16
230 "function baz() { var x = 1, y, z = 2; y = 3; return x + y + z; }\n"
231 "if (baz() == 6) r+=32;\n" // 32
232 "function Cons0() { this.x = 42; this.y = 87; }\n"
233 "if (new Cons0().x == 42) r+=64;\n" // 64
234 "if (new Cons0().y == 87) r+=128;\n" // 128
235 "function Cons2(x, y) { this.sum = x + y; }\n"
236 "if (new Cons2(3,4).sum == 7) r+=256;"; // 256
237
238 Handle<JSFunction> fun = Compile(source);
239 CHECK(!fun.is_null());
240 bool has_pending_exception;
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000241 Handle<JSObject> global(Isolate::Current()->context()->global_object());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000242 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
243 CHECK(!has_pending_exception);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000244 CHECK_EQ(511.0, GetGlobalProperty("r")->ToObjectChecked()->Number());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000245}
246
247
248TEST(UncaughtThrow) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000249 CcTest::InitializeVM();
250 v8::HandleScope scope(CcTest::isolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000251
252 const char* source = "throw 42;";
253 Handle<JSFunction> fun = Compile(source);
254 CHECK(!fun.is_null());
255 bool has_pending_exception;
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000256 Isolate* isolate = fun->GetIsolate();
257 Handle<JSObject> global(isolate->context()->global_object());
yangguo@chromium.org659ceec2012-01-26 07:37:54 +0000258 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000259 CHECK(has_pending_exception);
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000260 CHECK_EQ(42.0, isolate->pending_exception()->ToObjectChecked()->Number());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000261}
262
263
264// Tests calling a builtin function from C/C++ code, and the builtin function
265// performs GC. It creates a stack frame looks like following:
266// | C (PerformGC) |
267// | JS-to-C |
268// | JS |
269// | C-to-JS |
270TEST(C2JSFrames) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000271 CcTest::InitializeVM(PRINT_EXTENSION | GC_EXTENSION);
272 v8::HandleScope scope(CcTest::isolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000273
274 const char* source = "function foo(a) { gc(), print(a); }";
275
276 Handle<JSFunction> fun0 = Compile(source);
277 CHECK(!fun0.is_null());
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000278 Isolate* isolate = fun0->GetIsolate();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000279
280 // Run the generated code to populate the global object with 'foo'.
281 bool has_pending_exception;
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000282 Handle<JSObject> global(Isolate::Current()->context()->global_object());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000283 Execution::Call(fun0, global, 0, NULL, &has_pending_exception);
284 CHECK(!has_pending_exception);
285
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000286 Object* foo_string =
287 FACTORY->InternalizeOneByteString(STATIC_ASCII_VECTOR("foo"))->
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000288 ToObjectChecked();
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000289 MaybeObject* fun1_object = isolate->context()->global_object()->
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000290 GetProperty(String::cast(foo_string));
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000291 Handle<Object> fun1(fun1_object->ToObjectChecked(), isolate);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000292 CHECK(fun1->IsJSFunction());
293
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000294 Handle<Object> argv[] =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000295 { FACTORY->InternalizeOneByteString(STATIC_ASCII_VECTOR("hello")) };
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000296 Execution::Call(Handle<JSFunction>::cast(fun1),
297 global,
298 ARRAY_SIZE(argv),
299 argv,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000300 &has_pending_exception);
301 CHECK(!has_pending_exception);
302}
ager@chromium.org381abbb2009-02-25 13:23:22 +0000303
304
305// Regression 236. Calling InitLineEnds on a Script with undefined
306// source resulted in crash.
307TEST(Regression236) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000308 CcTest::InitializeVM();
309 v8::HandleScope scope(CcTest::isolate());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000310
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000311 Handle<Script> script = FACTORY->NewScript(FACTORY->empty_string());
312 script->set_source(HEAP->undefined_value());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000313 CHECK_EQ(-1, GetScriptLineNumber(script, 0));
314 CHECK_EQ(-1, GetScriptLineNumber(script, 100));
315 CHECK_EQ(-1, GetScriptLineNumber(script, -1));
ager@chromium.org381abbb2009-02-25 13:23:22 +0000316}
ager@chromium.org5c838252010-02-19 08:53:10 +0000317
318
319TEST(GetScriptLineNumber) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000320 CcTest::InitializeVM();
321 v8::HandleScope scope(CcTest::isolate());
ager@chromium.org5c838252010-02-19 08:53:10 +0000322 v8::ScriptOrigin origin = v8::ScriptOrigin(v8::String::New("test"));
323 const char function_f[] = "function f() {}";
324 const int max_rows = 1000;
325 const int buffer_size = max_rows + sizeof(function_f);
326 ScopedVector<char> buffer(buffer_size);
327 memset(buffer.start(), '\n', buffer_size - 1);
328 buffer[buffer_size - 1] = '\0';
329
330 for (int i = 0; i < max_rows; ++i) {
331 if (i > 0)
332 buffer[i - 1] = '\n';
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000333 OS::MemCopy(&buffer[i], function_f, sizeof(function_f) - 1);
ager@chromium.org5c838252010-02-19 08:53:10 +0000334 v8::Handle<v8::String> script_body = v8::String::New(buffer.start());
335 v8::Script::Compile(script_body, &origin)->Run();
336 v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast(
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000337 CcTest::env()->Global()->Get(v8::String::New("f")));
ager@chromium.org5c838252010-02-19 08:53:10 +0000338 CHECK_EQ(i, f->GetScriptLineNumber());
339 }
340}
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000341
342
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000343// Test that optimized code for different closures is actually shared
344// immediately by the FastNewClosureStub when run in the same context.
345TEST(OptimizedCodeSharing) {
yangguo@chromium.orgde0db002012-06-22 13:44:28 +0000346 // Skip test if --cache-optimized-code is not activated by default because
347 // FastNewClosureStub that is baked into the snapshot is incorrect.
348 if (!FLAG_cache_optimized_code) return;
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000349 FLAG_allow_natives_syntax = true;
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000350 CcTest::InitializeVM();
351 v8::HandleScope scope(CcTest::isolate());
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000352 for (int i = 0; i < 10; i++) {
353 LocalContext env;
354 env->Global()->Set(v8::String::New("x"), v8::Integer::New(i));
355 CompileRun("function MakeClosure() {"
356 " return function() { return x; };"
357 "}"
358 "var closure0 = MakeClosure();"
359 "%DebugPrint(closure0());"
360 "%OptimizeFunctionOnNextCall(closure0);"
361 "%DebugPrint(closure0());"
362 "var closure1 = MakeClosure();"
363 "var closure2 = MakeClosure();");
364 Handle<JSFunction> fun1 = v8::Utils::OpenHandle(
365 *v8::Local<v8::Function>::Cast(env->Global()->Get(v8_str("closure1"))));
366 Handle<JSFunction> fun2 = v8::Utils::OpenHandle(
367 *v8::Local<v8::Function>::Cast(env->Global()->Get(v8_str("closure2"))));
368 CHECK(fun1->IsOptimized() || !fun1->IsOptimizable());
369 CHECK(fun2->IsOptimized() || !fun2->IsOptimizable());
370 CHECK_EQ(fun1->code(), fun2->code());
371 }
372}
373
374
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000375#ifdef ENABLE_DISASSEMBLER
376static Handle<JSFunction> GetJSFunction(v8::Handle<v8::Object> obj,
377 const char* property_name) {
378 v8::Local<v8::Function> fun =
379 v8::Local<v8::Function>::Cast(obj->Get(v8_str(property_name)));
380 return v8::Utils::OpenHandle(*fun);
381}
382
383
384static void CheckCodeForUnsafeLiteral(Handle<JSFunction> f) {
385 // Create a disassembler with default name lookup.
386 disasm::NameConverter name_converter;
387 disasm::Disassembler d(name_converter);
388
389 if (f->code()->kind() == Code::FUNCTION) {
390 Address pc = f->code()->instruction_start();
391 int decode_size =
392 Min(f->code()->instruction_size(),
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000393 static_cast<int>(f->code()->back_edge_table_offset()));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000394 Address end = pc + decode_size;
395
396 v8::internal::EmbeddedVector<char, 128> decode_buffer;
jkummerow@chromium.org7a6fc812012-06-27 11:12:38 +0000397 v8::internal::EmbeddedVector<char, 128> smi_hex_buffer;
398 Smi* smi = Smi::FromInt(12345678);
399 OS::SNPrintF(smi_hex_buffer, "0x%lx", reinterpret_cast<intptr_t>(smi));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000400 while (pc < end) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000401 int num_const = d.ConstantPoolSizeAt(pc);
402 if (num_const >= 0) {
ricow@chromium.org9fa09672011-07-25 11:05:35 +0000403 pc += (num_const + 1) * kPointerSize;
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000404 } else {
405 pc += d.InstructionDecode(decode_buffer, pc);
jkummerow@chromium.org7a6fc812012-06-27 11:12:38 +0000406 CHECK(strstr(decode_buffer.start(), smi_hex_buffer.start()) == NULL);
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000407 }
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000408 }
409 }
410}
411
412
413TEST(SplitConstantsInFullCompiler) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000414 CcTest::InitializeVM();
415 v8::HandleScope scope(CcTest::isolate());
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000416
417 CompileRun("function f() { a = 12345678 }; f();");
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000418 CheckCodeForUnsafeLiteral(GetJSFunction(CcTest::env()->Global(), "f"));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000419 CompileRun("function f(x) { a = 12345678 + x}; f(1);");
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000420 CheckCodeForUnsafeLiteral(GetJSFunction(CcTest::env()->Global(), "f"));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000421 CompileRun("function f(x) { var arguments = 1; x += 12345678}; f(1);");
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000422 CheckCodeForUnsafeLiteral(GetJSFunction(CcTest::env()->Global(), "f"));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000423 CompileRun("function f(x) { var arguments = 1; x = 12345678}; f(1);");
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000424 CheckCodeForUnsafeLiteral(GetJSFunction(CcTest::env()->Global(), "f"));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000425}
426#endif