blob: bed8a6c92ba7822898a0fd9824fe17c02a088c45 [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);
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +000050 static void Print(const v8::FunctionCallbackInfo<v8::Value>& args);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000051 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
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +000065void PrintExtension::Print(const v8::FunctionCallbackInfo<v8::Value>& args) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000066 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]);
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +000070 if (*str == NULL) return;
ulan@chromium.org56c14af2012-09-20 12:51:09 +000071 printf("%s", *str);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000072 }
73 printf("\n");
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000074}
75
76
77static PrintExtension kPrintExtension;
78v8::DeclareExtension kPrintExtensionDeclaration(&kPrintExtension);
79
80
lrn@chromium.org303ada72010-10-27 09:33:13 +000081static MaybeObject* GetGlobalProperty(const char* name) {
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +000082 Isolate* isolate = Isolate::Current();
83 Handle<String> internalized_name =
84 isolate->factory()->InternalizeUtf8String(name);
85 return isolate->context()->global_object()->GetProperty(*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) {
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +0000100 Isolate* isolate = Isolate::Current();
101 Handle<String> source_code(
102 isolate->factory()->NewStringFromUtf8(CStrVector(source)));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000103 Handle<SharedFunctionInfo> shared_function =
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000104 Compiler::Compile(source_code,
105 Handle<String>(),
106 0,
107 0,
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +0000108 Handle<Context>(isolate->native_context()),
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000109 NULL,
110 NULL,
111 Handle<String>::null(),
112 NOT_NATIVES_CODE);
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +0000113 return isolate->factory()->NewFunctionFromSharedFunctionInfo(
114 shared_function, isolate->native_context());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000115}
116
117
118static double Inc(int x) {
119 const char* source = "result = %d + 1;";
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000120 EmbeddedVector<char, 512> buffer;
121 OS::SNPrintF(buffer, source, x);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000122
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000123 Handle<JSFunction> fun = Compile(buffer.start());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000124 if (fun.is_null()) return -1;
125
126 bool has_pending_exception;
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000127 Handle<JSObject> global(Isolate::Current()->context()->global_object());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000128 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
129 CHECK(!has_pending_exception);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000130 return GetGlobalProperty("result")->ToObjectChecked()->Number();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000131}
132
133
134TEST(Inc) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000135 CcTest::InitializeVM();
136 v8::HandleScope scope(CcTest::isolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000137 CHECK_EQ(4.0, Inc(3));
138}
139
140
141static double Add(int x, int y) {
142 Handle<JSFunction> fun = Compile("result = x + y;");
143 if (fun.is_null()) return -1;
144
145 SetGlobalProperty("x", Smi::FromInt(x));
146 SetGlobalProperty("y", Smi::FromInt(y));
147 bool has_pending_exception;
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000148 Handle<JSObject> global(Isolate::Current()->context()->global_object());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000149 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
150 CHECK(!has_pending_exception);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000151 return GetGlobalProperty("result")->ToObjectChecked()->Number();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000152}
153
154
155TEST(Add) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000156 CcTest::InitializeVM();
157 v8::HandleScope scope(CcTest::isolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000158 CHECK_EQ(5.0, Add(2, 3));
159}
160
161
162static double Abs(int x) {
163 Handle<JSFunction> fun = Compile("if (x < 0) result = -x; else result = x;");
164 if (fun.is_null()) return -1;
165
166 SetGlobalProperty("x", Smi::FromInt(x));
167 bool has_pending_exception;
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000168 Handle<JSObject> global(Isolate::Current()->context()->global_object());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000169 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
170 CHECK(!has_pending_exception);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000171 return GetGlobalProperty("result")->ToObjectChecked()->Number();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000172}
173
174
175TEST(Abs) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000176 CcTest::InitializeVM();
177 v8::HandleScope scope(CcTest::isolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000178 CHECK_EQ(3.0, Abs(-3));
179}
180
181
182static double Sum(int n) {
183 Handle<JSFunction> fun =
184 Compile("s = 0; while (n > 0) { s += n; n -= 1; }; result = s;");
185 if (fun.is_null()) return -1;
186
187 SetGlobalProperty("n", Smi::FromInt(n));
188 bool has_pending_exception;
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000189 Handle<JSObject> global(Isolate::Current()->context()->global_object());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000190 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
191 CHECK(!has_pending_exception);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000192 return GetGlobalProperty("result")->ToObjectChecked()->Number();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000193}
194
195
196TEST(Sum) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000197 CcTest::InitializeVM();
198 v8::HandleScope scope(CcTest::isolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000199 CHECK_EQ(5050.0, Sum(100));
200}
201
202
203TEST(Print) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000204 CcTest::InitializeVM(PRINT_EXTENSION);
205 v8::HandleScope scope(CcTest::isolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000206 const char* source = "for (n = 0; n < 100; ++n) print(n, 1, 2);";
207 Handle<JSFunction> fun = Compile(source);
208 if (fun.is_null()) return;
209 bool has_pending_exception;
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000210 Handle<JSObject> global(Isolate::Current()->context()->global_object());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000211 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
212 CHECK(!has_pending_exception);
213}
214
215
216// The following test method stems from my coding efforts today. It
217// tests all the functionality I have added to the compiler today
218TEST(Stuff) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000219 CcTest::InitializeVM();
220 v8::HandleScope scope(CcTest::isolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000221 const char* source =
222 "r = 0;\n"
223 "a = new Object;\n"
224 "if (a == a) r+=1;\n" // 1
225 "if (a != new Object()) r+=2;\n" // 2
226 "a.x = 42;\n"
227 "if (a.x == 42) r+=4;\n" // 4
228 "function foo() { var x = 87; return x; }\n"
229 "if (foo() == 87) r+=8;\n" // 8
230 "function bar() { var x; x = 99; return x; }\n"
231 "if (bar() == 99) r+=16;\n" // 16
232 "function baz() { var x = 1, y, z = 2; y = 3; return x + y + z; }\n"
233 "if (baz() == 6) r+=32;\n" // 32
234 "function Cons0() { this.x = 42; this.y = 87; }\n"
235 "if (new Cons0().x == 42) r+=64;\n" // 64
236 "if (new Cons0().y == 87) r+=128;\n" // 128
237 "function Cons2(x, y) { this.sum = x + y; }\n"
238 "if (new Cons2(3,4).sum == 7) r+=256;"; // 256
239
240 Handle<JSFunction> fun = Compile(source);
241 CHECK(!fun.is_null());
242 bool has_pending_exception;
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000243 Handle<JSObject> global(Isolate::Current()->context()->global_object());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000244 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
245 CHECK(!has_pending_exception);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000246 CHECK_EQ(511.0, GetGlobalProperty("r")->ToObjectChecked()->Number());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000247}
248
249
250TEST(UncaughtThrow) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000251 CcTest::InitializeVM();
252 v8::HandleScope scope(CcTest::isolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000253
254 const char* source = "throw 42;";
255 Handle<JSFunction> fun = Compile(source);
256 CHECK(!fun.is_null());
257 bool has_pending_exception;
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000258 Isolate* isolate = fun->GetIsolate();
259 Handle<JSObject> global(isolate->context()->global_object());
yangguo@chromium.org659ceec2012-01-26 07:37:54 +0000260 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000261 CHECK(has_pending_exception);
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000262 CHECK_EQ(42.0, isolate->pending_exception()->ToObjectChecked()->Number());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000263}
264
265
266// Tests calling a builtin function from C/C++ code, and the builtin function
267// performs GC. It creates a stack frame looks like following:
268// | C (PerformGC) |
269// | JS-to-C |
270// | JS |
271// | C-to-JS |
272TEST(C2JSFrames) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000273 CcTest::InitializeVM(PRINT_EXTENSION | GC_EXTENSION);
274 v8::HandleScope scope(CcTest::isolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000275
276 const char* source = "function foo(a) { gc(), print(a); }";
277
278 Handle<JSFunction> fun0 = Compile(source);
279 CHECK(!fun0.is_null());
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000280 Isolate* isolate = fun0->GetIsolate();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000281
282 // Run the generated code to populate the global object with 'foo'.
283 bool has_pending_exception;
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000284 Handle<JSObject> global(Isolate::Current()->context()->global_object());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000285 Execution::Call(fun0, global, 0, NULL, &has_pending_exception);
286 CHECK(!has_pending_exception);
287
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +0000288 Object* foo_string = isolate->factory()->InternalizeOneByteString(
289 STATIC_ASCII_VECTOR("foo"))->ToObjectChecked();
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000290 MaybeObject* fun1_object = isolate->context()->global_object()->
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000291 GetProperty(String::cast(foo_string));
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000292 Handle<Object> fun1(fun1_object->ToObjectChecked(), isolate);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000293 CHECK(fun1->IsJSFunction());
294
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +0000295 Handle<Object> argv[] = { isolate->factory()->InternalizeOneByteString(
296 STATIC_ASCII_VECTOR("hello")) };
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000297 Execution::Call(Handle<JSFunction>::cast(fun1),
298 global,
299 ARRAY_SIZE(argv),
300 argv,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000301 &has_pending_exception);
302 CHECK(!has_pending_exception);
303}
ager@chromium.org381abbb2009-02-25 13:23:22 +0000304
305
306// Regression 236. Calling InitLineEnds on a Script with undefined
307// source resulted in crash.
308TEST(Regression236) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000309 CcTest::InitializeVM();
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +0000310 Isolate* isolate = Isolate::Current();
311 Factory* factory = isolate->factory();
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000312 v8::HandleScope scope(CcTest::isolate());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000313
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +0000314 Handle<Script> script = factory->NewScript(factory->empty_string());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000315 script->set_source(HEAP->undefined_value());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000316 CHECK_EQ(-1, GetScriptLineNumber(script, 0));
317 CHECK_EQ(-1, GetScriptLineNumber(script, 100));
318 CHECK_EQ(-1, GetScriptLineNumber(script, -1));
ager@chromium.org381abbb2009-02-25 13:23:22 +0000319}
ager@chromium.org5c838252010-02-19 08:53:10 +0000320
321
322TEST(GetScriptLineNumber) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000323 CcTest::InitializeVM();
324 v8::HandleScope scope(CcTest::isolate());
ager@chromium.org5c838252010-02-19 08:53:10 +0000325 v8::ScriptOrigin origin = v8::ScriptOrigin(v8::String::New("test"));
326 const char function_f[] = "function f() {}";
327 const int max_rows = 1000;
328 const int buffer_size = max_rows + sizeof(function_f);
329 ScopedVector<char> buffer(buffer_size);
330 memset(buffer.start(), '\n', buffer_size - 1);
331 buffer[buffer_size - 1] = '\0';
332
333 for (int i = 0; i < max_rows; ++i) {
334 if (i > 0)
335 buffer[i - 1] = '\n';
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000336 OS::MemCopy(&buffer[i], function_f, sizeof(function_f) - 1);
ager@chromium.org5c838252010-02-19 08:53:10 +0000337 v8::Handle<v8::String> script_body = v8::String::New(buffer.start());
338 v8::Script::Compile(script_body, &origin)->Run();
339 v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast(
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000340 CcTest::env()->Global()->Get(v8::String::New("f")));
ager@chromium.org5c838252010-02-19 08:53:10 +0000341 CHECK_EQ(i, f->GetScriptLineNumber());
342 }
343}
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000344
345
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000346// Test that optimized code for different closures is actually shared
347// immediately by the FastNewClosureStub when run in the same context.
348TEST(OptimizedCodeSharing) {
yangguo@chromium.orgde0db002012-06-22 13:44:28 +0000349 // Skip test if --cache-optimized-code is not activated by default because
350 // FastNewClosureStub that is baked into the snapshot is incorrect.
351 if (!FLAG_cache_optimized_code) return;
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000352 FLAG_stress_compaction = false;
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000353 FLAG_allow_natives_syntax = true;
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000354 CcTest::InitializeVM();
355 v8::HandleScope scope(CcTest::isolate());
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000356 for (int i = 0; i < 10; i++) {
357 LocalContext env;
358 env->Global()->Set(v8::String::New("x"), v8::Integer::New(i));
359 CompileRun("function MakeClosure() {"
360 " return function() { return x; };"
361 "}"
362 "var closure0 = MakeClosure();"
363 "%DebugPrint(closure0());"
364 "%OptimizeFunctionOnNextCall(closure0);"
365 "%DebugPrint(closure0());"
366 "var closure1 = MakeClosure();"
367 "var closure2 = MakeClosure();");
368 Handle<JSFunction> fun1 = v8::Utils::OpenHandle(
369 *v8::Local<v8::Function>::Cast(env->Global()->Get(v8_str("closure1"))));
370 Handle<JSFunction> fun2 = v8::Utils::OpenHandle(
371 *v8::Local<v8::Function>::Cast(env->Global()->Get(v8_str("closure2"))));
372 CHECK(fun1->IsOptimized() || !fun1->IsOptimizable());
373 CHECK(fun2->IsOptimized() || !fun2->IsOptimizable());
374 CHECK_EQ(fun1->code(), fun2->code());
375 }
376}
377
378
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000379#ifdef ENABLE_DISASSEMBLER
380static Handle<JSFunction> GetJSFunction(v8::Handle<v8::Object> obj,
381 const char* property_name) {
382 v8::Local<v8::Function> fun =
383 v8::Local<v8::Function>::Cast(obj->Get(v8_str(property_name)));
384 return v8::Utils::OpenHandle(*fun);
385}
386
387
388static void CheckCodeForUnsafeLiteral(Handle<JSFunction> f) {
389 // Create a disassembler with default name lookup.
390 disasm::NameConverter name_converter;
391 disasm::Disassembler d(name_converter);
392
393 if (f->code()->kind() == Code::FUNCTION) {
394 Address pc = f->code()->instruction_start();
395 int decode_size =
396 Min(f->code()->instruction_size(),
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000397 static_cast<int>(f->code()->back_edge_table_offset()));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000398 Address end = pc + decode_size;
399
400 v8::internal::EmbeddedVector<char, 128> decode_buffer;
jkummerow@chromium.org7a6fc812012-06-27 11:12:38 +0000401 v8::internal::EmbeddedVector<char, 128> smi_hex_buffer;
402 Smi* smi = Smi::FromInt(12345678);
403 OS::SNPrintF(smi_hex_buffer, "0x%lx", reinterpret_cast<intptr_t>(smi));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000404 while (pc < end) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000405 int num_const = d.ConstantPoolSizeAt(pc);
406 if (num_const >= 0) {
ricow@chromium.org9fa09672011-07-25 11:05:35 +0000407 pc += (num_const + 1) * kPointerSize;
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000408 } else {
409 pc += d.InstructionDecode(decode_buffer, pc);
jkummerow@chromium.org7a6fc812012-06-27 11:12:38 +0000410 CHECK(strstr(decode_buffer.start(), smi_hex_buffer.start()) == NULL);
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000411 }
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000412 }
413 }
414}
415
416
417TEST(SplitConstantsInFullCompiler) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000418 CcTest::InitializeVM();
419 v8::HandleScope scope(CcTest::isolate());
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000420
421 CompileRun("function f() { a = 12345678 }; f();");
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) { a = 12345678 + x}; 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 CompileRun("function f(x) { var arguments = 1; x += 12345678}; f(1);");
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000426 CheckCodeForUnsafeLiteral(GetJSFunction(CcTest::env()->Global(), "f"));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000427 CompileRun("function f(x) { var arguments = 1; x = 12345678}; f(1);");
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000428 CheckCodeForUnsafeLiteral(GetJSFunction(CcTest::env()->Global(), "f"));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000429}
430#endif