OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #ifndef XFA_FXJSE_VALUE_H_ | 7 #ifndef XFA_FXJSE_VALUE_H_ |
8 #define XFA_FXJSE_VALUE_H_ | 8 #define XFA_FXJSE_VALUE_H_ |
9 | 9 |
10 #include "xfa/fxjse/scope_inline.h" | 10 #include "xfa/fxjse/scope_inline.h" |
11 | 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 |
12 class CFXJSE_Value { | 52 class CFXJSE_Value { |
13 public: | 53 public: |
14 CFXJSE_Value(v8::Isolate* pIsolate) : m_pIsolate(pIsolate) {} | 54 CFXJSE_Value(v8::Isolate* pIsolate) : m_pIsolate(pIsolate) {} |
15 | 55 |
16 V8_INLINE FX_BOOL IsUndefined() const { | 56 V8_INLINE FX_BOOL IsUndefined() const { |
17 if (m_hValue.IsEmpty()) { | 57 if (m_hValue.IsEmpty()) { |
18 return FALSE; | 58 return FALSE; |
19 } | 59 } |
20 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); | 60 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); |
21 v8::Local<v8::Value> hValue = | 61 v8::Local<v8::Value> hValue = |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 v8::Local<v8::Value> hValue = v8::Number::New(m_pIsolate, dDouble); | 209 v8::Local<v8::Value> hValue = v8::Number::New(m_pIsolate, dDouble); |
170 m_hValue.Reset(m_pIsolate, hValue); | 210 m_hValue.Reset(m_pIsolate, hValue); |
171 } | 211 } |
172 V8_INLINE void SetString(const CFX_ByteStringC& szString) { | 212 V8_INLINE void SetString(const CFX_ByteStringC& szString) { |
173 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); | 213 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); |
174 v8::Local<v8::Value> hValue = v8::String::NewFromUtf8( | 214 v8::Local<v8::Value> hValue = v8::String::NewFromUtf8( |
175 m_pIsolate, reinterpret_cast<const char*>(szString.raw_str()), | 215 m_pIsolate, reinterpret_cast<const char*>(szString.raw_str()), |
176 v8::String::kNormalString, szString.GetLength()); | 216 v8::String::kNormalString, szString.GetLength()); |
177 m_hValue.Reset(m_pIsolate, hValue); | 217 m_hValue.Reset(m_pIsolate, hValue); |
178 } | 218 } |
179 V8_INLINE void SetFloat(FX_FLOAT fFloat); | 219 V8_INLINE void SetFloat(FX_FLOAT fFloat) { |
| 220 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); |
| 221 v8::Local<v8::Value> pValue = |
| 222 v8::Number::New(m_pIsolate, FXJSE_ftod(fFloat)); |
| 223 m_hValue.Reset(m_pIsolate, pValue); |
| 224 } |
180 V8_INLINE void SetJSObject() { | 225 V8_INLINE void SetJSObject() { |
181 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); | 226 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); |
182 v8::Local<v8::Value> hValue = v8::Object::New(m_pIsolate); | 227 v8::Local<v8::Value> hValue = v8::Object::New(m_pIsolate); |
183 m_hValue.Reset(m_pIsolate, hValue); | 228 m_hValue.Reset(m_pIsolate, hValue); |
184 } | 229 } |
| 230 |
| 231 void SetObject(CFXJSE_HostObject* lpObject, CFXJSE_Class* pClass); |
185 void SetHostObject(CFXJSE_HostObject* lpObject, CFXJSE_Class* lpClass); | 232 void SetHostObject(CFXJSE_HostObject* lpObject, CFXJSE_Class* lpClass); |
186 void SetArray(uint32_t uValueCount, CFXJSE_Value** rgValues); | 233 void SetArray(uint32_t uValueCount, CFXJSE_Value** rgValues); |
187 void SetDate(double dDouble); | 234 void SetDate(double dDouble); |
188 | 235 |
189 FX_BOOL GetObjectProperty(const CFX_ByteStringC& szPropName, | 236 FX_BOOL GetObjectProperty(const CFX_ByteStringC& szPropName, |
190 CFXJSE_Value* lpPropValue); | 237 CFXJSE_Value* lpPropValue); |
191 FX_BOOL SetObjectProperty(const CFX_ByteStringC& szPropName, | 238 FX_BOOL SetObjectProperty(const CFX_ByteStringC& szPropName, |
192 CFXJSE_Value* lpPropValue); | 239 CFXJSE_Value* lpPropValue); |
193 FX_BOOL GetObjectProperty(uint32_t uPropIdx, CFXJSE_Value* lpPropValue); | 240 FX_BOOL GetObjectPropertyByIdx(uint32_t uPropIdx, CFXJSE_Value* lpPropValue); |
194 FX_BOOL SetObjectProperty(uint32_t uPropIdx, CFXJSE_Value* lpPropValue); | 241 FX_BOOL SetObjectProperty(uint32_t uPropIdx, CFXJSE_Value* lpPropValue); |
195 FX_BOOL DeleteObjectProperty(const CFX_ByteStringC& szPropName); | 242 FX_BOOL DeleteObjectProperty(const CFX_ByteStringC& szPropName); |
196 FX_BOOL HasObjectOwnProperty(const CFX_ByteStringC& szPropName, | 243 FX_BOOL HasObjectOwnProperty(const CFX_ByteStringC& szPropName, |
197 FX_BOOL bUseTypeGetter); | 244 FX_BOOL bUseTypeGetter); |
198 FX_BOOL SetObjectOwnProperty(const CFX_ByteStringC& szPropName, | 245 FX_BOOL SetObjectOwnProperty(const CFX_ByteStringC& szPropName, |
199 CFXJSE_Value* lpPropValue); | 246 CFXJSE_Value* lpPropValue); |
200 FX_BOOL SetFunctionBind(CFXJSE_Value* lpOldFunction, CFXJSE_Value* lpNewThis); | 247 FX_BOOL SetFunctionBind(CFXJSE_Value* lpOldFunction, CFXJSE_Value* lpNewThis); |
201 FX_BOOL Call(CFXJSE_Value* lpReceiver, | 248 FX_BOOL Call(CFXJSE_Value* lpReceiver, |
202 CFXJSE_Value* lpRetValue, | 249 CFXJSE_Value* lpRetValue, |
203 uint32_t nArgCount, | 250 uint32_t nArgCount, |
204 CFXJSE_Value** lpArgs); | 251 CFXJSE_Value** lpArgs); |
205 | 252 |
206 V8_INLINE v8::Isolate* GetIsolate() const { return m_pIsolate; } | 253 V8_INLINE v8::Isolate* GetIsolate() const { return m_pIsolate; } |
207 V8_INLINE const v8::Global<v8::Value>& DirectGetValue() const { | 254 V8_INLINE const v8::Global<v8::Value>& DirectGetValue() const { |
208 return m_hValue; | 255 return m_hValue; |
209 } | 256 } |
210 V8_INLINE void ForceSetValue(v8::Local<v8::Value> hValue) { | 257 V8_INLINE void ForceSetValue(v8::Local<v8::Value> hValue) { |
211 m_hValue.Reset(m_pIsolate, hValue); | 258 m_hValue.Reset(m_pIsolate, hValue); |
212 } | 259 } |
213 V8_INLINE void Assign(const CFXJSE_Value* lpValue) { | 260 V8_INLINE void Assign(const CFXJSE_Value* lpValue) { |
| 261 ASSERT(lpValue); |
214 if (lpValue) { | 262 if (lpValue) { |
215 m_hValue.Reset(m_pIsolate, lpValue->m_hValue); | 263 m_hValue.Reset(m_pIsolate, lpValue->m_hValue); |
216 } else { | 264 } else { |
217 m_hValue.Reset(); | 265 m_hValue.Reset(); |
218 } | 266 } |
219 } | 267 } |
220 | 268 |
221 private: | 269 private: |
222 friend class CFXJSE_Class; | 270 friend class CFXJSE_Class; |
223 friend class CFXJSE_Context; | 271 friend class CFXJSE_Context; |
224 | 272 |
225 CFXJSE_Value(); | 273 CFXJSE_Value(); |
226 CFXJSE_Value(const CFXJSE_Value&); | 274 CFXJSE_Value(const CFXJSE_Value&); |
227 CFXJSE_Value& operator=(const CFXJSE_Value&); | 275 CFXJSE_Value& operator=(const CFXJSE_Value&); |
228 | 276 |
229 v8::Isolate* m_pIsolate; | 277 v8::Isolate* m_pIsolate; |
230 v8::Global<v8::Value> m_hValue; | 278 v8::Global<v8::Value> m_hValue; |
231 }; | 279 }; |
232 | 280 |
233 #endif // XFA_FXJSE_VALUE_H_ | 281 #endif // XFA_FXJSE_VALUE_H_ |
OLD | NEW |