| 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 #include "JS_Value.h" | 7 #include "JS_Value.h" |
| 8 | 8 |
| 9 #include <time.h> | 9 #include <time.h> |
| 10 #include <cmath> | 10 #include <cmath> |
| 11 #include <limits> | 11 #include <limits> |
| 12 | 12 |
| 13 #include "Document.h" | 13 #include "Document.h" |
| 14 #include "JS_Define.h" | 14 #include "JS_Define.h" |
| 15 #include "JS_Object.h" | 15 #include "JS_Object.h" |
| 16 | 16 |
| 17 static const FX_DWORD g_nan[2] = {0, 0x7FF80000}; | 17 static const FX_DWORD g_nan[2] = {0, 0x7FF80000}; |
| 18 static double GetNan() { | 18 static double GetNan() { |
| 19 return *(double*)g_nan; | 19 return *(double*)g_nan; |
| 20 } | 20 } |
| 21 | 21 |
| 22 /* ---------------------------- CJS_Value ---------------------------- */ | |
| 23 | |
| 24 CJS_Value::CJS_Value(CJS_Runtime* pRuntime) | 22 CJS_Value::CJS_Value(CJS_Runtime* pRuntime) |
| 25 : m_eType(VT_unknown), m_pJSRuntime(pRuntime) { | 23 : m_eType(VT_unknown), m_pJSRuntime(pRuntime) { |
| 26 } | 24 } |
| 27 | 25 |
| 28 CJS_Value::CJS_Value(CJS_Runtime* pRuntime, v8::Local<v8::Value> pValue, Type t) | 26 CJS_Value::CJS_Value(CJS_Runtime* pRuntime, v8::Local<v8::Value> pValue, Type t) |
| 29 : m_eType(t), m_pValue(pValue), m_pJSRuntime(pRuntime) { | 27 : m_eType(t), m_pValue(pValue), m_pJSRuntime(pRuntime) { |
| 30 } | 28 } |
| 31 | 29 |
| 32 CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const int& iValue) | 30 CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const int& iValue) |
| 33 : m_pJSRuntime(pRuntime) { | 31 : m_pJSRuntime(pRuntime) { |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 void CJS_Value::Attach(CJS_Value* pValue) { | 89 void CJS_Value::Attach(CJS_Value* pValue) { |
| 92 if (pValue) | 90 if (pValue) |
| 93 Attach(pValue->ToV8Value(), pValue->GetType()); | 91 Attach(pValue->ToV8Value(), pValue->GetType()); |
| 94 } | 92 } |
| 95 | 93 |
| 96 void CJS_Value::Detach() { | 94 void CJS_Value::Detach() { |
| 97 m_pValue = v8::Local<v8::Value>(); | 95 m_pValue = v8::Local<v8::Value>(); |
| 98 m_eType = VT_unknown; | 96 m_eType = VT_unknown; |
| 99 } | 97 } |
| 100 | 98 |
| 101 /* -----------------------------------------------------------------------------
----------- | |
| 102 */ | |
| 103 | |
| 104 int CJS_Value::ToInt() const { | 99 int CJS_Value::ToInt() const { |
| 105 return FXJS_ToInt32(m_pJSRuntime->GetIsolate(), m_pValue); | 100 return FXJS_ToInt32(m_pJSRuntime->GetIsolate(), m_pValue); |
| 106 } | 101 } |
| 107 | 102 |
| 108 bool CJS_Value::ToBool() const { | 103 bool CJS_Value::ToBool() const { |
| 109 return FXJS_ToBoolean(m_pJSRuntime->GetIsolate(), m_pValue); | 104 return FXJS_ToBoolean(m_pJSRuntime->GetIsolate(), m_pValue); |
| 110 } | 105 } |
| 111 | 106 |
| 112 double CJS_Value::ToDouble() const { | 107 double CJS_Value::ToDouble() const { |
| 113 return FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pValue); | 108 return FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pValue); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 139 return m_pValue; | 134 return m_pValue; |
| 140 } | 135 } |
| 141 | 136 |
| 142 v8::Local<v8::Array> CJS_Value::ToV8Array() const { | 137 v8::Local<v8::Array> CJS_Value::ToV8Array() const { |
| 143 if (IsArrayObject()) | 138 if (IsArrayObject()) |
| 144 return v8::Local<v8::Array>::Cast( | 139 return v8::Local<v8::Array>::Cast( |
| 145 FXJS_ToObject(m_pJSRuntime->GetIsolate(), m_pValue)); | 140 FXJS_ToObject(m_pJSRuntime->GetIsolate(), m_pValue)); |
| 146 return v8::Local<v8::Array>(); | 141 return v8::Local<v8::Array>(); |
| 147 } | 142 } |
| 148 | 143 |
| 149 /* -----------------------------------------------------------------------------
----------- | 144 void CJS_Value::MaybeCoerceToNumber() { |
| 150 */ | 145 if (m_eType == VT_string && ToCFXByteString().GetLength() == 0) |
| 146 return; |
| 147 v8::TryCatch(m_pJSRuntime->GetIsolate()); |
| 148 v8::MaybeLocal<v8::Number> maybeNum = |
| 149 m_pValue->ToNumber(m_pJSRuntime->GetIsolate()->GetCurrentContext()); |
| 150 if (maybeNum.IsEmpty()) |
| 151 return; |
| 152 v8::Local<v8::Number> num = maybeNum.ToLocalChecked(); |
| 153 if (std::isnan(num->Value())) |
| 154 return; |
| 155 m_pValue = num; |
| 156 m_eType = VT_number; |
| 157 } |
| 151 | 158 |
| 152 void CJS_Value::operator=(int iValue) { | 159 void CJS_Value::operator=(int iValue) { |
| 153 m_pValue = FXJS_NewNumber(m_pJSRuntime->GetIsolate(), iValue); | 160 m_pValue = FXJS_NewNumber(m_pJSRuntime->GetIsolate(), iValue); |
| 154 m_eType = VT_number; | 161 m_eType = VT_number; |
| 155 } | 162 } |
| 156 | 163 |
| 157 void CJS_Value::operator=(bool bValue) { | 164 void CJS_Value::operator=(bool bValue) { |
| 158 m_pValue = FXJS_NewBoolean(m_pJSRuntime->GetIsolate(), bValue); | 165 m_pValue = FXJS_NewBoolean(m_pJSRuntime->GetIsolate(), bValue); |
| 159 m_eType = VT_boolean; | 166 m_eType = VT_boolean; |
| 160 } | 167 } |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 m_pValue = FXJS_NewDate(m_pJSRuntime->GetIsolate(), (double)date); | 217 m_pValue = FXJS_NewDate(m_pJSRuntime->GetIsolate(), (double)date); |
| 211 m_eType = VT_date; | 218 m_eType = VT_date; |
| 212 } | 219 } |
| 213 | 220 |
| 214 void CJS_Value::operator=(CJS_Value value) { | 221 void CJS_Value::operator=(CJS_Value value) { |
| 215 m_pValue = value.ToV8Value(); | 222 m_pValue = value.ToV8Value(); |
| 216 m_eType = value.m_eType; | 223 m_eType = value.m_eType; |
| 217 m_pJSRuntime = value.m_pJSRuntime; | 224 m_pJSRuntime = value.m_pJSRuntime; |
| 218 } | 225 } |
| 219 | 226 |
| 220 /* -----------------------------------------------------------------------------
----------- | |
| 221 */ | |
| 222 | |
| 223 CJS_Value::Type CJS_Value::GetType() const { | 227 CJS_Value::Type CJS_Value::GetType() const { |
| 224 if (m_pValue.IsEmpty()) | 228 if (m_pValue.IsEmpty()) |
| 225 return VT_unknown; | 229 return VT_unknown; |
| 226 if (m_pValue->IsString()) | 230 if (m_pValue->IsString()) |
| 227 return VT_string; | 231 return VT_string; |
| 228 if (m_pValue->IsNumber()) | 232 if (m_pValue->IsNumber()) |
| 229 return VT_number; | 233 return VT_number; |
| 230 if (m_pValue->IsBoolean()) | 234 if (m_pValue->IsBoolean()) |
| 231 return VT_boolean; | 235 return VT_boolean; |
| 232 if (m_pValue->IsDate()) | 236 if (m_pValue->IsDate()) |
| (...skipping 629 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 862 return day * 86400000 + time; | 866 return day * 86400000 + time; |
| 863 } | 867 } |
| 864 | 868 |
| 865 bool JS_PortIsNan(double d) { | 869 bool JS_PortIsNan(double d) { |
| 866 return d != d; | 870 return d != d; |
| 867 } | 871 } |
| 868 | 872 |
| 869 double JS_LocalTime(double d) { | 873 double JS_LocalTime(double d) { |
| 870 return JS_GetDateTime() + _getDaylightSavingTA(d); | 874 return JS_GetDateTime() + _getDaylightSavingTA(d); |
| 871 } | 875 } |
| OLD | NEW |