aboutsummaryrefslogtreecommitdiff
path: root/fxjs/xfa/cfxjse_value.h
blob: b8b66879bf3dadb6bce022421d08706adfe0e1d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2014 The PDFium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com

#ifndef FXJS_XFA_CFXJSE_VALUE_H_
#define FXJS_XFA_CFXJSE_VALUE_H_

#include <stdint.h>

#include <memory>
#include <vector>

#include "core/fxcrt/fx_string.h"
#include "core/fxcrt/unowned_ptr.h"
#include "third_party/base/check.h"
#include "v8/include/v8-forward.h"
#include "v8/include/v8-persistent-handle.h"

class CFXJSE_Class;
class CFXJSE_HostObject;

class CFXJSE_Value {
 public:
  CFXJSE_Value();
  CFXJSE_Value(v8::Isolate* pIsolate, v8::Local<v8::Value> value);
  ~CFXJSE_Value();

  bool IsEmpty() const;
  bool IsUndefined(v8::Isolate* pIsolate) const;
  bool IsNull(v8::Isolate* pIsolate) const;
  bool IsBoolean(v8::Isolate* pIsolate) const;
  bool IsString(v8::Isolate* pIsolate) const;
  bool IsNumber(v8::Isolate* pIsolate) const;
  bool IsInteger(v8::Isolate* pIsolate) const;
  bool IsObject(v8::Isolate* pIsolate) const;
  bool IsArray(v8::Isolate* pIsolate) const;
  bool IsFunction(v8::Isolate* pIsolate) const;
  bool ToBoolean(v8::Isolate* pIsolate) const;
  float ToFloat(v8::Isolate* pIsolate) const;
  double ToDouble(v8::Isolate* pIsolate) const;
  int32_t ToInteger(v8::Isolate* pIsolate) const;
  ByteString ToString(v8::Isolate* pIsolate) const;
  WideString ToWideString(v8::Isolate* pIsolate) const {
    return WideString::FromUTF8(ToString(pIsolate).AsStringView());
  }
  CFXJSE_HostObject* ToHostObject(v8::Isolate* pIsolate) const;

  void SetUndefined(v8::Isolate* pIsolate);
  void SetNull(v8::Isolate* pIsolate);
  void SetBoolean(v8::Isolate* pIsolate, bool bBoolean);
  void SetInteger(v8::Isolate* pIsolate, int32_t nInteger);
  void SetDouble(v8::Isolate* pIsolate, double dDouble);
  void SetString(v8::Isolate* pIsolate, ByteStringView szString);
  void SetFloat(v8::Isolate* pIsolate, float fFloat);

  void SetHostObject(v8::Isolate* pIsolate,
                     CFXJSE_HostObject* pObject,
                     CFXJSE_Class* pClass);

  void SetArray(v8::Isolate* pIsolate,
                const std::vector<std::unique_ptr<CFXJSE_Value>>& values);

  bool GetObjectProperty(v8::Isolate* pIsolate,
                         ByteStringView szPropName,
                         CFXJSE_Value* pPropValue);
  bool SetObjectProperty(v8::Isolate* pIsolate,
                         ByteStringView szPropName,
                         CFXJSE_Value* pPropValue);
  bool GetObjectPropertyByIdx(v8::Isolate* pIsolate,
                              uint32_t uPropIdx,
                              CFXJSE_Value* pPropValue);
  void DeleteObjectProperty(v8::Isolate* pIsolate, ByteStringView szPropName);
  bool SetObjectOwnProperty(v8::Isolate* pIsolate,
                            ByteStringView szPropName,
                            CFXJSE_Value* pPropValue);

  // Return empty local on error.
  static v8::Local<v8::Function> NewBoundFunction(
      v8::Isolate* pIsolate,
      v8::Local<v8::Function> hOldFunction,
      v8::Local<v8::Object> lpNewThis);

  v8::Local<v8::Value> GetValue(v8::Isolate* pIsolate) const;
  const v8::Global<v8::Value>& DirectGetValue() const { return m_hValue; }
  void ForceSetValue(v8::Isolate* pIsolate, v8::Local<v8::Value> hValue) {
    m_hValue.Reset(pIsolate, hValue);
  }

 private:
  CFXJSE_Value(const CFXJSE_Value&) = delete;
  CFXJSE_Value& operator=(const CFXJSE_Value&) = delete;

  v8::Global<v8::Value> m_hValue;
};

#endif  // FXJS_XFA_CFXJSE_VALUE_H_