Commit 857ec798 authored by Clemens Hammacher's avatar Clemens Hammacher Committed by Commit Bot

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/458376Reviewed-by: 's avatarBenedikt Meurer <bmeurer@chromium.org>
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#44012}
parent dfcc4aa3
......@@ -68,10 +68,8 @@ CompilerDispatcherJob::CompilerDispatcherJob(Isolate* isolate,
: status_(CompileJobStatus::kInitial),
isolate_(isolate),
tracer_(tracer),
context_(Handle<Context>::cast(
isolate_->global_handles()->Create(isolate->context()))),
shared_(Handle<SharedFunctionInfo>::cast(
isolate_->global_handles()->Create(*shared))),
context_(isolate_->global_handles()->Create(isolate->context())),
shared_(isolate_->global_handles()->Create(*shared)),
max_stack_size_(max_stack_size),
trace_compiler_dispatcher_jobs_(FLAG_trace_compiler_dispatcher_jobs) {
DCHECK(!shared_->is_toplevel());
......@@ -94,10 +92,8 @@ CompilerDispatcherJob::CompilerDispatcherJob(
: status_(CompileJobStatus::kAnalyzed),
isolate_(isolate),
tracer_(tracer),
context_(Handle<Context>::cast(
isolate_->global_handles()->Create(isolate->context()))),
shared_(Handle<SharedFunctionInfo>::cast(
isolate_->global_handles()->Create(*shared))),
context_(isolate_->global_handles()->Create(isolate->context())),
shared_(isolate_->global_handles()->Create(*shared)),
max_stack_size_(max_stack_size),
parse_info_(new ParseInfo(shared_)),
parse_zone_(parse_zone),
......@@ -158,8 +154,7 @@ void CompilerDispatcherJob::PrepareToParseOnMainThread() {
if (isolate_->heap()->lo_space()->Contains(*source)) {
// We need to globalize the handle to the flattened string here, in
// case it's not referenced from anywhere else.
source_ =
Handle<String>::cast(isolate_->global_handles()->Create(*source));
source_ = isolate_->global_handles()->Create(*source);
DisallowHeapAllocation no_allocation;
String::FlatContent content = source->GetFlatContent();
DCHECK(content.IsFlat());
......@@ -205,8 +200,7 @@ void CompilerDispatcherJob::PrepareToParseOnMainThread() {
->NewExternalStringFromTwoByte(resource)
.ToHandleChecked();
}
wrapper_ =
Handle<String>::cast(isolate_->global_handles()->Create(*wrapper));
wrapper_ = isolate_->global_handles()->Create(*wrapper);
character_stream_.reset(
ScannerStream::For(wrapper_, shared_->start_position() - offset,
......
......@@ -423,8 +423,7 @@ void Debug::Iterate(ObjectVisitor* v) {
DebugInfoListNode::DebugInfoListNode(DebugInfo* debug_info): next_(NULL) {
// Globalize the request debug info object and make it weak.
GlobalHandles* global_handles = debug_info->GetIsolate()->global_handles();
debug_info_ =
Handle<DebugInfo>::cast(global_handles->Create(debug_info)).location();
debug_info_ = global_handles->Create(debug_info).location();
}
......@@ -464,8 +463,7 @@ bool Debug::Load() {
// Fail if no context could be created.
if (context.is_null()) return false;
debug_context_ = Handle<Context>::cast(
isolate_->global_handles()->Create(*context));
debug_context_ = isolate_->global_handles()->Create(*context);
feature_tracker()->Track(DebugFeatureTracker::kActive);
......@@ -2369,7 +2367,7 @@ JavaScriptDebugDelegate::JavaScriptDebugDelegate(Isolate* isolate,
Handle<Object> data)
: LegacyDebugDelegate(isolate) {
GlobalHandles* global_handles = isolate->global_handles();
listener_ = Handle<JSFunction>::cast(global_handles->Create(*listener));
listener_ = global_handles->Create(*listener);
data_ = global_handles->Create(*data);
}
......
......@@ -5,6 +5,8 @@
#ifndef V8_GLOBAL_HANDLES_H_
#define V8_GLOBAL_HANDLES_H_
#include <type_traits>
#include "include/v8.h"
#include "include/v8-profiler.h"
......@@ -52,6 +54,14 @@ class GlobalHandles {
// Creates a new global handle that is alive until Destroy is called.
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
static Handle<Object> CopyGlobal(Object** location);
......
......@@ -1846,8 +1846,7 @@ bool Isolate::OptionalRescheduleException(bool is_bottom_call) {
void Isolate::PushPromise(Handle<JSObject> promise) {
ThreadLocalTop* tltop = thread_local_top();
PromiseOnStack* prev = tltop->promise_on_stack_;
Handle<JSObject> global_promise =
Handle<JSObject>::cast(global_handles()->Create(*promise));
Handle<JSObject> global_promise = global_handles()->Create(*promise);
tltop->promise_on_stack_ = new PromiseOnStack(global_promise, prev);
}
......
......@@ -302,8 +302,7 @@ AllocationTracker::UnresolvedLocation::UnresolvedLocation(
Script* script, int start, FunctionInfo* info)
: start_position_(start),
info_(info) {
script_ = Handle<Script>::cast(
script->GetIsolate()->global_handles()->Create(script));
script_ = script->GetIsolate()->global_handles()->Create(script);
GlobalHandles::MakeWeak(reinterpret_cast<Object**>(script_.location()), this,
&HandleWeakScript, v8::WeakCallbackType::kParameter);
}
......
......@@ -922,8 +922,8 @@ ValueDeserializer::ValueDeserializer(Isolate* isolate,
position_(data.start()),
end_(data.start() + data.length()),
pretenure_(data.length() > kPretenureThreshold ? TENURED : NOT_TENURED),
id_map_(Handle<FixedArray>::cast(isolate->global_handles()->Create(
isolate_->heap()->empty_fixed_array()))) {}
id_map_(isolate->global_handles()->Create(
isolate_->heap()->empty_fixed_array())) {}
ValueDeserializer::~ValueDeserializer() {
GlobalHandles::Destroy(Handle<Object>::cast(id_map_).location());
......@@ -1052,9 +1052,8 @@ bool ValueDeserializer::ReadRawBytes(size_t length, const void** data) {
void ValueDeserializer::TransferArrayBuffer(
uint32_t transfer_id, Handle<JSArrayBuffer> array_buffer) {
if (array_buffer_transfer_map_.is_null()) {
array_buffer_transfer_map_ =
Handle<SeededNumberDictionary>::cast(isolate_->global_handles()->Create(
*SeededNumberDictionary::New(isolate_, 0)));
array_buffer_transfer_map_ = isolate_->global_handles()->Create(
*SeededNumberDictionary::New(isolate_, 0));
}
Handle<SeededNumberDictionary> dictionary =
array_buffer_transfer_map_.ToHandleChecked();
......@@ -1064,8 +1063,8 @@ void ValueDeserializer::TransferArrayBuffer(
not_a_prototype_holder);
if (!new_dictionary.is_identical_to(dictionary)) {
GlobalHandles::Destroy(Handle<Object>::cast(dictionary).location());
array_buffer_transfer_map_ = Handle<SeededNumberDictionary>::cast(
isolate_->global_handles()->Create(*new_dictionary));
array_buffer_transfer_map_ =
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 (!new_array.is_identical_to(id_map_)) {
GlobalHandles::Destroy(Handle<Object>::cast(id_map_).location());
id_map_ = Handle<FixedArray>::cast(
isolate_->global_handles()->Create(*new_array));
id_map_ = isolate_->global_handles()->Create(*new_array);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment