| 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 TEST(TwoPassPhantomCallbacks) { |
| 3188 auto isolate = CcTest::isolate(); |
| 3189 const size_t kLength = 20; |
| 3190 int instance_counter = 0; |
| 3191 for (size_t i = 0; i < kLength; ++i) { |
| 3192 auto data = new TwoPassCallbackData(isolate, &instance_counter); |
| 3193 data->SetWeak(); |
| 3194 } |
| 3195 CHECK_EQ(static_cast<int>(kLength), instance_counter); |
| 3196 CcTest::heap()->CollectAllGarbage(i::Heap::kAbortIncrementalMarkingMask); |
| 3197 CHECK_EQ(0, instance_counter); |
| 3198 } |
| 3199 |
| 3200 |
| 3201 TEST(TwoPassPhantomCallbacksNestedGc) { |
| 3202 auto isolate = CcTest::isolate(); |
| 3203 const size_t kLength = 20; |
| 3204 TwoPassCallbackData* array[kLength]; |
| 3205 int instance_counter = 0; |
| 3206 for (size_t i = 0; i < kLength; ++i) { |
| 3207 array[i] = new TwoPassCallbackData(isolate, &instance_counter); |
| 3208 array[i]->SetWeak(); |
| 3209 } |
| 3210 array[5]->MarkTriggerGc(); |
| 3211 array[10]->MarkTriggerGc(); |
| 3212 array[15]->MarkTriggerGc(); |
| 3213 CHECK_EQ(static_cast<int>(kLength), instance_counter); |
| 3214 CcTest::heap()->CollectAllGarbage(i::Heap::kAbortIncrementalMarkingMask); |
| 3215 CHECK_EQ(0, instance_counter); |
| 3216 } |
| 3217 |
| 3218 |
| 3102 template <typename K, typename V> | 3219 template <typename K, typename V> |
| 3103 class WeakStdMapTraits : public v8::StdMapTraits<K, V> { | 3220 class WeakStdMapTraits : public v8::StdMapTraits<K, V> { |
| 3104 public: | 3221 public: |
| 3105 typedef typename v8::PersistentValueMap<K, V, WeakStdMapTraits<K, V>> MapType; | 3222 typedef typename v8::PersistentValueMap<K, V, WeakStdMapTraits<K, V>> MapType; |
| 3106 static const v8::PersistentContainerCallbackType kCallbackType = v8::kWeak; | 3223 static const v8::PersistentContainerCallbackType kCallbackType = v8::kWeak; |
| 3107 struct WeakCallbackDataType { | 3224 struct WeakCallbackDataType { |
| 3108 MapType* map; | 3225 MapType* map; |
| 3109 K key; | 3226 K key; |
| 3110 }; | 3227 }; |
| 3111 static WeakCallbackDataType* WeakCallbackParameter(MapType* map, const K& key, | 3228 static WeakCallbackDataType* WeakCallbackParameter(MapType* map, const K& key, |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3235 } | 3352 } |
| 3236 static void DisposeCallbackData(WeakCallbackDataType* data) { delete data; } | 3353 static void DisposeCallbackData(WeakCallbackDataType* data) { delete data; } |
| 3237 static void Dispose(v8::Isolate* isolate, v8::Global<V> value, K key) { | 3354 static void Dispose(v8::Isolate* isolate, v8::Global<V> value, K key) { |
| 3238 CHECK_EQ(IntKeyToVoidPointer(key), | 3355 CHECK_EQ(IntKeyToVoidPointer(key), |
| 3239 v8::Object::GetAlignedPointerFromInternalField(value, 0)); | 3356 v8::Object::GetAlignedPointerFromInternalField(value, 0)); |
| 3240 } | 3357 } |
| 3241 static void DisposeWeak( | 3358 static void DisposeWeak( |
| 3242 v8::Isolate* isolate, | 3359 v8::Isolate* isolate, |
| 3243 const v8::WeakCallbackInfo<WeakCallbackDataType>& info, K key) { | 3360 const v8::WeakCallbackInfo<WeakCallbackDataType>& info, K key) { |
| 3244 CHECK_EQ(IntKeyToVoidPointer(key), info.GetInternalField(0)); | 3361 CHECK_EQ(IntKeyToVoidPointer(key), info.GetInternalField(0)); |
| 3362 DisposeCallbackData(info.GetParameter()); |
| 3245 } | 3363 } |
| 3246 }; | 3364 }; |
| 3247 } | 3365 |
| 3366 } // namespace |
| 3248 | 3367 |
| 3249 | 3368 |
| 3250 TEST(GlobalValueMap) { | 3369 TEST(GlobalValueMap) { |
| 3251 typedef v8::GlobalValueMap<int, v8::Object, | 3370 typedef v8::GlobalValueMap<int, v8::Object, |
| 3252 PhantomStdMapTraits<int, v8::Object>> Map; | 3371 PhantomStdMapTraits<int, v8::Object>> Map; |
| 3253 LocalContext env; | 3372 LocalContext env; |
| 3254 v8::Isolate* isolate = env->GetIsolate(); | 3373 v8::Isolate* isolate = env->GetIsolate(); |
| 3255 v8::Global<ObjectTemplate> templ; | 3374 v8::Global<ObjectTemplate> templ; |
| 3256 { | 3375 { |
| 3257 HandleScope scope(isolate); | 3376 HandleScope scope(isolate); |
| (...skipping 3116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6374 LocalContext context; | 6493 LocalContext context; |
| 6375 v8::HandleScope scope(context->GetIsolate()); | 6494 v8::HandleScope scope(context->GetIsolate()); |
| 6376 v8::V8::AddMessageListener(MissingScriptInfoMessageListener); | 6495 v8::V8::AddMessageListener(MissingScriptInfoMessageListener); |
| 6377 CompileRun("throw Error()"); | 6496 CompileRun("throw Error()"); |
| 6378 v8::V8::RemoveMessageListeners(MissingScriptInfoMessageListener); | 6497 v8::V8::RemoveMessageListeners(MissingScriptInfoMessageListener); |
| 6379 } | 6498 } |
| 6380 | 6499 |
| 6381 | 6500 |
| 6382 struct FlagAndPersistent { | 6501 struct FlagAndPersistent { |
| 6383 bool flag; | 6502 bool flag; |
| 6384 v8::Persistent<v8::Object> handle; | 6503 v8::Global<v8::Object> handle; |
| 6385 }; | 6504 }; |
| 6386 | 6505 |
| 6387 | 6506 |
| 6388 static void SetFlag(const v8::WeakCallbackInfo<FlagAndPersistent>& data) { | 6507 static void SetFlag(const v8::WeakCallbackInfo<FlagAndPersistent>& data) { |
| 6389 data.GetParameter()->flag = true; | 6508 data.GetParameter()->flag = true; |
| 6509 data.GetParameter()->handle.Reset(); |
| 6390 } | 6510 } |
| 6391 | 6511 |
| 6392 | 6512 |
| 6393 static void IndependentWeakHandle(bool global_gc, bool interlinked) { | 6513 static void IndependentWeakHandle(bool global_gc, bool interlinked) { |
| 6394 v8::Isolate* iso = CcTest::isolate(); | 6514 v8::Isolate* iso = CcTest::isolate(); |
| 6395 v8::HandleScope scope(iso); | 6515 v8::HandleScope scope(iso); |
| 6396 v8::Handle<Context> context = Context::New(iso); | 6516 v8::Handle<Context> context = Context::New(iso); |
| 6397 Context::Scope context_scope(context); | 6517 Context::Scope context_scope(context); |
| 6398 | 6518 |
| 6399 FlagAndPersistent object_a, object_b; | 6519 FlagAndPersistent object_a, object_b; |
| (...skipping 15373 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 21773 } | 21893 } |
| 21774 { | 21894 { |
| 21775 v8::TryCatch try_catch; | 21895 v8::TryCatch try_catch; |
| 21776 uint16_t* data = reinterpret_cast<uint16_t*>(buffer); | 21896 uint16_t* data = reinterpret_cast<uint16_t*>(buffer); |
| 21777 CHECK(v8::String::NewFromTwoByte(isolate, data, v8::String::kNormalString, | 21897 CHECK(v8::String::NewFromTwoByte(isolate, data, v8::String::kNormalString, |
| 21778 length).IsEmpty()); | 21898 length).IsEmpty()); |
| 21779 CHECK(!try_catch.HasCaught()); | 21899 CHECK(!try_catch.HasCaught()); |
| 21780 } | 21900 } |
| 21781 free(buffer); | 21901 free(buffer); |
| 21782 } | 21902 } |
| OLD | NEW |