yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2 | // 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.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 29 | #include <wchar.h> |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 30 | |
| 31 | #include "v8.h" |
| 32 | |
| 33 | #include "compiler.h" |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 34 | #include "disasm.h" |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 35 | #include "cctest.h" |
| 36 | |
| 37 | using namespace v8::internal; |
| 38 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 39 | static MaybeObject* GetGlobalProperty(const char* name) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 40 | Isolate* isolate = CcTest::i_isolate(); |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 41 | Handle<String> internalized_name = |
| 42 | isolate->factory()->InternalizeUtf8String(name); |
| 43 | return isolate->context()->global_object()->GetProperty(*internalized_name); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | |
| 47 | static void SetGlobalProperty(const char* name, Object* value) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 48 | Isolate* isolate = CcTest::i_isolate(); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 49 | Handle<Object> object(value, isolate); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 50 | Handle<String> internalized_name = |
| 51 | isolate->factory()->InternalizeUtf8String(name); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 52 | Handle<JSObject> global(isolate->context()->global_object()); |
machenbach@chromium.org | e8412be | 2013-11-08 10:23:52 +0000 | [diff] [blame] | 53 | Runtime::SetObjectProperty(isolate, global, internalized_name, object, NONE, |
dslomov@chromium.org | 486536d | 2014-03-12 13:09:18 +0000 | [diff] [blame] | 54 | SLOPPY); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | |
| 58 | static Handle<JSFunction> Compile(const char* source) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 59 | Isolate* isolate = CcTest::i_isolate(); |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 60 | Handle<String> source_code( |
| 61 | isolate->factory()->NewStringFromUtf8(CStrVector(source))); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 62 | Handle<SharedFunctionInfo> shared_function = |
yangguo@chromium.org | 4954674 | 2013-12-23 16:17:49 +0000 | [diff] [blame] | 63 | Compiler::CompileScript(source_code, |
| 64 | Handle<String>(), |
| 65 | 0, |
| 66 | 0, |
| 67 | false, |
| 68 | Handle<Context>(isolate->native_context()), |
| 69 | NULL, NULL, |
yangguo@chromium.org | 4954674 | 2013-12-23 16:17:49 +0000 | [diff] [blame] | 70 | NOT_NATIVES_CODE); |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 71 | return isolate->factory()->NewFunctionFromSharedFunctionInfo( |
| 72 | shared_function, isolate->native_context()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 76 | static double Inc(Isolate* isolate, int x) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 77 | const char* source = "result = %d + 1;"; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 78 | EmbeddedVector<char, 512> buffer; |
| 79 | OS::SNPrintF(buffer, source, x); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 80 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 81 | Handle<JSFunction> fun = Compile(buffer.start()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 82 | if (fun.is_null()) return -1; |
| 83 | |
| 84 | bool has_pending_exception; |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 85 | Handle<JSObject> global(isolate->context()->global_object()); |
| 86 | Execution::Call(isolate, fun, global, 0, NULL, &has_pending_exception); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 87 | CHECK(!has_pending_exception); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 88 | return GetGlobalProperty("result")->ToObjectChecked()->Number(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | |
| 92 | TEST(Inc) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 93 | CcTest::InitializeVM(); |
| 94 | v8::HandleScope scope(CcTest::isolate()); |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 95 | CHECK_EQ(4.0, Inc(CcTest::i_isolate(), 3)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 99 | static double Add(Isolate* isolate, int x, int y) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 100 | 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.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 106 | Handle<JSObject> global(isolate->context()->global_object()); |
| 107 | Execution::Call(isolate, fun, global, 0, NULL, &has_pending_exception); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 108 | CHECK(!has_pending_exception); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 109 | return GetGlobalProperty("result")->ToObjectChecked()->Number(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | |
| 113 | TEST(Add) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 114 | CcTest::InitializeVM(); |
| 115 | v8::HandleScope scope(CcTest::isolate()); |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 116 | CHECK_EQ(5.0, Add(CcTest::i_isolate(), 2, 3)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 120 | static double Abs(Isolate* isolate, int x) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 121 | 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.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 126 | Handle<JSObject> global(isolate->context()->global_object()); |
| 127 | Execution::Call(isolate, fun, global, 0, NULL, &has_pending_exception); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 128 | CHECK(!has_pending_exception); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 129 | return GetGlobalProperty("result")->ToObjectChecked()->Number(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | |
| 133 | TEST(Abs) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 134 | CcTest::InitializeVM(); |
| 135 | v8::HandleScope scope(CcTest::isolate()); |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 136 | CHECK_EQ(3.0, Abs(CcTest::i_isolate(), -3)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 140 | static double Sum(Isolate* isolate, int n) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 141 | 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.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 147 | Handle<JSObject> global(isolate->context()->global_object()); |
| 148 | Execution::Call(isolate, fun, global, 0, NULL, &has_pending_exception); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 149 | CHECK(!has_pending_exception); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 150 | return GetGlobalProperty("result")->ToObjectChecked()->Number(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | |
| 154 | TEST(Sum) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 155 | CcTest::InitializeVM(); |
| 156 | v8::HandleScope scope(CcTest::isolate()); |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 157 | CHECK_EQ(5050.0, Sum(CcTest::i_isolate(), 100)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | |
| 161 | TEST(Print) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 162 | v8::HandleScope scope(CcTest::isolate()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 163 | v8::Local<v8::Context> context = CcTest::NewContext(PRINT_EXTENSION); |
| 164 | v8::Context::Scope context_scope(context); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 165 | 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.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 169 | 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.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 172 | 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 |
| 178 | TEST(Stuff) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 179 | CcTest::InitializeVM(); |
| 180 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 181 | 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.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 203 | 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.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 206 | CHECK(!has_pending_exception); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 207 | CHECK_EQ(511.0, GetGlobalProperty("r")->ToObjectChecked()->Number()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | |
| 211 | TEST(UncaughtThrow) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 212 | CcTest::InitializeVM(); |
| 213 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 214 | |
| 215 | const char* source = "throw 42;"; |
| 216 | Handle<JSFunction> fun = Compile(source); |
| 217 | CHECK(!fun.is_null()); |
| 218 | bool has_pending_exception; |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 219 | Isolate* isolate = fun->GetIsolate(); |
| 220 | Handle<JSObject> global(isolate->context()->global_object()); |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 221 | Execution::Call(isolate, fun, global, 0, NULL, &has_pending_exception); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 222 | CHECK(has_pending_exception); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 223 | CHECK_EQ(42.0, isolate->pending_exception()->ToObjectChecked()->Number()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 224 | } |
| 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 | |
| 233 | TEST(C2JSFrames) { |
machenbach@chromium.org | c8cbc43 | 2014-01-21 09:01:57 +0000 | [diff] [blame] | 234 | FLAG_expose_gc = true; |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 235 | v8::HandleScope scope(CcTest::isolate()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 236 | v8::Local<v8::Context> context = |
| 237 | CcTest::NewContext(PRINT_EXTENSION | GC_EXTENSION); |
| 238 | v8::Context::Scope context_scope(context); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 239 | |
| 240 | const char* source = "function foo(a) { gc(), print(a); }"; |
| 241 | |
| 242 | Handle<JSFunction> fun0 = Compile(source); |
| 243 | CHECK(!fun0.is_null()); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 244 | Isolate* isolate = fun0->GetIsolate(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 245 | |
| 246 | // Run the generated code to populate the global object with 'foo'. |
| 247 | bool has_pending_exception; |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 248 | Handle<JSObject> global(isolate->context()->global_object()); |
| 249 | Execution::Call( |
| 250 | isolate, fun0, global, 0, NULL, &has_pending_exception); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 251 | CHECK(!has_pending_exception); |
| 252 | |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 253 | Object* foo_string = isolate->factory()->InternalizeOneByteString( |
| 254 | STATIC_ASCII_VECTOR("foo"))->ToObjectChecked(); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 255 | MaybeObject* fun1_object = isolate->context()->global_object()-> |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 256 | GetProperty(String::cast(foo_string)); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 257 | Handle<Object> fun1(fun1_object->ToObjectChecked(), isolate); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 258 | CHECK(fun1->IsJSFunction()); |
| 259 | |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 260 | Handle<Object> argv[] = { isolate->factory()->InternalizeOneByteString( |
| 261 | STATIC_ASCII_VECTOR("hello")) }; |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 262 | Execution::Call(isolate, |
| 263 | Handle<JSFunction>::cast(fun1), |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 264 | global, |
| 265 | ARRAY_SIZE(argv), |
| 266 | argv, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 267 | &has_pending_exception); |
| 268 | CHECK(!has_pending_exception); |
| 269 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 270 | |
| 271 | |
| 272 | // Regression 236. Calling InitLineEnds on a Script with undefined |
| 273 | // source resulted in crash. |
| 274 | TEST(Regression236) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 275 | CcTest::InitializeVM(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 276 | Isolate* isolate = CcTest::i_isolate(); |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 277 | Factory* factory = isolate->factory(); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 278 | v8::HandleScope scope(CcTest::isolate()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 279 | |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 280 | Handle<Script> script = factory->NewScript(factory->empty_string()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 281 | script->set_source(CcTest::heap()->undefined_value()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 282 | CHECK_EQ(-1, GetScriptLineNumber(script, 0)); |
| 283 | CHECK_EQ(-1, GetScriptLineNumber(script, 100)); |
| 284 | CHECK_EQ(-1, GetScriptLineNumber(script, -1)); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 285 | } |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 286 | |
| 287 | |
| 288 | TEST(GetScriptLineNumber) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 289 | LocalContext context; |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 290 | v8::HandleScope scope(CcTest::isolate()); |
machenbach@chromium.org | f984189 | 2013-11-25 12:01:13 +0000 | [diff] [blame] | 291 | v8::ScriptOrigin origin = |
| 292 | v8::ScriptOrigin(v8::String::NewFromUtf8(CcTest::isolate(), "test")); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 293 | 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.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 303 | OS::MemCopy(&buffer[i], function_f, sizeof(function_f) - 1); |
machenbach@chromium.org | f984189 | 2013-11-25 12:01:13 +0000 | [diff] [blame] | 304 | v8::Handle<v8::String> script_body = |
| 305 | v8::String::NewFromUtf8(CcTest::isolate(), buffer.start()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 306 | v8::Script::Compile(script_body, &origin)->Run(); |
machenbach@chromium.org | f984189 | 2013-11-25 12:01:13 +0000 | [diff] [blame] | 307 | v8::Local<v8::Function> f = |
| 308 | v8::Local<v8::Function>::Cast(context->Global()->Get( |
| 309 | v8::String::NewFromUtf8(CcTest::isolate(), "f"))); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 310 | CHECK_EQ(i, f->GetScriptLineNumber()); |
| 311 | } |
| 312 | } |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 313 | |
| 314 | |
dslomov@chromium.org | 6b6df38 | 2014-03-14 16:14:53 +0000 | [diff] [blame] | 315 | TEST(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.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 352 | // Test that optimized code for different closures is actually shared |
| 353 | // immediately by the FastNewClosureStub when run in the same context. |
| 354 | TEST(OptimizedCodeSharing) { |
yangguo@chromium.org | de0db00 | 2012-06-22 13:44:28 +0000 | [diff] [blame] | 355 | // 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.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 358 | FLAG_stress_compaction = false; |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 359 | FLAG_allow_natives_syntax = true; |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 360 | CcTest::InitializeVM(); |
| 361 | v8::HandleScope scope(CcTest::isolate()); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 362 | for (int i = 0; i < 10; i++) { |
| 363 | LocalContext env; |
machenbach@chromium.org | f984189 | 2013-11-25 12:01:13 +0000 | [diff] [blame] | 364 | env->Global()->Set(v8::String::NewFromUtf8(CcTest::isolate(), "x"), |
ulan@chromium.org | 0f13e74 | 2014-01-03 15:51:11 +0000 | [diff] [blame] | 365 | v8::Integer::New(CcTest::isolate(), i)); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 366 | 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.org | c9913f0 | 2013-10-24 06:31:36 +0000 | [diff] [blame] | 379 | 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.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 383 | CHECK_EQ(fun1->code(), fun2->code()); |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 388 | #ifdef ENABLE_DISASSEMBLER |
| 389 | static 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 | |
| 397 | static 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.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 406 | static_cast<int>(f->code()->back_edge_table_offset())); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 407 | Address end = pc + decode_size; |
| 408 | |
| 409 | v8::internal::EmbeddedVector<char, 128> decode_buffer; |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 410 | 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.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 413 | while (pc < end) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 414 | int num_const = d.ConstantPoolSizeAt(pc); |
| 415 | if (num_const >= 0) { |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 416 | pc += (num_const + 1) * kPointerSize; |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 417 | } else { |
| 418 | pc += d.InstructionDecode(decode_buffer, pc); |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 419 | CHECK(strstr(decode_buffer.start(), smi_hex_buffer.start()) == NULL); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 420 | } |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | |
| 426 | TEST(SplitConstantsInFullCompiler) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 427 | LocalContext context; |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 428 | v8::HandleScope scope(CcTest::isolate()); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 429 | |
| 430 | CompileRun("function f() { a = 12345678 }; f();"); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 431 | CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 432 | CompileRun("function f(x) { a = 12345678 + x}; f(1);"); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 433 | CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 434 | CompileRun("function f(x) { var arguments = 1; x += 12345678}; f(1);"); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 435 | CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 436 | CompileRun("function f(x) { var arguments = 1; x = 12345678}; f(1);"); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 437 | CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 438 | } |
| 439 | #endif |