caa1d4b262
Native resources allocated by v8, as internal implementation detail, and held by a Foreign object, must be released when the Isolate is torn down. Example: wasm::WasmModule allocated by wasm compile, and held throughout the lifetime of the WebAssembly.Module object. This change: - Extends Managed<CppType> with a mechanism for doing just that - Separates the role of Managed<CppType> to be strictly an owner of the lifetime of the native resource. For cases where that's not desirable, we can polymorphically use Foregin. - moves managed.h out of wasm, since it's not wasm-specific. BUG=680065 Review-Url: https://codereview.chromium.org/2676513008 Cr-Commit-Position: refs/heads/master@{#43350}
79 lines
2.0 KiB
C++
79 lines
2.0 KiB
C++
// Copyright 2016 the V8 project authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
#include <stdint.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "src/managed.h"
|
|
|
|
#include "src/objects-inl.h"
|
|
#include "test/cctest/cctest.h"
|
|
|
|
using namespace v8::base;
|
|
using namespace v8::internal;
|
|
|
|
class DeleteRecorder {
|
|
public:
|
|
explicit DeleteRecorder(bool* deleted) : deleted_(deleted) {
|
|
*deleted_ = false;
|
|
}
|
|
~DeleteRecorder() { *deleted_ = true; }
|
|
static void Deleter(void* value) {
|
|
delete reinterpret_cast<DeleteRecorder*>(value);
|
|
}
|
|
|
|
private:
|
|
bool* deleted_;
|
|
};
|
|
|
|
TEST(ManagedCollect) {
|
|
Isolate* isolate = CcTest::InitIsolateOnce();
|
|
bool deleted1 = false;
|
|
bool deleted2 = false;
|
|
DeleteRecorder* d1 = new DeleteRecorder(&deleted1);
|
|
DeleteRecorder* d2 = new DeleteRecorder(&deleted2);
|
|
Isolate::ManagedObjectFinalizer* finalizer =
|
|
isolate->RegisterForReleaseAtTeardown(d2, DeleteRecorder::Deleter);
|
|
{
|
|
HandleScope scope(isolate);
|
|
auto handle = Managed<DeleteRecorder>::New(isolate, d1);
|
|
USE(handle);
|
|
}
|
|
|
|
CcTest::CollectAllAvailableGarbage();
|
|
|
|
CHECK(deleted1);
|
|
CHECK(!deleted2);
|
|
isolate->UnregisterFromReleaseAtTeardown(&finalizer);
|
|
CHECK_NULL(finalizer);
|
|
delete d2;
|
|
CHECK(deleted2);
|
|
}
|
|
|
|
TEST(DisposeCollect) {
|
|
v8::Isolate::CreateParams create_params;
|
|
create_params.array_buffer_allocator =
|
|
CcTest::InitIsolateOnce()->array_buffer_allocator();
|
|
|
|
v8::Isolate* isolate = v8::Isolate::New(create_params);
|
|
isolate->Enter();
|
|
Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
|
|
bool deleted1 = false;
|
|
bool deleted2 = false;
|
|
DeleteRecorder* d1 = new DeleteRecorder(&deleted1);
|
|
DeleteRecorder* d2 = new DeleteRecorder(&deleted2);
|
|
{
|
|
HandleScope scope(i_isolate);
|
|
auto handle = Managed<DeleteRecorder>::New(i_isolate, d1);
|
|
USE(handle);
|
|
}
|
|
i_isolate->RegisterForReleaseAtTeardown(d2, DeleteRecorder::Deleter);
|
|
|
|
isolate->Exit();
|
|
isolate->Dispose();
|
|
CHECK(deleted1);
|
|
CHECK(deleted2);
|
|
}
|