Commit ac01752a authored by Clemens Hammacher's avatar Clemens Hammacher Committed by Commit Bot

[wasm] [cleanup] Introduce and use base::make_unique

Introduce base::make_unique as a replacement for std::make_unique,
introduced in C++14.
Use it in the wasm code base to construct new objects and wrap them in
a unique_ptr, making the code more compact and readable.

R=ahaas@chromium.org
BUG=v8:6474

Change-Id: I2b9c800edc456021b057f1e69d3c144889b1c9f4
Reviewed-on: https://chromium-review.googlesource.com/529167Reviewed-by: 's avatarAndreas Haas <ahaas@chromium.org>
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#45822}
parent fc826e37
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
// found in the LICENSE file. // found in the LICENSE file.
#include <array> #include <array>
#include <memory>
namespace v8 { namespace v8 {
namespace base { namespace base {
...@@ -43,5 +44,13 @@ constexpr auto make_array(Function f) ...@@ -43,5 +44,13 @@ constexpr auto make_array(Function f)
return detail::make_array_helper<Function, Size - 1>::make_array(f); return detail::make_array_helper<Function, Size - 1>::make_array(f);
} }
// base::make_unique<T>: Construct an object of type T and wrap it in a
// std::unique_ptr.
// Replacement for C++14's std::make_unique.
template <typename T, typename... Args>
std::unique_ptr<T> make_unique(Args&&... args) {
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
} // namespace base } // namespace base
} // namespace v8 } // namespace v8
...@@ -3,18 +3,18 @@ ...@@ -3,18 +3,18 @@
// found in the LICENSE file. // found in the LICENSE file.
#include "src/wasm/module-decoder.h" #include "src/wasm/module-decoder.h"
#include "src/wasm/function-body-decoder-impl.h"
#include "src/base/functional.h" #include "src/base/functional.h"
#include "src/base/platform/platform.h" #include "src/base/platform/platform.h"
#include "src/base/template-utils.h"
#include "src/counters.h" #include "src/counters.h"
#include "src/flags.h" #include "src/flags.h"
#include "src/macro-assembler.h" #include "src/macro-assembler.h"
#include "src/objects-inl.h" #include "src/objects-inl.h"
#include "src/ostreams.h" #include "src/ostreams.h"
#include "src/v8.h" #include "src/v8.h"
#include "src/wasm/decoder.h" #include "src/wasm/decoder.h"
#include "src/wasm/function-body-decoder-impl.h"
#include "src/wasm/wasm-limits.h" #include "src/wasm/wasm-limits.h"
namespace v8 { namespace v8 {
...@@ -273,7 +273,7 @@ class ModuleDecoder : public Decoder { ...@@ -273,7 +273,7 @@ class ModuleDecoder : public Decoder {
void StartDecoding(Isolate* isolate) { void StartDecoding(Isolate* isolate) {
CHECK_NULL(module_); CHECK_NULL(module_);
module_.reset(new WasmModule( module_.reset(new WasmModule(
std::unique_ptr<Zone>(new Zone(isolate->allocator(), "signatures")))); base::make_unique<Zone>(isolate->allocator(), "signatures")));
module_->min_mem_pages = 0; module_->min_mem_pages = 0;
module_->max_mem_pages = 0; module_->max_mem_pages = 0;
module_->mem_export = false; module_->mem_export = false;
...@@ -1302,8 +1302,8 @@ FunctionResult DecodeWasmFunctionInternal(Isolate* isolate, Zone* zone, ...@@ -1302,8 +1302,8 @@ FunctionResult DecodeWasmFunctionInternal(Isolate* isolate, Zone* zone,
->AddSample(static_cast<int>(size)); ->AddSample(static_cast<int>(size));
} }
ModuleDecoder decoder(function_start, function_end, kWasmOrigin); ModuleDecoder decoder(function_start, function_end, kWasmOrigin);
return decoder.DecodeSingleFunction( return decoder.DecodeSingleFunction(zone, module_env,
zone, module_env, std::unique_ptr<WasmFunction>(new WasmFunction())); base::make_unique<WasmFunction>());
} }
} // namespace } // namespace
......
...@@ -4,18 +4,17 @@ ...@@ -4,18 +4,17 @@
#include "src/wasm/streaming-decoder.h" #include "src/wasm/streaming-decoder.h"
#include "src/objects-inl.h" #include "src/base/template-utils.h"
#include "src/handles.h" #include "src/handles.h"
#include "src/objects-inl.h"
#include "src/objects/descriptor-array.h"
#include "src/objects/dictionary.h"
#include "src/wasm/decoder.h" #include "src/wasm/decoder.h"
#include "src/wasm/leb-helper.h" #include "src/wasm/leb-helper.h"
#include "src/wasm/module-decoder.h" #include "src/wasm/module-decoder.h"
#include "src/wasm/wasm-objects.h" #include "src/wasm/wasm-objects.h"
#include "src/wasm/wasm-result.h" #include "src/wasm/wasm-result.h"
#include "src/objects/descriptor-array.h"
#include "src/objects/dictionary.h"
using namespace v8::internal; using namespace v8::internal;
using namespace v8::internal::wasm; using namespace v8::internal::wasm;
...@@ -235,12 +234,12 @@ size_t StreamingDecoder::DecodeVarInt32::ReadBytes( ...@@ -235,12 +234,12 @@ size_t StreamingDecoder::DecodeVarInt32::ReadBytes(
std::unique_ptr<StreamingDecoder::DecodingState> std::unique_ptr<StreamingDecoder::DecodingState>
StreamingDecoder::DecodeVarInt32::Next(StreamingDecoder* streaming) { StreamingDecoder::DecodeVarInt32::Next(StreamingDecoder* streaming) {
if (streaming->decoder()->failed()) { if (streaming->decoder()->failed()) {
return std::unique_ptr<DecodingState>(nullptr); return nullptr;
} }
if (value() > max_value_) { if (value() > max_value_) {
streaming->decoder()->errorf(buffer(), "size > maximum function size: %zu", streaming->decoder()->errorf(buffer(), "size > maximum function size: %zu",
value()); value());
return std::unique_ptr<DecodingState>(nullptr); return nullptr;
} }
return NextWithValue(streaming); return NextWithValue(streaming);
...@@ -271,12 +270,12 @@ void StreamingDecoder::DecodeModuleHeader::CheckHeader(Decoder* decoder) { ...@@ -271,12 +270,12 @@ void StreamingDecoder::DecodeModuleHeader::CheckHeader(Decoder* decoder) {
std::unique_ptr<StreamingDecoder::DecodingState> std::unique_ptr<StreamingDecoder::DecodingState>
StreamingDecoder::DecodeModuleHeader::Next(StreamingDecoder* streaming) { StreamingDecoder::DecodeModuleHeader::Next(StreamingDecoder* streaming) {
CheckHeader(streaming->decoder()); CheckHeader(streaming->decoder());
return std::unique_ptr<DecodingState>(new DecodeSectionID()); return base::make_unique<DecodeSectionID>();
} }
std::unique_ptr<StreamingDecoder::DecodingState> std::unique_ptr<StreamingDecoder::DecodingState>
StreamingDecoder::DecodeSectionID::Next(StreamingDecoder* streaming) { StreamingDecoder::DecodeSectionID::Next(StreamingDecoder* streaming) {
return std::unique_ptr<DecodingState>(new DecodeSectionLength(id())); return base::make_unique<DecodeSectionLength>(id());
} }
std::unique_ptr<StreamingDecoder::DecodingState> std::unique_ptr<StreamingDecoder::DecodingState>
...@@ -287,19 +286,19 @@ StreamingDecoder::DecodeSectionLength::NextWithValue( ...@@ -287,19 +286,19 @@ StreamingDecoder::DecodeSectionLength::NextWithValue(
Vector<const uint8_t>(buffer(), static_cast<int>(bytes_needed()))); Vector<const uint8_t>(buffer(), static_cast<int>(bytes_needed())));
if (value() == 0) { if (value() == 0) {
// There is no payload, we go to the next section immediately. // There is no payload, we go to the next section immediately.
return std::unique_ptr<DecodingState>(new DecodeSectionID()); return base::make_unique<DecodeSectionID>();
} else if (section_id() == SectionCode::kCodeSectionCode) { } else if (section_id() == SectionCode::kCodeSectionCode) {
// We reached the code section. All functions of the code section are put // We reached the code section. All functions of the code section are put
// into the same SectionBuffer. // into the same SectionBuffer.
return std::unique_ptr<DecodingState>(new DecodeNumberOfFunctions(buf)); return base::make_unique<DecodeNumberOfFunctions>(buf);
} else { } else {
return std::unique_ptr<DecodingState>(new DecodeSectionPayload(buf)); return base::make_unique<DecodeSectionPayload>(buf);
} }
} }
std::unique_ptr<StreamingDecoder::DecodingState> std::unique_ptr<StreamingDecoder::DecodingState>
StreamingDecoder::DecodeSectionPayload::Next(StreamingDecoder* streaming) { StreamingDecoder::DecodeSectionPayload::Next(StreamingDecoder* streaming) {
return std::unique_ptr<DecodingState>(new DecodeSectionID()); return base::make_unique<DecodeSectionID>();
} }
std::unique_ptr<StreamingDecoder::DecodingState> std::unique_ptr<StreamingDecoder::DecodingState>
...@@ -311,16 +310,16 @@ StreamingDecoder::DecodeNumberOfFunctions::NextWithValue( ...@@ -311,16 +310,16 @@ StreamingDecoder::DecodeNumberOfFunctions::NextWithValue(
buffer(), bytes_needed()); buffer(), bytes_needed());
} else { } else {
streaming->decoder()->error("Invalid code section length"); streaming->decoder()->error("Invalid code section length");
return std::unique_ptr<DecodingState>(new DecodeSectionID()); return base::make_unique<DecodeSectionID>();
} }
// {value} is the number of functions. // {value} is the number of functions.
if (value() > 0) { if (value() > 0) {
return std::unique_ptr<DecodingState>(new DecodeFunctionLength( return base::make_unique<DecodeFunctionLength>(
section_buffer(), section_buffer()->payload_offset() + bytes_needed(), section_buffer(), section_buffer()->payload_offset() + bytes_needed(),
value())); value());
} else { } else {
return std::unique_ptr<DecodingState>(new DecodeSectionID()); return base::make_unique<DecodeSectionID>();
} }
} }
...@@ -332,30 +331,30 @@ StreamingDecoder::DecodeFunctionLength::NextWithValue( ...@@ -332,30 +331,30 @@ StreamingDecoder::DecodeFunctionLength::NextWithValue(
memcpy(section_buffer_->bytes() + buffer_offset_, buffer(), bytes_needed()); memcpy(section_buffer_->bytes() + buffer_offset_, buffer(), bytes_needed());
} else { } else {
streaming->decoder()->error("Invalid code section length"); streaming->decoder()->error("Invalid code section length");
return std::unique_ptr<DecodingState>(new DecodeSectionID()); return base::make_unique<DecodeSectionID>();
} }
// {value} is the length of the function. // {value} is the length of the function.
if (value() == 0) { if (value() == 0) {
streaming->decoder()->errorf(buffer(), "Invalid function length (0)"); streaming->decoder()->errorf(buffer(), "Invalid function length (0)");
return std::unique_ptr<DecodingState>(nullptr); return nullptr;
} else if (buffer_offset() + bytes_needed() + value() > } else if (buffer_offset() + bytes_needed() + value() >
section_buffer()->length()) { section_buffer()->length()) {
streaming->decoder()->errorf(buffer(), "not enough code section bytes"); streaming->decoder()->errorf(buffer(), "not enough code section bytes");
return std::unique_ptr<DecodingState>(nullptr); return nullptr;
} }
return std::unique_ptr<DecodingState>( return base::make_unique<DecodeFunctionBody>(
new DecodeFunctionBody(section_buffer(), buffer_offset() + bytes_needed(), section_buffer(), buffer_offset() + bytes_needed(), value(),
value(), num_remaining_functions())); num_remaining_functions());
} }
std::unique_ptr<StreamingDecoder::DecodingState> std::unique_ptr<StreamingDecoder::DecodingState>
StreamingDecoder::DecodeFunctionBody::Next(StreamingDecoder* streaming) { StreamingDecoder::DecodeFunctionBody::Next(StreamingDecoder* streaming) {
// TODO(ahaas): Start compilation of the function here. // TODO(ahaas): Start compilation of the function here.
if (num_remaining_functions() != 0) { if (num_remaining_functions() != 0) {
return std::unique_ptr<DecodingState>(new DecodeFunctionLength( return base::make_unique<DecodeFunctionLength>(
section_buffer(), buffer_offset() + size(), num_remaining_functions())); section_buffer(), buffer_offset() + size(), num_remaining_functions());
} else { } else {
if (buffer_offset() + size() != section_buffer()->length()) { if (buffer_offset() + size() != section_buffer()->length()) {
streaming->decoder()->Reset( streaming->decoder()->Reset(
...@@ -364,9 +363,9 @@ StreamingDecoder::DecodeFunctionBody::Next(StreamingDecoder* streaming) { ...@@ -364,9 +363,9 @@ StreamingDecoder::DecodeFunctionBody::Next(StreamingDecoder* streaming) {
streaming->decoder()->errorf( streaming->decoder()->errorf(
section_buffer()->bytes() + buffer_offset() + size(), section_buffer()->bytes() + buffer_offset() + size(),
"not all code section bytes were used"); "not all code section bytes were used");
return std::unique_ptr<DecodingState>(nullptr); return nullptr;
} }
return std::unique_ptr<DecodingState>(new DecodeSectionID()); return base::make_unique<DecodeSectionID>();
} }
} }
......
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