Tom Sepez | 3a6d058 | 2018-08-17 19:28:52 +0000 | [diff] [blame] | 1 | // Copyright 2017 PDFium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
| 7 | #ifndef FXJS_CJS_RESULT_H_ |
| 8 | #define FXJS_CJS_RESULT_H_ |
| 9 | |
Tom Sepez | 3a6d058 | 2018-08-17 19:28:52 +0000 | [diff] [blame] | 10 | #include "fxjs/js_resources.h" |
| 11 | #include "third_party/base/optional.h" |
Lei Zhang | 7015634 | 2018-10-18 19:29:59 +0000 | [diff] [blame] | 12 | #include "v8/include/v8.h" |
Tom Sepez | 3a6d058 | 2018-08-17 19:28:52 +0000 | [diff] [blame] | 13 | |
| 14 | class CJS_Result { |
| 15 | public: |
| 16 | // Wrap constructors with static methods so we can apply WARN_UNUSED_RESULT, |
| 17 | // otherwise we can't catch places where someone mistakenly writes: |
| 18 | // |
| 19 | // if (error) |
| 20 | // CJS_Result(JS_ERROR_CODE); |
| 21 | // |
| 22 | // instead of |
| 23 | // |
| 24 | // if (error) |
| 25 | // return CJS_Result(JS_ERROR_CODE); |
| 26 | // |
| 27 | static CJS_Result Success() WARN_UNUSED_RESULT { return CJS_Result(); } |
| 28 | static CJS_Result Success(v8::Local<v8::Value> value) WARN_UNUSED_RESULT { |
| 29 | return CJS_Result(value); |
| 30 | } |
| 31 | static CJS_Result Failure(const WideString& str) WARN_UNUSED_RESULT { |
| 32 | return CJS_Result(str); |
| 33 | } |
| 34 | static CJS_Result Failure(JSMessage id) WARN_UNUSED_RESULT { |
| 35 | return CJS_Result(id); |
| 36 | } |
| 37 | |
| 38 | CJS_Result(const CJS_Result&); |
| 39 | ~CJS_Result(); |
| 40 | |
| 41 | bool HasError() const { return error_.has_value(); } |
| 42 | WideString Error() const { return error_.value(); } |
| 43 | |
| 44 | bool HasReturn() const { return !return_.IsEmpty(); } |
| 45 | v8::Local<v8::Value> Return() const { return return_; } |
| 46 | |
| 47 | private: |
| 48 | CJS_Result(); // Successful but empty return. |
| 49 | explicit CJS_Result(v8::Local<v8::Value>); // Successful return with value. |
| 50 | explicit CJS_Result(const WideString&); // Error with custom message. |
| 51 | explicit CJS_Result(JSMessage id); // Error with stock message. |
| 52 | |
| 53 | Optional<WideString> error_; |
| 54 | v8::Local<v8::Value> return_; |
| 55 | }; |
| 56 | |
| 57 | #endif // FXJS_CJS_RESULT_H_ |