Add templatized GlobalHandles::Create method

The old method always returned a Handle<Object>, requiring an explicit
cast in the caller. This CL makes it return Handle<T> if called with a
T* as parameter.

Also, remove now redundant casts from callers.

R=bmeurer@chromium.org

Change-Id: I13cfb2f2e812e8582a9a1d9d6c8a5a24f40d0e79
Reviewed-on: https://chromium-review.googlesource.com/458376
Reviewed-by: Benedikt Meurer <bmeurer@chromium.org>
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#44012}
This commit is contained in:
Clemens Hammacher 2017-03-22 10:19:25 +01:00 committed by Commit Bot
parent dfcc4aa34a
commit 857ec7980b
6 changed files with 28 additions and 30 deletions

View File

@ -68,10 +68,8 @@ CompilerDispatcherJob::CompilerDispatcherJob(Isolate* isolate,
: status_(CompileJobStatus::kInitial), : status_(CompileJobStatus::kInitial),
isolate_(isolate), isolate_(isolate),
tracer_(tracer), tracer_(tracer),
context_(Handle<Context>::cast( context_(isolate_->global_handles()->Create(isolate->context())),
isolate_->global_handles()->Create(isolate->context()))), shared_(isolate_->global_handles()->Create(*shared)),
shared_(Handle<SharedFunctionInfo>::cast(
isolate_->global_handles()->Create(*shared))),
max_stack_size_(max_stack_size), max_stack_size_(max_stack_size),
trace_compiler_dispatcher_jobs_(FLAG_trace_compiler_dispatcher_jobs) { trace_compiler_dispatcher_jobs_(FLAG_trace_compiler_dispatcher_jobs) {
DCHECK(!shared_->is_toplevel()); DCHECK(!shared_->is_toplevel());
@ -94,10 +92,8 @@ CompilerDispatcherJob::CompilerDispatcherJob(
: status_(CompileJobStatus::kAnalyzed), : status_(CompileJobStatus::kAnalyzed),
isolate_(isolate), isolate_(isolate),
tracer_(tracer), tracer_(tracer),
context_(Handle<Context>::cast( context_(isolate_->global_handles()->Create(isolate->context())),
isolate_->global_handles()->Create(isolate->context()))), shared_(isolate_->global_handles()->Create(*shared)),
shared_(Handle<SharedFunctionInfo>::cast(
isolate_->global_handles()->Create(*shared))),
max_stack_size_(max_stack_size), max_stack_size_(max_stack_size),
parse_info_(new ParseInfo(shared_)), parse_info_(new ParseInfo(shared_)),
parse_zone_(parse_zone), parse_zone_(parse_zone),
@ -158,8 +154,7 @@ void CompilerDispatcherJob::PrepareToParseOnMainThread() {
if (isolate_->heap()->lo_space()->Contains(*source)) { if (isolate_->heap()->lo_space()->Contains(*source)) {
// We need to globalize the handle to the flattened string here, in // We need to globalize the handle to the flattened string here, in
// case it's not referenced from anywhere else. // case it's not referenced from anywhere else.
source_ = source_ = isolate_->global_handles()->Create(*source);
Handle<String>::cast(isolate_->global_handles()->Create(*source));
DisallowHeapAllocation no_allocation; DisallowHeapAllocation no_allocation;
String::FlatContent content = source->GetFlatContent(); String::FlatContent content = source->GetFlatContent();
DCHECK(content.IsFlat()); DCHECK(content.IsFlat());
@ -205,8 +200,7 @@ void CompilerDispatcherJob::PrepareToParseOnMainThread() {
->NewExternalStringFromTwoByte(resource) ->NewExternalStringFromTwoByte(resource)
.ToHandleChecked(); .ToHandleChecked();
} }
wrapper_ = wrapper_ = isolate_->global_handles()->Create(*wrapper);
Handle<String>::cast(isolate_->global_handles()->Create(*wrapper));
character_stream_.reset( character_stream_.reset(
ScannerStream::For(wrapper_, shared_->start_position() - offset, ScannerStream::For(wrapper_, shared_->start_position() - offset,

View File

@ -423,8 +423,7 @@ void Debug::Iterate(ObjectVisitor* v) {
DebugInfoListNode::DebugInfoListNode(DebugInfo* debug_info): next_(NULL) { DebugInfoListNode::DebugInfoListNode(DebugInfo* debug_info): next_(NULL) {
// Globalize the request debug info object and make it weak. // Globalize the request debug info object and make it weak.
GlobalHandles* global_handles = debug_info->GetIsolate()->global_handles(); GlobalHandles* global_handles = debug_info->GetIsolate()->global_handles();
debug_info_ = debug_info_ = global_handles->Create(debug_info).location();
Handle<DebugInfo>::cast(global_handles->Create(debug_info)).location();
} }
@ -464,8 +463,7 @@ bool Debug::Load() {
// Fail if no context could be created. // Fail if no context could be created.
if (context.is_null()) return false; if (context.is_null()) return false;
debug_context_ = Handle<Context>::cast( debug_context_ = isolate_->global_handles()->Create(*context);
isolate_->global_handles()->Create(*context));
feature_tracker()->Track(DebugFeatureTracker::kActive); feature_tracker()->Track(DebugFeatureTracker::kActive);
@ -2369,7 +2367,7 @@ JavaScriptDebugDelegate::JavaScriptDebugDelegate(Isolate* isolate,
Handle<Object> data) Handle<Object> data)
: LegacyDebugDelegate(isolate) { : LegacyDebugDelegate(isolate) {
GlobalHandles* global_handles = isolate->global_handles(); GlobalHandles* global_handles = isolate->global_handles();
listener_ = Handle<JSFunction>::cast(global_handles->Create(*listener)); listener_ = global_handles->Create(*listener);
data_ = global_handles->Create(*data); data_ = global_handles->Create(*data);
} }

View File

@ -5,6 +5,8 @@
#ifndef V8_GLOBAL_HANDLES_H_ #ifndef V8_GLOBAL_HANDLES_H_
#define V8_GLOBAL_HANDLES_H_ #define V8_GLOBAL_HANDLES_H_
#include <type_traits>
#include "include/v8.h" #include "include/v8.h"
#include "include/v8-profiler.h" #include "include/v8-profiler.h"
@ -52,6 +54,14 @@ class GlobalHandles {
// Creates a new global handle that is alive until Destroy is called. // Creates a new global handle that is alive until Destroy is called.
Handle<Object> Create(Object* value); Handle<Object> Create(Object* value);
template <typename T>
Handle<T> Create(T* value) {
static_assert(std::is_base_of<Object, T>::value, "static type violation");
// The compiler should only pick this method if T is not Object.
static_assert(!std::is_same<Object, T>::value, "compiler error");
return Handle<T>::cast(Create(static_cast<Object*>(value)));
}
// Copy a global handle // Copy a global handle
static Handle<Object> CopyGlobal(Object** location); static Handle<Object> CopyGlobal(Object** location);

View File

@ -1846,8 +1846,7 @@ bool Isolate::OptionalRescheduleException(bool is_bottom_call) {
void Isolate::PushPromise(Handle<JSObject> promise) { void Isolate::PushPromise(Handle<JSObject> promise) {
ThreadLocalTop* tltop = thread_local_top(); ThreadLocalTop* tltop = thread_local_top();
PromiseOnStack* prev = tltop->promise_on_stack_; PromiseOnStack* prev = tltop->promise_on_stack_;
Handle<JSObject> global_promise = Handle<JSObject> global_promise = global_handles()->Create(*promise);
Handle<JSObject>::cast(global_handles()->Create(*promise));
tltop->promise_on_stack_ = new PromiseOnStack(global_promise, prev); tltop->promise_on_stack_ = new PromiseOnStack(global_promise, prev);
} }

View File

@ -302,8 +302,7 @@ AllocationTracker::UnresolvedLocation::UnresolvedLocation(
Script* script, int start, FunctionInfo* info) Script* script, int start, FunctionInfo* info)
: start_position_(start), : start_position_(start),
info_(info) { info_(info) {
script_ = Handle<Script>::cast( script_ = script->GetIsolate()->global_handles()->Create(script);
script->GetIsolate()->global_handles()->Create(script));
GlobalHandles::MakeWeak(reinterpret_cast<Object**>(script_.location()), this, GlobalHandles::MakeWeak(reinterpret_cast<Object**>(script_.location()), this,
&HandleWeakScript, v8::WeakCallbackType::kParameter); &HandleWeakScript, v8::WeakCallbackType::kParameter);
} }

View File

@ -922,8 +922,8 @@ ValueDeserializer::ValueDeserializer(Isolate* isolate,
position_(data.start()), position_(data.start()),
end_(data.start() + data.length()), end_(data.start() + data.length()),
pretenure_(data.length() > kPretenureThreshold ? TENURED : NOT_TENURED), pretenure_(data.length() > kPretenureThreshold ? TENURED : NOT_TENURED),
id_map_(Handle<FixedArray>::cast(isolate->global_handles()->Create( id_map_(isolate->global_handles()->Create(
isolate_->heap()->empty_fixed_array()))) {} isolate_->heap()->empty_fixed_array())) {}
ValueDeserializer::~ValueDeserializer() { ValueDeserializer::~ValueDeserializer() {
GlobalHandles::Destroy(Handle<Object>::cast(id_map_).location()); GlobalHandles::Destroy(Handle<Object>::cast(id_map_).location());
@ -1052,9 +1052,8 @@ bool ValueDeserializer::ReadRawBytes(size_t length, const void** data) {
void ValueDeserializer::TransferArrayBuffer( void ValueDeserializer::TransferArrayBuffer(
uint32_t transfer_id, Handle<JSArrayBuffer> array_buffer) { uint32_t transfer_id, Handle<JSArrayBuffer> array_buffer) {
if (array_buffer_transfer_map_.is_null()) { if (array_buffer_transfer_map_.is_null()) {
array_buffer_transfer_map_ = array_buffer_transfer_map_ = isolate_->global_handles()->Create(
Handle<SeededNumberDictionary>::cast(isolate_->global_handles()->Create( *SeededNumberDictionary::New(isolate_, 0));
*SeededNumberDictionary::New(isolate_, 0)));
} }
Handle<SeededNumberDictionary> dictionary = Handle<SeededNumberDictionary> dictionary =
array_buffer_transfer_map_.ToHandleChecked(); array_buffer_transfer_map_.ToHandleChecked();
@ -1064,8 +1063,8 @@ void ValueDeserializer::TransferArrayBuffer(
not_a_prototype_holder); not_a_prototype_holder);
if (!new_dictionary.is_identical_to(dictionary)) { if (!new_dictionary.is_identical_to(dictionary)) {
GlobalHandles::Destroy(Handle<Object>::cast(dictionary).location()); GlobalHandles::Destroy(Handle<Object>::cast(dictionary).location());
array_buffer_transfer_map_ = Handle<SeededNumberDictionary>::cast( array_buffer_transfer_map_ =
isolate_->global_handles()->Create(*new_dictionary)); isolate_->global_handles()->Create(*new_dictionary);
} }
} }
@ -1877,8 +1876,7 @@ void ValueDeserializer::AddObjectWithID(uint32_t id,
// If the dictionary was reallocated, update the global handle. // If the dictionary was reallocated, update the global handle.
if (!new_array.is_identical_to(id_map_)) { if (!new_array.is_identical_to(id_map_)) {
GlobalHandles::Destroy(Handle<Object>::cast(id_map_).location()); GlobalHandles::Destroy(Handle<Object>::cast(id_map_).location());
id_map_ = Handle<FixedArray>::cast( id_map_ = isolate_->global_handles()->Create(*new_array);
isolate_->global_handles()->Create(*new_array));
} }
} }