| OLD | NEW |
| 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 | 27 |
| 28 #include <stdlib.h> | 28 #include <stdlib.h> |
| 29 #include <wchar.h> // wint_t | 29 #include <wchar.h> // wint_t |
| 30 | 30 |
| 31 #include "v8.h" | 31 #include "v8.h" |
| 32 | 32 |
| 33 #include "compiler.h" | 33 #include "compiler.h" |
| 34 #include "execution.h" | 34 #include "execution.h" |
| 35 #include "factory.h" | 35 #include "factory.h" |
| 36 #include "platform.h" | 36 #include "platform.h" |
| 37 #include "top.h" | |
| 38 #include "cctest.h" | 37 #include "cctest.h" |
| 39 | 38 |
| 40 using namespace v8::internal; | 39 using namespace v8::internal; |
| 41 | 40 |
| 42 static v8::Persistent<v8::Context> env; | 41 static v8::Persistent<v8::Context> env; |
| 43 | 42 |
| 44 // --- P r i n t E x t e n s i o n --- | 43 // --- P r i n t E x t e n s i o n --- |
| 45 | 44 |
| 46 class PrintExtension : public v8::Extension { | 45 class PrintExtension : public v8::Extension { |
| 47 public: | 46 public: |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 const char* extensions[] = { "v8/print", "v8/gc" }; | 91 const char* extensions[] = { "v8/print", "v8/gc" }; |
| 93 v8::ExtensionConfiguration config(2, extensions); | 92 v8::ExtensionConfiguration config(2, extensions); |
| 94 env = v8::Context::New(&config); | 93 env = v8::Context::New(&config); |
| 95 } | 94 } |
| 96 v8::HandleScope scope; | 95 v8::HandleScope scope; |
| 97 env->Enter(); | 96 env->Enter(); |
| 98 } | 97 } |
| 99 | 98 |
| 100 | 99 |
| 101 static MaybeObject* GetGlobalProperty(const char* name) { | 100 static MaybeObject* GetGlobalProperty(const char* name) { |
| 102 Handle<String> symbol = Factory::LookupAsciiSymbol(name); | 101 Handle<String> symbol = FACTORY->LookupAsciiSymbol(name); |
| 103 return Top::context()->global()->GetProperty(*symbol); | 102 return Isolate::Current()->context()->global()->GetProperty(*symbol); |
| 104 } | 103 } |
| 105 | 104 |
| 106 | 105 |
| 107 static void SetGlobalProperty(const char* name, Object* value) { | 106 static void SetGlobalProperty(const char* name, Object* value) { |
| 108 Handle<Object> object(value); | 107 Handle<Object> object(value); |
| 109 Handle<String> symbol = Factory::LookupAsciiSymbol(name); | 108 Handle<String> symbol = FACTORY->LookupAsciiSymbol(name); |
| 110 Handle<JSObject> global(Top::context()->global()); | 109 Handle<JSObject> global(Isolate::Current()->context()->global()); |
| 111 SetProperty(global, symbol, object, NONE, kNonStrictMode); | 110 SetProperty(global, symbol, object, NONE, kNonStrictMode); |
| 112 } | 111 } |
| 113 | 112 |
| 114 | 113 |
| 115 static Handle<JSFunction> Compile(const char* source) { | 114 static Handle<JSFunction> Compile(const char* source) { |
| 116 Handle<String> source_code(Factory::NewStringFromUtf8(CStrVector(source))); | 115 Handle<String> source_code(FACTORY->NewStringFromUtf8(CStrVector(source))); |
| 117 Handle<SharedFunctionInfo> shared_function = | 116 Handle<SharedFunctionInfo> shared_function = |
| 118 Compiler::Compile(source_code, | 117 Compiler::Compile(source_code, |
| 119 Handle<String>(), | 118 Handle<String>(), |
| 120 0, | 119 0, |
| 121 0, | 120 0, |
| 122 NULL, | 121 NULL, |
| 123 NULL, | 122 NULL, |
| 124 Handle<String>::null(), | 123 Handle<String>::null(), |
| 125 NOT_NATIVES_CODE); | 124 NOT_NATIVES_CODE); |
| 126 return Factory::NewFunctionFromSharedFunctionInfo(shared_function, | 125 return FACTORY->NewFunctionFromSharedFunctionInfo(shared_function, |
| 127 Top::global_context()); | 126 Isolate::Current()->global_context()); |
| 128 } | 127 } |
| 129 | 128 |
| 130 | 129 |
| 131 static double Inc(int x) { | 130 static double Inc(int x) { |
| 132 const char* source = "result = %d + 1;"; | 131 const char* source = "result = %d + 1;"; |
| 133 EmbeddedVector<char, 512> buffer; | 132 EmbeddedVector<char, 512> buffer; |
| 134 OS::SNPrintF(buffer, source, x); | 133 OS::SNPrintF(buffer, source, x); |
| 135 | 134 |
| 136 Handle<JSFunction> fun = Compile(buffer.start()); | 135 Handle<JSFunction> fun = Compile(buffer.start()); |
| 137 if (fun.is_null()) return -1; | 136 if (fun.is_null()) return -1; |
| 138 | 137 |
| 139 bool has_pending_exception; | 138 bool has_pending_exception; |
| 140 Handle<JSObject> global(Top::context()->global()); | 139 Handle<JSObject> global(Isolate::Current()->context()->global()); |
| 141 Execution::Call(fun, global, 0, NULL, &has_pending_exception); | 140 Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 142 CHECK(!has_pending_exception); | 141 CHECK(!has_pending_exception); |
| 143 return GetGlobalProperty("result")->ToObjectChecked()->Number(); | 142 return GetGlobalProperty("result")->ToObjectChecked()->Number(); |
| 144 } | 143 } |
| 145 | 144 |
| 146 | 145 |
| 147 TEST(Inc) { | 146 TEST(Inc) { |
| 148 InitializeVM(); | 147 InitializeVM(); |
| 149 v8::HandleScope scope; | 148 v8::HandleScope scope; |
| 150 CHECK_EQ(4.0, Inc(3)); | 149 CHECK_EQ(4.0, Inc(3)); |
| 151 } | 150 } |
| 152 | 151 |
| 153 | 152 |
| 154 static double Add(int x, int y) { | 153 static double Add(int x, int y) { |
| 155 Handle<JSFunction> fun = Compile("result = x + y;"); | 154 Handle<JSFunction> fun = Compile("result = x + y;"); |
| 156 if (fun.is_null()) return -1; | 155 if (fun.is_null()) return -1; |
| 157 | 156 |
| 158 SetGlobalProperty("x", Smi::FromInt(x)); | 157 SetGlobalProperty("x", Smi::FromInt(x)); |
| 159 SetGlobalProperty("y", Smi::FromInt(y)); | 158 SetGlobalProperty("y", Smi::FromInt(y)); |
| 160 bool has_pending_exception; | 159 bool has_pending_exception; |
| 161 Handle<JSObject> global(Top::context()->global()); | 160 Handle<JSObject> global(Isolate::Current()->context()->global()); |
| 162 Execution::Call(fun, global, 0, NULL, &has_pending_exception); | 161 Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 163 CHECK(!has_pending_exception); | 162 CHECK(!has_pending_exception); |
| 164 return GetGlobalProperty("result")->ToObjectChecked()->Number(); | 163 return GetGlobalProperty("result")->ToObjectChecked()->Number(); |
| 165 } | 164 } |
| 166 | 165 |
| 167 | 166 |
| 168 TEST(Add) { | 167 TEST(Add) { |
| 169 InitializeVM(); | 168 InitializeVM(); |
| 170 v8::HandleScope scope; | 169 v8::HandleScope scope; |
| 171 CHECK_EQ(5.0, Add(2, 3)); | 170 CHECK_EQ(5.0, Add(2, 3)); |
| 172 } | 171 } |
| 173 | 172 |
| 174 | 173 |
| 175 static double Abs(int x) { | 174 static double Abs(int x) { |
| 176 Handle<JSFunction> fun = Compile("if (x < 0) result = -x; else result = x;"); | 175 Handle<JSFunction> fun = Compile("if (x < 0) result = -x; else result = x;"); |
| 177 if (fun.is_null()) return -1; | 176 if (fun.is_null()) return -1; |
| 178 | 177 |
| 179 SetGlobalProperty("x", Smi::FromInt(x)); | 178 SetGlobalProperty("x", Smi::FromInt(x)); |
| 180 bool has_pending_exception; | 179 bool has_pending_exception; |
| 181 Handle<JSObject> global(Top::context()->global()); | 180 Handle<JSObject> global(Isolate::Current()->context()->global()); |
| 182 Execution::Call(fun, global, 0, NULL, &has_pending_exception); | 181 Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 183 CHECK(!has_pending_exception); | 182 CHECK(!has_pending_exception); |
| 184 return GetGlobalProperty("result")->ToObjectChecked()->Number(); | 183 return GetGlobalProperty("result")->ToObjectChecked()->Number(); |
| 185 } | 184 } |
| 186 | 185 |
| 187 | 186 |
| 188 TEST(Abs) { | 187 TEST(Abs) { |
| 189 InitializeVM(); | 188 InitializeVM(); |
| 190 v8::HandleScope scope; | 189 v8::HandleScope scope; |
| 191 CHECK_EQ(3.0, Abs(-3)); | 190 CHECK_EQ(3.0, Abs(-3)); |
| 192 } | 191 } |
| 193 | 192 |
| 194 | 193 |
| 195 static double Sum(int n) { | 194 static double Sum(int n) { |
| 196 Handle<JSFunction> fun = | 195 Handle<JSFunction> fun = |
| 197 Compile("s = 0; while (n > 0) { s += n; n -= 1; }; result = s;"); | 196 Compile("s = 0; while (n > 0) { s += n; n -= 1; }; result = s;"); |
| 198 if (fun.is_null()) return -1; | 197 if (fun.is_null()) return -1; |
| 199 | 198 |
| 200 SetGlobalProperty("n", Smi::FromInt(n)); | 199 SetGlobalProperty("n", Smi::FromInt(n)); |
| 201 bool has_pending_exception; | 200 bool has_pending_exception; |
| 202 Handle<JSObject> global(Top::context()->global()); | 201 Handle<JSObject> global(Isolate::Current()->context()->global()); |
| 203 Execution::Call(fun, global, 0, NULL, &has_pending_exception); | 202 Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 204 CHECK(!has_pending_exception); | 203 CHECK(!has_pending_exception); |
| 205 return GetGlobalProperty("result")->ToObjectChecked()->Number(); | 204 return GetGlobalProperty("result")->ToObjectChecked()->Number(); |
| 206 } | 205 } |
| 207 | 206 |
| 208 | 207 |
| 209 TEST(Sum) { | 208 TEST(Sum) { |
| 210 InitializeVM(); | 209 InitializeVM(); |
| 211 v8::HandleScope scope; | 210 v8::HandleScope scope; |
| 212 CHECK_EQ(5050.0, Sum(100)); | 211 CHECK_EQ(5050.0, Sum(100)); |
| 213 } | 212 } |
| 214 | 213 |
| 215 | 214 |
| 216 TEST(Print) { | 215 TEST(Print) { |
| 217 InitializeVM(); | 216 InitializeVM(); |
| 218 v8::HandleScope scope; | 217 v8::HandleScope scope; |
| 219 const char* source = "for (n = 0; n < 100; ++n) print(n, 1, 2);"; | 218 const char* source = "for (n = 0; n < 100; ++n) print(n, 1, 2);"; |
| 220 Handle<JSFunction> fun = Compile(source); | 219 Handle<JSFunction> fun = Compile(source); |
| 221 if (fun.is_null()) return; | 220 if (fun.is_null()) return; |
| 222 bool has_pending_exception; | 221 bool has_pending_exception; |
| 223 Handle<JSObject> global(Top::context()->global()); | 222 Handle<JSObject> global(Isolate::Current()->context()->global()); |
| 224 Execution::Call(fun, global, 0, NULL, &has_pending_exception); | 223 Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 225 CHECK(!has_pending_exception); | 224 CHECK(!has_pending_exception); |
| 226 } | 225 } |
| 227 | 226 |
| 228 | 227 |
| 229 // The following test method stems from my coding efforts today. It | 228 // The following test method stems from my coding efforts today. It |
| 230 // tests all the functionality I have added to the compiler today | 229 // tests all the functionality I have added to the compiler today |
| 231 TEST(Stuff) { | 230 TEST(Stuff) { |
| 232 InitializeVM(); | 231 InitializeVM(); |
| 233 v8::HandleScope scope; | 232 v8::HandleScope scope; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 246 "if (baz() == 6) r+=32;\n" // 32 | 245 "if (baz() == 6) r+=32;\n" // 32 |
| 247 "function Cons0() { this.x = 42; this.y = 87; }\n" | 246 "function Cons0() { this.x = 42; this.y = 87; }\n" |
| 248 "if (new Cons0().x == 42) r+=64;\n" // 64 | 247 "if (new Cons0().x == 42) r+=64;\n" // 64 |
| 249 "if (new Cons0().y == 87) r+=128;\n" // 128 | 248 "if (new Cons0().y == 87) r+=128;\n" // 128 |
| 250 "function Cons2(x, y) { this.sum = x + y; }\n" | 249 "function Cons2(x, y) { this.sum = x + y; }\n" |
| 251 "if (new Cons2(3,4).sum == 7) r+=256;"; // 256 | 250 "if (new Cons2(3,4).sum == 7) r+=256;"; // 256 |
| 252 | 251 |
| 253 Handle<JSFunction> fun = Compile(source); | 252 Handle<JSFunction> fun = Compile(source); |
| 254 CHECK(!fun.is_null()); | 253 CHECK(!fun.is_null()); |
| 255 bool has_pending_exception; | 254 bool has_pending_exception; |
| 256 Handle<JSObject> global(Top::context()->global()); | 255 Handle<JSObject> global(Isolate::Current()->context()->global()); |
| 257 Execution::Call(fun, global, 0, NULL, &has_pending_exception); | 256 Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 258 CHECK(!has_pending_exception); | 257 CHECK(!has_pending_exception); |
| 259 CHECK_EQ(511.0, GetGlobalProperty("r")->ToObjectChecked()->Number()); | 258 CHECK_EQ(511.0, GetGlobalProperty("r")->ToObjectChecked()->Number()); |
| 260 } | 259 } |
| 261 | 260 |
| 262 | 261 |
| 263 TEST(UncaughtThrow) { | 262 TEST(UncaughtThrow) { |
| 264 InitializeVM(); | 263 InitializeVM(); |
| 265 v8::HandleScope scope; | 264 v8::HandleScope scope; |
| 266 | 265 |
| 267 const char* source = "throw 42;"; | 266 const char* source = "throw 42;"; |
| 268 Handle<JSFunction> fun = Compile(source); | 267 Handle<JSFunction> fun = Compile(source); |
| 269 CHECK(!fun.is_null()); | 268 CHECK(!fun.is_null()); |
| 270 bool has_pending_exception; | 269 bool has_pending_exception; |
| 271 Handle<JSObject> global(Top::context()->global()); | 270 Handle<JSObject> global(Isolate::Current()->context()->global()); |
| 272 Handle<Object> result = | 271 Handle<Object> result = |
| 273 Execution::Call(fun, global, 0, NULL, &has_pending_exception); | 272 Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 274 CHECK(has_pending_exception); | 273 CHECK(has_pending_exception); |
| 275 CHECK_EQ(42.0, Top::pending_exception()->ToObjectChecked()->Number()); | 274 CHECK_EQ(42.0, Isolate::Current()->pending_exception()-> |
| 275 ToObjectChecked()->Number()); |
| 276 } | 276 } |
| 277 | 277 |
| 278 | 278 |
| 279 // Tests calling a builtin function from C/C++ code, and the builtin function | 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: | 280 // performs GC. It creates a stack frame looks like following: |
| 281 // | C (PerformGC) | | 281 // | C (PerformGC) | |
| 282 // | JS-to-C | | 282 // | JS-to-C | |
| 283 // | JS | | 283 // | JS | |
| 284 // | C-to-JS | | 284 // | C-to-JS | |
| 285 TEST(C2JSFrames) { | 285 TEST(C2JSFrames) { |
| 286 InitializeVM(); | 286 InitializeVM(); |
| 287 v8::HandleScope scope; | 287 v8::HandleScope scope; |
| 288 | 288 |
| 289 const char* source = "function foo(a) { gc(), print(a); }"; | 289 const char* source = "function foo(a) { gc(), print(a); }"; |
| 290 | 290 |
| 291 Handle<JSFunction> fun0 = Compile(source); | 291 Handle<JSFunction> fun0 = Compile(source); |
| 292 CHECK(!fun0.is_null()); | 292 CHECK(!fun0.is_null()); |
| 293 | 293 |
| 294 // Run the generated code to populate the global object with 'foo'. | 294 // Run the generated code to populate the global object with 'foo'. |
| 295 bool has_pending_exception; | 295 bool has_pending_exception; |
| 296 Handle<JSObject> global(Top::context()->global()); | 296 Handle<JSObject> global(Isolate::Current()->context()->global()); |
| 297 Execution::Call(fun0, global, 0, NULL, &has_pending_exception); | 297 Execution::Call(fun0, global, 0, NULL, &has_pending_exception); |
| 298 CHECK(!has_pending_exception); | 298 CHECK(!has_pending_exception); |
| 299 | 299 |
| 300 Object* foo_symbol = Factory::LookupAsciiSymbol("foo")->ToObjectChecked(); | 300 Object* foo_symbol = FACTORY->LookupAsciiSymbol("foo")->ToObjectChecked(); |
| 301 MaybeObject* fun1_object = | 301 MaybeObject* fun1_object = Isolate::Current()->context()->global()-> |
| 302 Top::context()->global()->GetProperty(String::cast(foo_symbol)); | 302 GetProperty(String::cast(foo_symbol)); |
| 303 Handle<Object> fun1(fun1_object->ToObjectChecked()); | 303 Handle<Object> fun1(fun1_object->ToObjectChecked()); |
| 304 CHECK(fun1->IsJSFunction()); | 304 CHECK(fun1->IsJSFunction()); |
| 305 | 305 |
| 306 Object** argv[1] = { | 306 Object** argv[1] = { |
| 307 Handle<Object>::cast(Factory::LookupAsciiSymbol("hello")).location() | 307 Handle<Object>::cast(FACTORY->LookupAsciiSymbol("hello")).location() |
| 308 }; | 308 }; |
| 309 Execution::Call(Handle<JSFunction>::cast(fun1), global, 1, argv, | 309 Execution::Call(Handle<JSFunction>::cast(fun1), global, 1, argv, |
| 310 &has_pending_exception); | 310 &has_pending_exception); |
| 311 CHECK(!has_pending_exception); | 311 CHECK(!has_pending_exception); |
| 312 } | 312 } |
| 313 | 313 |
| 314 | 314 |
| 315 // Regression 236. Calling InitLineEnds on a Script with undefined | 315 // Regression 236. Calling InitLineEnds on a Script with undefined |
| 316 // source resulted in crash. | 316 // source resulted in crash. |
| 317 TEST(Regression236) { | 317 TEST(Regression236) { |
| 318 InitializeVM(); | 318 InitializeVM(); |
| 319 v8::HandleScope scope; | 319 v8::HandleScope scope; |
| 320 | 320 |
| 321 Handle<Script> script = Factory::NewScript(Factory::empty_string()); | 321 Handle<Script> script = FACTORY->NewScript(FACTORY->empty_string()); |
| 322 script->set_source(Heap::undefined_value()); | 322 script->set_source(HEAP->undefined_value()); |
| 323 CHECK_EQ(-1, GetScriptLineNumber(script, 0)); | 323 CHECK_EQ(-1, GetScriptLineNumber(script, 0)); |
| 324 CHECK_EQ(-1, GetScriptLineNumber(script, 100)); | 324 CHECK_EQ(-1, GetScriptLineNumber(script, 100)); |
| 325 CHECK_EQ(-1, GetScriptLineNumber(script, -1)); | 325 CHECK_EQ(-1, GetScriptLineNumber(script, -1)); |
| 326 } | 326 } |
| 327 | 327 |
| 328 | 328 |
| 329 TEST(GetScriptLineNumber) { | 329 TEST(GetScriptLineNumber) { |
| 330 LocalContext env; | 330 LocalContext env; |
| 331 v8::HandleScope scope; | 331 v8::HandleScope scope; |
| 332 v8::ScriptOrigin origin = v8::ScriptOrigin(v8::String::New("test")); | 332 v8::ScriptOrigin origin = v8::ScriptOrigin(v8::String::New("test")); |
| 333 const char function_f[] = "function f() {}"; | 333 const char function_f[] = "function f() {}"; |
| 334 const int max_rows = 1000; | 334 const int max_rows = 1000; |
| 335 const int buffer_size = max_rows + sizeof(function_f); | 335 const int buffer_size = max_rows + sizeof(function_f); |
| 336 ScopedVector<char> buffer(buffer_size); | 336 ScopedVector<char> buffer(buffer_size); |
| 337 memset(buffer.start(), '\n', buffer_size - 1); | 337 memset(buffer.start(), '\n', buffer_size - 1); |
| 338 buffer[buffer_size - 1] = '\0'; | 338 buffer[buffer_size - 1] = '\0'; |
| 339 | 339 |
| 340 for (int i = 0; i < max_rows; ++i) { | 340 for (int i = 0; i < max_rows; ++i) { |
| 341 if (i > 0) | 341 if (i > 0) |
| 342 buffer[i - 1] = '\n'; | 342 buffer[i - 1] = '\n'; |
| 343 memcpy(&buffer[i], function_f, sizeof(function_f) - 1); | 343 memcpy(&buffer[i], function_f, sizeof(function_f) - 1); |
| 344 v8::Handle<v8::String> script_body = v8::String::New(buffer.start()); | 344 v8::Handle<v8::String> script_body = v8::String::New(buffer.start()); |
| 345 v8::Script::Compile(script_body, &origin)->Run(); | 345 v8::Script::Compile(script_body, &origin)->Run(); |
| 346 v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast( | 346 v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast( |
| 347 env->Global()->Get(v8::String::New("f"))); | 347 env->Global()->Get(v8::String::New("f"))); |
| 348 CHECK_EQ(i, f->GetScriptLineNumber()); | 348 CHECK_EQ(i, f->GetScriptLineNumber()); |
| 349 } | 349 } |
| 350 } | 350 } |
| OLD | NEW |