| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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 XFA_FXJSE_VALUE_H_ | |
| 8 #define XFA_FXJSE_VALUE_H_ | |
| 9 | |
| 10 #include "xfa/fxjse/scope_inline.h" | |
| 11 | |
| 12 V8_INLINE static double FXJSE_ftod(FX_FLOAT fNumber) { | |
| 13 if (sizeof(FX_FLOAT) != 4) { | |
| 14 ASSERT(FALSE); | |
| 15 return fNumber; | |
| 16 } | |
| 17 | |
| 18 uint32_t nFloatBits = (uint32_t&)fNumber; | |
| 19 uint8_t nExponent = (uint8_t)(nFloatBits >> 16 >> 7); | |
| 20 if (nExponent == 0 || nExponent == 255) | |
| 21 return fNumber; | |
| 22 | |
| 23 int8_t nErrExp = nExponent - 127 - 23; | |
| 24 if (nErrExp >= 0) | |
| 25 return fNumber; | |
| 26 | |
| 27 double dwError = pow(2.0, nErrExp), dwErrorHalf = dwError / 2; | |
| 28 double dNumber = fNumber, dNumberAbs = fabs(fNumber); | |
| 29 double dNumberAbsMin = dNumberAbs - dwErrorHalf, | |
| 30 dNumberAbsMax = dNumberAbs + dwErrorHalf; | |
| 31 int32_t iErrPos = 0; | |
| 32 if (floor(dNumberAbsMin) == floor(dNumberAbsMax)) { | |
| 33 dNumberAbsMin = fmod(dNumberAbsMin, 1.0); | |
| 34 dNumberAbsMax = fmod(dNumberAbsMax, 1.0); | |
| 35 int32_t iErrPosMin = 1, iErrPosMax = 38; | |
| 36 do { | |
| 37 int32_t iMid = (iErrPosMin + iErrPosMax) / 2; | |
| 38 double dPow = pow(10.0, iMid); | |
| 39 if (floor(dNumberAbsMin * dPow) == floor(dNumberAbsMax * dPow)) { | |
| 40 iErrPosMin = iMid + 1; | |
| 41 } else { | |
| 42 iErrPosMax = iMid; | |
| 43 } | |
| 44 } while (iErrPosMin < iErrPosMax); | |
| 45 iErrPos = iErrPosMax; | |
| 46 } | |
| 47 double dPow = pow(10.0, iErrPos); | |
| 48 return fNumber < 0 ? ceil(dNumber * dPow - 0.5) / dPow | |
| 49 : floor(dNumber * dPow + 0.5) / dPow; | |
| 50 } | |
| 51 | |
| 52 class CFXJSE_Value { | |
| 53 public: | |
| 54 CFXJSE_Value(v8::Isolate* pIsolate) : m_pIsolate(pIsolate) {} | |
| 55 | |
| 56 V8_INLINE FX_BOOL IsUndefined() const { | |
| 57 if (m_hValue.IsEmpty()) { | |
| 58 return FALSE; | |
| 59 } | |
| 60 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); | |
| 61 v8::Local<v8::Value> hValue = | |
| 62 v8::Local<v8::Value>::New(m_pIsolate, m_hValue); | |
| 63 return hValue->IsUndefined(); | |
| 64 } | |
| 65 V8_INLINE FX_BOOL IsNull() const { | |
| 66 if (m_hValue.IsEmpty()) { | |
| 67 return FALSE; | |
| 68 } | |
| 69 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); | |
| 70 v8::Local<v8::Value> hValue = | |
| 71 v8::Local<v8::Value>::New(m_pIsolate, m_hValue); | |
| 72 return hValue->IsNull(); | |
| 73 } | |
| 74 V8_INLINE FX_BOOL IsBoolean() const { | |
| 75 if (m_hValue.IsEmpty()) { | |
| 76 return FALSE; | |
| 77 } | |
| 78 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); | |
| 79 v8::Local<v8::Value> hValue = | |
| 80 v8::Local<v8::Value>::New(m_pIsolate, m_hValue); | |
| 81 return hValue->IsBoolean(); | |
| 82 } | |
| 83 V8_INLINE FX_BOOL IsString() const { | |
| 84 if (m_hValue.IsEmpty()) { | |
| 85 return FALSE; | |
| 86 } | |
| 87 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); | |
| 88 v8::Local<v8::Value> hValue = | |
| 89 v8::Local<v8::Value>::New(m_pIsolate, m_hValue); | |
| 90 return hValue->IsString(); | |
| 91 } | |
| 92 V8_INLINE FX_BOOL IsNumber() const { | |
| 93 if (m_hValue.IsEmpty()) { | |
| 94 return FALSE; | |
| 95 } | |
| 96 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); | |
| 97 v8::Local<v8::Value> hValue = | |
| 98 v8::Local<v8::Value>::New(m_pIsolate, m_hValue); | |
| 99 return hValue->IsNumber(); | |
| 100 } | |
| 101 V8_INLINE FX_BOOL IsInteger() const { | |
| 102 if (m_hValue.IsEmpty()) { | |
| 103 return FALSE; | |
| 104 } | |
| 105 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); | |
| 106 v8::Local<v8::Value> hValue = | |
| 107 v8::Local<v8::Value>::New(m_pIsolate, m_hValue); | |
| 108 return hValue->IsInt32(); | |
| 109 } | |
| 110 V8_INLINE FX_BOOL IsObject() const { | |
| 111 if (m_hValue.IsEmpty()) { | |
| 112 return FALSE; | |
| 113 } | |
| 114 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); | |
| 115 v8::Local<v8::Value> hValue = | |
| 116 v8::Local<v8::Value>::New(m_pIsolate, m_hValue); | |
| 117 return hValue->IsObject(); | |
| 118 } | |
| 119 V8_INLINE FX_BOOL IsArray() const { | |
| 120 if (m_hValue.IsEmpty()) { | |
| 121 return FALSE; | |
| 122 } | |
| 123 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); | |
| 124 v8::Local<v8::Value> hValue = | |
| 125 v8::Local<v8::Value>::New(m_pIsolate, m_hValue); | |
| 126 return hValue->IsArray(); | |
| 127 } | |
| 128 V8_INLINE FX_BOOL IsFunction() const { | |
| 129 if (m_hValue.IsEmpty()) { | |
| 130 return FALSE; | |
| 131 } | |
| 132 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); | |
| 133 v8::Local<v8::Value> hValue = | |
| 134 v8::Local<v8::Value>::New(m_pIsolate, m_hValue); | |
| 135 return hValue->IsFunction(); | |
| 136 } | |
| 137 V8_INLINE FX_BOOL IsDate() const { | |
| 138 if (m_hValue.IsEmpty()) { | |
| 139 return FALSE; | |
| 140 } | |
| 141 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); | |
| 142 v8::Local<v8::Value> hValue = | |
| 143 v8::Local<v8::Value>::New(m_pIsolate, m_hValue); | |
| 144 return hValue->IsDate(); | |
| 145 } | |
| 146 | |
| 147 V8_INLINE FX_BOOL ToBoolean() const { | |
| 148 ASSERT(!m_hValue.IsEmpty()); | |
| 149 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); | |
| 150 v8::Local<v8::Value> hValue = | |
| 151 v8::Local<v8::Value>::New(m_pIsolate, m_hValue); | |
| 152 return static_cast<FX_BOOL>(hValue->BooleanValue()); | |
| 153 } | |
| 154 V8_INLINE FX_FLOAT ToFloat() const { | |
| 155 ASSERT(!m_hValue.IsEmpty()); | |
| 156 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); | |
| 157 v8::Local<v8::Value> hValue = | |
| 158 v8::Local<v8::Value>::New(m_pIsolate, m_hValue); | |
| 159 return static_cast<FX_FLOAT>(hValue->NumberValue()); | |
| 160 } | |
| 161 V8_INLINE double ToDouble() const { | |
| 162 ASSERT(!m_hValue.IsEmpty()); | |
| 163 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); | |
| 164 v8::Local<v8::Value> hValue = | |
| 165 v8::Local<v8::Value>::New(m_pIsolate, m_hValue); | |
| 166 return static_cast<double>(hValue->NumberValue()); | |
| 167 } | |
| 168 V8_INLINE int32_t ToInteger() const { | |
| 169 ASSERT(!m_hValue.IsEmpty()); | |
| 170 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); | |
| 171 v8::Local<v8::Value> hValue = | |
| 172 v8::Local<v8::Value>::New(m_pIsolate, m_hValue); | |
| 173 return static_cast<int32_t>(hValue->NumberValue()); | |
| 174 } | |
| 175 V8_INLINE CFX_ByteString ToString() const { | |
| 176 ASSERT(!m_hValue.IsEmpty()); | |
| 177 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); | |
| 178 v8::Local<v8::Value> hValue = | |
| 179 v8::Local<v8::Value>::New(m_pIsolate, m_hValue); | |
| 180 v8::Local<v8::String> hString = hValue->ToString(); | |
| 181 v8::String::Utf8Value hStringVal(hString); | |
| 182 return CFX_ByteString(*hStringVal); | |
| 183 } | |
| 184 V8_INLINE CFX_WideString ToWideString() const { | |
| 185 return CFX_WideString::FromUTF8(ToString().AsStringC()); | |
| 186 } | |
| 187 CFXJSE_HostObject* ToHostObject(CFXJSE_Class* lpClass) const; | |
| 188 | |
| 189 V8_INLINE void SetUndefined() { | |
| 190 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); | |
| 191 v8::Local<v8::Value> hValue = v8::Undefined(m_pIsolate); | |
| 192 m_hValue.Reset(m_pIsolate, hValue); | |
| 193 } | |
| 194 V8_INLINE void SetNull() { | |
| 195 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); | |
| 196 v8::Local<v8::Value> hValue = v8::Null(m_pIsolate); | |
| 197 m_hValue.Reset(m_pIsolate, hValue); | |
| 198 } | |
| 199 V8_INLINE void SetBoolean(FX_BOOL bBoolean) { | |
| 200 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); | |
| 201 v8::Local<v8::Value> hValue = | |
| 202 v8::Boolean::New(m_pIsolate, bBoolean != FALSE); | |
| 203 m_hValue.Reset(m_pIsolate, hValue); | |
| 204 } | |
| 205 V8_INLINE void SetInteger(int32_t nInteger) { | |
| 206 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); | |
| 207 v8::Local<v8::Value> hValue = v8::Integer::New(m_pIsolate, nInteger); | |
| 208 m_hValue.Reset(m_pIsolate, hValue); | |
| 209 } | |
| 210 V8_INLINE void SetDouble(double dDouble) { | |
| 211 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); | |
| 212 v8::Local<v8::Value> hValue = v8::Number::New(m_pIsolate, dDouble); | |
| 213 m_hValue.Reset(m_pIsolate, hValue); | |
| 214 } | |
| 215 V8_INLINE void SetString(const CFX_ByteStringC& szString) { | |
| 216 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); | |
| 217 v8::Local<v8::Value> hValue = v8::String::NewFromUtf8( | |
| 218 m_pIsolate, reinterpret_cast<const char*>(szString.raw_str()), | |
| 219 v8::String::kNormalString, szString.GetLength()); | |
| 220 m_hValue.Reset(m_pIsolate, hValue); | |
| 221 } | |
| 222 V8_INLINE void SetFloat(FX_FLOAT fFloat) { | |
| 223 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); | |
| 224 v8::Local<v8::Value> pValue = | |
| 225 v8::Number::New(m_pIsolate, FXJSE_ftod(fFloat)); | |
| 226 m_hValue.Reset(m_pIsolate, pValue); | |
| 227 } | |
| 228 V8_INLINE void SetJSObject() { | |
| 229 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); | |
| 230 v8::Local<v8::Value> hValue = v8::Object::New(m_pIsolate); | |
| 231 m_hValue.Reset(m_pIsolate, hValue); | |
| 232 } | |
| 233 | |
| 234 void SetObject(CFXJSE_HostObject* lpObject, CFXJSE_Class* pClass); | |
| 235 void SetHostObject(CFXJSE_HostObject* lpObject, CFXJSE_Class* lpClass); | |
| 236 void SetArray(uint32_t uValueCount, CFXJSE_Value** rgValues); | |
| 237 void SetDate(double dDouble); | |
| 238 | |
| 239 FX_BOOL GetObjectProperty(const CFX_ByteStringC& szPropName, | |
| 240 CFXJSE_Value* lpPropValue); | |
| 241 FX_BOOL SetObjectProperty(const CFX_ByteStringC& szPropName, | |
| 242 CFXJSE_Value* lpPropValue); | |
| 243 FX_BOOL GetObjectPropertyByIdx(uint32_t uPropIdx, CFXJSE_Value* lpPropValue); | |
| 244 FX_BOOL SetObjectProperty(uint32_t uPropIdx, CFXJSE_Value* lpPropValue); | |
| 245 FX_BOOL DeleteObjectProperty(const CFX_ByteStringC& szPropName); | |
| 246 FX_BOOL HasObjectOwnProperty(const CFX_ByteStringC& szPropName, | |
| 247 FX_BOOL bUseTypeGetter); | |
| 248 FX_BOOL SetObjectOwnProperty(const CFX_ByteStringC& szPropName, | |
| 249 CFXJSE_Value* lpPropValue); | |
| 250 FX_BOOL SetFunctionBind(CFXJSE_Value* lpOldFunction, CFXJSE_Value* lpNewThis); | |
| 251 FX_BOOL Call(CFXJSE_Value* lpReceiver, | |
| 252 CFXJSE_Value* lpRetValue, | |
| 253 uint32_t nArgCount, | |
| 254 CFXJSE_Value** lpArgs); | |
| 255 | |
| 256 V8_INLINE v8::Isolate* GetIsolate() const { return m_pIsolate; } | |
| 257 V8_INLINE const v8::Global<v8::Value>& DirectGetValue() const { | |
| 258 return m_hValue; | |
| 259 } | |
| 260 V8_INLINE void ForceSetValue(v8::Local<v8::Value> hValue) { | |
| 261 m_hValue.Reset(m_pIsolate, hValue); | |
| 262 } | |
| 263 V8_INLINE void Assign(const CFXJSE_Value* lpValue) { | |
| 264 ASSERT(lpValue); | |
| 265 if (lpValue) { | |
| 266 m_hValue.Reset(m_pIsolate, lpValue->m_hValue); | |
| 267 } else { | |
| 268 m_hValue.Reset(); | |
| 269 } | |
| 270 } | |
| 271 | |
| 272 private: | |
| 273 friend class CFXJSE_Class; | |
| 274 friend class CFXJSE_Context; | |
| 275 | |
| 276 CFXJSE_Value(); | |
| 277 CFXJSE_Value(const CFXJSE_Value&); | |
| 278 CFXJSE_Value& operator=(const CFXJSE_Value&); | |
| 279 | |
| 280 v8::Isolate* m_pIsolate; | |
| 281 v8::Global<v8::Value> m_hValue; | |
| 282 }; | |
| 283 | |
| 284 #endif // XFA_FXJSE_VALUE_H_ | |
| OLD | NEW |