Commit 3ad032b7 authored by Clemens Hammacher's avatar Clemens Hammacher Committed by Commit Bot

[base] Introduce VectorOf helper

We often need to create a {Vector} view of data owned by a container
like {std::vector}. The canonical way to do this is this:
Vector<T>{vec.data(), vec.size()}

This pattern is repeating information which can be deduced
automatically, like the type T.

This CL introduces a {VectorOf} helper which can construct a {Vector}
for any container providing a {data()} and {size()} accessor, and uses
it to replace the pattern above.

R=ishell@chromium.org

Bug: v8:8238
Change-Id: Ib3a11662acc82cb83f2b4afd07ba88e579d71dba
Reviewed-on: https://chromium-review.googlesource.com/c/1337584Reviewed-by: 's avatarIgor Sheludko <ishell@chromium.org>
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#57538}
parent 11a10861
...@@ -308,8 +308,7 @@ void AsmJsScanner::ConsumeNumber(uc32 ch) { ...@@ -308,8 +308,7 @@ void AsmJsScanner::ConsumeNumber(uc32 ch) {
} }
// Decode numbers. // Decode numbers.
double_value_ = StringToDouble( double_value_ = StringToDouble(
Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(number.data()), Vector<const uint8_t>::cast(VectorOf(number)),
static_cast<int>(number.size())),
ALLOW_HEX | ALLOW_OCTAL | ALLOW_BINARY | ALLOW_IMPLICIT_OCTAL); ALLOW_HEX | ALLOW_OCTAL | ALLOW_BINARY | ALLOW_IMPLICIT_OCTAL);
if (std::isnan(double_value_)) { if (std::isnan(double_value_)) {
// Check if string to number conversion didn't consume all the characters. // Check if string to number conversion didn't consume all the characters.
......
...@@ -34,8 +34,7 @@ class VectorSegment { ...@@ -34,8 +34,7 @@ class VectorSegment {
~VectorSegment() { container_.resize(begin_); } ~VectorSegment() { container_.resize(begin_); }
Vector<const value_type> GetVector() const { Vector<const value_type> GetVector() const {
return Vector<const value_type>(container_.data() + begin_, return VectorOf(container_) + begin_;
container_.size() - begin_);
} }
template <typename T> template <typename T>
......
...@@ -307,8 +307,7 @@ MaybeHandle<String> Uri::Encode(Isolate* isolate, Handle<String> uri, ...@@ -307,8 +307,7 @@ MaybeHandle<String> Uri::Encode(Isolate* isolate, Handle<String> uri,
} }
} }
return isolate->factory()->NewStringFromOneByte( return isolate->factory()->NewStringFromOneByte(VectorOf(buffer));
Vector<const uint8_t>(buffer.data(), static_cast<int>(buffer.size())));
} }
namespace { // Anonymous namespace for Escape and Unescape namespace { // Anonymous namespace for Escape and Unescape
......
...@@ -288,6 +288,15 @@ inline constexpr Vector<T> ArrayVector(T (&arr)[N]) { ...@@ -288,6 +288,15 @@ inline constexpr Vector<T> ArrayVector(T (&arr)[N]) {
return Vector<T>(arr); return Vector<T>(arr);
} }
// Construct a Vector from anything providing a {data()} and {size()} accessor.
template <typename Container,
typename T = typename std::remove_reference<
decltype(*(std::declval<Container>()).data())>::type,
typename = decltype((std::declval<Container>()).size())>
inline constexpr Vector<T> VectorOf(Container&& c) {
return Vector<T>(c.data(), c.size());
}
} // namespace internal } // namespace internal
} // namespace v8 } // namespace v8
......
...@@ -127,11 +127,6 @@ struct WasmException; ...@@ -127,11 +127,6 @@ struct WasmException;
V(I64AtomicStore16U, Uint16) \ V(I64AtomicStore16U, Uint16) \
V(I64AtomicStore32U, Uint32) V(I64AtomicStore32U, Uint32)
template <typename T, typename Allocator>
Vector<T> vec2vec(std::vector<T, Allocator>& vec) {
return Vector<T>(vec.data(), vec.size());
}
// Helpers for decoding different kinds of immediates which follow bytecodes. // Helpers for decoding different kinds of immediates which follow bytecodes.
template <Decoder::ValidateFlag validate> template <Decoder::ValidateFlag validate>
struct LocalIndexImmediate { struct LocalIndexImmediate {
...@@ -1491,7 +1486,7 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -1491,7 +1486,7 @@ class WasmFullDecoder : public WasmDecoder<validate> {
len = 1 + imm.length; len = 1 + imm.length;
if (!this->Validate(this->pc_, imm)) break; if (!this->Validate(this->pc_, imm)) break;
PopArgs(imm.exception->ToFunctionSig()); PopArgs(imm.exception->ToFunctionSig());
CALL_INTERFACE_IF_REACHABLE(Throw, imm, vec2vec(args_)); CALL_INTERFACE_IF_REACHABLE(Throw, imm, VectorOf(args_));
EndControl(); EndControl();
break; break;
} }
...@@ -2278,7 +2273,7 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -2278,7 +2273,7 @@ class WasmFullDecoder : public WasmDecoder<validate> {
PopArgs(sig); PopArgs(sig);
auto* results = auto* results =
sig->return_count() == 0 ? nullptr : Push(GetReturnType(sig)); sig->return_count() == 0 ? nullptr : Push(GetReturnType(sig));
CALL_INTERFACE_IF_REACHABLE(SimdOp, opcode, vec2vec(args_), results); CALL_INTERFACE_IF_REACHABLE(SimdOp, opcode, VectorOf(args_), results);
} }
} }
return len; return len;
...@@ -2316,7 +2311,7 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -2316,7 +2311,7 @@ class WasmFullDecoder : public WasmDecoder<validate> {
len += imm.length; len += imm.length;
PopArgs(sig); PopArgs(sig);
auto result = ret_type == kWasmStmt ? nullptr : Push(GetReturnType(sig)); auto result = ret_type == kWasmStmt ? nullptr : Push(GetReturnType(sig));
CALL_INTERFACE_IF_REACHABLE(AtomicOp, opcode, vec2vec(args_), imm, CALL_INTERFACE_IF_REACHABLE(AtomicOp, opcode, VectorOf(args_), imm,
result); result);
} else { } else {
this->error("invalid atomic opcode"); this->error("invalid atomic opcode");
...@@ -2335,7 +2330,7 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -2335,7 +2330,7 @@ class WasmFullDecoder : public WasmDecoder<validate> {
// Simulate that an implicit return morally comes after the current block. // Simulate that an implicit return morally comes after the current block.
if (implicit && c->end_merge.reached) c->reachability = kReachable; if (implicit && c->end_merge.reached) c->reachability = kReachable;
CALL_INTERFACE_IF_REACHABLE(DoReturn, vec2vec(args_), implicit); CALL_INTERFACE_IF_REACHABLE(DoReturn, VectorOf(args_), implicit);
EndControl(); EndControl();
} }
......
...@@ -414,7 +414,7 @@ class WasmGraphBuildingInterface { ...@@ -414,7 +414,7 @@ class WasmGraphBuildingInterface {
for (int i = 0; i < count; ++i) { for (int i = 0; i < count; ++i) {
args[i] = value_args[i].node; args[i] = value_args[i].node;
} }
BUILD(Throw, imm.index, imm.exception, vec2vec(args)); BUILD(Throw, imm.index, imm.exception, VectorOf(args));
builder_->TerminateThrow(ssa_env_->effect, ssa_env_->control); builder_->TerminateThrow(ssa_env_->effect, ssa_env_->control);
} }
......
...@@ -1308,9 +1308,7 @@ MaybeHandle<WasmInstanceObject> InstanceBuilder::Build() { ...@@ -1308,9 +1308,7 @@ MaybeHandle<WasmInstanceObject> InstanceBuilder::Build() {
func_index < num_wasm_functions; ++func_index) { func_index < num_wasm_functions; ++func_index) {
func_indexes.push_back(func_index); func_indexes.push_back(func_index);
} }
WasmDebugInfo::RedirectToInterpreter( WasmDebugInfo::RedirectToInterpreter(debug_info, VectorOf(func_indexes));
debug_info, Vector<int>(func_indexes.data(),
static_cast<int>(func_indexes.size())));
} }
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
......
...@@ -66,8 +66,7 @@ void StreamingDecoder::Finish() { ...@@ -66,8 +66,7 @@ void StreamingDecoder::Finish() {
if (!ok()) return; if (!ok()) return;
if (deserializing()) { if (deserializing()) {
Vector<const uint8_t> wire_bytes(wire_bytes_for_deserializing_.data(), Vector<const uint8_t> wire_bytes = VectorOf(wire_bytes_for_deserializing_);
wire_bytes_for_deserializing_.size());
// Try to deserialize the module from wire bytes and module bytes. // Try to deserialize the module from wire bytes and module bytes.
if (processor_->Deserialize(compiled_module_bytes_, wire_bytes)) return; if (processor_->Deserialize(compiled_module_bytes_, wire_bytes)) return;
......
...@@ -126,9 +126,9 @@ Handle<Object> ErrorThrower::Reify() { ...@@ -126,9 +126,9 @@ Handle<Object> ErrorThrower::Reify() {
constructor = isolate_->wasm_runtime_error_function(); constructor = isolate_->wasm_runtime_error_function();
break; break;
} }
Vector<const char> msg_vec(error_msg_.data(), error_msg_.size()); Handle<String> message = isolate_->factory()
Handle<String> message = ->NewStringFromUtf8(VectorOf(error_msg_))
isolate_->factory()->NewStringFromUtf8(msg_vec).ToHandleChecked(); .ToHandleChecked();
Reset(); Reset();
return isolate_->factory()->NewError(constructor, message); return isolate_->factory()->NewError(constructor, message);
} }
......
...@@ -383,14 +383,14 @@ WasmSerializer::WasmSerializer(Isolate* isolate, NativeModule* native_module) ...@@ -383,14 +383,14 @@ WasmSerializer::WasmSerializer(Isolate* isolate, NativeModule* native_module)
code_table_(native_module->SnapshotCodeTable()) {} code_table_(native_module->SnapshotCodeTable()) {}
size_t WasmSerializer::GetSerializedNativeModuleSize() const { size_t WasmSerializer::GetSerializedNativeModuleSize() const {
Vector<WasmCode* const> code_table(code_table_.data(), code_table_.size()); NativeModuleSerializer serializer(isolate_, native_module_,
NativeModuleSerializer serializer(isolate_, native_module_, code_table); VectorOf(code_table_));
return kVersionSize + serializer.Measure(); return kVersionSize + serializer.Measure();
} }
bool WasmSerializer::SerializeNativeModule(Vector<byte> buffer) const { bool WasmSerializer::SerializeNativeModule(Vector<byte> buffer) const {
Vector<WasmCode* const> code_table(code_table_.data(), code_table_.size()); NativeModuleSerializer serializer(isolate_, native_module_,
NativeModuleSerializer serializer(isolate_, native_module_, code_table); VectorOf(code_table_));
size_t measured_size = kVersionSize + serializer.Measure(); size_t measured_size = kVersionSize + serializer.Measure();
if (buffer.size() < measured_size) return false; if (buffer.size() < measured_size) return false;
......
...@@ -29,8 +29,7 @@ template <size_t kBufferSize> ...@@ -29,8 +29,7 @@ template <size_t kBufferSize>
void DecodeUtf16(unibrow::Utf8Decoder<kBufferSize>* decoder, void DecodeUtf16(unibrow::Utf8Decoder<kBufferSize>* decoder,
const std::vector<byte>& bytes, const std::vector<byte>& bytes,
std::vector<unibrow::uchar>* output) { std::vector<unibrow::uchar>* output) {
const char* bytes_begin = reinterpret_cast<const char*>(&(*bytes.begin())); auto vector = Vector<const char>::cast(VectorOf(bytes));
auto vector = Vector<const char>(bytes_begin, bytes.size());
decoder->Reset(vector); decoder->Reset(vector);
std::vector<uint16_t> utf16(decoder->Utf16Length()); std::vector<uint16_t> utf16(decoder->Utf16Length());
......
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