1 // Copyright 2017 The PDFium Authors 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 10 #include "fxjs/js_resources.h" 11 #include "third_party/abseil-cpp/absl/types/optional.h" 12 #include "v8/include/v8-forward.h" 13 14 class CJS_Result { 15 public: 16 // Wrap constructors with static methods so we can apply [[nodiscard]], 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 // Success()27 [[nodiscard]] static CJS_Result Success() { return CJS_Result(); } Success(v8::Local<v8::Value> value)28 [[nodiscard]] static CJS_Result Success(v8::Local<v8::Value> value) { 29 return CJS_Result(value); 30 } Failure(const WideString & str)31 [[nodiscard]] static CJS_Result Failure(const WideString& str) { 32 return CJS_Result(str); 33 } Failure(JSMessage id)34 [[nodiscard]] static CJS_Result Failure(JSMessage id) { 35 return CJS_Result(id); 36 } 37 38 CJS_Result(const CJS_Result&); 39 ~CJS_Result(); 40 HasError()41 bool HasError() const { return error_.has_value(); } Error()42 const WideString& Error() const { return error_.value(); } 43 HasReturn()44 bool HasReturn() const { return !return_.IsEmpty(); } Return()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 absl::optional<WideString> error_; 54 v8::Local<v8::Value> return_; 55 }; 56 57 #endif // FXJS_CJS_RESULT_H_ 58