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); |
| 50 | static v8::Handle<v8::Value> Print(const v8::Arguments& args); |
| 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 | |
| 65 | v8::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.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]); |
| 70 | if (*str == NULL) return v8::Undefined(); |
| 71 | printf("%s", *str); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 72 | } |
| 73 | printf("\n"); |
| 74 | return v8::Undefined(); |
| 75 | } |
| 76 | |
| 77 | |
| 78 | static PrintExtension kPrintExtension; |
| 79 | v8::DeclareExtension kPrintExtensionDeclaration(&kPrintExtension); |
| 80 | |
| 81 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 82 | static MaybeObject* GetGlobalProperty(const char* name) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 83 | Handle<String> internalized_name = FACTORY->InternalizeUtf8String(name); |
| 84 | return Isolate::Current()->context()->global_object()->GetProperty( |
| 85 | *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) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 100 | Handle<String> source_code(FACTORY->NewStringFromUtf8(CStrVector(source))); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 101 | Handle<SharedFunctionInfo> shared_function = |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 102 | Compiler::Compile(source_code, |
| 103 | Handle<String>(), |
| 104 | 0, |
| 105 | 0, |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 106 | Handle<Context>(Isolate::Current()->native_context()), |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 107 | NULL, |
| 108 | NULL, |
| 109 | Handle<String>::null(), |
| 110 | NOT_NATIVES_CODE); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 111 | return FACTORY->NewFunctionFromSharedFunctionInfo(shared_function, |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 112 | Isolate::Current()->native_context()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | |
| 116 | static double Inc(int x) { |
| 117 | const char* source = "result = %d + 1;"; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 118 | EmbeddedVector<char, 512> buffer; |
| 119 | OS::SNPrintF(buffer, source, x); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 120 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 121 | Handle<JSFunction> fun = Compile(buffer.start()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 122 | if (fun.is_null()) return -1; |
| 123 | |
| 124 | bool has_pending_exception; |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 125 | Handle<JSObject> global(Isolate::Current()->context()->global_object()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 126 | Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 127 | CHECK(!has_pending_exception); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 128 | return GetGlobalProperty("result")->ToObjectChecked()->Number(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | |
| 132 | TEST(Inc) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 133 | CcTest::InitializeVM(); |
| 134 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 135 | CHECK_EQ(4.0, Inc(3)); |
| 136 | } |
| 137 | |
| 138 | |
| 139 | static 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.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 146 | Handle<JSObject> global(Isolate::Current()->context()->global_object()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 147 | Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 148 | CHECK(!has_pending_exception); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 149 | return GetGlobalProperty("result")->ToObjectChecked()->Number(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | |
| 153 | TEST(Add) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 154 | CcTest::InitializeVM(); |
| 155 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 156 | CHECK_EQ(5.0, Add(2, 3)); |
| 157 | } |
| 158 | |
| 159 | |
| 160 | static 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.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 166 | Handle<JSObject> global(Isolate::Current()->context()->global_object()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 167 | Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 168 | CHECK(!has_pending_exception); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 169 | return GetGlobalProperty("result")->ToObjectChecked()->Number(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | |
| 173 | TEST(Abs) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 174 | CcTest::InitializeVM(); |
| 175 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 176 | CHECK_EQ(3.0, Abs(-3)); |
| 177 | } |
| 178 | |
| 179 | |
| 180 | static 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.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 187 | Handle<JSObject> global(Isolate::Current()->context()->global_object()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 188 | Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 189 | CHECK(!has_pending_exception); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 190 | return GetGlobalProperty("result")->ToObjectChecked()->Number(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | |
| 194 | TEST(Sum) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 195 | CcTest::InitializeVM(); |
| 196 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 197 | CHECK_EQ(5050.0, Sum(100)); |
| 198 | } |
| 199 | |
| 200 | |
| 201 | TEST(Print) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 202 | CcTest::InitializeVM(PRINT_EXTENSION); |
| 203 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 204 | 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.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 208 | Handle<JSObject> global(Isolate::Current()->context()->global_object()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 209 | 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 |
| 216 | TEST(Stuff) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 217 | CcTest::InitializeVM(); |
| 218 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 219 | 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.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 241 | Handle<JSObject> global(Isolate::Current()->context()->global_object()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 242 | Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 243 | CHECK(!has_pending_exception); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 244 | CHECK_EQ(511.0, GetGlobalProperty("r")->ToObjectChecked()->Number()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | |
| 248 | TEST(UncaughtThrow) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 249 | CcTest::InitializeVM(); |
| 250 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 251 | |
| 252 | const char* source = "throw 42;"; |
| 253 | Handle<JSFunction> fun = Compile(source); |
| 254 | CHECK(!fun.is_null()); |
| 255 | bool has_pending_exception; |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 256 | Isolate* isolate = fun->GetIsolate(); |
| 257 | Handle<JSObject> global(isolate->context()->global_object()); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 258 | Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 259 | CHECK(has_pending_exception); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 260 | CHECK_EQ(42.0, isolate->pending_exception()->ToObjectChecked()->Number()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 261 | } |
| 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 | |
| 270 | TEST(C2JSFrames) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 271 | CcTest::InitializeVM(PRINT_EXTENSION | GC_EXTENSION); |
| 272 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 273 | |
| 274 | const char* source = "function foo(a) { gc(), print(a); }"; |
| 275 | |
| 276 | Handle<JSFunction> fun0 = Compile(source); |
| 277 | CHECK(!fun0.is_null()); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 278 | Isolate* isolate = fun0->GetIsolate(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 279 | |
| 280 | // Run the generated code to populate the global object with 'foo'. |
| 281 | bool has_pending_exception; |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 282 | Handle<JSObject> global(Isolate::Current()->context()->global_object()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 283 | Execution::Call(fun0, global, 0, NULL, &has_pending_exception); |
| 284 | CHECK(!has_pending_exception); |
| 285 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 286 | Object* foo_string = |
| 287 | FACTORY->InternalizeOneByteString(STATIC_ASCII_VECTOR("foo"))-> |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 288 | ToObjectChecked(); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 289 | MaybeObject* fun1_object = isolate->context()->global_object()-> |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 290 | GetProperty(String::cast(foo_string)); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 291 | Handle<Object> fun1(fun1_object->ToObjectChecked(), isolate); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 292 | CHECK(fun1->IsJSFunction()); |
| 293 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 294 | Handle<Object> argv[] = |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 295 | { FACTORY->InternalizeOneByteString(STATIC_ASCII_VECTOR("hello")) }; |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 296 | Execution::Call(Handle<JSFunction>::cast(fun1), |
| 297 | global, |
| 298 | ARRAY_SIZE(argv), |
| 299 | argv, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 300 | &has_pending_exception); |
| 301 | CHECK(!has_pending_exception); |
| 302 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 303 | |
| 304 | |
| 305 | // Regression 236. Calling InitLineEnds on a Script with undefined |
| 306 | // source resulted in crash. |
| 307 | TEST(Regression236) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 308 | CcTest::InitializeVM(); |
| 309 | v8::HandleScope scope(CcTest::isolate()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 310 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 311 | Handle<Script> script = FACTORY->NewScript(FACTORY->empty_string()); |
| 312 | script->set_source(HEAP->undefined_value()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 313 | CHECK_EQ(-1, GetScriptLineNumber(script, 0)); |
| 314 | CHECK_EQ(-1, GetScriptLineNumber(script, 100)); |
| 315 | CHECK_EQ(-1, GetScriptLineNumber(script, -1)); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 316 | } |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 317 | |
| 318 | |
| 319 | TEST(GetScriptLineNumber) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 320 | CcTest::InitializeVM(); |
| 321 | v8::HandleScope scope(CcTest::isolate()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 322 | 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.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 333 | OS::MemCopy(&buffer[i], function_f, sizeof(function_f) - 1); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 334 | 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.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 337 | CcTest::env()->Global()->Get(v8::String::New("f"))); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 338 | CHECK_EQ(i, f->GetScriptLineNumber()); |
| 339 | } |
| 340 | } |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 341 | |
| 342 | |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 343 | // Test that optimized code for different closures is actually shared |
| 344 | // immediately by the FastNewClosureStub when run in the same context. |
| 345 | TEST(OptimizedCodeSharing) { |
yangguo@chromium.org | de0db00 | 2012-06-22 13:44:28 +0000 | [diff] [blame] | 346 | // 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.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 349 | FLAG_allow_natives_syntax = true; |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 350 | CcTest::InitializeVM(); |
| 351 | v8::HandleScope scope(CcTest::isolate()); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 352 | 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.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 375 | #ifdef ENABLE_DISASSEMBLER |
| 376 | static 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 | |
| 384 | static 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.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 393 | static_cast<int>(f->code()->back_edge_table_offset())); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 394 | Address end = pc + decode_size; |
| 395 | |
| 396 | v8::internal::EmbeddedVector<char, 128> decode_buffer; |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 397 | 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.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 400 | while (pc < end) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 401 | int num_const = d.ConstantPoolSizeAt(pc); |
| 402 | if (num_const >= 0) { |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 403 | pc += (num_const + 1) * kPointerSize; |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 404 | } else { |
| 405 | pc += d.InstructionDecode(decode_buffer, pc); |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 406 | CHECK(strstr(decode_buffer.start(), smi_hex_buffer.start()) == NULL); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 407 | } |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | |
| 413 | TEST(SplitConstantsInFullCompiler) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 414 | CcTest::InitializeVM(); |
| 415 | v8::HandleScope scope(CcTest::isolate()); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 416 | |
| 417 | CompileRun("function f() { a = 12345678 }; f();"); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 418 | CheckCodeForUnsafeLiteral(GetJSFunction(CcTest::env()->Global(), "f")); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 419 | CompileRun("function f(x) { a = 12345678 + x}; f(1);"); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 420 | CheckCodeForUnsafeLiteral(GetJSFunction(CcTest::env()->Global(), "f")); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 421 | CompileRun("function f(x) { var arguments = 1; x += 12345678}; f(1);"); |
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) { var arguments = 1; x = 12345678}; 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 | } |
| 426 | #endif |