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 2003 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2014 const v8::HeapGraphNode* global_handles = GetNode( | 2014 const v8::HeapGraphNode* global_handles = GetNode( |
2015 gc_roots, v8::HeapGraphNode::kSynthetic, "(Global handles)"); | 2015 gc_roots, v8::HeapGraphNode::kSynthetic, "(Global handles)"); |
2016 CHECK(global_handles); | 2016 CHECK(global_handles); |
2017 return HasWeakEdge(global_handles); | 2017 return HasWeakEdge(global_handles); |
2018 } | 2018 } |
2019 | 2019 |
2020 | 2020 |
2021 static void PersistentHandleCallback( | 2021 static void PersistentHandleCallback( |
2022 const v8::WeakCallbackInfo<v8::Persistent<v8::Object> >& data) { | 2022 const v8::WeakCallbackInfo<v8::Persistent<v8::Object> >& data) { |
2023 data.GetParameter()->Reset(); | 2023 data.GetParameter()->Reset(); |
| 2024 delete data.GetParameter(); |
2024 } | 2025 } |
2025 | 2026 |
2026 | 2027 |
2027 TEST(WeakGlobalHandle) { | 2028 TEST(WeakGlobalHandle) { |
2028 LocalContext env; | 2029 LocalContext env; |
2029 v8::HandleScope scope(env->GetIsolate()); | 2030 v8::HandleScope scope(env->GetIsolate()); |
2030 | 2031 |
2031 CHECK(!HasWeakGlobalHandle()); | 2032 CHECK(!HasWeakGlobalHandle()); |
2032 | 2033 |
2033 v8::Persistent<v8::Object> handle(env->GetIsolate(), | 2034 // We cannot stack allocate the handle because the weak callback |
2034 v8::Object::New(env->GetIsolate())); | 2035 // can be invoked in the message loop after returning from this function. |
2035 handle.SetWeak(&handle, PersistentHandleCallback, | 2036 v8::Persistent<v8::Object>* handle = new v8::Persistent<v8::Object>(); |
2036 v8::WeakCallbackType::kParameter); | 2037 |
| 2038 handle->Reset(env->GetIsolate(), v8::Object::New(env->GetIsolate())); |
| 2039 handle->SetWeak(handle, PersistentHandleCallback, |
| 2040 v8::WeakCallbackType::kParameter); |
2037 | 2041 |
2038 CHECK(HasWeakGlobalHandle()); | 2042 CHECK(HasWeakGlobalHandle()); |
| 2043 CcTest::CollectAllGarbage(); |
2039 } | 2044 } |
2040 | 2045 |
2041 | 2046 |
2042 TEST(SfiAndJsFunctionWeakRefs) { | 2047 TEST(SfiAndJsFunctionWeakRefs) { |
2043 LocalContext env; | 2048 LocalContext env; |
2044 v8::HandleScope scope(env->GetIsolate()); | 2049 v8::HandleScope scope(env->GetIsolate()); |
2045 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); | 2050 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
2046 | 2051 |
2047 CompileRun( | 2052 CompileRun( |
2048 "fun = (function (x) { return function () { return x + 1; } })(1);"); | 2053 "fun = (function (x) { return function () { return x + 1; } })(1);"); |
(...skipping 908 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2957 " a[i] = i;\n" | 2962 " a[i] = i;\n" |
2958 " for (var i = 0; i < 3; ++i)\n" | 2963 " for (var i = 0; i < 3; ++i)\n" |
2959 " a.shift();\n" | 2964 " a.shift();\n" |
2960 "}\n"); | 2965 "}\n"); |
2961 | 2966 |
2962 CcTest::CollectGarbage(v8::internal::NEW_SPACE); | 2967 CcTest::CollectGarbage(v8::internal::NEW_SPACE); |
2963 // Should not crash. | 2968 // Should not crash. |
2964 | 2969 |
2965 heap_profiler->StopSamplingHeapProfiler(); | 2970 heap_profiler->StopSamplingHeapProfiler(); |
2966 } | 2971 } |
OLD | NEW |