ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 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> |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 29 | #include <wchar.h> // wint_t |
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" |
| 34 | #include "execution.h" |
| 35 | #include "factory.h" |
| 36 | #include "platform.h" |
| 37 | #include "top.h" |
| 38 | #include "cctest.h" |
| 39 | |
| 40 | using namespace v8::internal; |
| 41 | |
| 42 | static v8::Persistent<v8::Context> env; |
| 43 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 44 | // --- P r i n t E x t e n s i o n --- |
| 45 | |
| 46 | class PrintExtension : public v8::Extension { |
| 47 | public: |
| 48 | PrintExtension() : v8::Extension("v8/print", kSource) { } |
| 49 | virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( |
| 50 | v8::Handle<v8::String> name); |
| 51 | static v8::Handle<v8::Value> Print(const v8::Arguments& args); |
| 52 | private: |
| 53 | static const char* kSource; |
| 54 | }; |
| 55 | |
| 56 | |
| 57 | const char* PrintExtension::kSource = "native function print();"; |
| 58 | |
| 59 | |
| 60 | v8::Handle<v8::FunctionTemplate> PrintExtension::GetNativeFunction( |
| 61 | v8::Handle<v8::String> str) { |
| 62 | return v8::FunctionTemplate::New(PrintExtension::Print); |
| 63 | } |
| 64 | |
| 65 | |
| 66 | v8::Handle<v8::Value> PrintExtension::Print(const v8::Arguments& args) { |
| 67 | for (int i = 0; i < args.Length(); i++) { |
| 68 | if (i != 0) printf(" "); |
| 69 | v8::HandleScope scope; |
| 70 | v8::Handle<v8::Value> arg = args[i]; |
| 71 | v8::Handle<v8::String> string_obj = arg->ToString(); |
| 72 | if (string_obj.IsEmpty()) return string_obj; |
| 73 | int length = string_obj->Length(); |
| 74 | uint16_t* string = NewArray<uint16_t>(length + 1); |
| 75 | string_obj->Write(string); |
| 76 | for (int j = 0; j < length; j++) |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 77 | printf("%lc", static_cast<wint_t>(string[j])); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 78 | DeleteArray(string); |
| 79 | } |
| 80 | printf("\n"); |
| 81 | return v8::Undefined(); |
| 82 | } |
| 83 | |
| 84 | |
| 85 | static PrintExtension kPrintExtension; |
| 86 | v8::DeclareExtension kPrintExtensionDeclaration(&kPrintExtension); |
| 87 | |
| 88 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 89 | static void InitializeVM() { |
| 90 | if (env.IsEmpty()) { |
| 91 | v8::HandleScope scope; |
| 92 | const char* extensions[] = { "v8/print", "v8/gc" }; |
| 93 | v8::ExtensionConfiguration config(2, extensions); |
| 94 | env = v8::Context::New(&config); |
| 95 | } |
| 96 | v8::HandleScope scope; |
| 97 | env->Enter(); |
| 98 | } |
| 99 | |
| 100 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 101 | static MaybeObject* GetGlobalProperty(const char* name) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 102 | Handle<String> symbol = Factory::LookupAsciiSymbol(name); |
| 103 | return Top::context()->global()->GetProperty(*symbol); |
| 104 | } |
| 105 | |
| 106 | |
| 107 | static void SetGlobalProperty(const char* name, Object* value) { |
| 108 | Handle<Object> object(value); |
| 109 | Handle<String> symbol = Factory::LookupAsciiSymbol(name); |
| 110 | Handle<JSObject> global(Top::context()->global()); |
| 111 | SetProperty(global, symbol, object, NONE); |
| 112 | } |
| 113 | |
| 114 | |
| 115 | static Handle<JSFunction> Compile(const char* source) { |
| 116 | Handle<String> source_code(Factory::NewStringFromUtf8(CStrVector(source))); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 117 | Handle<SharedFunctionInfo> shared_function = |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 118 | Compiler::Compile(source_code, |
| 119 | Handle<String>(), |
| 120 | 0, |
| 121 | 0, |
| 122 | NULL, |
| 123 | NULL, |
| 124 | Handle<String>::null(), |
| 125 | NOT_NATIVES_CODE); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 126 | return Factory::NewFunctionFromSharedFunctionInfo(shared_function, |
| 127 | Top::global_context()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | |
| 131 | static double Inc(int x) { |
| 132 | const char* source = "result = %d + 1;"; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 133 | EmbeddedVector<char, 512> buffer; |
| 134 | OS::SNPrintF(buffer, source, x); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 135 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 136 | Handle<JSFunction> fun = Compile(buffer.start()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 137 | if (fun.is_null()) return -1; |
| 138 | |
| 139 | bool has_pending_exception; |
| 140 | Handle<JSObject> global(Top::context()->global()); |
| 141 | Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 142 | CHECK(!has_pending_exception); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 143 | return GetGlobalProperty("result")->ToObjectChecked()->Number(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | |
| 147 | TEST(Inc) { |
| 148 | InitializeVM(); |
| 149 | v8::HandleScope scope; |
| 150 | CHECK_EQ(4.0, Inc(3)); |
| 151 | } |
| 152 | |
| 153 | |
| 154 | static double Add(int x, int y) { |
| 155 | Handle<JSFunction> fun = Compile("result = x + y;"); |
| 156 | if (fun.is_null()) return -1; |
| 157 | |
| 158 | SetGlobalProperty("x", Smi::FromInt(x)); |
| 159 | SetGlobalProperty("y", Smi::FromInt(y)); |
| 160 | bool has_pending_exception; |
| 161 | Handle<JSObject> global(Top::context()->global()); |
| 162 | Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 163 | CHECK(!has_pending_exception); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 164 | return GetGlobalProperty("result")->ToObjectChecked()->Number(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | |
| 168 | TEST(Add) { |
| 169 | InitializeVM(); |
| 170 | v8::HandleScope scope; |
| 171 | CHECK_EQ(5.0, Add(2, 3)); |
| 172 | } |
| 173 | |
| 174 | |
| 175 | static double Abs(int x) { |
| 176 | Handle<JSFunction> fun = Compile("if (x < 0) result = -x; else result = x;"); |
| 177 | if (fun.is_null()) return -1; |
| 178 | |
| 179 | SetGlobalProperty("x", Smi::FromInt(x)); |
| 180 | bool has_pending_exception; |
| 181 | Handle<JSObject> global(Top::context()->global()); |
| 182 | Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 183 | CHECK(!has_pending_exception); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 184 | return GetGlobalProperty("result")->ToObjectChecked()->Number(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | |
| 188 | TEST(Abs) { |
| 189 | InitializeVM(); |
| 190 | v8::HandleScope scope; |
| 191 | CHECK_EQ(3.0, Abs(-3)); |
| 192 | } |
| 193 | |
| 194 | |
| 195 | static double Sum(int n) { |
| 196 | Handle<JSFunction> fun = |
| 197 | Compile("s = 0; while (n > 0) { s += n; n -= 1; }; result = s;"); |
| 198 | if (fun.is_null()) return -1; |
| 199 | |
| 200 | SetGlobalProperty("n", Smi::FromInt(n)); |
| 201 | bool has_pending_exception; |
| 202 | Handle<JSObject> global(Top::context()->global()); |
| 203 | Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 204 | CHECK(!has_pending_exception); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 205 | return GetGlobalProperty("result")->ToObjectChecked()->Number(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | |
| 209 | TEST(Sum) { |
| 210 | InitializeVM(); |
| 211 | v8::HandleScope scope; |
| 212 | CHECK_EQ(5050.0, Sum(100)); |
| 213 | } |
| 214 | |
| 215 | |
| 216 | TEST(Print) { |
| 217 | InitializeVM(); |
| 218 | v8::HandleScope scope; |
| 219 | const char* source = "for (n = 0; n < 100; ++n) print(n, 1, 2);"; |
| 220 | Handle<JSFunction> fun = Compile(source); |
| 221 | if (fun.is_null()) return; |
| 222 | bool has_pending_exception; |
| 223 | Handle<JSObject> global(Top::context()->global()); |
| 224 | Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 225 | CHECK(!has_pending_exception); |
| 226 | } |
| 227 | |
| 228 | |
| 229 | // The following test method stems from my coding efforts today. It |
| 230 | // tests all the functionality I have added to the compiler today |
| 231 | TEST(Stuff) { |
| 232 | InitializeVM(); |
| 233 | v8::HandleScope scope; |
| 234 | const char* source = |
| 235 | "r = 0;\n" |
| 236 | "a = new Object;\n" |
| 237 | "if (a == a) r+=1;\n" // 1 |
| 238 | "if (a != new Object()) r+=2;\n" // 2 |
| 239 | "a.x = 42;\n" |
| 240 | "if (a.x == 42) r+=4;\n" // 4 |
| 241 | "function foo() { var x = 87; return x; }\n" |
| 242 | "if (foo() == 87) r+=8;\n" // 8 |
| 243 | "function bar() { var x; x = 99; return x; }\n" |
| 244 | "if (bar() == 99) r+=16;\n" // 16 |
| 245 | "function baz() { var x = 1, y, z = 2; y = 3; return x + y + z; }\n" |
| 246 | "if (baz() == 6) r+=32;\n" // 32 |
| 247 | "function Cons0() { this.x = 42; this.y = 87; }\n" |
| 248 | "if (new Cons0().x == 42) r+=64;\n" // 64 |
| 249 | "if (new Cons0().y == 87) r+=128;\n" // 128 |
| 250 | "function Cons2(x, y) { this.sum = x + y; }\n" |
| 251 | "if (new Cons2(3,4).sum == 7) r+=256;"; // 256 |
| 252 | |
| 253 | Handle<JSFunction> fun = Compile(source); |
| 254 | CHECK(!fun.is_null()); |
| 255 | bool has_pending_exception; |
| 256 | Handle<JSObject> global(Top::context()->global()); |
| 257 | Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 258 | CHECK(!has_pending_exception); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 259 | CHECK_EQ(511.0, GetGlobalProperty("r")->ToObjectChecked()->Number()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | |
| 263 | TEST(UncaughtThrow) { |
| 264 | InitializeVM(); |
| 265 | v8::HandleScope scope; |
| 266 | |
| 267 | const char* source = "throw 42;"; |
| 268 | Handle<JSFunction> fun = Compile(source); |
| 269 | CHECK(!fun.is_null()); |
| 270 | bool has_pending_exception; |
| 271 | Handle<JSObject> global(Top::context()->global()); |
| 272 | Handle<Object> result = |
| 273 | Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 274 | CHECK(has_pending_exception); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 275 | CHECK_EQ(42.0, Top::pending_exception()->ToObjectChecked()->Number()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | |
| 279 | // Tests calling a builtin function from C/C++ code, and the builtin function |
| 280 | // performs GC. It creates a stack frame looks like following: |
| 281 | // | C (PerformGC) | |
| 282 | // | JS-to-C | |
| 283 | // | JS | |
| 284 | // | C-to-JS | |
| 285 | TEST(C2JSFrames) { |
| 286 | InitializeVM(); |
| 287 | v8::HandleScope scope; |
| 288 | |
| 289 | const char* source = "function foo(a) { gc(), print(a); }"; |
| 290 | |
| 291 | Handle<JSFunction> fun0 = Compile(source); |
| 292 | CHECK(!fun0.is_null()); |
| 293 | |
| 294 | // Run the generated code to populate the global object with 'foo'. |
| 295 | bool has_pending_exception; |
| 296 | Handle<JSObject> global(Top::context()->global()); |
| 297 | Execution::Call(fun0, global, 0, NULL, &has_pending_exception); |
| 298 | CHECK(!has_pending_exception); |
| 299 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 300 | Object* foo_symbol = Factory::LookupAsciiSymbol("foo")->ToObjectChecked(); |
| 301 | MaybeObject* fun1_object = |
| 302 | Top::context()->global()->GetProperty(String::cast(foo_symbol)); |
| 303 | Handle<Object> fun1(fun1_object->ToObjectChecked()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 304 | CHECK(fun1->IsJSFunction()); |
| 305 | |
| 306 | Object** argv[1] = { |
| 307 | Handle<Object>::cast(Factory::LookupAsciiSymbol("hello")).location() |
| 308 | }; |
| 309 | Execution::Call(Handle<JSFunction>::cast(fun1), global, 1, argv, |
| 310 | &has_pending_exception); |
| 311 | CHECK(!has_pending_exception); |
| 312 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 313 | |
| 314 | |
| 315 | // Regression 236. Calling InitLineEnds on a Script with undefined |
| 316 | // source resulted in crash. |
| 317 | TEST(Regression236) { |
| 318 | InitializeVM(); |
| 319 | v8::HandleScope scope; |
| 320 | |
| 321 | Handle<Script> script = Factory::NewScript(Factory::empty_string()); |
| 322 | script->set_source(Heap::undefined_value()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 323 | CHECK_EQ(-1, GetScriptLineNumber(script, 0)); |
| 324 | CHECK_EQ(-1, GetScriptLineNumber(script, 100)); |
| 325 | CHECK_EQ(-1, GetScriptLineNumber(script, -1)); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 326 | } |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 327 | |
| 328 | |
| 329 | TEST(GetScriptLineNumber) { |
| 330 | LocalContext env; |
| 331 | v8::HandleScope scope; |
| 332 | v8::ScriptOrigin origin = v8::ScriptOrigin(v8::String::New("test")); |
| 333 | const char function_f[] = "function f() {}"; |
| 334 | const int max_rows = 1000; |
| 335 | const int buffer_size = max_rows + sizeof(function_f); |
| 336 | ScopedVector<char> buffer(buffer_size); |
| 337 | memset(buffer.start(), '\n', buffer_size - 1); |
| 338 | buffer[buffer_size - 1] = '\0'; |
| 339 | |
| 340 | for (int i = 0; i < max_rows; ++i) { |
| 341 | if (i > 0) |
| 342 | buffer[i - 1] = '\n'; |
| 343 | memcpy(&buffer[i], function_f, sizeof(function_f) - 1); |
| 344 | v8::Handle<v8::String> script_body = v8::String::New(buffer.start()); |
| 345 | v8::Script::Compile(script_body, &origin)->Run(); |
| 346 | v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast( |
| 347 | env->Global()->Get(v8::String::New("f"))); |
| 348 | CHECK_EQ(i, f->GetScriptLineNumber()); |
| 349 | } |
| 350 | } |