diff options
Diffstat (limited to 'deps/v8/src/wasm/wasm-result.h')
-rw-r--r-- | deps/v8/src/wasm/wasm-result.h | 138 |
1 files changed, 79 insertions, 59 deletions
diff --git a/deps/v8/src/wasm/wasm-result.h b/deps/v8/src/wasm/wasm-result.h index 79d06758b1..848170c80e 100644 --- a/deps/v8/src/wasm/wasm-result.h +++ b/deps/v8/src/wasm/wasm-result.h @@ -21,38 +21,18 @@ class Isolate; namespace wasm { -// The overall result of decoding a function or a module. -template <typename T> -class Result { - public: - Result() = default; +// Base class for Result<T>. +class V8_EXPORT_PRIVATE ResultBase { + protected: + ResultBase(ResultBase&& other) + : error_offset_(other.error_offset_), + error_msg_(std::move(other.error_msg_)) {} + ResultBase() = default; - template <typename S> - explicit Result(S&& value) : val(value) {} + ResultBase& operator=(ResultBase&& other) = default; - template <typename S> - Result(Result<S>&& other) - : val(std::move(other.val)), - error_offset(other.error_offset), - error_msg(std::move(other.error_msg)) {} - - Result& operator=(Result&& other) = default; - - T val = T{}; - uint32_t error_offset = 0; - std::string error_msg; - - bool ok() const { return error_msg.empty(); } - bool failed() const { return !ok(); } - - template <typename V> - void MoveErrorFrom(Result<V>& that) { - error_offset = that.error_offset; - // Use {swap()} + {clear()} instead of move assign, as {that} might still be - // used afterwards. - error_msg.swap(that.error_msg); - that.error_msg.clear(); - } + public: + void error(uint32_t offset, std::string error_msg); void PRINTF_FORMAT(2, 3) error(const char* format, ...) { va_list args; @@ -61,22 +41,42 @@ class Result { va_end(args); } - void PRINTF_FORMAT(2, 0) verror(const char* format, va_list args) { - size_t len = base::bits::RoundUpToPowerOfTwo32( - static_cast<uint32_t>(strlen(format))); - // Allocate increasingly large buffers until the message fits. - for (;; len *= 2) { - DCHECK_GE(kMaxInt, len); - error_msg.resize(len); - int written = - VSNPrintF(Vector<char>(&error_msg.front(), static_cast<int>(len)), - format, args); - if (written < 0) continue; // not enough space. - if (written == 0) error_msg = "Error"; // assign default message. - return; - } + void PRINTF_FORMAT(2, 0) verror(const char* format, va_list args); + + void MoveErrorFrom(ResultBase& that) { + error_offset_ = that.error_offset_; + // Use {swap()} + {clear()} instead of move assign, as {that} might still + // be used afterwards. + error_msg_.swap(that.error_msg_); + that.error_msg_.clear(); } + bool ok() const { return error_msg_.empty(); } + bool failed() const { return !ok(); } + + uint32_t error_offset() const { return error_offset_; } + const std::string& error_msg() const { return error_msg_; } + + private: + uint32_t error_offset_ = 0; + std::string error_msg_; +}; + +// The overall result of decoding a function or a module. +template <typename T> +class Result : public ResultBase { + public: + Result() = default; + + template <typename S> + explicit Result(S&& value) : val(std::forward<S>(value)) {} + + template <typename S> + Result(Result<S>&& other) + : ResultBase(std::move(other)), val(std::move(other.val)) {} + + Result& operator=(Result&& other) = default; + static Result<T> PRINTF_FORMAT(1, 2) Error(const char* format, ...) { va_list args; va_start(args, format); @@ -86,6 +86,8 @@ class Result { return result; } + T val = T{}; + private: DISALLOW_COPY_AND_ASSIGN(Result); }; @@ -93,8 +95,10 @@ class Result { // A helper for generating error messages that bubble up to JS exceptions. class V8_EXPORT_PRIVATE ErrorThrower { public: - ErrorThrower(i::Isolate* isolate, const char* context) + ErrorThrower(Isolate* isolate, const char* context) : isolate_(isolate), context_(context) {} + // Explicitly allow move-construction. Disallow copy (below). + ErrorThrower(ErrorThrower&& other); ~ErrorThrower(); PRINTF_FORMAT(2, 3) void TypeError(const char* fmt, ...); @@ -106,26 +110,42 @@ class V8_EXPORT_PRIVATE ErrorThrower { template <typename T> void CompileFailed(const char* error, Result<T>& result) { DCHECK(result.failed()); - CompileError("%s: %s @+%u", error, result.error_msg.c_str(), - result.error_offset); + CompileError("%s: %s @+%u", error, result.error_msg().c_str(), + result.error_offset()); } - i::Handle<i::Object> Reify() { - i::Handle<i::Object> result = exception_; - exception_ = i::Handle<i::Object>::null(); - return result; - } + // Create and return exception object. + MUST_USE_RESULT Handle<Object> Reify(); - bool error() const { return !exception_.is_null(); } - bool wasm_error() { return wasm_error_; } + // Reset any error which was set on this thrower. + void Reset(); - private: - void Format(i::Handle<i::JSFunction> constructor, const char* fmt, va_list); + bool error() const { return error_type_ != kNone; } + bool wasm_error() { return error_type_ >= kFirstWasmError; } - i::Isolate* isolate_; + private: + enum ErrorType { + kNone, + // General errors. + kTypeError, + kRangeError, + // Wasm errors. + kCompileError, + kLinkError, + kRuntimeError, + + // Marker. + kFirstWasmError = kCompileError + }; + + void Format(ErrorType error_type_, const char* fmt, va_list); + + Isolate* isolate_; const char* context_; - i::Handle<i::Object> exception_; - bool wasm_error_ = false; + ErrorType error_type_ = kNone; + std::string error_msg_; + + DISALLOW_COPY_AND_ASSIGN(ErrorThrower); }; } // namespace wasm |