Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(341)

Unified Diff: src/wasm/managed.h

Issue 2676513008: [wasm] Managed<T> ensures T's lifetime does not leak past Isolate's (Closed)
Patch Set: better api fixes Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/wasm/managed.h
diff --git a/src/wasm/managed.h b/src/wasm/managed.h
index 5e2a9c82d561b858223af982533cbfc46e58539c..3b324da724b059311dd36f39fd84f39d0b54df5d 100644
--- a/src/wasm/managed.h
+++ b/src/wasm/managed.h
@@ -19,7 +19,9 @@ template <class CppType>
class Managed : public Foreign {
public:
V8_INLINE CppType* get() {
- return reinterpret_cast<CppType*>(foreign_address());
+ return reinterpret_cast<CppType*>(
+ reinterpret_cast<Isolate::ManagedLifeline*>(foreign_address())
+ ->value());
}
static Managed<CppType>* cast(Object* obj) {
@@ -27,13 +29,20 @@ class Managed : public Foreign {
return reinterpret_cast<Managed<CppType>*>(obj);
}
- static Handle<Managed<CppType>> New(Isolate* isolate, CppType* ptr,
- bool delete_on_gc = true) {
+ static Handle<Managed<CppType>> New(Isolate* isolate, CppType* ptr) {
+ Isolate::ManagedLifeline* node =
+ isolate->AddManagedObject(ptr, Managed<CppType>::NativeDelete);
Handle<Managed<CppType>> handle = Handle<Managed<CppType>>::cast(
- isolate->factory()->NewForeign(reinterpret_cast<Address>(ptr)));
- if (delete_on_gc) {
- RegisterWeakCallbackForDelete(isolate, handle);
- }
+ isolate->factory()->NewForeign(reinterpret_cast<Address>(node)));
+ RegisterWeakCallbackForDelete(isolate, handle);
+ return handle;
+ }
+
+ static Handle<Managed<CppType>> NewForTesting(Isolate* isolate,
titzer 2017/02/08 23:32:48 Do we really need to have a separate constructor f
Mircea Trofin 2017/02/08 23:52:36 Not super convinced of the cleanliness of the case
+ CppType* ptr) {
+ Isolate::ManagedLifeline* node = isolate->AddManagedObject(ptr, nullptr);
+ Handle<Managed<CppType>> handle = Handle<Managed<CppType>>::cast(
+ isolate->factory()->NewForeign(reinterpret_cast<Address>(node)));
return handle;
}
@@ -42,16 +51,23 @@ class Managed : public Foreign {
Handle<Managed<CppType>> handle) {
Handle<Object> global_handle = isolate->global_handles()->Create(*handle);
GlobalHandles::MakeWeak(global_handle.location(), global_handle.location(),
- &Managed<CppType>::Delete,
+ &Managed<CppType>::GCDelete,
v8::WeakCallbackType::kFinalizer);
}
- static void Delete(const v8::WeakCallbackInfo<void>& data) {
+
+ static void GCDelete(const v8::WeakCallbackInfo<void>& data) {
Managed<CppType>** p =
reinterpret_cast<Managed<CppType>**>(data.GetParameter());
- delete (*p)->get();
+ delete (
+ reinterpret_cast<Isolate::ManagedLifeline*>((*p)->foreign_address()));
(*p)->set_foreign_address(0);
GlobalHandles::Destroy(reinterpret_cast<Object**>(p));
}
+
+ static void NativeDelete(void* value) {
+ CppType* typed_value = reinterpret_cast<CppType*>(value);
+ delete typed_value;
+ }
};
} // namespace internal
} // namespace v8
« src/isolate.cc ('K') | « src/isolate.cc ('k') | test/cctest/wasm/test-managed.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698