Commit 9d642b11 authored by Manos Koukoutos's avatar Manos Koukoutos Committed by V8 LUCI CQ

[wasm] Simplify {Result}

Change-Id: I6e84533581917afe90796265c563868fa1ab4448
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3734810
Commit-Queue: Manos Koukoutos <manoskouk@chromium.org>
Reviewed-by: 's avatarAndreas Haas <ahaas@chromium.org>
Cr-Commit-Position: refs/heads/main@{#81464}
parent 8d496bed
...@@ -311,13 +311,13 @@ class Decoder { ...@@ -311,13 +311,13 @@ class Decoder {
} }
// Converts the given value to a {Result}, copying the error if necessary. // Converts the given value to a {Result}, copying the error if necessary.
template <typename T, typename U = typename std::remove_reference<T>::type> template <typename T>
Result<U> toResult(T&& val) { Result<T> toResult(T&& val) {
if (failed()) { if (failed()) {
TRACE("Result error: %s\n", error_.message().c_str()); TRACE("Result error: %s\n", error_.message().c_str());
return Result<U>{error_}; return Result<T>(error_);
} }
return Result<U>{std::forward<T>(val)}; return Result<T>(std::move(val));
} }
// Resets the boundaries of this decoder. // Resets the boundaries of this decoder.
......
...@@ -1610,7 +1610,7 @@ class ModuleDecoderImpl : public Decoder { ...@@ -1610,7 +1610,7 @@ class ModuleDecoderImpl : public Decoder {
if (FLAG_dump_wasm_module) DumpModule(orig_bytes); if (FLAG_dump_wasm_module) DumpModule(orig_bytes);
if (decoder.failed()) { if (decoder.failed()) {
return decoder.toResult<std::unique_ptr<WasmModule>>(nullptr); return decoder.toResult<std::shared_ptr<WasmModule>>(nullptr);
} }
return FinishDecoding(verify_functions); return FinishDecoding(verify_functions);
...@@ -1619,23 +1619,22 @@ class ModuleDecoderImpl : public Decoder { ...@@ -1619,23 +1619,22 @@ class ModuleDecoderImpl : public Decoder {
// Decodes a single anonymous function starting at {start_}. // Decodes a single anonymous function starting at {start_}.
FunctionResult DecodeSingleFunction(Zone* zone, FunctionResult DecodeSingleFunction(Zone* zone,
const ModuleWireBytes& wire_bytes, const ModuleWireBytes& wire_bytes,
const WasmModule* module, const WasmModule* module) {
std::unique_ptr<WasmFunction> function) {
pc_ = start_; pc_ = start_;
expect_u8("type form", kWasmFunctionTypeCode); expect_u8("type form", kWasmFunctionTypeCode);
if (!ok()) return FunctionResult{std::move(intermediate_error_)}; if (!ok()) return FunctionResult{std::move(intermediate_error_)};
function->sig = consume_sig(zone); WasmFunction function;
function->code = {off(pc_), static_cast<uint32_t>(end_ - pc_)}; function.sig = consume_sig(zone);
function.code = {off(pc_), static_cast<uint32_t>(end_ - pc_)};
if (ok()) if (ok())
VerifyFunctionBody(zone->allocator(), 0, wire_bytes, module, VerifyFunctionBody(zone->allocator(), 0, wire_bytes, module, &function);
function.get());
if (intermediate_error_.has_error()) { if (intermediate_error_.has_error()) {
return FunctionResult{std::move(intermediate_error_)}; return FunctionResult{std::move(intermediate_error_)};
} }
return FunctionResult(std::move(function)); return FunctionResult{std::make_unique<WasmFunction>(function)};
} }
// Decodes a single function signature at {start}. // Decodes a single function signature at {start}.
...@@ -2447,8 +2446,7 @@ FunctionResult DecodeWasmFunctionForTesting( ...@@ -2447,8 +2446,7 @@ FunctionResult DecodeWasmFunctionForTesting(
} }
ModuleDecoderImpl decoder(enabled, function_start, function_end, kWasmOrigin); ModuleDecoderImpl decoder(enabled, function_start, function_end, kWasmOrigin);
decoder.SetCounters(counters); decoder.SetCounters(counters);
return decoder.DecodeSingleFunction(zone, wire_bytes, module, return decoder.DecodeSingleFunction(zone, wire_bytes, module);
std::make_unique<WasmFunction>());
} }
AsmJsOffsetsResult DecodeAsmJsOffsets( AsmJsOffsetsResult DecodeAsmJsOffsets(
......
...@@ -66,26 +66,18 @@ class V8_EXPORT_PRIVATE WasmError { ...@@ -66,26 +66,18 @@ class V8_EXPORT_PRIVATE WasmError {
template <typename T> template <typename T>
class Result { class Result {
public: public:
static_assert(!std::is_same<T, WasmError>::value);
Result() = default; Result() = default;
Result(const Result&) = delete; Result(const Result&) = delete;
Result& operator=(const Result&) = delete; Result& operator=(const Result<T>&) = delete;
Result(Result<T>&&) = default;
template <typename S> Result& operator=(Result<T>&&) = default;
explicit Result(S&& value) : value_(std::forward<S>(value)) {}
template <typename S> explicit Result(T&& value) : value_(std::forward<T>(value)) {}
Result(Result<S>&& other) V8_NOEXCEPT : value_(std::move(other.value_)),
error_(std::move(other.error_)) {}
explicit Result(WasmError error) : error_(std::move(error)) {} explicit Result(WasmError error) : error_(std::move(error)) {}
template <typename S>
Result& operator=(Result<S>&& other) V8_NOEXCEPT {
value_ = std::move(other.value_);
error_ = std::move(other.error_);
return *this;
}
bool ok() const { return error_.empty(); } bool ok() const { return error_.empty(); }
bool failed() const { return error_.has_error(); } bool failed() const { return error_.has_error(); }
const WasmError& error() const& { return error_; } const WasmError& error() const& { return error_; }
...@@ -105,9 +97,6 @@ class Result { ...@@ -105,9 +97,6 @@ class Result {
} }
private: private:
template <typename S>
friend class Result;
T value_ = T{}; T value_ = T{};
WasmError error_; WasmError error_;
}; };
......
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