OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 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 4414 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4425 "}" | 4425 "}" |
4426 "gc();" | 4426 "gc();" |
4427 "4"; | 4427 "4"; |
4428 v8::Handle<Value> result = CompileRun(code); | 4428 v8::Handle<Value> result = CompileRun(code); |
4429 CHECK_EQ(4.0, result->NumberValue()); | 4429 CHECK_EQ(4.0, result->NumberValue()); |
4430 delete whammy; | 4430 delete whammy; |
4431 context.Dispose(); | 4431 context.Dispose(); |
4432 } | 4432 } |
4433 | 4433 |
4434 | 4434 |
4435 static bool in_scavenge = false; | 4435 static void DisposeAndSetFlag(v8::Persistent<v8::Value> obj, void* data) { |
4436 static int last = -1; | |
4437 | |
4438 static void ForceScavenge(v8::Persistent<v8::Value> obj, void* data) { | |
4439 CHECK_EQ(-1, last); | |
4440 last = 0; | |
4441 obj.Dispose(); | 4436 obj.Dispose(); |
4442 obj.Clear(); | 4437 obj.Clear(); |
4443 in_scavenge = true; | |
4444 HEAP->PerformScavenge(); | |
4445 in_scavenge = false; | |
4446 *(reinterpret_cast<bool*>(data)) = true; | 4438 *(reinterpret_cast<bool*>(data)) = true; |
4447 } | 4439 } |
4448 | 4440 |
4449 static void CheckIsNotInvokedInScavenge(v8::Persistent<v8::Value> obj, | |
4450 void* data) { | |
4451 CHECK_EQ(0, last); | |
4452 last = 1; | |
4453 *(reinterpret_cast<bool*>(data)) = in_scavenge; | |
4454 obj.Dispose(); | |
4455 obj.Clear(); | |
4456 } | |
4457 | 4441 |
4458 THREADED_TEST(NoWeakRefCallbacksInScavenge) { | 4442 THREADED_TEST(IndependentWeakHandle) { |
4459 // Test verifies that scavenge cannot invoke WeakReferenceCallbacks. | |
4460 // Calling callbacks from scavenges is unsafe as objects held by those | |
4461 // handlers might have become strongly reachable, but scavenge doesn't | |
4462 // check that. | |
4463 v8::Persistent<Context> context = Context::New(); | 4443 v8::Persistent<Context> context = Context::New(); |
4464 Context::Scope context_scope(context); | 4444 Context::Scope context_scope(context); |
4465 | 4445 |
4466 v8::Persistent<v8::Object> object_a; | 4446 v8::Persistent<v8::Object> object_a; |
4467 v8::Persistent<v8::Object> object_b; | |
4468 | 4447 |
4469 { | 4448 { |
4470 v8::HandleScope handle_scope; | 4449 v8::HandleScope handle_scope; |
4471 object_b = v8::Persistent<v8::Object>::New(v8::Object::New()); | |
4472 object_a = v8::Persistent<v8::Object>::New(v8::Object::New()); | 4450 object_a = v8::Persistent<v8::Object>::New(v8::Object::New()); |
4473 } | 4451 } |
4474 | 4452 |
4475 bool object_a_disposed = false; | 4453 bool object_a_disposed = false; |
4476 object_a.MakeWeak(&object_a_disposed, &ForceScavenge); | 4454 object_a.MakeWeak(&object_a_disposed, &DisposeAndSetFlag); |
4477 bool released_in_scavenge = false; | 4455 object_a.MarkIndependent(); |
4478 object_b.MakeWeak(&released_in_scavenge, &CheckIsNotInvokedInScavenge); | 4456 HEAP->PerformScavenge(); |
4479 | 4457 CHECK(object_a_disposed); |
4480 while (!object_a_disposed) { | |
4481 HEAP->CollectAllGarbage(false); | |
4482 } | |
4483 CHECK(!released_in_scavenge); | |
4484 } | 4458 } |
4485 | 4459 |
4486 | 4460 |
| 4461 static void InvokeScavenge() { |
| 4462 HEAP->PerformScavenge(); |
| 4463 } |
| 4464 |
| 4465 |
| 4466 static void InvokeMarkSweep() { |
| 4467 HEAP->CollectAllGarbage(false); |
| 4468 } |
| 4469 |
| 4470 |
| 4471 static void ForceScavenge(v8::Persistent<v8::Value> obj, void* data) { |
| 4472 obj.Dispose(); |
| 4473 obj.Clear(); |
| 4474 *(reinterpret_cast<bool*>(data)) = true; |
| 4475 InvokeScavenge(); |
| 4476 } |
| 4477 |
| 4478 |
| 4479 static void ForceMarkSweep(v8::Persistent<v8::Value> obj, void* data) { |
| 4480 obj.Dispose(); |
| 4481 obj.Clear(); |
| 4482 *(reinterpret_cast<bool*>(data)) = true; |
| 4483 InvokeMarkSweep(); |
| 4484 } |
| 4485 |
| 4486 |
| 4487 THREADED_TEST(GCFromWeakCallbacks) { |
| 4488 v8::Persistent<Context> context = Context::New(); |
| 4489 Context::Scope context_scope(context); |
| 4490 |
| 4491 static const int kNumberOfGCTypes = 2; |
| 4492 v8::WeakReferenceCallback gc_forcing_callback[kNumberOfGCTypes] = |
| 4493 {&ForceScavenge, &ForceMarkSweep}; |
| 4494 |
| 4495 typedef void (*GCInvoker)(); |
| 4496 GCInvoker invoke_gc[kNumberOfGCTypes] = {&InvokeScavenge, &InvokeMarkSweep}; |
| 4497 |
| 4498 for (int outer_gc = 0; outer_gc < kNumberOfGCTypes; outer_gc++) { |
| 4499 for (int inner_gc = 0; inner_gc < kNumberOfGCTypes; inner_gc++) { |
| 4500 v8::Persistent<v8::Object> object; |
| 4501 { |
| 4502 v8::HandleScope handle_scope; |
| 4503 object = v8::Persistent<v8::Object>::New(v8::Object::New()); |
| 4504 } |
| 4505 bool disposed = false; |
| 4506 object.MakeWeak(&disposed, gc_forcing_callback[inner_gc]); |
| 4507 object.MarkIndependent(); |
| 4508 invoke_gc[outer_gc](); |
| 4509 CHECK(disposed); |
| 4510 } |
| 4511 } |
| 4512 } |
| 4513 |
| 4514 |
| 4515 static void RevivingCallback(v8::Persistent<v8::Value> obj, void* data) { |
| 4516 obj.ClearWeak(); |
| 4517 *(reinterpret_cast<bool*>(data)) = true; |
| 4518 } |
| 4519 |
| 4520 |
| 4521 THREADED_TEST(IndependentHandleRevival) { |
| 4522 v8::Persistent<Context> context = Context::New(); |
| 4523 Context::Scope context_scope(context); |
| 4524 |
| 4525 v8::Persistent<v8::Object> object; |
| 4526 { |
| 4527 v8::HandleScope handle_scope; |
| 4528 object = v8::Persistent<v8::Object>::New(v8::Object::New()); |
| 4529 object->Set(v8_str("x"), v8::Integer::New(1)); |
| 4530 v8::Local<String> y_str = v8_str("y"); |
| 4531 object->Set(y_str, y_str); |
| 4532 } |
| 4533 bool revived = false; |
| 4534 object.MakeWeak(&revived, &RevivingCallback); |
| 4535 object.MarkIndependent(); |
| 4536 HEAP->PerformScavenge(); |
| 4537 CHECK(revived); |
| 4538 HEAP->CollectAllGarbage(true); |
| 4539 { |
| 4540 v8::HandleScope handle_scope; |
| 4541 v8::Local<String> y_str = v8_str("y"); |
| 4542 CHECK_EQ(v8::Integer::New(1), object->Get(v8_str("x"))); |
| 4543 CHECK(object->Get(y_str)->Equals(y_str)); |
| 4544 } |
| 4545 } |
| 4546 |
| 4547 |
4487 v8::Handle<Function> args_fun; | 4548 v8::Handle<Function> args_fun; |
4488 | 4549 |
4489 | 4550 |
4490 static v8::Handle<Value> ArgumentsTestCallback(const v8::Arguments& args) { | 4551 static v8::Handle<Value> ArgumentsTestCallback(const v8::Arguments& args) { |
4491 ApiTestFuzzer::Fuzz(); | 4552 ApiTestFuzzer::Fuzz(); |
4492 CHECK_EQ(args_fun, args.Callee()); | 4553 CHECK_EQ(args_fun, args.Callee()); |
4493 CHECK_EQ(3, args.Length()); | 4554 CHECK_EQ(3, args.Length()); |
4494 CHECK_EQ(v8::Integer::New(1), args[0]); | 4555 CHECK_EQ(v8::Integer::New(1), args[0]); |
4495 CHECK_EQ(v8::Integer::New(2), args[1]); | 4556 CHECK_EQ(v8::Integer::New(2), args[1]); |
4496 CHECK_EQ(v8::Integer::New(3), args[2]); | 4557 CHECK_EQ(v8::Integer::New(3), args[2]); |
(...skipping 9949 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
14446 | 14507 |
14447 THREADED_TEST(CallAPIFunctionOnNonObject) { | 14508 THREADED_TEST(CallAPIFunctionOnNonObject) { |
14448 v8::HandleScope scope; | 14509 v8::HandleScope scope; |
14449 LocalContext context; | 14510 LocalContext context; |
14450 Handle<FunctionTemplate> templ = v8::FunctionTemplate::New(NonObjectThis); | 14511 Handle<FunctionTemplate> templ = v8::FunctionTemplate::New(NonObjectThis); |
14451 Handle<Function> function = templ->GetFunction(); | 14512 Handle<Function> function = templ->GetFunction(); |
14452 context->Global()->Set(v8_str("f"), function); | 14513 context->Global()->Set(v8_str("f"), function); |
14453 TryCatch try_catch; | 14514 TryCatch try_catch; |
14454 CompileRun("f.call(2)"); | 14515 CompileRun("f.call(2)"); |
14455 } | 14516 } |
OLD | NEW |