| OLD | NEW |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 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 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 Handle<Context>(isolate->native_context()), | 109 Handle<Context>(isolate->native_context()), |
| 110 NULL, | 110 NULL, |
| 111 NULL, | 111 NULL, |
| 112 Handle<String>::null(), | 112 Handle<String>::null(), |
| 113 NOT_NATIVES_CODE); | 113 NOT_NATIVES_CODE); |
| 114 return isolate->factory()->NewFunctionFromSharedFunctionInfo( | 114 return isolate->factory()->NewFunctionFromSharedFunctionInfo( |
| 115 shared_function, isolate->native_context()); | 115 shared_function, isolate->native_context()); |
| 116 } | 116 } |
| 117 | 117 |
| 118 | 118 |
| 119 static double Inc(int x) { | 119 static double Inc(Isolate* isolate, int x) { |
| 120 const char* source = "result = %d + 1;"; | 120 const char* source = "result = %d + 1;"; |
| 121 EmbeddedVector<char, 512> buffer; | 121 EmbeddedVector<char, 512> buffer; |
| 122 OS::SNPrintF(buffer, source, x); | 122 OS::SNPrintF(buffer, source, x); |
| 123 | 123 |
| 124 Handle<JSFunction> fun = Compile(buffer.start()); | 124 Handle<JSFunction> fun = Compile(buffer.start()); |
| 125 if (fun.is_null()) return -1; | 125 if (fun.is_null()) return -1; |
| 126 | 126 |
| 127 bool has_pending_exception; | 127 bool has_pending_exception; |
| 128 Handle<JSObject> global(Isolate::Current()->context()->global_object()); | 128 Handle<JSObject> global(isolate->context()->global_object()); |
| 129 Execution::Call(fun, global, 0, NULL, &has_pending_exception); | 129 Execution::Call(isolate, fun, global, 0, NULL, &has_pending_exception); |
| 130 CHECK(!has_pending_exception); | 130 CHECK(!has_pending_exception); |
| 131 return GetGlobalProperty("result")->ToObjectChecked()->Number(); | 131 return GetGlobalProperty("result")->ToObjectChecked()->Number(); |
| 132 } | 132 } |
| 133 | 133 |
| 134 | 134 |
| 135 TEST(Inc) { | 135 TEST(Inc) { |
| 136 CcTest::InitializeVM(); | 136 CcTest::InitializeVM(); |
| 137 v8::HandleScope scope(CcTest::isolate()); | 137 v8::HandleScope scope(CcTest::isolate()); |
| 138 CHECK_EQ(4.0, Inc(3)); | 138 CHECK_EQ(4.0, Inc(CcTest::i_isolate(), 3)); |
| 139 } | 139 } |
| 140 | 140 |
| 141 | 141 |
| 142 static double Add(int x, int y) { | 142 static double Add(Isolate* isolate, int x, int y) { |
| 143 Handle<JSFunction> fun = Compile("result = x + y;"); | 143 Handle<JSFunction> fun = Compile("result = x + y;"); |
| 144 if (fun.is_null()) return -1; | 144 if (fun.is_null()) return -1; |
| 145 | 145 |
| 146 SetGlobalProperty("x", Smi::FromInt(x)); | 146 SetGlobalProperty("x", Smi::FromInt(x)); |
| 147 SetGlobalProperty("y", Smi::FromInt(y)); | 147 SetGlobalProperty("y", Smi::FromInt(y)); |
| 148 bool has_pending_exception; | 148 bool has_pending_exception; |
| 149 Handle<JSObject> global(Isolate::Current()->context()->global_object()); | 149 Handle<JSObject> global(isolate->context()->global_object()); |
| 150 Execution::Call(fun, global, 0, NULL, &has_pending_exception); | 150 Execution::Call(isolate, fun, global, 0, NULL, &has_pending_exception); |
| 151 CHECK(!has_pending_exception); | 151 CHECK(!has_pending_exception); |
| 152 return GetGlobalProperty("result")->ToObjectChecked()->Number(); | 152 return GetGlobalProperty("result")->ToObjectChecked()->Number(); |
| 153 } | 153 } |
| 154 | 154 |
| 155 | 155 |
| 156 TEST(Add) { | 156 TEST(Add) { |
| 157 CcTest::InitializeVM(); | 157 CcTest::InitializeVM(); |
| 158 v8::HandleScope scope(CcTest::isolate()); | 158 v8::HandleScope scope(CcTest::isolate()); |
| 159 CHECK_EQ(5.0, Add(2, 3)); | 159 CHECK_EQ(5.0, Add(CcTest::i_isolate(), 2, 3)); |
| 160 } | 160 } |
| 161 | 161 |
| 162 | 162 |
| 163 static double Abs(int x) { | 163 static double Abs(Isolate* isolate, int x) { |
| 164 Handle<JSFunction> fun = Compile("if (x < 0) result = -x; else result = x;"); | 164 Handle<JSFunction> fun = Compile("if (x < 0) result = -x; else result = x;"); |
| 165 if (fun.is_null()) return -1; | 165 if (fun.is_null()) return -1; |
| 166 | 166 |
| 167 SetGlobalProperty("x", Smi::FromInt(x)); | 167 SetGlobalProperty("x", Smi::FromInt(x)); |
| 168 bool has_pending_exception; | 168 bool has_pending_exception; |
| 169 Handle<JSObject> global(Isolate::Current()->context()->global_object()); | 169 Handle<JSObject> global(isolate->context()->global_object()); |
| 170 Execution::Call(fun, global, 0, NULL, &has_pending_exception); | 170 Execution::Call(isolate, fun, global, 0, NULL, &has_pending_exception); |
| 171 CHECK(!has_pending_exception); | 171 CHECK(!has_pending_exception); |
| 172 return GetGlobalProperty("result")->ToObjectChecked()->Number(); | 172 return GetGlobalProperty("result")->ToObjectChecked()->Number(); |
| 173 } | 173 } |
| 174 | 174 |
| 175 | 175 |
| 176 TEST(Abs) { | 176 TEST(Abs) { |
| 177 CcTest::InitializeVM(); | 177 CcTest::InitializeVM(); |
| 178 v8::HandleScope scope(CcTest::isolate()); | 178 v8::HandleScope scope(CcTest::isolate()); |
| 179 CHECK_EQ(3.0, Abs(-3)); | 179 CHECK_EQ(3.0, Abs(CcTest::i_isolate(), -3)); |
| 180 } | 180 } |
| 181 | 181 |
| 182 | 182 |
| 183 static double Sum(int n) { | 183 static double Sum(Isolate* isolate, int n) { |
| 184 Handle<JSFunction> fun = | 184 Handle<JSFunction> fun = |
| 185 Compile("s = 0; while (n > 0) { s += n; n -= 1; }; result = s;"); | 185 Compile("s = 0; while (n > 0) { s += n; n -= 1; }; result = s;"); |
| 186 if (fun.is_null()) return -1; | 186 if (fun.is_null()) return -1; |
| 187 | 187 |
| 188 SetGlobalProperty("n", Smi::FromInt(n)); | 188 SetGlobalProperty("n", Smi::FromInt(n)); |
| 189 bool has_pending_exception; | 189 bool has_pending_exception; |
| 190 Handle<JSObject> global(Isolate::Current()->context()->global_object()); | 190 Handle<JSObject> global(isolate->context()->global_object()); |
| 191 Execution::Call(fun, global, 0, NULL, &has_pending_exception); | 191 Execution::Call(isolate, fun, global, 0, NULL, &has_pending_exception); |
| 192 CHECK(!has_pending_exception); | 192 CHECK(!has_pending_exception); |
| 193 return GetGlobalProperty("result")->ToObjectChecked()->Number(); | 193 return GetGlobalProperty("result")->ToObjectChecked()->Number(); |
| 194 } | 194 } |
| 195 | 195 |
| 196 | 196 |
| 197 TEST(Sum) { | 197 TEST(Sum) { |
| 198 CcTest::InitializeVM(); | 198 CcTest::InitializeVM(); |
| 199 v8::HandleScope scope(CcTest::isolate()); | 199 v8::HandleScope scope(CcTest::isolate()); |
| 200 CHECK_EQ(5050.0, Sum(100)); | 200 CHECK_EQ(5050.0, Sum(CcTest::i_isolate(), 100)); |
| 201 } | 201 } |
| 202 | 202 |
| 203 | 203 |
| 204 TEST(Print) { | 204 TEST(Print) { |
| 205 CcTest::InitializeVM(PRINT_EXTENSION); | 205 CcTest::InitializeVM(PRINT_EXTENSION); |
| 206 v8::HandleScope scope(CcTest::isolate()); | 206 v8::HandleScope scope(CcTest::isolate()); |
| 207 const char* source = "for (n = 0; n < 100; ++n) print(n, 1, 2);"; | 207 const char* source = "for (n = 0; n < 100; ++n) print(n, 1, 2);"; |
| 208 Handle<JSFunction> fun = Compile(source); | 208 Handle<JSFunction> fun = Compile(source); |
| 209 if (fun.is_null()) return; | 209 if (fun.is_null()) return; |
| 210 bool has_pending_exception; | 210 bool has_pending_exception; |
| 211 Handle<JSObject> global(Isolate::Current()->context()->global_object()); | 211 Handle<JSObject> global(CcTest::i_isolate()->context()->global_object()); |
| 212 Execution::Call(fun, global, 0, NULL, &has_pending_exception); | 212 Execution::Call( |
| 213 CcTest::i_isolate(), fun, global, 0, NULL, &has_pending_exception); |
| 213 CHECK(!has_pending_exception); | 214 CHECK(!has_pending_exception); |
| 214 } | 215 } |
| 215 | 216 |
| 216 | 217 |
| 217 // The following test method stems from my coding efforts today. It | 218 // The following test method stems from my coding efforts today. It |
| 218 // tests all the functionality I have added to the compiler today | 219 // tests all the functionality I have added to the compiler today |
| 219 TEST(Stuff) { | 220 TEST(Stuff) { |
| 220 CcTest::InitializeVM(); | 221 CcTest::InitializeVM(); |
| 221 v8::HandleScope scope(CcTest::isolate()); | 222 v8::HandleScope scope(CcTest::isolate()); |
| 222 const char* source = | 223 const char* source = |
| (...skipping 11 matching lines...) Expand all Loading... |
| 234 "if (baz() == 6) r+=32;\n" // 32 | 235 "if (baz() == 6) r+=32;\n" // 32 |
| 235 "function Cons0() { this.x = 42; this.y = 87; }\n" | 236 "function Cons0() { this.x = 42; this.y = 87; }\n" |
| 236 "if (new Cons0().x == 42) r+=64;\n" // 64 | 237 "if (new Cons0().x == 42) r+=64;\n" // 64 |
| 237 "if (new Cons0().y == 87) r+=128;\n" // 128 | 238 "if (new Cons0().y == 87) r+=128;\n" // 128 |
| 238 "function Cons2(x, y) { this.sum = x + y; }\n" | 239 "function Cons2(x, y) { this.sum = x + y; }\n" |
| 239 "if (new Cons2(3,4).sum == 7) r+=256;"; // 256 | 240 "if (new Cons2(3,4).sum == 7) r+=256;"; // 256 |
| 240 | 241 |
| 241 Handle<JSFunction> fun = Compile(source); | 242 Handle<JSFunction> fun = Compile(source); |
| 242 CHECK(!fun.is_null()); | 243 CHECK(!fun.is_null()); |
| 243 bool has_pending_exception; | 244 bool has_pending_exception; |
| 244 Handle<JSObject> global(Isolate::Current()->context()->global_object()); | 245 Handle<JSObject> global(CcTest::i_isolate()->context()->global_object()); |
| 245 Execution::Call(fun, global, 0, NULL, &has_pending_exception); | 246 Execution::Call( |
| 247 CcTest::i_isolate(), fun, global, 0, NULL, &has_pending_exception); |
| 246 CHECK(!has_pending_exception); | 248 CHECK(!has_pending_exception); |
| 247 CHECK_EQ(511.0, GetGlobalProperty("r")->ToObjectChecked()->Number()); | 249 CHECK_EQ(511.0, GetGlobalProperty("r")->ToObjectChecked()->Number()); |
| 248 } | 250 } |
| 249 | 251 |
| 250 | 252 |
| 251 TEST(UncaughtThrow) { | 253 TEST(UncaughtThrow) { |
| 252 CcTest::InitializeVM(); | 254 CcTest::InitializeVM(); |
| 253 v8::HandleScope scope(CcTest::isolate()); | 255 v8::HandleScope scope(CcTest::isolate()); |
| 254 | 256 |
| 255 const char* source = "throw 42;"; | 257 const char* source = "throw 42;"; |
| 256 Handle<JSFunction> fun = Compile(source); | 258 Handle<JSFunction> fun = Compile(source); |
| 257 CHECK(!fun.is_null()); | 259 CHECK(!fun.is_null()); |
| 258 bool has_pending_exception; | 260 bool has_pending_exception; |
| 259 Isolate* isolate = fun->GetIsolate(); | 261 Isolate* isolate = fun->GetIsolate(); |
| 260 Handle<JSObject> global(isolate->context()->global_object()); | 262 Handle<JSObject> global(isolate->context()->global_object()); |
| 261 Execution::Call(fun, global, 0, NULL, &has_pending_exception); | 263 Execution::Call(isolate, fun, global, 0, NULL, &has_pending_exception); |
| 262 CHECK(has_pending_exception); | 264 CHECK(has_pending_exception); |
| 263 CHECK_EQ(42.0, isolate->pending_exception()->ToObjectChecked()->Number()); | 265 CHECK_EQ(42.0, isolate->pending_exception()->ToObjectChecked()->Number()); |
| 264 } | 266 } |
| 265 | 267 |
| 266 | 268 |
| 267 // Tests calling a builtin function from C/C++ code, and the builtin function | 269 // Tests calling a builtin function from C/C++ code, and the builtin function |
| 268 // performs GC. It creates a stack frame looks like following: | 270 // performs GC. It creates a stack frame looks like following: |
| 269 // | C (PerformGC) | | 271 // | C (PerformGC) | |
| 270 // | JS-to-C | | 272 // | JS-to-C | |
| 271 // | JS | | 273 // | JS | |
| 272 // | C-to-JS | | 274 // | C-to-JS | |
| 273 TEST(C2JSFrames) { | 275 TEST(C2JSFrames) { |
| 274 CcTest::InitializeVM(PRINT_EXTENSION | GC_EXTENSION); | 276 CcTest::InitializeVM(PRINT_EXTENSION | GC_EXTENSION); |
| 275 v8::HandleScope scope(CcTest::isolate()); | 277 v8::HandleScope scope(CcTest::isolate()); |
| 276 | 278 |
| 277 const char* source = "function foo(a) { gc(), print(a); }"; | 279 const char* source = "function foo(a) { gc(), print(a); }"; |
| 278 | 280 |
| 279 Handle<JSFunction> fun0 = Compile(source); | 281 Handle<JSFunction> fun0 = Compile(source); |
| 280 CHECK(!fun0.is_null()); | 282 CHECK(!fun0.is_null()); |
| 281 Isolate* isolate = fun0->GetIsolate(); | 283 Isolate* isolate = fun0->GetIsolate(); |
| 282 | 284 |
| 283 // Run the generated code to populate the global object with 'foo'. | 285 // Run the generated code to populate the global object with 'foo'. |
| 284 bool has_pending_exception; | 286 bool has_pending_exception; |
| 285 Handle<JSObject> global(Isolate::Current()->context()->global_object()); | 287 Handle<JSObject> global(isolate->context()->global_object()); |
| 286 Execution::Call(fun0, global, 0, NULL, &has_pending_exception); | 288 Execution::Call( |
| 289 isolate, fun0, global, 0, NULL, &has_pending_exception); |
| 287 CHECK(!has_pending_exception); | 290 CHECK(!has_pending_exception); |
| 288 | 291 |
| 289 Object* foo_string = isolate->factory()->InternalizeOneByteString( | 292 Object* foo_string = isolate->factory()->InternalizeOneByteString( |
| 290 STATIC_ASCII_VECTOR("foo"))->ToObjectChecked(); | 293 STATIC_ASCII_VECTOR("foo"))->ToObjectChecked(); |
| 291 MaybeObject* fun1_object = isolate->context()->global_object()-> | 294 MaybeObject* fun1_object = isolate->context()->global_object()-> |
| 292 GetProperty(String::cast(foo_string)); | 295 GetProperty(String::cast(foo_string)); |
| 293 Handle<Object> fun1(fun1_object->ToObjectChecked(), isolate); | 296 Handle<Object> fun1(fun1_object->ToObjectChecked(), isolate); |
| 294 CHECK(fun1->IsJSFunction()); | 297 CHECK(fun1->IsJSFunction()); |
| 295 | 298 |
| 296 Handle<Object> argv[] = { isolate->factory()->InternalizeOneByteString( | 299 Handle<Object> argv[] = { isolate->factory()->InternalizeOneByteString( |
| 297 STATIC_ASCII_VECTOR("hello")) }; | 300 STATIC_ASCII_VECTOR("hello")) }; |
| 298 Execution::Call(Handle<JSFunction>::cast(fun1), | 301 Execution::Call(isolate, |
| 302 Handle<JSFunction>::cast(fun1), |
| 299 global, | 303 global, |
| 300 ARRAY_SIZE(argv), | 304 ARRAY_SIZE(argv), |
| 301 argv, | 305 argv, |
| 302 &has_pending_exception); | 306 &has_pending_exception); |
| 303 CHECK(!has_pending_exception); | 307 CHECK(!has_pending_exception); |
| 304 } | 308 } |
| 305 | 309 |
| 306 | 310 |
| 307 // Regression 236. Calling InitLineEnds on a Script with undefined | 311 // Regression 236. Calling InitLineEnds on a Script with undefined |
| 308 // source resulted in crash. | 312 // source resulted in crash. |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 422 CompileRun("function f() { a = 12345678 }; f();"); | 426 CompileRun("function f() { a = 12345678 }; f();"); |
| 423 CheckCodeForUnsafeLiteral(GetJSFunction(CcTest::env()->Global(), "f")); | 427 CheckCodeForUnsafeLiteral(GetJSFunction(CcTest::env()->Global(), "f")); |
| 424 CompileRun("function f(x) { a = 12345678 + x}; f(1);"); | 428 CompileRun("function f(x) { a = 12345678 + x}; f(1);"); |
| 425 CheckCodeForUnsafeLiteral(GetJSFunction(CcTest::env()->Global(), "f")); | 429 CheckCodeForUnsafeLiteral(GetJSFunction(CcTest::env()->Global(), "f")); |
| 426 CompileRun("function f(x) { var arguments = 1; x += 12345678}; f(1);"); | 430 CompileRun("function f(x) { var arguments = 1; x += 12345678}; f(1);"); |
| 427 CheckCodeForUnsafeLiteral(GetJSFunction(CcTest::env()->Global(), "f")); | 431 CheckCodeForUnsafeLiteral(GetJSFunction(CcTest::env()->Global(), "f")); |
| 428 CompileRun("function f(x) { var arguments = 1; x = 12345678}; f(1);"); | 432 CompileRun("function f(x) { var arguments = 1; x = 12345678}; f(1);"); |
| 429 CheckCodeForUnsafeLiteral(GetJSFunction(CcTest::env()->Global(), "f")); | 433 CheckCodeForUnsafeLiteral(GetJSFunction(CcTest::env()->Global(), "f")); |
| 430 } | 434 } |
| 431 #endif | 435 #endif |
| OLD | NEW |