summaryrefslogtreecommitdiff
path: root/xfa/fxjse/value.h
blob: d74fc325dd967913c7ee1161f4c3825f9635a53b (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Copyright 2014 PDFium Authors. All rights reserved.
// 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 XFA_FXJSE_VALUE_H_
#define XFA_FXJSE_VALUE_H_

#include "xfa/fxjse/scope_inline.h"

V8_INLINE static double FXJSE_ftod(FX_FLOAT fNumber) {
  if (sizeof(FX_FLOAT) != 4) {
    ASSERT(FALSE);
    return fNumber;
  }

  uint32_t nFloatBits = (uint32_t&)fNumber;
  uint8_t nExponent = (uint8_t)(nFloatBits >> 16 >> 7);
  if (nExponent == 0 || nExponent == 255)
    return fNumber;

  int8_t nErrExp = nExponent - 127 - 23;
  if (nErrExp >= 0)
    return fNumber;

  double dwError = pow(2.0, nErrExp), dwErrorHalf = dwError / 2;
  double dNumber = fNumber, dNumberAbs = fabs(fNumber);
  double dNumberAbsMin = dNumberAbs - dwErrorHalf,
         dNumberAbsMax = dNumberAbs + dwErrorHalf;
  int32_t iErrPos = 0;
  if (floor(dNumberAbsMin) == floor(dNumberAbsMax)) {
    dNumberAbsMin = fmod(dNumberAbsMin, 1.0);
    dNumberAbsMax = fmod(dNumberAbsMax, 1.0);
    int32_t iErrPosMin = 1, iErrPosMax = 38;
    do {
      int32_t iMid = (iErrPosMin + iErrPosMax) / 2;
      double dPow = pow(10.0, iMid);
      if (floor(dNumberAbsMin * dPow) == floor(dNumberAbsMax * dPow)) {
        iErrPosMin = iMid + 1;
      } else {
        iErrPosMax = iMid;
      }
    } while (iErrPosMin < iErrPosMax);
    iErrPos = iErrPosMax;
  }
  double dPow = pow(10.0, iErrPos);
  return fNumber < 0 ? ceil(dNumber * dPow - 0.5) / dPow
                     : floor(dNumber * dPow + 0.5) / dPow;
}

class CFXJSE_Value {
 public:
  CFXJSE_Value(v8::Isolate* pIsolate) : m_pIsolate(pIsolate) {}

  V8_INLINE FX_BOOL IsUndefined() const {
    if (m_hValue.IsEmpty()) {
      return FALSE;
    }
    CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
    v8::Local<v8::Value> hValue =
        v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
    return hValue->IsUndefined();
  }
  V8_INLINE FX_BOOL IsNull() const {
    if (m_hValue.IsEmpty()) {
      return FALSE;
    }
    CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
    v8::Local<v8::Value> hValue =
        v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
    return hValue->IsNull();
  }
  V8_INLINE FX_BOOL IsBoolean() const {
    if (m_hValue.IsEmpty()) {
      return FALSE;
    }
    CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
    v8::Local<v8::Value> hValue =
        v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
    return hValue->IsBoolean();
  }
  V8_INLINE FX_BOOL IsString() const {
    if (m_hValue.IsEmpty()) {
      return FALSE;
    }
    CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
    v8::Local<v8::Value> hValue =
        v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
    return hValue->IsString();
  }
  V8_INLINE FX_BOOL IsNumber() const {
    if (m_hValue.IsEmpty()) {
      return FALSE;
    }
    CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
    v8::Local<v8::Value> hValue =
        v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
    return hValue->IsNumber();
  }
  V8_INLINE FX_BOOL IsInteger() const {
    if (m_hValue.IsEmpty()) {
      return FALSE;
    }
    CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
    v8::Local<v8::Value> hValue =
        v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
    return hValue->IsInt32();
  }
  V8_INLINE FX_BOOL IsObject() const {
    if (m_hValue.IsEmpty()) {
      return FALSE;
    }
    CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
    v8::Local<v8::Value> hValue =
        v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
    return hValue->IsObject();
  }
  V8_INLINE FX_BOOL IsArray() const {
    if (m_hValue.IsEmpty()) {
      return FALSE;
    }
    CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
    v8::Local<v8::Value> hValue =
        v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
    return hValue->IsArray();
  }
  V8_INLINE FX_BOOL IsFunction() const {
    if (m_hValue.IsEmpty()) {
      return FALSE;
    }
    CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
    v8::Local<v8::Value> hValue =
        v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
    return hValue->IsFunction();
  }
  V8_INLINE FX_BOOL IsDate() const {
    if (m_hValue.IsEmpty()) {
      return FALSE;
    }
    CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
    v8::Local<v8::Value> hValue =
        v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
    return hValue->IsDate();
  }

  V8_INLINE FX_BOOL ToBoolean() const {
    ASSERT(!m_hValue.IsEmpty());
    CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
    v8::Local<v8::Value> hValue =
        v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
    return static_cast<FX_BOOL>(hValue->BooleanValue());
  }
  V8_INLINE FX_FLOAT ToFloat() const {
    ASSERT(!m_hValue.IsEmpty());
    CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
    v8::Local<v8::Value> hValue =
        v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
    return static_cast<FX_FLOAT>(hValue->NumberValue());
  }
  V8_INLINE double ToDouble() const {
    ASSERT(!m_hValue.IsEmpty());
    CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
    v8::Local<v8::Value> hValue =
        v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
    return static_cast<double>(hValue->NumberValue());
  }
  V8_INLINE int32_t ToInteger() const {
    ASSERT(!m_hValue.IsEmpty());
    CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
    v8::Local<v8::Value> hValue =
        v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
    return static_cast<int32_t>(hValue->NumberValue());
  }
  V8_INLINE CFX_ByteString ToString() const {
    ASSERT(!m_hValue.IsEmpty());
    CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
    v8::Local<v8::Value> hValue =
        v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
    v8::Local<v8::String> hString = hValue->ToString();
    v8::String::Utf8Value hStringVal(hString);
    return CFX_ByteString(*hStringVal);
  }
  V8_INLINE CFX_WideString ToWideString() const {
    return CFX_WideString::FromUTF8(ToString().AsStringC());
  }
  CFXJSE_HostObject* ToHostObject(CFXJSE_Class* lpClass) const;

  V8_INLINE void SetUndefined() {
    CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
    v8::Local<v8::Value> hValue = v8::Undefined(m_pIsolate);
    m_hValue.Reset(m_pIsolate, hValue);
  }
  V8_INLINE void SetNull() {
    CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
    v8::Local<v8::Value> hValue = v8::Null(m_pIsolate);
    m_hValue.Reset(m_pIsolate, hValue);
  }
  V8_INLINE void SetBoolean(FX_BOOL bBoolean) {
    CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
    v8::Local<v8::Value> hValue =
        v8::Boolean::New(m_pIsolate, bBoolean != FALSE);
    m_hValue.Reset(m_pIsolate, hValue);
  }
  V8_INLINE void SetInteger(int32_t nInteger) {
    CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
    v8::Local<v8::Value> hValue = v8::Integer::New(m_pIsolate, nInteger);
    m_hValue.Reset(m_pIsolate, hValue);
  }
  V8_INLINE void SetDouble(double dDouble) {
    CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
    v8::Local<v8::Value> hValue = v8::Number::New(m_pIsolate, dDouble);
    m_hValue.Reset(m_pIsolate, hValue);
  }
  V8_INLINE void SetString(const CFX_ByteStringC& szString) {
    CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
    v8::Local<v8::Value> hValue = v8::String::NewFromUtf8(
        m_pIsolate, reinterpret_cast<const char*>(szString.raw_str()),
        v8::String::kNormalString, szString.GetLength());
    m_hValue.Reset(m_pIsolate, hValue);
  }
  V8_INLINE void SetFloat(FX_FLOAT fFloat) {
    CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
    v8::Local<v8::Value> pValue =
        v8::Number::New(m_pIsolate, FXJSE_ftod(fFloat));
    m_hValue.Reset(m_pIsolate, pValue);
  }
  V8_INLINE void SetJSObject() {
    CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
    v8::Local<v8::Value> hValue = v8::Object::New(m_pIsolate);
    m_hValue.Reset(m_pIsolate, hValue);
  }

  void SetObject(CFXJSE_HostObject* lpObject, CFXJSE_Class* pClass);
  void SetHostObject(CFXJSE_HostObject* lpObject, CFXJSE_Class* lpClass);
  void SetArray(uint32_t uValueCount, CFXJSE_Value** rgValues);
  void SetDate(double dDouble);

  FX_BOOL GetObjectProperty(const CFX_ByteStringC& szPropName,
                            CFXJSE_Value* lpPropValue);
  FX_BOOL SetObjectProperty(const CFX_ByteStringC& szPropName,
                            CFXJSE_Value* lpPropValue);
  FX_BOOL GetObjectPropertyByIdx(uint32_t uPropIdx, CFXJSE_Value* lpPropValue);
  FX_BOOL SetObjectProperty(uint32_t uPropIdx, CFXJSE_Value* lpPropValue);
  FX_BOOL DeleteObjectProperty(const CFX_ByteStringC& szPropName);
  FX_BOOL HasObjectOwnProperty(const CFX_ByteStringC& szPropName,
                               FX_BOOL bUseTypeGetter);
  FX_BOOL SetObjectOwnProperty(const CFX_ByteStringC& szPropName,
                               CFXJSE_Value* lpPropValue);
  FX_BOOL SetFunctionBind(CFXJSE_Value* lpOldFunction, CFXJSE_Value* lpNewThis);
  FX_BOOL Call(CFXJSE_Value* lpReceiver,
               CFXJSE_Value* lpRetValue,
               uint32_t nArgCount,
               CFXJSE_Value** lpArgs);

  V8_INLINE v8::Isolate* GetIsolate() const { return m_pIsolate; }
  V8_INLINE const v8::Global<v8::Value>& DirectGetValue() const {
    return m_hValue;
  }
  V8_INLINE void ForceSetValue(v8::Local<v8::Value> hValue) {
    m_hValue.Reset(m_pIsolate, hValue);
  }
  V8_INLINE void Assign(const CFXJSE_Value* lpValue) {
    ASSERT(lpValue);
    if (lpValue) {
      m_hValue.Reset(m_pIsolate, lpValue->m_hValue);
    } else {
      m_hValue.Reset();
    }
  }

 private:
  friend class CFXJSE_Class;
  friend class CFXJSE_Context;

  CFXJSE_Value();
  CFXJSE_Value(const CFXJSE_Value&);
  CFXJSE_Value& operator=(const CFXJSE_Value&);

  v8::Isolate* m_pIsolate;
  v8::Global<v8::Value> m_hValue;
};

#endif  // XFA_FXJSE_VALUE_H_