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 3081 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3092 { | 3092 { |
3093 v8::Global<String> unique = ReturnUnique(isolate, global); | 3093 v8::Global<String> unique = ReturnUnique(isolate, global); |
3094 CHECK(unique == global); | 3094 CHECK(unique == global); |
3095 CHECK_EQ(initial_handle_count + 1, global_handles->global_handles_count()); | 3095 CHECK_EQ(initial_handle_count + 1, global_handles->global_handles_count()); |
3096 } | 3096 } |
3097 CHECK_EQ(initial_handle_count, global_handles->global_handles_count()); | 3097 CHECK_EQ(initial_handle_count, global_handles->global_handles_count()); |
3098 global.Reset(); | 3098 global.Reset(); |
3099 } | 3099 } |
3100 | 3100 |
3101 | 3101 |
| 3102 namespace { |
| 3103 |
| 3104 class TwoPassCallbackData; |
| 3105 void FirstPassCallback(const v8::WeakCallbackInfo<TwoPassCallbackData>& data); |
| 3106 void SecondPassCallback(const v8::WeakCallbackInfo<TwoPassCallbackData>& data); |
| 3107 |
| 3108 |
| 3109 class TwoPassCallbackData { |
| 3110 public: |
| 3111 TwoPassCallbackData(v8::Isolate* isolate, int* instance_counter) |
| 3112 : first_pass_called_(false), |
| 3113 second_pass_called_(false), |
| 3114 trigger_gc_(false), |
| 3115 instance_counter_(instance_counter) { |
| 3116 HandleScope scope(isolate); |
| 3117 i::ScopedVector<char> buffer(40); |
| 3118 i::SNPrintF(buffer, "%p", static_cast<void*>(this)); |
| 3119 auto string = |
| 3120 v8::String::NewFromUtf8(isolate, buffer.start(), |
| 3121 v8::NewStringType::kNormal).ToLocalChecked(); |
| 3122 cell_.Reset(isolate, string); |
| 3123 (*instance_counter_)++; |
| 3124 } |
| 3125 |
| 3126 ~TwoPassCallbackData() { |
| 3127 CHECK(first_pass_called_); |
| 3128 CHECK(second_pass_called_); |
| 3129 CHECK(cell_.IsEmpty()); |
| 3130 (*instance_counter_)--; |
| 3131 } |
| 3132 |
| 3133 void FirstPass() { |
| 3134 CHECK(!first_pass_called_); |
| 3135 CHECK(!second_pass_called_); |
| 3136 CHECK(!cell_.IsEmpty()); |
| 3137 cell_.Reset(); |
| 3138 first_pass_called_ = true; |
| 3139 } |
| 3140 |
| 3141 void SecondPass() { |
| 3142 CHECK(first_pass_called_); |
| 3143 CHECK(!second_pass_called_); |
| 3144 CHECK(cell_.IsEmpty()); |
| 3145 second_pass_called_ = true; |
| 3146 delete this; |
| 3147 } |
| 3148 |
| 3149 void SetWeak() { |
| 3150 cell_.SetWeak(this, FirstPassCallback, v8::WeakCallbackType::kParameter); |
| 3151 } |
| 3152 |
| 3153 void MarkTriggerGc() { trigger_gc_ = true; } |
| 3154 bool trigger_gc() { return trigger_gc_; } |
| 3155 |
| 3156 int* instance_counter() { return instance_counter_; } |
| 3157 |
| 3158 private: |
| 3159 bool first_pass_called_; |
| 3160 bool second_pass_called_; |
| 3161 bool trigger_gc_; |
| 3162 v8::Global<v8::String> cell_; |
| 3163 int* instance_counter_; |
| 3164 }; |
| 3165 |
| 3166 |
| 3167 void SecondPassCallback(const v8::WeakCallbackInfo<TwoPassCallbackData>& data) { |
| 3168 ApiTestFuzzer::Fuzz(); |
| 3169 bool trigger_gc = data.GetParameter()->trigger_gc(); |
| 3170 int* instance_counter = data.GetParameter()->instance_counter(); |
| 3171 data.GetParameter()->SecondPass(); |
| 3172 if (!trigger_gc) return; |
| 3173 auto data_2 = new TwoPassCallbackData(data.GetIsolate(), instance_counter); |
| 3174 data_2->SetWeak(); |
| 3175 CcTest::heap()->CollectAllGarbage(i::Heap::kAbortIncrementalMarkingMask); |
| 3176 } |
| 3177 |
| 3178 |
| 3179 void FirstPassCallback(const v8::WeakCallbackInfo<TwoPassCallbackData>& data) { |
| 3180 data.GetParameter()->FirstPass(); |
| 3181 data.SetSecondPassCallback(SecondPassCallback); |
| 3182 } |
| 3183 |
| 3184 } // namespace |
| 3185 |
| 3186 |
| 3187 THREADED_TEST(TwoPassPhantomCallbacks) { |
| 3188 auto isolate = CcTest::isolate(); |
| 3189 const size_t kLength = 20; |
| 3190 auto global_handles = |
| 3191 reinterpret_cast<v8::internal::Isolate*>(isolate)->global_handles(); |
| 3192 int instance_counter = 0; |
| 3193 int initial_handle_count = global_handles->global_handles_count(); |
| 3194 for (size_t i = 0; i < kLength; ++i) { |
| 3195 auto data = new TwoPassCallbackData(isolate, &instance_counter); |
| 3196 data->SetWeak(); |
| 3197 } |
| 3198 CHECK_EQ(static_cast<int>(kLength), instance_counter); |
| 3199 CcTest::heap()->CollectAllGarbage(i::Heap::kAbortIncrementalMarkingMask); |
| 3200 CHECK_EQ(initial_handle_count, global_handles->global_handles_count()); |
| 3201 CHECK_EQ(0, instance_counter); |
| 3202 } |
| 3203 |
| 3204 |
| 3205 THREADED_TEST(TwoPassPhantomCallbacksNestedGc) { |
| 3206 auto isolate = CcTest::isolate(); |
| 3207 const size_t kLength = 20; |
| 3208 TwoPassCallbackData* array[kLength]; |
| 3209 v8::internal::GlobalHandles* global_handles = |
| 3210 reinterpret_cast<v8::internal::Isolate*>(isolate)->global_handles(); |
| 3211 int instance_counter = 0; |
| 3212 int initial_handle_count = global_handles->global_handles_count(); |
| 3213 for (size_t i = 0; i < kLength; ++i) { |
| 3214 array[i] = new TwoPassCallbackData(isolate, &instance_counter); |
| 3215 array[i]->SetWeak(); |
| 3216 } |
| 3217 array[5]->MarkTriggerGc(); |
| 3218 array[10]->MarkTriggerGc(); |
| 3219 array[15]->MarkTriggerGc(); |
| 3220 CHECK_EQ(static_cast<int>(kLength), instance_counter); |
| 3221 CcTest::heap()->CollectAllGarbage(i::Heap::kAbortIncrementalMarkingMask); |
| 3222 CHECK_EQ(initial_handle_count, global_handles->global_handles_count()); |
| 3223 CHECK_EQ(0, instance_counter); |
| 3224 } |
| 3225 |
| 3226 |
3102 template <typename K, typename V> | 3227 template <typename K, typename V> |
3103 class WeakStdMapTraits : public v8::StdMapTraits<K, V> { | 3228 class WeakStdMapTraits : public v8::StdMapTraits<K, V> { |
3104 public: | 3229 public: |
3105 typedef typename v8::PersistentValueMap<K, V, WeakStdMapTraits<K, V>> MapType; | 3230 typedef typename v8::PersistentValueMap<K, V, WeakStdMapTraits<K, V>> MapType; |
3106 static const v8::PersistentContainerCallbackType kCallbackType = v8::kWeak; | 3231 static const v8::PersistentContainerCallbackType kCallbackType = v8::kWeak; |
3107 struct WeakCallbackDataType { | 3232 struct WeakCallbackDataType { |
3108 MapType* map; | 3233 MapType* map; |
3109 K key; | 3234 K key; |
3110 }; | 3235 }; |
3111 static WeakCallbackDataType* WeakCallbackParameter(MapType* map, const K& key, | 3236 static WeakCallbackDataType* WeakCallbackParameter(MapType* map, const K& key, |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3235 } | 3360 } |
3236 static void DisposeCallbackData(WeakCallbackDataType* data) { delete data; } | 3361 static void DisposeCallbackData(WeakCallbackDataType* data) { delete data; } |
3237 static void Dispose(v8::Isolate* isolate, v8::Global<V> value, K key) { | 3362 static void Dispose(v8::Isolate* isolate, v8::Global<V> value, K key) { |
3238 CHECK_EQ(IntKeyToVoidPointer(key), | 3363 CHECK_EQ(IntKeyToVoidPointer(key), |
3239 v8::Object::GetAlignedPointerFromInternalField(value, 0)); | 3364 v8::Object::GetAlignedPointerFromInternalField(value, 0)); |
3240 } | 3365 } |
3241 static void DisposeWeak( | 3366 static void DisposeWeak( |
3242 v8::Isolate* isolate, | 3367 v8::Isolate* isolate, |
3243 const v8::WeakCallbackInfo<WeakCallbackDataType>& info, K key) { | 3368 const v8::WeakCallbackInfo<WeakCallbackDataType>& info, K key) { |
3244 CHECK_EQ(IntKeyToVoidPointer(key), info.GetInternalField(0)); | 3369 CHECK_EQ(IntKeyToVoidPointer(key), info.GetInternalField(0)); |
| 3370 DisposeCallbackData(info.GetParameter()); |
3245 } | 3371 } |
3246 }; | 3372 }; |
3247 } | 3373 } |
3248 | 3374 |
3249 | 3375 |
3250 TEST(GlobalValueMap) { | 3376 TEST(GlobalValueMap) { |
3251 typedef v8::GlobalValueMap<int, v8::Object, | 3377 typedef v8::GlobalValueMap<int, v8::Object, |
3252 PhantomStdMapTraits<int, v8::Object>> Map; | 3378 PhantomStdMapTraits<int, v8::Object>> Map; |
3253 LocalContext env; | 3379 LocalContext env; |
3254 v8::Isolate* isolate = env->GetIsolate(); | 3380 v8::Isolate* isolate = env->GetIsolate(); |
(...skipping 3119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6374 LocalContext context; | 6500 LocalContext context; |
6375 v8::HandleScope scope(context->GetIsolate()); | 6501 v8::HandleScope scope(context->GetIsolate()); |
6376 v8::V8::AddMessageListener(MissingScriptInfoMessageListener); | 6502 v8::V8::AddMessageListener(MissingScriptInfoMessageListener); |
6377 CompileRun("throw Error()"); | 6503 CompileRun("throw Error()"); |
6378 v8::V8::RemoveMessageListeners(MissingScriptInfoMessageListener); | 6504 v8::V8::RemoveMessageListeners(MissingScriptInfoMessageListener); |
6379 } | 6505 } |
6380 | 6506 |
6381 | 6507 |
6382 struct FlagAndPersistent { | 6508 struct FlagAndPersistent { |
6383 bool flag; | 6509 bool flag; |
6384 v8::Persistent<v8::Object> handle; | 6510 v8::Global<v8::Object> handle; |
6385 }; | 6511 }; |
6386 | 6512 |
6387 | 6513 |
6388 static void SetFlag(const v8::WeakCallbackInfo<FlagAndPersistent>& data) { | 6514 static void SetFlag(const v8::WeakCallbackInfo<FlagAndPersistent>& data) { |
6389 data.GetParameter()->flag = true; | 6515 data.GetParameter()->flag = true; |
| 6516 data.GetParameter()->handle.Reset(); |
6390 } | 6517 } |
6391 | 6518 |
6392 | 6519 |
6393 static void IndependentWeakHandle(bool global_gc, bool interlinked) { | 6520 static void IndependentWeakHandle(bool global_gc, bool interlinked) { |
6394 v8::Isolate* iso = CcTest::isolate(); | 6521 v8::Isolate* iso = CcTest::isolate(); |
6395 v8::HandleScope scope(iso); | 6522 v8::HandleScope scope(iso); |
6396 v8::Handle<Context> context = Context::New(iso); | 6523 v8::Handle<Context> context = Context::New(iso); |
6397 Context::Scope context_scope(context); | 6524 Context::Scope context_scope(context); |
6398 | 6525 |
6399 FlagAndPersistent object_a, object_b; | 6526 FlagAndPersistent object_a, object_b; |
(...skipping 15373 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
21773 } | 21900 } |
21774 { | 21901 { |
21775 v8::TryCatch try_catch; | 21902 v8::TryCatch try_catch; |
21776 uint16_t* data = reinterpret_cast<uint16_t*>(buffer); | 21903 uint16_t* data = reinterpret_cast<uint16_t*>(buffer); |
21777 CHECK(v8::String::NewFromTwoByte(isolate, data, v8::String::kNormalString, | 21904 CHECK(v8::String::NewFromTwoByte(isolate, data, v8::String::kNormalString, |
21778 length).IsEmpty()); | 21905 length).IsEmpty()); |
21779 CHECK(!try_catch.HasCaught()); | 21906 CHECK(!try_catch.HasCaught()); |
21780 } | 21907 } |
21781 free(buffer); | 21908 free(buffer); |
21782 } | 21909 } |
OLD | NEW |