Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
| 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> |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 29 | #include <wchar.h> // wint_t |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +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" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 37 | #include "cctest.h" |
| 38 | |
| 39 | using namespace v8::internal; |
| 40 | |
| 41 | static v8::Persistent<v8::Context> env; |
| 42 | |
| 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(" "); |
| 68 | v8::HandleScope scope; |
| 69 | v8::Handle<v8::Value> arg = args[i]; |
| 70 | v8::Handle<v8::String> string_obj = arg->ToString(); |
| 71 | if (string_obj.IsEmpty()) return string_obj; |
| 72 | int length = string_obj->Length(); |
| 73 | uint16_t* string = NewArray<uint16_t>(length + 1); |
| 74 | string_obj->Write(string); |
| 75 | for (int j = 0; j < length; j++) |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 76 | printf("%lc", static_cast<wint_t>(string[j])); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 77 | DeleteArray(string); |
| 78 | } |
| 79 | printf("\n"); |
| 80 | return v8::Undefined(); |
| 81 | } |
| 82 | |
| 83 | |
| 84 | static PrintExtension kPrintExtension; |
| 85 | v8::DeclareExtension kPrintExtensionDeclaration(&kPrintExtension); |
| 86 | |
| 87 | |
| 88 | static void InitializeVM() { |
| 89 | if (env.IsEmpty()) { |
| 90 | v8::HandleScope scope; |
| 91 | const char* extensions[] = { "v8/print", "v8/gc" }; |
| 92 | v8::ExtensionConfiguration config(2, extensions); |
| 93 | env = v8::Context::New(&config); |
| 94 | } |
| 95 | v8::HandleScope scope; |
| 96 | env->Enter(); |
| 97 | } |
| 98 | |
| 99 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 100 | static MaybeObject* GetGlobalProperty(const char* name) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 101 | Handle<String> symbol = FACTORY->LookupAsciiSymbol(name); |
| 102 | return Isolate::Current()->context()->global()->GetProperty(*symbol); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | |
| 106 | static void SetGlobalProperty(const char* name, Object* value) { |
| 107 | Handle<Object> object(value); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 108 | Handle<String> symbol = FACTORY->LookupAsciiSymbol(name); |
| 109 | Handle<JSObject> global(Isolate::Current()->context()->global()); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 110 | SetProperty(global, symbol, object, NONE, kNonStrictMode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | |
| 114 | static Handle<JSFunction> Compile(const char* source) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 115 | Handle<String> source_code(FACTORY->NewStringFromUtf8(CStrVector(source))); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 116 | Handle<SharedFunctionInfo> shared_function = |
| 117 | Compiler::Compile(source_code, |
| 118 | Handle<String>(), |
| 119 | 0, |
| 120 | 0, |
| 121 | NULL, |
| 122 | NULL, |
| 123 | Handle<String>::null(), |
| 124 | NOT_NATIVES_CODE); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 125 | return FACTORY->NewFunctionFromSharedFunctionInfo(shared_function, |
| 126 | Isolate::Current()->global_context()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | |
| 130 | static double Inc(int x) { |
| 131 | const char* source = "result = %d + 1;"; |
| 132 | EmbeddedVector<char, 512> buffer; |
| 133 | OS::SNPrintF(buffer, source, x); |
| 134 | |
| 135 | Handle<JSFunction> fun = Compile(buffer.start()); |
| 136 | if (fun.is_null()) return -1; |
| 137 | |
| 138 | bool has_pending_exception; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 139 | Handle<JSObject> global(Isolate::Current()->context()->global()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 140 | Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 141 | CHECK(!has_pending_exception); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 142 | return GetGlobalProperty("result")->ToObjectChecked()->Number(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | |
| 146 | TEST(Inc) { |
| 147 | InitializeVM(); |
| 148 | v8::HandleScope scope; |
| 149 | CHECK_EQ(4.0, Inc(3)); |
| 150 | } |
| 151 | |
| 152 | |
| 153 | static double Add(int x, int y) { |
| 154 | Handle<JSFunction> fun = Compile("result = x + y;"); |
| 155 | if (fun.is_null()) return -1; |
| 156 | |
| 157 | SetGlobalProperty("x", Smi::FromInt(x)); |
| 158 | SetGlobalProperty("y", Smi::FromInt(y)); |
| 159 | bool has_pending_exception; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 160 | Handle<JSObject> global(Isolate::Current()->context()->global()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 161 | Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 162 | CHECK(!has_pending_exception); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 163 | return GetGlobalProperty("result")->ToObjectChecked()->Number(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | |
| 167 | TEST(Add) { |
| 168 | InitializeVM(); |
| 169 | v8::HandleScope scope; |
| 170 | CHECK_EQ(5.0, Add(2, 3)); |
| 171 | } |
| 172 | |
| 173 | |
| 174 | static double Abs(int x) { |
| 175 | Handle<JSFunction> fun = Compile("if (x < 0) result = -x; else result = x;"); |
| 176 | if (fun.is_null()) return -1; |
| 177 | |
| 178 | SetGlobalProperty("x", Smi::FromInt(x)); |
| 179 | bool has_pending_exception; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 180 | Handle<JSObject> global(Isolate::Current()->context()->global()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 181 | Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 182 | CHECK(!has_pending_exception); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 183 | return GetGlobalProperty("result")->ToObjectChecked()->Number(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | |
| 187 | TEST(Abs) { |
| 188 | InitializeVM(); |
| 189 | v8::HandleScope scope; |
| 190 | CHECK_EQ(3.0, Abs(-3)); |
| 191 | } |
| 192 | |
| 193 | |
| 194 | static double Sum(int n) { |
| 195 | Handle<JSFunction> fun = |
| 196 | Compile("s = 0; while (n > 0) { s += n; n -= 1; }; result = s;"); |
| 197 | if (fun.is_null()) return -1; |
| 198 | |
| 199 | SetGlobalProperty("n", Smi::FromInt(n)); |
| 200 | bool has_pending_exception; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 201 | Handle<JSObject> global(Isolate::Current()->context()->global()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 202 | Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 203 | CHECK(!has_pending_exception); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 204 | return GetGlobalProperty("result")->ToObjectChecked()->Number(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | |
| 208 | TEST(Sum) { |
| 209 | InitializeVM(); |
| 210 | v8::HandleScope scope; |
| 211 | CHECK_EQ(5050.0, Sum(100)); |
| 212 | } |
| 213 | |
| 214 | |
| 215 | TEST(Print) { |
| 216 | InitializeVM(); |
| 217 | v8::HandleScope scope; |
| 218 | const char* source = "for (n = 0; n < 100; ++n) print(n, 1, 2);"; |
| 219 | Handle<JSFunction> fun = Compile(source); |
| 220 | if (fun.is_null()) return; |
| 221 | bool has_pending_exception; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 222 | Handle<JSObject> global(Isolate::Current()->context()->global()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 223 | Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 224 | CHECK(!has_pending_exception); |
| 225 | } |
| 226 | |
| 227 | |
| 228 | // The following test method stems from my coding efforts today. It |
| 229 | // tests all the functionality I have added to the compiler today |
| 230 | TEST(Stuff) { |
| 231 | InitializeVM(); |
| 232 | v8::HandleScope scope; |
| 233 | const char* source = |
| 234 | "r = 0;\n" |
| 235 | "a = new Object;\n" |
| 236 | "if (a == a) r+=1;\n" // 1 |
| 237 | "if (a != new Object()) r+=2;\n" // 2 |
| 238 | "a.x = 42;\n" |
| 239 | "if (a.x == 42) r+=4;\n" // 4 |
| 240 | "function foo() { var x = 87; return x; }\n" |
| 241 | "if (foo() == 87) r+=8;\n" // 8 |
| 242 | "function bar() { var x; x = 99; return x; }\n" |
| 243 | "if (bar() == 99) r+=16;\n" // 16 |
| 244 | "function baz() { var x = 1, y, z = 2; y = 3; return x + y + z; }\n" |
| 245 | "if (baz() == 6) r+=32;\n" // 32 |
| 246 | "function Cons0() { this.x = 42; this.y = 87; }\n" |
| 247 | "if (new Cons0().x == 42) r+=64;\n" // 64 |
| 248 | "if (new Cons0().y == 87) r+=128;\n" // 128 |
| 249 | "function Cons2(x, y) { this.sum = x + y; }\n" |
| 250 | "if (new Cons2(3,4).sum == 7) r+=256;"; // 256 |
| 251 | |
| 252 | Handle<JSFunction> fun = Compile(source); |
| 253 | CHECK(!fun.is_null()); |
| 254 | bool has_pending_exception; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 255 | Handle<JSObject> global(Isolate::Current()->context()->global()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 256 | Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 257 | CHECK(!has_pending_exception); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 258 | CHECK_EQ(511.0, GetGlobalProperty("r")->ToObjectChecked()->Number()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | |
| 262 | TEST(UncaughtThrow) { |
| 263 | InitializeVM(); |
| 264 | v8::HandleScope scope; |
| 265 | |
| 266 | const char* source = "throw 42;"; |
| 267 | Handle<JSFunction> fun = Compile(source); |
| 268 | CHECK(!fun.is_null()); |
| 269 | bool has_pending_exception; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 270 | Handle<JSObject> global(Isolate::Current()->context()->global()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 271 | Handle<Object> result = |
| 272 | Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 273 | CHECK(has_pending_exception); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 274 | CHECK_EQ(42.0, Isolate::Current()->pending_exception()-> |
| 275 | ToObjectChecked()->Number()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +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; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 296 | Handle<JSObject> global(Isolate::Current()->context()->global()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 297 | Execution::Call(fun0, global, 0, NULL, &has_pending_exception); |
| 298 | CHECK(!has_pending_exception); |
| 299 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 300 | Object* foo_symbol = FACTORY->LookupAsciiSymbol("foo")->ToObjectChecked(); |
| 301 | MaybeObject* fun1_object = Isolate::Current()->context()->global()-> |
| 302 | GetProperty(String::cast(foo_symbol)); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 303 | Handle<Object> fun1(fun1_object->ToObjectChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 304 | CHECK(fun1->IsJSFunction()); |
| 305 | |
| 306 | Object** argv[1] = { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 307 | Handle<Object>::cast(FACTORY->LookupAsciiSymbol("hello")).location() |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 308 | }; |
| 309 | Execution::Call(Handle<JSFunction>::cast(fun1), global, 1, argv, |
| 310 | &has_pending_exception); |
| 311 | CHECK(!has_pending_exception); |
| 312 | } |
| 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 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 321 | Handle<Script> script = FACTORY->NewScript(FACTORY->empty_string()); |
| 322 | script->set_source(HEAP->undefined_value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 323 | CHECK_EQ(-1, GetScriptLineNumber(script, 0)); |
| 324 | CHECK_EQ(-1, GetScriptLineNumber(script, 100)); |
| 325 | CHECK_EQ(-1, GetScriptLineNumber(script, -1)); |
| 326 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +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 | } |