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" |
| 35 | #include "disassembler.h" |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 36 | #include "execution.h" |
| 37 | #include "factory.h" |
| 38 | #include "platform.h" |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 39 | #include "cctest.h" |
| 40 | |
| 41 | using namespace v8::internal; |
| 42 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 43 | // --- P r i n t E x t e n s i o n --- |
| 44 | |
| 45 | class 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.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame^] | 50 | static void Print(const v8::FunctionCallbackInfo<v8::Value>& args); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 51 | private: |
| 52 | static const char* kSource; |
| 53 | }; |
| 54 | |
| 55 | |
| 56 | const char* PrintExtension::kSource = "native function print();"; |
| 57 | |
| 58 | |
| 59 | v8::Handle<v8::FunctionTemplate> PrintExtension::GetNativeFunction( |
| 60 | v8::Handle<v8::String> str) { |
| 61 | return v8::FunctionTemplate::New(PrintExtension::Print); |
| 62 | } |
| 63 | |
| 64 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame^] | 65 | void PrintExtension::Print(const v8::FunctionCallbackInfo<v8::Value>& args) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 66 | for (int i = 0; i < args.Length(); i++) { |
| 67 | if (i != 0) printf(" "); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 68 | v8::HandleScope scope(args.GetIsolate()); |
ulan@chromium.org | 56c14af | 2012-09-20 12:51:09 +0000 | [diff] [blame] | 69 | v8::String::Utf8Value str(args[i]); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame^] | 70 | if (*str == NULL) return; |
ulan@chromium.org | 56c14af | 2012-09-20 12:51:09 +0000 | [diff] [blame] | 71 | printf("%s", *str); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 72 | } |
| 73 | printf("\n"); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | |
| 77 | static PrintExtension kPrintExtension; |
| 78 | v8::DeclareExtension kPrintExtensionDeclaration(&kPrintExtension); |
| 79 | |
| 80 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 81 | static MaybeObject* GetGlobalProperty(const char* name) { |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 82 | 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.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | |
| 89 | static void SetGlobalProperty(const char* name, Object* value) { |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 90 | Isolate* isolate = Isolate::Current(); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 91 | Handle<Object> object(value, isolate); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 92 | Handle<String> internalized_name = |
| 93 | isolate->factory()->InternalizeUtf8String(name); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 94 | Handle<JSObject> global(isolate->context()->global_object()); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 95 | SetProperty(isolate, global, internalized_name, object, NONE, kNonStrictMode); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | |
| 99 | static Handle<JSFunction> Compile(const char* source) { |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 100 | Isolate* isolate = Isolate::Current(); |
| 101 | Handle<String> source_code( |
| 102 | isolate->factory()->NewStringFromUtf8(CStrVector(source))); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 103 | Handle<SharedFunctionInfo> shared_function = |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 104 | Compiler::Compile(source_code, |
| 105 | Handle<String>(), |
| 106 | 0, |
| 107 | 0, |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 108 | Handle<Context>(isolate->native_context()), |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 109 | NULL, |
| 110 | NULL, |
| 111 | Handle<String>::null(), |
| 112 | NOT_NATIVES_CODE); |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 113 | return isolate->factory()->NewFunctionFromSharedFunctionInfo( |
| 114 | shared_function, isolate->native_context()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | |
| 118 | static double Inc(int x) { |
| 119 | const char* source = "result = %d + 1;"; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 120 | EmbeddedVector<char, 512> buffer; |
| 121 | OS::SNPrintF(buffer, source, x); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 122 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 123 | Handle<JSFunction> fun = Compile(buffer.start()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 124 | if (fun.is_null()) return -1; |
| 125 | |
| 126 | bool has_pending_exception; |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 127 | Handle<JSObject> global(Isolate::Current()->context()->global_object()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 128 | Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 129 | CHECK(!has_pending_exception); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 130 | return GetGlobalProperty("result")->ToObjectChecked()->Number(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | |
| 134 | TEST(Inc) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 135 | CcTest::InitializeVM(); |
| 136 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 137 | CHECK_EQ(4.0, Inc(3)); |
| 138 | } |
| 139 | |
| 140 | |
| 141 | static 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.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 148 | Handle<JSObject> global(Isolate::Current()->context()->global_object()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 149 | Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 150 | CHECK(!has_pending_exception); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 151 | return GetGlobalProperty("result")->ToObjectChecked()->Number(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | |
| 155 | TEST(Add) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 156 | CcTest::InitializeVM(); |
| 157 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 158 | CHECK_EQ(5.0, Add(2, 3)); |
| 159 | } |
| 160 | |
| 161 | |
| 162 | static 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.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 168 | Handle<JSObject> global(Isolate::Current()->context()->global_object()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 169 | Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 170 | CHECK(!has_pending_exception); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 171 | return GetGlobalProperty("result")->ToObjectChecked()->Number(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | |
| 175 | TEST(Abs) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 176 | CcTest::InitializeVM(); |
| 177 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 178 | CHECK_EQ(3.0, Abs(-3)); |
| 179 | } |
| 180 | |
| 181 | |
| 182 | static 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.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 189 | Handle<JSObject> global(Isolate::Current()->context()->global_object()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 190 | Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 191 | CHECK(!has_pending_exception); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 192 | return GetGlobalProperty("result")->ToObjectChecked()->Number(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | |
| 196 | TEST(Sum) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 197 | CcTest::InitializeVM(); |
| 198 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 199 | CHECK_EQ(5050.0, Sum(100)); |
| 200 | } |
| 201 | |
| 202 | |
| 203 | TEST(Print) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 204 | CcTest::InitializeVM(PRINT_EXTENSION); |
| 205 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 206 | 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.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 210 | Handle<JSObject> global(Isolate::Current()->context()->global_object()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 211 | 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 |
| 218 | TEST(Stuff) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 219 | CcTest::InitializeVM(); |
| 220 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 221 | 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.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 243 | Handle<JSObject> global(Isolate::Current()->context()->global_object()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 244 | Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 245 | CHECK(!has_pending_exception); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 246 | CHECK_EQ(511.0, GetGlobalProperty("r")->ToObjectChecked()->Number()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | |
| 250 | TEST(UncaughtThrow) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 251 | CcTest::InitializeVM(); |
| 252 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 253 | |
| 254 | const char* source = "throw 42;"; |
| 255 | Handle<JSFunction> fun = Compile(source); |
| 256 | CHECK(!fun.is_null()); |
| 257 | bool has_pending_exception; |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 258 | Isolate* isolate = fun->GetIsolate(); |
| 259 | Handle<JSObject> global(isolate->context()->global_object()); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 260 | Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 261 | CHECK(has_pending_exception); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 262 | CHECK_EQ(42.0, isolate->pending_exception()->ToObjectChecked()->Number()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 263 | } |
| 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 | |
| 272 | TEST(C2JSFrames) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 273 | CcTest::InitializeVM(PRINT_EXTENSION | GC_EXTENSION); |
| 274 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 275 | |
| 276 | const char* source = "function foo(a) { gc(), print(a); }"; |
| 277 | |
| 278 | Handle<JSFunction> fun0 = Compile(source); |
| 279 | CHECK(!fun0.is_null()); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 280 | Isolate* isolate = fun0->GetIsolate(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 281 | |
| 282 | // Run the generated code to populate the global object with 'foo'. |
| 283 | bool has_pending_exception; |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 284 | Handle<JSObject> global(Isolate::Current()->context()->global_object()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 285 | Execution::Call(fun0, global, 0, NULL, &has_pending_exception); |
| 286 | CHECK(!has_pending_exception); |
| 287 | |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 288 | Object* foo_string = isolate->factory()->InternalizeOneByteString( |
| 289 | STATIC_ASCII_VECTOR("foo"))->ToObjectChecked(); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 290 | MaybeObject* fun1_object = isolate->context()->global_object()-> |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 291 | GetProperty(String::cast(foo_string)); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 292 | Handle<Object> fun1(fun1_object->ToObjectChecked(), isolate); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 293 | CHECK(fun1->IsJSFunction()); |
| 294 | |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 295 | Handle<Object> argv[] = { isolate->factory()->InternalizeOneByteString( |
| 296 | STATIC_ASCII_VECTOR("hello")) }; |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 297 | Execution::Call(Handle<JSFunction>::cast(fun1), |
| 298 | global, |
| 299 | ARRAY_SIZE(argv), |
| 300 | argv, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 301 | &has_pending_exception); |
| 302 | CHECK(!has_pending_exception); |
| 303 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 304 | |
| 305 | |
| 306 | // Regression 236. Calling InitLineEnds on a Script with undefined |
| 307 | // source resulted in crash. |
| 308 | TEST(Regression236) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 309 | CcTest::InitializeVM(); |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 310 | Isolate* isolate = Isolate::Current(); |
| 311 | Factory* factory = isolate->factory(); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 312 | v8::HandleScope scope(CcTest::isolate()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 313 | |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 314 | Handle<Script> script = factory->NewScript(factory->empty_string()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 315 | script->set_source(HEAP->undefined_value()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 316 | CHECK_EQ(-1, GetScriptLineNumber(script, 0)); |
| 317 | CHECK_EQ(-1, GetScriptLineNumber(script, 100)); |
| 318 | CHECK_EQ(-1, GetScriptLineNumber(script, -1)); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 319 | } |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 320 | |
| 321 | |
| 322 | TEST(GetScriptLineNumber) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 323 | CcTest::InitializeVM(); |
| 324 | v8::HandleScope scope(CcTest::isolate()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 325 | 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.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 336 | OS::MemCopy(&buffer[i], function_f, sizeof(function_f) - 1); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 337 | 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.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 340 | CcTest::env()->Global()->Get(v8::String::New("f"))); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 341 | CHECK_EQ(i, f->GetScriptLineNumber()); |
| 342 | } |
| 343 | } |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 344 | |
| 345 | |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 346 | // Test that optimized code for different closures is actually shared |
| 347 | // immediately by the FastNewClosureStub when run in the same context. |
| 348 | TEST(OptimizedCodeSharing) { |
yangguo@chromium.org | de0db00 | 2012-06-22 13:44:28 +0000 | [diff] [blame] | 349 | // 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.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame^] | 352 | FLAG_stress_compaction = false; |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 353 | FLAG_allow_natives_syntax = true; |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 354 | CcTest::InitializeVM(); |
| 355 | v8::HandleScope scope(CcTest::isolate()); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 356 | 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.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 379 | #ifdef ENABLE_DISASSEMBLER |
| 380 | static 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 | |
| 388 | static 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.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 397 | static_cast<int>(f->code()->back_edge_table_offset())); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 398 | Address end = pc + decode_size; |
| 399 | |
| 400 | v8::internal::EmbeddedVector<char, 128> decode_buffer; |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 401 | 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.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 404 | while (pc < end) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 405 | int num_const = d.ConstantPoolSizeAt(pc); |
| 406 | if (num_const >= 0) { |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 407 | pc += (num_const + 1) * kPointerSize; |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 408 | } else { |
| 409 | pc += d.InstructionDecode(decode_buffer, pc); |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 410 | CHECK(strstr(decode_buffer.start(), smi_hex_buffer.start()) == NULL); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 411 | } |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 412 | } |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | |
| 417 | TEST(SplitConstantsInFullCompiler) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 418 | CcTest::InitializeVM(); |
| 419 | v8::HandleScope scope(CcTest::isolate()); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 420 | |
| 421 | CompileRun("function f() { a = 12345678 }; f();"); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 422 | CheckCodeForUnsafeLiteral(GetJSFunction(CcTest::env()->Global(), "f")); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 423 | CompileRun("function f(x) { a = 12345678 + x}; f(1);"); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 424 | CheckCodeForUnsafeLiteral(GetJSFunction(CcTest::env()->Global(), "f")); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 425 | CompileRun("function f(x) { var arguments = 1; x += 12345678}; f(1);"); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 426 | CheckCodeForUnsafeLiteral(GetJSFunction(CcTest::env()->Global(), "f")); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 427 | CompileRun("function f(x) { var arguments = 1; x = 12345678}; f(1);"); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 428 | CheckCodeForUnsafeLiteral(GetJSFunction(CcTest::env()->Global(), "f")); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 429 | } |
| 430 | #endif |