blob: 5e1ed7cee9d766ed47da4f002399556766877834 [file] [log] [blame]
Tom Sepez3a6d0582018-08-17 19:28:52 +00001// 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 Sepez3a6d0582018-08-17 19:28:52 +000010#include "fxjs/js_resources.h"
11#include "third_party/base/optional.h"
Lei Zhang70156342018-10-18 19:29:59 +000012#include "v8/include/v8.h"
Tom Sepez3a6d0582018-08-17 19:28:52 +000013
14class 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_