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 | |
| 43 | static v8::Persistent<v8::Context> env; |
| 44 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 45 | // --- P r i n t E x t e n s i o n --- |
| 46 | |
| 47 | class PrintExtension : public v8::Extension { |
| 48 | public: |
| 49 | PrintExtension() : v8::Extension("v8/print", kSource) { } |
| 50 | virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( |
| 51 | v8::Handle<v8::String> name); |
| 52 | static v8::Handle<v8::Value> Print(const v8::Arguments& args); |
| 53 | private: |
| 54 | static const char* kSource; |
| 55 | }; |
| 56 | |
| 57 | |
| 58 | const char* PrintExtension::kSource = "native function print();"; |
| 59 | |
| 60 | |
| 61 | v8::Handle<v8::FunctionTemplate> PrintExtension::GetNativeFunction( |
| 62 | v8::Handle<v8::String> str) { |
| 63 | return v8::FunctionTemplate::New(PrintExtension::Print); |
| 64 | } |
| 65 | |
| 66 | |
| 67 | v8::Handle<v8::Value> PrintExtension::Print(const v8::Arguments& args) { |
| 68 | for (int i = 0; i < args.Length(); i++) { |
| 69 | if (i != 0) printf(" "); |
| 70 | v8::HandleScope scope; |
ulan@chromium.org | 56c14af | 2012-09-20 12:51:09 +0000 | [diff] [blame] | 71 | v8::String::Utf8Value str(args[i]); |
| 72 | if (*str == NULL) return v8::Undefined(); |
| 73 | printf("%s", *str); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 74 | } |
| 75 | printf("\n"); |
| 76 | return v8::Undefined(); |
| 77 | } |
| 78 | |
| 79 | |
| 80 | static PrintExtension kPrintExtension; |
| 81 | v8::DeclareExtension kPrintExtensionDeclaration(&kPrintExtension); |
| 82 | |
| 83 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 84 | static void InitializeVM() { |
| 85 | if (env.IsEmpty()) { |
| 86 | v8::HandleScope scope; |
| 87 | const char* extensions[] = { "v8/print", "v8/gc" }; |
| 88 | v8::ExtensionConfiguration config(2, extensions); |
| 89 | env = v8::Context::New(&config); |
| 90 | } |
| 91 | v8::HandleScope scope; |
| 92 | env->Enter(); |
| 93 | } |
| 94 | |
| 95 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 96 | static MaybeObject* GetGlobalProperty(const char* name) { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame^] | 97 | Handle<String> symbol = FACTORY->LookupUtf8Symbol(name); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 98 | return Isolate::Current()->context()->global_object()->GetProperty(*symbol); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | |
| 102 | static void SetGlobalProperty(const char* name, Object* value) { |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 103 | Isolate* isolate = Isolate::Current(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 104 | Handle<Object> object(value); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame^] | 105 | Handle<String> symbol = FACTORY->LookupUtf8Symbol(name); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 106 | Handle<JSObject> global(Isolate::Current()->context()->global_object()); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 107 | SetProperty(isolate, global, symbol, object, NONE, kNonStrictMode); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | |
| 111 | static Handle<JSFunction> Compile(const char* source) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 112 | Handle<String> source_code(FACTORY->NewStringFromUtf8(CStrVector(source))); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 113 | Handle<SharedFunctionInfo> shared_function = |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 114 | Compiler::Compile(source_code, |
| 115 | Handle<String>(), |
| 116 | 0, |
| 117 | 0, |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 118 | Handle<Context>(Isolate::Current()->native_context()), |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 119 | NULL, |
| 120 | NULL, |
| 121 | Handle<String>::null(), |
| 122 | NOT_NATIVES_CODE); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 123 | return FACTORY->NewFunctionFromSharedFunctionInfo(shared_function, |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 124 | Isolate::Current()->native_context()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | |
| 128 | static double Inc(int x) { |
| 129 | const char* source = "result = %d + 1;"; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 130 | EmbeddedVector<char, 512> buffer; |
| 131 | OS::SNPrintF(buffer, source, x); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 132 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 133 | Handle<JSFunction> fun = Compile(buffer.start()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 134 | if (fun.is_null()) return -1; |
| 135 | |
| 136 | bool has_pending_exception; |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 137 | Handle<JSObject> global(Isolate::Current()->context()->global_object()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 138 | Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 139 | CHECK(!has_pending_exception); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 140 | return GetGlobalProperty("result")->ToObjectChecked()->Number(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | |
| 144 | TEST(Inc) { |
| 145 | InitializeVM(); |
| 146 | v8::HandleScope scope; |
| 147 | CHECK_EQ(4.0, Inc(3)); |
| 148 | } |
| 149 | |
| 150 | |
| 151 | static double Add(int x, int y) { |
| 152 | Handle<JSFunction> fun = Compile("result = x + y;"); |
| 153 | if (fun.is_null()) return -1; |
| 154 | |
| 155 | SetGlobalProperty("x", Smi::FromInt(x)); |
| 156 | SetGlobalProperty("y", Smi::FromInt(y)); |
| 157 | bool has_pending_exception; |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 158 | Handle<JSObject> global(Isolate::Current()->context()->global_object()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 159 | Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 160 | CHECK(!has_pending_exception); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 161 | return GetGlobalProperty("result")->ToObjectChecked()->Number(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | |
| 165 | TEST(Add) { |
| 166 | InitializeVM(); |
| 167 | v8::HandleScope scope; |
| 168 | CHECK_EQ(5.0, Add(2, 3)); |
| 169 | } |
| 170 | |
| 171 | |
| 172 | static double Abs(int x) { |
| 173 | Handle<JSFunction> fun = Compile("if (x < 0) result = -x; else result = x;"); |
| 174 | if (fun.is_null()) return -1; |
| 175 | |
| 176 | SetGlobalProperty("x", Smi::FromInt(x)); |
| 177 | bool has_pending_exception; |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 178 | Handle<JSObject> global(Isolate::Current()->context()->global_object()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 179 | Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 180 | CHECK(!has_pending_exception); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 181 | return GetGlobalProperty("result")->ToObjectChecked()->Number(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | |
| 185 | TEST(Abs) { |
| 186 | InitializeVM(); |
| 187 | v8::HandleScope scope; |
| 188 | CHECK_EQ(3.0, Abs(-3)); |
| 189 | } |
| 190 | |
| 191 | |
| 192 | static double Sum(int n) { |
| 193 | Handle<JSFunction> fun = |
| 194 | Compile("s = 0; while (n > 0) { s += n; n -= 1; }; result = s;"); |
| 195 | if (fun.is_null()) return -1; |
| 196 | |
| 197 | SetGlobalProperty("n", Smi::FromInt(n)); |
| 198 | bool has_pending_exception; |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 199 | Handle<JSObject> global(Isolate::Current()->context()->global_object()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 200 | Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 201 | CHECK(!has_pending_exception); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 202 | return GetGlobalProperty("result")->ToObjectChecked()->Number(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | |
| 206 | TEST(Sum) { |
| 207 | InitializeVM(); |
| 208 | v8::HandleScope scope; |
| 209 | CHECK_EQ(5050.0, Sum(100)); |
| 210 | } |
| 211 | |
| 212 | |
| 213 | TEST(Print) { |
| 214 | InitializeVM(); |
| 215 | v8::HandleScope scope; |
| 216 | const char* source = "for (n = 0; n < 100; ++n) print(n, 1, 2);"; |
| 217 | Handle<JSFunction> fun = Compile(source); |
| 218 | if (fun.is_null()) return; |
| 219 | bool has_pending_exception; |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 220 | Handle<JSObject> global(Isolate::Current()->context()->global_object()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 221 | Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 222 | CHECK(!has_pending_exception); |
| 223 | } |
| 224 | |
| 225 | |
| 226 | // The following test method stems from my coding efforts today. It |
| 227 | // tests all the functionality I have added to the compiler today |
| 228 | TEST(Stuff) { |
| 229 | InitializeVM(); |
| 230 | v8::HandleScope scope; |
| 231 | const char* source = |
| 232 | "r = 0;\n" |
| 233 | "a = new Object;\n" |
| 234 | "if (a == a) r+=1;\n" // 1 |
| 235 | "if (a != new Object()) r+=2;\n" // 2 |
| 236 | "a.x = 42;\n" |
| 237 | "if (a.x == 42) r+=4;\n" // 4 |
| 238 | "function foo() { var x = 87; return x; }\n" |
| 239 | "if (foo() == 87) r+=8;\n" // 8 |
| 240 | "function bar() { var x; x = 99; return x; }\n" |
| 241 | "if (bar() == 99) r+=16;\n" // 16 |
| 242 | "function baz() { var x = 1, y, z = 2; y = 3; return x + y + z; }\n" |
| 243 | "if (baz() == 6) r+=32;\n" // 32 |
| 244 | "function Cons0() { this.x = 42; this.y = 87; }\n" |
| 245 | "if (new Cons0().x == 42) r+=64;\n" // 64 |
| 246 | "if (new Cons0().y == 87) r+=128;\n" // 128 |
| 247 | "function Cons2(x, y) { this.sum = x + y; }\n" |
| 248 | "if (new Cons2(3,4).sum == 7) r+=256;"; // 256 |
| 249 | |
| 250 | Handle<JSFunction> fun = Compile(source); |
| 251 | CHECK(!fun.is_null()); |
| 252 | bool has_pending_exception; |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 253 | Handle<JSObject> global(Isolate::Current()->context()->global_object()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 254 | Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 255 | CHECK(!has_pending_exception); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 256 | CHECK_EQ(511.0, GetGlobalProperty("r")->ToObjectChecked()->Number()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | |
| 260 | TEST(UncaughtThrow) { |
| 261 | InitializeVM(); |
| 262 | v8::HandleScope scope; |
| 263 | |
| 264 | const char* source = "throw 42;"; |
| 265 | Handle<JSFunction> fun = Compile(source); |
| 266 | CHECK(!fun.is_null()); |
| 267 | bool has_pending_exception; |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 268 | Handle<JSObject> global(Isolate::Current()->context()->global_object()); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 269 | Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 270 | CHECK(has_pending_exception); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 271 | CHECK_EQ(42.0, Isolate::Current()->pending_exception()-> |
| 272 | ToObjectChecked()->Number()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | |
| 276 | // Tests calling a builtin function from C/C++ code, and the builtin function |
| 277 | // performs GC. It creates a stack frame looks like following: |
| 278 | // | C (PerformGC) | |
| 279 | // | JS-to-C | |
| 280 | // | JS | |
| 281 | // | C-to-JS | |
| 282 | TEST(C2JSFrames) { |
| 283 | InitializeVM(); |
| 284 | v8::HandleScope scope; |
| 285 | |
| 286 | const char* source = "function foo(a) { gc(), print(a); }"; |
| 287 | |
| 288 | Handle<JSFunction> fun0 = Compile(source); |
| 289 | CHECK(!fun0.is_null()); |
| 290 | |
| 291 | // Run the generated code to populate the global object with 'foo'. |
| 292 | bool has_pending_exception; |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 293 | Handle<JSObject> global(Isolate::Current()->context()->global_object()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 294 | Execution::Call(fun0, global, 0, NULL, &has_pending_exception); |
| 295 | CHECK(!has_pending_exception); |
| 296 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame^] | 297 | Object* foo_symbol = |
| 298 | FACTORY->LookupOneByteSymbol(STATIC_ASCII_VECTOR("foo"))-> |
| 299 | ToObjectChecked(); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 300 | MaybeObject* fun1_object = Isolate::Current()->context()->global_object()-> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 301 | GetProperty(String::cast(foo_symbol)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 302 | Handle<Object> fun1(fun1_object->ToObjectChecked()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 303 | CHECK(fun1->IsJSFunction()); |
| 304 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame^] | 305 | Handle<Object> argv[] = |
| 306 | { FACTORY->LookupOneByteSymbol(STATIC_ASCII_VECTOR("hello")) }; |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 307 | Execution::Call(Handle<JSFunction>::cast(fun1), |
| 308 | global, |
| 309 | ARRAY_SIZE(argv), |
| 310 | argv, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 311 | &has_pending_exception); |
| 312 | CHECK(!has_pending_exception); |
| 313 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 314 | |
| 315 | |
| 316 | // Regression 236. Calling InitLineEnds on a Script with undefined |
| 317 | // source resulted in crash. |
| 318 | TEST(Regression236) { |
| 319 | InitializeVM(); |
| 320 | v8::HandleScope scope; |
| 321 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 322 | Handle<Script> script = FACTORY->NewScript(FACTORY->empty_string()); |
| 323 | script->set_source(HEAP->undefined_value()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 324 | CHECK_EQ(-1, GetScriptLineNumber(script, 0)); |
| 325 | CHECK_EQ(-1, GetScriptLineNumber(script, 100)); |
| 326 | CHECK_EQ(-1, GetScriptLineNumber(script, -1)); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 327 | } |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 328 | |
| 329 | |
| 330 | TEST(GetScriptLineNumber) { |
| 331 | LocalContext env; |
| 332 | v8::HandleScope scope; |
| 333 | v8::ScriptOrigin origin = v8::ScriptOrigin(v8::String::New("test")); |
| 334 | const char function_f[] = "function f() {}"; |
| 335 | const int max_rows = 1000; |
| 336 | const int buffer_size = max_rows + sizeof(function_f); |
| 337 | ScopedVector<char> buffer(buffer_size); |
| 338 | memset(buffer.start(), '\n', buffer_size - 1); |
| 339 | buffer[buffer_size - 1] = '\0'; |
| 340 | |
| 341 | for (int i = 0; i < max_rows; ++i) { |
| 342 | if (i > 0) |
| 343 | buffer[i - 1] = '\n'; |
| 344 | memcpy(&buffer[i], function_f, sizeof(function_f) - 1); |
| 345 | v8::Handle<v8::String> script_body = v8::String::New(buffer.start()); |
| 346 | v8::Script::Compile(script_body, &origin)->Run(); |
| 347 | v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast( |
| 348 | env->Global()->Get(v8::String::New("f"))); |
| 349 | CHECK_EQ(i, f->GetScriptLineNumber()); |
| 350 | } |
| 351 | } |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 352 | |
| 353 | |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 354 | // Test that optimized code for different closures is actually shared |
| 355 | // immediately by the FastNewClosureStub when run in the same context. |
| 356 | TEST(OptimizedCodeSharing) { |
yangguo@chromium.org | de0db00 | 2012-06-22 13:44:28 +0000 | [diff] [blame] | 357 | // Skip test if --cache-optimized-code is not activated by default because |
| 358 | // FastNewClosureStub that is baked into the snapshot is incorrect. |
| 359 | if (!FLAG_cache_optimized_code) return; |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 360 | FLAG_allow_natives_syntax = true; |
| 361 | InitializeVM(); |
| 362 | v8::HandleScope scope; |
| 363 | for (int i = 0; i < 10; i++) { |
| 364 | LocalContext env; |
| 365 | env->Global()->Set(v8::String::New("x"), v8::Integer::New(i)); |
| 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")))); |
| 379 | CHECK(fun1->IsOptimized() || !fun1->IsOptimizable()); |
| 380 | CHECK(fun2->IsOptimized() || !fun2->IsOptimizable()); |
| 381 | CHECK_EQ(fun1->code(), fun2->code()); |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 386 | #ifdef ENABLE_DISASSEMBLER |
| 387 | static Handle<JSFunction> GetJSFunction(v8::Handle<v8::Object> obj, |
| 388 | const char* property_name) { |
| 389 | v8::Local<v8::Function> fun = |
| 390 | v8::Local<v8::Function>::Cast(obj->Get(v8_str(property_name))); |
| 391 | return v8::Utils::OpenHandle(*fun); |
| 392 | } |
| 393 | |
| 394 | |
| 395 | static void CheckCodeForUnsafeLiteral(Handle<JSFunction> f) { |
| 396 | // Create a disassembler with default name lookup. |
| 397 | disasm::NameConverter name_converter; |
| 398 | disasm::Disassembler d(name_converter); |
| 399 | |
| 400 | if (f->code()->kind() == Code::FUNCTION) { |
| 401 | Address pc = f->code()->instruction_start(); |
| 402 | int decode_size = |
| 403 | Min(f->code()->instruction_size(), |
| 404 | static_cast<int>(f->code()->stack_check_table_offset())); |
| 405 | Address end = pc + decode_size; |
| 406 | |
| 407 | v8::internal::EmbeddedVector<char, 128> decode_buffer; |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 408 | v8::internal::EmbeddedVector<char, 128> smi_hex_buffer; |
| 409 | Smi* smi = Smi::FromInt(12345678); |
| 410 | 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] | 411 | while (pc < end) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 412 | int num_const = d.ConstantPoolSizeAt(pc); |
| 413 | if (num_const >= 0) { |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 414 | pc += (num_const + 1) * kPointerSize; |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 415 | } else { |
| 416 | pc += d.InstructionDecode(decode_buffer, pc); |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 417 | CHECK(strstr(decode_buffer.start(), smi_hex_buffer.start()) == NULL); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 418 | } |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | |
| 424 | TEST(SplitConstantsInFullCompiler) { |
| 425 | v8::HandleScope scope; |
| 426 | LocalContext env; |
| 427 | |
| 428 | CompileRun("function f() { a = 12345678 }; f();"); |
| 429 | CheckCodeForUnsafeLiteral(GetJSFunction(env->Global(), "f")); |
| 430 | CompileRun("function f(x) { a = 12345678 + x}; f(1);"); |
| 431 | CheckCodeForUnsafeLiteral(GetJSFunction(env->Global(), "f")); |
| 432 | CompileRun("function f(x) { var arguments = 1; x += 12345678}; f(1);"); |
| 433 | CheckCodeForUnsafeLiteral(GetJSFunction(env->Global(), "f")); |
| 434 | CompileRun("function f(x) { var arguments = 1; x = 12345678}; f(1);"); |
| 435 | CheckCodeForUnsafeLiteral(GetJSFunction(env->Global(), "f")); |
| 436 | } |
| 437 | #endif |