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 {
}
// Converts the given value to a {Result}, copying the error if necessary.
template <typename T, typename U = typename std::remove_reference<T>::type>
Result<U> toResult(T&& val) {
template <typename T>
Result<T> toResult(T&& val) {
if (failed()) {
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.
......
......@@ -1610,7 +1610,7 @@ class ModuleDecoderImpl : public Decoder {
if (FLAG_dump_wasm_module) DumpModule(orig_bytes);
if (decoder.failed()) {
return decoder.toResult<std::unique_ptr<WasmModule>>(nullptr);
return decoder.toResult<std::shared_ptr<WasmModule>>(nullptr);
}
return FinishDecoding(verify_functions);
......@@ -1619,23 +1619,22 @@ class ModuleDecoderImpl : public Decoder {
// Decodes a single anonymous function starting at {start_}.
FunctionResult DecodeSingleFunction(Zone* zone,
const ModuleWireBytes& wire_bytes,
const WasmModule* module,
std::unique_ptr<WasmFunction> function) {
const WasmModule* module) {
pc_ = start_;
expect_u8("type form", kWasmFunctionTypeCode);
if (!ok()) return FunctionResult{std::move(intermediate_error_)};
function->sig = consume_sig(zone);
function->code = {off(pc_), static_cast<uint32_t>(end_ - pc_)};
WasmFunction function;
function.sig = consume_sig(zone);
function.code = {off(pc_), static_cast<uint32_t>(end_ - pc_)};
if (ok())
VerifyFunctionBody(zone->allocator(), 0, wire_bytes, module,
function.get());
VerifyFunctionBody(zone->allocator(), 0, wire_bytes, module, &function);
if (intermediate_error_.has_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}.
......@@ -2447,8 +2446,7 @@ FunctionResult DecodeWasmFunctionForTesting(
}
ModuleDecoderImpl decoder(enabled, function_start, function_end, kWasmOrigin);
decoder.SetCounters(counters);
return decoder.DecodeSingleFunction(zone, wire_bytes, module,
std::make_unique<WasmFunction>());
return decoder.DecodeSingleFunction(zone, wire_bytes, module);
}
AsmJsOffsetsResult DecodeAsmJsOffsets(
......
......@@ -66,26 +66,18 @@ class V8_EXPORT_PRIVATE WasmError {
template <typename T>
class Result {
public:
static_assert(!std::is_same<T, WasmError>::value);
Result() = default;
Result(const Result&) = delete;
Result& operator=(const Result&) = delete;
template <typename S>
explicit Result(S&& value) : value_(std::forward<S>(value)) {}
Result& operator=(const Result<T>&) = delete;
Result(Result<T>&&) = default;
Result& operator=(Result<T>&&) = default;
template <typename S>
Result(Result<S>&& other) V8_NOEXCEPT : value_(std::move(other.value_)),
error_(std::move(other.error_)) {}
explicit Result(T&& value) : value_(std::forward<T>(value)) {}
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 failed() const { return error_.has_error(); }
const WasmError& error() const& { return error_; }
......@@ -105,9 +97,6 @@ class Result {
}
private:
template <typename S>
friend class Result;
T value_ = T{};
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