John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 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. |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 4 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
Tom Sepez | 3745841 | 2015-10-06 11:33:46 -0700 | [diff] [blame] | 7 | #include "JS_Value.h" |
| 8 | |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 9 | #include <time.h> |
| 10 | #include <cmath> |
| 11 | #include <limits> |
| 12 | |
Tom Sepez | 3745841 | 2015-10-06 11:33:46 -0700 | [diff] [blame] | 13 | #include "Document.h" |
| 14 | #include "JS_Define.h" |
| 15 | #include "JS_Object.h" |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 16 | |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 17 | static const FX_DWORD g_nan[2] = {0, 0x7FF80000}; |
| 18 | static double GetNan() { |
| 19 | return *(double*)g_nan; |
| 20 | } |
| 21 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 22 | /* ---------------------------- CJS_Value ---------------------------- */ |
| 23 | |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 24 | CJS_Value::CJS_Value(CJS_Runtime* pRuntime) |
| 25 | : m_eType(VT_unknown), m_pJSRuntime(pRuntime) { |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 26 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 27 | |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 28 | CJS_Value::CJS_Value(CJS_Runtime* pRuntime, v8::Local<v8::Value> pValue, Type t) |
| 29 | : m_eType(t), m_pValue(pValue), m_pJSRuntime(pRuntime) { |
| 30 | } |
| 31 | |
| 32 | CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const int& iValue) |
| 33 | : m_pJSRuntime(pRuntime) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 34 | operator=(iValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 35 | } |
| 36 | |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 37 | CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const bool& bValue) |
| 38 | : m_pJSRuntime(pRuntime) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 39 | operator=(bValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 42 | CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const float& fValue) |
| 43 | : m_pJSRuntime(pRuntime) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 44 | operator=(fValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 47 | CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const double& dValue) |
| 48 | : m_pJSRuntime(pRuntime) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 49 | operator=(dValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 52 | CJS_Value::CJS_Value(CJS_Runtime* pRuntime, v8::Local<v8::Object> pJsObj) |
| 53 | : m_pJSRuntime(pRuntime) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 54 | operator=(pJsObj); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 57 | CJS_Value::CJS_Value(CJS_Runtime* pRuntime, CJS_Object* pJsObj) |
| 58 | : m_pJSRuntime(pRuntime) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 59 | operator=(pJsObj); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 62 | CJS_Value::CJS_Value(CJS_Runtime* pRuntime, CJS_Document* pJsDoc) |
| 63 | : m_pJSRuntime(pRuntime) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 64 | m_eType = VT_object; |
| 65 | if (pJsDoc) |
Tom Sepez | 116e4ad | 2015-09-21 09:22:05 -0700 | [diff] [blame] | 66 | m_pValue = pJsDoc->ToV8Object(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 69 | CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const FX_WCHAR* pWstr) |
| 70 | : m_pJSRuntime(pRuntime) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 71 | operator=(pWstr); |
Tom Sepez | f79a69c | 2014-10-30 13:23:42 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 74 | CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const FX_CHAR* pStr) |
| 75 | : m_pJSRuntime(pRuntime) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 76 | operator=(pStr); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 79 | CJS_Value::CJS_Value(CJS_Runtime* pRuntime, CJS_Array& array) |
| 80 | : m_pJSRuntime(pRuntime) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 81 | operator=(array); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 84 | CJS_Value::~CJS_Value() {} |
| 85 | |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 86 | void CJS_Value::Attach(v8::Local<v8::Value> pValue, Type t) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 87 | m_pValue = pValue; |
| 88 | m_eType = t; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 91 | void CJS_Value::Attach(CJS_Value* pValue) { |
| 92 | if (pValue) |
| 93 | Attach(pValue->ToV8Value(), pValue->GetType()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 96 | void CJS_Value::Detach() { |
| 97 | m_pValue = v8::Local<v8::Value>(); |
| 98 | m_eType = VT_unknown; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 101 | /* ---------------------------------------------------------------------------------------- |
| 102 | */ |
| 103 | |
| 104 | int CJS_Value::ToInt() const { |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 105 | return FXJS_ToInt32(m_pJSRuntime->GetIsolate(), m_pValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 108 | bool CJS_Value::ToBool() const { |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 109 | return FXJS_ToBoolean(m_pJSRuntime->GetIsolate(), m_pValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 112 | double CJS_Value::ToDouble() const { |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 113 | return FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 116 | float CJS_Value::ToFloat() const { |
| 117 | return (float)ToDouble(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 120 | CJS_Object* CJS_Value::ToCJSObject() const { |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 121 | v8::Local<v8::Object> pObj = |
| 122 | FXJS_ToObject(m_pJSRuntime->GetIsolate(), m_pValue); |
| 123 | return (CJS_Object*)FXJS_GetPrivate(m_pJSRuntime->GetIsolate(), pObj); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 126 | v8::Local<v8::Object> CJS_Value::ToV8Object() const { |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 127 | return FXJS_ToObject(m_pJSRuntime->GetIsolate(), m_pValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 130 | CFX_WideString CJS_Value::ToCFXWideString() const { |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 131 | return FXJS_ToString(m_pJSRuntime->GetIsolate(), m_pValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 134 | CFX_ByteString CJS_Value::ToCFXByteString() const { |
| 135 | return CFX_ByteString::FromUnicode(ToCFXWideString()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 138 | v8::Local<v8::Value> CJS_Value::ToV8Value() const { |
| 139 | return m_pValue; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 142 | v8::Local<v8::Array> CJS_Value::ToV8Array() const { |
| 143 | if (IsArrayObject()) |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 144 | return v8::Local<v8::Array>::Cast( |
| 145 | FXJS_ToObject(m_pJSRuntime->GetIsolate(), m_pValue)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 146 | return v8::Local<v8::Array>(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 149 | /* ---------------------------------------------------------------------------------------- |
| 150 | */ |
| 151 | |
| 152 | void CJS_Value::operator=(int iValue) { |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 153 | m_pValue = FXJS_NewNumber(m_pJSRuntime->GetIsolate(), iValue); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 154 | m_eType = VT_number; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 157 | void CJS_Value::operator=(bool bValue) { |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 158 | m_pValue = FXJS_NewBoolean(m_pJSRuntime->GetIsolate(), bValue); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 159 | m_eType = VT_boolean; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 162 | void CJS_Value::operator=(double dValue) { |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 163 | m_pValue = FXJS_NewNumber(m_pJSRuntime->GetIsolate(), dValue); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 164 | m_eType = VT_number; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 167 | void CJS_Value::operator=(float fValue) { |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 168 | m_pValue = FXJS_NewNumber(m_pJSRuntime->GetIsolate(), fValue); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 169 | m_eType = VT_number; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 172 | void CJS_Value::operator=(v8::Local<v8::Object> pObj) { |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 173 | m_pValue = FXJS_NewObject(m_pJSRuntime->GetIsolate(), pObj); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 174 | m_eType = VT_fxobject; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 177 | void CJS_Value::operator=(CJS_Object* pObj) { |
| 178 | if (pObj) |
Tom Sepez | 116e4ad | 2015-09-21 09:22:05 -0700 | [diff] [blame] | 179 | operator=(pObj->ToV8Object()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 182 | void CJS_Value::operator=(CJS_Document* pJsDoc) { |
| 183 | m_eType = VT_object; |
| 184 | if (pJsDoc) { |
Tom Sepez | 116e4ad | 2015-09-21 09:22:05 -0700 | [diff] [blame] | 185 | m_pValue = pJsDoc->ToV8Object(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 186 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 189 | void CJS_Value::operator=(const FX_WCHAR* pWstr) { |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 190 | m_pValue = FXJS_NewString(m_pJSRuntime->GetIsolate(), (wchar_t*)pWstr); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 191 | m_eType = VT_string; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 194 | void CJS_Value::SetNull() { |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 195 | m_pValue = FXJS_NewNull(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 196 | m_eType = VT_null; |
JUN FANG | 33f6f0d | 2015-04-06 12:39:51 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 199 | void CJS_Value::operator=(const FX_CHAR* pStr) { |
| 200 | operator=(CFX_WideString::FromLocal(pStr).c_str()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 203 | void CJS_Value::operator=(CJS_Array& array) { |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 204 | m_pValue = |
| 205 | FXJS_NewObject2(m_pJSRuntime->GetIsolate(), (v8::Local<v8::Array>)array); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 206 | m_eType = VT_object; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 209 | void CJS_Value::operator=(CJS_Date& date) { |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 210 | m_pValue = FXJS_NewDate(m_pJSRuntime->GetIsolate(), (double)date); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 211 | m_eType = VT_date; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 214 | void CJS_Value::operator=(CJS_Value value) { |
| 215 | m_pValue = value.ToV8Value(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 216 | m_eType = value.m_eType; |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 217 | m_pJSRuntime = value.m_pJSRuntime; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 220 | /* ---------------------------------------------------------------------------------------- |
| 221 | */ |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 222 | |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 223 | CJS_Value::Type CJS_Value::GetType() const { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 224 | if (m_pValue.IsEmpty()) |
| 225 | return VT_unknown; |
| 226 | if (m_pValue->IsString()) |
| 227 | return VT_string; |
| 228 | if (m_pValue->IsNumber()) |
| 229 | return VT_number; |
| 230 | if (m_pValue->IsBoolean()) |
| 231 | return VT_boolean; |
| 232 | if (m_pValue->IsDate()) |
| 233 | return VT_date; |
| 234 | if (m_pValue->IsObject()) |
| 235 | return VT_object; |
| 236 | if (m_pValue->IsNull()) |
| 237 | return VT_null; |
| 238 | if (m_pValue->IsUndefined()) |
| 239 | return VT_undefined; |
| 240 | return VT_unknown; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 243 | FX_BOOL CJS_Value::IsArrayObject() const { |
| 244 | if (m_pValue.IsEmpty()) |
| 245 | return FALSE; |
| 246 | return m_pValue->IsArray(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 249 | FX_BOOL CJS_Value::IsDateObject() const { |
| 250 | if (m_pValue.IsEmpty()) |
| 251 | return FALSE; |
| 252 | return m_pValue->IsDate(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 255 | // CJS_Value::operator CJS_Array() |
| 256 | FX_BOOL CJS_Value::ConvertToArray(CJS_Array& array) const { |
| 257 | if (IsArrayObject()) { |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 258 | array.Attach(FXJS_ToArray(m_pJSRuntime->GetIsolate(), m_pValue)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 259 | return TRUE; |
| 260 | } |
| 261 | |
| 262 | return FALSE; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 265 | FX_BOOL CJS_Value::ConvertToDate(CJS_Date& date) const { |
| 266 | // if (GetType() == VT_date) |
| 267 | // { |
| 268 | // date = (double)(*this); |
| 269 | // return TRUE; |
| 270 | // } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 271 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 272 | if (IsDateObject()) { |
| 273 | date.Attach(m_pValue); |
| 274 | return TRUE; |
| 275 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 276 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 277 | return FALSE; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | /* ---------------------------- CJS_PropValue ---------------------------- */ |
| 281 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 282 | CJS_PropValue::CJS_PropValue(const CJS_Value& value) |
| 283 | : CJS_Value(value), m_bIsSetting(0) {} |
| 284 | |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 285 | CJS_PropValue::CJS_PropValue(CJS_Runtime* pRuntime) |
| 286 | : CJS_Value(pRuntime), m_bIsSetting(0) { |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 289 | CJS_PropValue::~CJS_PropValue() { |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 292 | void CJS_PropValue::operator<<(int iValue) { |
| 293 | ASSERT(!m_bIsSetting); |
| 294 | CJS_Value::operator=(iValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 297 | void CJS_PropValue::operator>>(int& iValue) const { |
| 298 | ASSERT(m_bIsSetting); |
| 299 | iValue = CJS_Value::ToInt(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 302 | void CJS_PropValue::operator<<(bool bValue) { |
| 303 | ASSERT(!m_bIsSetting); |
| 304 | CJS_Value::operator=(bValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 307 | void CJS_PropValue::operator>>(bool& bValue) const { |
| 308 | ASSERT(m_bIsSetting); |
| 309 | bValue = CJS_Value::ToBool(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 312 | void CJS_PropValue::operator<<(double dValue) { |
| 313 | ASSERT(!m_bIsSetting); |
| 314 | CJS_Value::operator=(dValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 317 | void CJS_PropValue::operator>>(double& dValue) const { |
| 318 | ASSERT(m_bIsSetting); |
| 319 | dValue = CJS_Value::ToDouble(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 320 | } |
| 321 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 322 | void CJS_PropValue::operator<<(CJS_Object* pObj) { |
| 323 | ASSERT(!m_bIsSetting); |
| 324 | CJS_Value::operator=(pObj); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 327 | void CJS_PropValue::operator>>(CJS_Object*& ppObj) const { |
| 328 | ASSERT(m_bIsSetting); |
| 329 | ppObj = CJS_Value::ToCJSObject(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 332 | void CJS_PropValue::operator<<(CJS_Document* pJsDoc) { |
| 333 | ASSERT(!m_bIsSetting); |
| 334 | CJS_Value::operator=(pJsDoc); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 335 | } |
| 336 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 337 | void CJS_PropValue::operator>>(CJS_Document*& ppJsDoc) const { |
| 338 | ASSERT(m_bIsSetting); |
| 339 | ppJsDoc = static_cast<CJS_Document*>(CJS_Value::ToCJSObject()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Tom Sepez | 808a99e | 2015-09-10 12:28:37 -0700 | [diff] [blame] | 342 | void CJS_PropValue::operator<<(v8::Local<v8::Object> pObj) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 343 | ASSERT(!m_bIsSetting); |
| 344 | CJS_Value::operator=(pObj); |
JUN FANG | 33f6f0d | 2015-04-06 12:39:51 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Tom Sepez | 808a99e | 2015-09-10 12:28:37 -0700 | [diff] [blame] | 347 | void CJS_PropValue::operator>>(v8::Local<v8::Object>& ppObj) const { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 348 | ASSERT(m_bIsSetting); |
| 349 | ppObj = CJS_Value::ToV8Object(); |
JUN FANG | 33f6f0d | 2015-04-06 12:39:51 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 352 | void CJS_PropValue::StartSetting() { |
| 353 | m_bIsSetting = 1; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 356 | void CJS_PropValue::StartGetting() { |
| 357 | m_bIsSetting = 0; |
| 358 | } |
| 359 | void CJS_PropValue::operator<<(CFX_ByteString string) { |
| 360 | ASSERT(!m_bIsSetting); |
| 361 | CJS_Value::operator=(string.c_str()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 362 | } |
| 363 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 364 | void CJS_PropValue::operator>>(CFX_ByteString& string) const { |
| 365 | ASSERT(m_bIsSetting); |
| 366 | string = CJS_Value::ToCFXByteString(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 369 | void CJS_PropValue::operator<<(const FX_WCHAR* c_string) { |
| 370 | ASSERT(!m_bIsSetting); |
| 371 | CJS_Value::operator=(c_string); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 374 | void CJS_PropValue::operator>>(CFX_WideString& wide_string) const { |
| 375 | ASSERT(m_bIsSetting); |
| 376 | wide_string = CJS_Value::ToCFXWideString(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 377 | } |
| 378 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 379 | void CJS_PropValue::operator<<(CFX_WideString wide_string) { |
| 380 | ASSERT(!m_bIsSetting); |
| 381 | CJS_Value::operator=(wide_string.c_str()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 382 | } |
| 383 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 384 | void CJS_PropValue::operator>>(CJS_Array& array) const { |
| 385 | ASSERT(m_bIsSetting); |
| 386 | ConvertToArray(array); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 389 | void CJS_PropValue::operator<<(CJS_Array& array) { |
| 390 | ASSERT(!m_bIsSetting); |
| 391 | CJS_Value::operator=(array); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 394 | void CJS_PropValue::operator>>(CJS_Date& date) const { |
| 395 | ASSERT(m_bIsSetting); |
| 396 | ConvertToDate(date); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 399 | void CJS_PropValue::operator<<(CJS_Date& date) { |
| 400 | ASSERT(!m_bIsSetting); |
| 401 | CJS_Value::operator=(date); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 404 | CJS_PropValue::operator v8::Local<v8::Value>() const { |
| 405 | return m_pValue; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 406 | } |
| 407 | |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 408 | CJS_Array::CJS_Array(CJS_Runtime* pRuntime) : m_pJSRuntime(pRuntime) { |
| 409 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 410 | |
| 411 | CJS_Array::~CJS_Array() {} |
| 412 | |
| 413 | void CJS_Array::Attach(v8::Local<v8::Array> pArray) { |
| 414 | m_pArray = pArray; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 417 | FX_BOOL CJS_Array::IsAttached() { |
| 418 | return FALSE; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 421 | void CJS_Array::GetElement(unsigned index, CJS_Value& value) { |
| 422 | if (m_pArray.IsEmpty()) |
| 423 | return; |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 424 | v8::Local<v8::Value> p = |
| 425 | FXJS_GetArrayElement(m_pJSRuntime->GetIsolate(), m_pArray, index); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 426 | value.Attach(p, CJS_Value::VT_object); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 429 | void CJS_Array::SetElement(unsigned index, CJS_Value value) { |
| 430 | if (m_pArray.IsEmpty()) |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 431 | m_pArray = FXJS_NewArray(m_pJSRuntime->GetIsolate()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 432 | |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 433 | FXJS_PutArrayElement(m_pJSRuntime->GetIsolate(), m_pArray, index, |
| 434 | value.ToV8Value()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 437 | int CJS_Array::GetLength() { |
| 438 | if (m_pArray.IsEmpty()) |
| 439 | return 0; |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 440 | return FXJS_GetArrayLength(m_pArray); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 441 | } |
| 442 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 443 | CJS_Array::operator v8::Local<v8::Array>() { |
| 444 | if (m_pArray.IsEmpty()) |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 445 | m_pArray = FXJS_NewArray(m_pJSRuntime->GetIsolate()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 446 | |
| 447 | return m_pArray; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 448 | } |
| 449 | |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 450 | CJS_Date::CJS_Date(CJS_Runtime* pRuntime) : m_pJSRuntime(pRuntime) { |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 451 | } |
| 452 | |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 453 | CJS_Date::CJS_Date(CJS_Runtime* pRuntime, double dMsecTime) |
| 454 | : m_pJSRuntime(pRuntime) { |
| 455 | m_pDate = FXJS_NewDate(pRuntime->GetIsolate(), dMsecTime); |
| 456 | } |
| 457 | |
| 458 | CJS_Date::CJS_Date(CJS_Runtime* pRuntime, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 459 | int year, |
| 460 | int mon, |
| 461 | int day, |
| 462 | int hour, |
| 463 | int min, |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 464 | int sec) |
| 465 | : m_pJSRuntime(pRuntime) { |
| 466 | m_pDate = FXJS_NewDate(pRuntime->GetIsolate(), |
| 467 | MakeDate(year, mon, day, hour, min, sec, 0)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 468 | } |
| 469 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 470 | double CJS_Date::MakeDate(int year, |
| 471 | int mon, |
| 472 | int day, |
| 473 | int hour, |
| 474 | int min, |
| 475 | int sec, |
| 476 | int ms) { |
| 477 | return JS_MakeDate(JS_MakeDay(year, mon, day), |
| 478 | JS_MakeTime(hour, min, sec, ms)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 479 | } |
| 480 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 481 | CJS_Date::~CJS_Date() {} |
| 482 | |
| 483 | FX_BOOL CJS_Date::IsValidDate() { |
| 484 | if (m_pDate.IsEmpty()) |
| 485 | return FALSE; |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 486 | return !JS_PortIsNan(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 487 | } |
| 488 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 489 | void CJS_Date::Attach(v8::Local<v8::Value> pDate) { |
| 490 | m_pDate = pDate; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 491 | } |
| 492 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 493 | int CJS_Date::GetYear() { |
| 494 | if (IsValidDate()) |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 495 | return JS_GetYearFromTime( |
| 496 | JS_LocalTime(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate))); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 497 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 498 | return 0; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 499 | } |
| 500 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 501 | void CJS_Date::SetYear(int iYear) { |
| 502 | double date = MakeDate(iYear, GetMonth(), GetDay(), GetHours(), GetMinutes(), |
| 503 | GetSeconds(), 0); |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 504 | FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_pJSRuntime->GetIsolate(), date)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 505 | } |
| 506 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 507 | int CJS_Date::GetMonth() { |
| 508 | if (IsValidDate()) |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 509 | return JS_GetMonthFromTime( |
| 510 | JS_LocalTime(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate))); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 511 | |
| 512 | return 0; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 513 | } |
| 514 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 515 | void CJS_Date::SetMonth(int iMonth) { |
| 516 | double date = MakeDate(GetYear(), iMonth, GetDay(), GetHours(), GetMinutes(), |
| 517 | GetSeconds(), 0); |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 518 | FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_pJSRuntime->GetIsolate(), date)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 519 | } |
| 520 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 521 | int CJS_Date::GetDay() { |
| 522 | if (IsValidDate()) |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 523 | return JS_GetDayFromTime( |
| 524 | JS_LocalTime(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate))); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 525 | |
| 526 | return 0; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 527 | } |
| 528 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 529 | void CJS_Date::SetDay(int iDay) { |
| 530 | double date = MakeDate(GetYear(), GetMonth(), iDay, GetHours(), GetMinutes(), |
| 531 | GetSeconds(), 0); |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 532 | FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_pJSRuntime->GetIsolate(), date)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 533 | } |
| 534 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 535 | int CJS_Date::GetHours() { |
| 536 | if (IsValidDate()) |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 537 | return JS_GetHourFromTime( |
| 538 | JS_LocalTime(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate))); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 539 | |
| 540 | return 0; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 541 | } |
| 542 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 543 | void CJS_Date::SetHours(int iHours) { |
| 544 | double date = MakeDate(GetYear(), GetMonth(), GetDay(), iHours, GetMinutes(), |
| 545 | GetSeconds(), 0); |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 546 | FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_pJSRuntime->GetIsolate(), date)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 547 | } |
| 548 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 549 | int CJS_Date::GetMinutes() { |
| 550 | if (IsValidDate()) |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 551 | return JS_GetMinFromTime( |
| 552 | JS_LocalTime(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate))); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 553 | |
| 554 | return 0; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 555 | } |
| 556 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 557 | void CJS_Date::SetMinutes(int minutes) { |
| 558 | double date = MakeDate(GetYear(), GetMonth(), GetDay(), GetHours(), minutes, |
| 559 | GetSeconds(), 0); |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 560 | FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_pJSRuntime->GetIsolate(), date)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 561 | } |
| 562 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 563 | int CJS_Date::GetSeconds() { |
| 564 | if (IsValidDate()) |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 565 | return JS_GetSecFromTime( |
| 566 | JS_LocalTime(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate))); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 567 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 568 | return 0; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 569 | } |
| 570 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 571 | void CJS_Date::SetSeconds(int seconds) { |
| 572 | double date = MakeDate(GetYear(), GetMonth(), GetDay(), GetHours(), |
| 573 | GetMinutes(), seconds, 0); |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 574 | FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_pJSRuntime->GetIsolate(), date)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 575 | } |
| 576 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 577 | CJS_Date::operator v8::Local<v8::Value>() { |
| 578 | return m_pDate; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 579 | } |
| 580 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 581 | CJS_Date::operator double() const { |
| 582 | if (m_pDate.IsEmpty()) |
| 583 | return 0.0; |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 584 | return FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 585 | } |
| 586 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 587 | CFX_WideString CJS_Date::ToString() const { |
| 588 | if (m_pDate.IsEmpty()) |
| 589 | return L""; |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 590 | return FXJS_ToString(m_pJSRuntime->GetIsolate(), m_pDate); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | double _getLocalTZA() { |
| 594 | if (!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS)) |
| 595 | return 0; |
| 596 | time_t t = 0; |
| 597 | time(&t); |
| 598 | localtime(&t); |
| 599 | #if _MSC_VER >= 1900 |
| 600 | // In gcc and in Visual Studio prior to VS 2015 'timezone' is a global |
| 601 | // variable declared in time.h. That variable was deprecated and in VS 2015 |
| 602 | // is removed, with _get_timezone replacing it. |
| 603 | long timezone = 0; |
| 604 | _get_timezone(&timezone); |
| 605 | #endif |
| 606 | return (double)(-(timezone * 1000)); |
| 607 | } |
| 608 | |
| 609 | int _getDaylightSavingTA(double d) { |
| 610 | if (!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS)) |
| 611 | return 0; |
| 612 | time_t t = (time_t)(d / 1000); |
| 613 | struct tm* tmp = localtime(&t); |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 614 | if (!tmp) |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 615 | return 0; |
| 616 | if (tmp->tm_isdst > 0) |
| 617 | // One hour. |
| 618 | return (int)60 * 60 * 1000; |
| 619 | return 0; |
| 620 | } |
| 621 | |
| 622 | double _Mod(double x, double y) { |
| 623 | double r = fmod(x, y); |
| 624 | if (r < 0) |
| 625 | r += y; |
| 626 | return r; |
| 627 | } |
| 628 | |
| 629 | int _isfinite(double v) { |
| 630 | #if _MSC_VER |
| 631 | return ::_finite(v); |
| 632 | #else |
| 633 | return std::fabs(v) < std::numeric_limits<double>::max(); |
| 634 | #endif |
| 635 | } |
| 636 | |
| 637 | double _toInteger(double n) { |
| 638 | return (n >= 0) ? FXSYS_floor(n) : -FXSYS_floor(-n); |
| 639 | } |
| 640 | |
| 641 | bool _isLeapYear(int year) { |
| 642 | return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 != 0)); |
| 643 | } |
| 644 | |
| 645 | int _DayFromYear(int y) { |
| 646 | return (int)(365 * (y - 1970.0) + FXSYS_floor((y - 1969.0) / 4) - |
| 647 | FXSYS_floor((y - 1901.0) / 100) + |
| 648 | FXSYS_floor((y - 1601.0) / 400)); |
| 649 | } |
| 650 | |
| 651 | double _TimeFromYear(int y) { |
Lei Zhang | 1ac47eb | 2015-12-21 11:04:44 -0800 | [diff] [blame^] | 652 | return 86400000.0 * _DayFromYear(y); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | double _TimeFromYearMonth(int y, int m) { |
| 656 | static int daysMonth[12] = { |
| 657 | 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; |
| 658 | static int leapDaysMonth[12] = { |
| 659 | 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}; |
| 660 | int* pMonth = daysMonth; |
| 661 | if (_isLeapYear(y)) |
| 662 | pMonth = leapDaysMonth; |
| 663 | return _TimeFromYear(y) + ((double)pMonth[m]) * 86400000; |
| 664 | } |
| 665 | |
| 666 | int _Day(double t) { |
| 667 | return (int)FXSYS_floor(t / 86400000); |
| 668 | } |
| 669 | |
| 670 | int _YearFromTime(double t) { |
| 671 | // estimate the time. |
Lei Zhang | 1ac47eb | 2015-12-21 11:04:44 -0800 | [diff] [blame^] | 672 | int y = 1970 + static_cast<int>(t / (365.2425 * 86400000)); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 673 | if (_TimeFromYear(y) <= t) { |
| 674 | while (_TimeFromYear(y + 1) <= t) |
| 675 | y++; |
| 676 | } else |
Lei Zhang | 1ac47eb | 2015-12-21 11:04:44 -0800 | [diff] [blame^] | 677 | while (_TimeFromYear(y) > t) |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 678 | y--; |
| 679 | return y; |
| 680 | } |
| 681 | |
| 682 | int _DayWithinYear(double t) { |
| 683 | int year = _YearFromTime(t); |
| 684 | int day = _Day(t); |
| 685 | return day - _DayFromYear(year); |
| 686 | } |
| 687 | |
| 688 | int _MonthFromTime(double t) { |
| 689 | int day = _DayWithinYear(t); |
| 690 | int year = _YearFromTime(t); |
| 691 | if (0 <= day && day < 31) |
| 692 | return 0; |
| 693 | if (31 <= day && day < 59 + _isLeapYear(year)) |
| 694 | return 1; |
| 695 | if ((59 + _isLeapYear(year)) <= day && day < (90 + _isLeapYear(year))) |
| 696 | return 2; |
| 697 | if ((90 + _isLeapYear(year)) <= day && day < (120 + _isLeapYear(year))) |
| 698 | return 3; |
| 699 | if ((120 + _isLeapYear(year)) <= day && day < (151 + _isLeapYear(year))) |
| 700 | return 4; |
| 701 | if ((151 + _isLeapYear(year)) <= day && day < (181 + _isLeapYear(year))) |
| 702 | return 5; |
| 703 | if ((181 + _isLeapYear(year)) <= day && day < (212 + _isLeapYear(year))) |
| 704 | return 6; |
| 705 | if ((212 + _isLeapYear(year)) <= day && day < (243 + _isLeapYear(year))) |
| 706 | return 7; |
| 707 | if ((243 + _isLeapYear(year)) <= day && day < (273 + _isLeapYear(year))) |
| 708 | return 8; |
| 709 | if ((273 + _isLeapYear(year)) <= day && day < (304 + _isLeapYear(year))) |
| 710 | return 9; |
| 711 | if ((304 + _isLeapYear(year)) <= day && day < (334 + _isLeapYear(year))) |
| 712 | return 10; |
| 713 | if ((334 + _isLeapYear(year)) <= day && day < (365 + _isLeapYear(year))) |
| 714 | return 11; |
| 715 | |
| 716 | return -1; |
| 717 | } |
| 718 | |
| 719 | int _DateFromTime(double t) { |
| 720 | int day = _DayWithinYear(t); |
| 721 | int year = _YearFromTime(t); |
| 722 | bool leap = _isLeapYear(year); |
| 723 | int month = _MonthFromTime(t); |
| 724 | switch (month) { |
| 725 | case 0: |
| 726 | return day + 1; |
| 727 | case 1: |
| 728 | return day - 30; |
| 729 | case 2: |
| 730 | return day - 58 - leap; |
| 731 | case 3: |
| 732 | return day - 89 - leap; |
| 733 | case 4: |
| 734 | return day - 119 - leap; |
| 735 | case 5: |
| 736 | return day - 150 - leap; |
| 737 | case 6: |
| 738 | return day - 180 - leap; |
| 739 | case 7: |
| 740 | return day - 211 - leap; |
| 741 | case 8: |
| 742 | return day - 242 - leap; |
| 743 | case 9: |
| 744 | return day - 272 - leap; |
| 745 | case 10: |
| 746 | return day - 303 - leap; |
| 747 | case 11: |
| 748 | return day - 333 - leap; |
| 749 | default: |
| 750 | return 0; |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | double JS_GetDateTime() { |
| 755 | if (!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS)) |
| 756 | return 0; |
| 757 | time_t t = time(NULL); |
| 758 | struct tm* pTm = localtime(&t); |
| 759 | |
| 760 | int year = pTm->tm_year + 1900; |
| 761 | double t1 = _TimeFromYear(year); |
| 762 | |
| 763 | return t1 + pTm->tm_yday * 86400000.0 + pTm->tm_hour * 3600000.0 + |
| 764 | pTm->tm_min * 60000.0 + pTm->tm_sec * 1000.0; |
| 765 | } |
| 766 | |
| 767 | int JS_GetYearFromTime(double dt) { |
| 768 | return _YearFromTime(dt); |
| 769 | } |
| 770 | |
| 771 | int JS_GetMonthFromTime(double dt) { |
| 772 | return _MonthFromTime(dt); |
| 773 | } |
| 774 | |
| 775 | int JS_GetDayFromTime(double dt) { |
| 776 | return _DateFromTime(dt); |
| 777 | } |
| 778 | |
| 779 | int JS_GetHourFromTime(double dt) { |
| 780 | return (int)_Mod(FXSYS_floor((double)(dt / (60 * 60 * 1000))), 24); |
| 781 | } |
| 782 | |
| 783 | int JS_GetMinFromTime(double dt) { |
| 784 | return (int)_Mod(FXSYS_floor((double)(dt / (60 * 1000))), 60); |
| 785 | } |
| 786 | |
| 787 | int JS_GetSecFromTime(double dt) { |
| 788 | return (int)_Mod(FXSYS_floor((double)(dt / 1000)), 60); |
| 789 | } |
| 790 | |
| 791 | double JS_DateParse(const wchar_t* string) { |
| 792 | v8::Isolate* pIsolate = v8::Isolate::GetCurrent(); |
| 793 | v8::Isolate::Scope isolate_scope(pIsolate); |
| 794 | v8::HandleScope scope(pIsolate); |
| 795 | |
| 796 | v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); |
| 797 | |
| 798 | // Use the built-in object method. |
| 799 | v8::Local<v8::Value> v = |
| 800 | context->Global() |
| 801 | ->Get(context, v8::String::NewFromUtf8(pIsolate, "Date", |
| 802 | v8::NewStringType::kNormal) |
| 803 | .ToLocalChecked()) |
| 804 | .ToLocalChecked(); |
| 805 | if (v->IsObject()) { |
| 806 | v8::Local<v8::Object> o = v->ToObject(context).ToLocalChecked(); |
| 807 | v = o->Get(context, v8::String::NewFromUtf8(pIsolate, "parse", |
| 808 | v8::NewStringType::kNormal) |
| 809 | .ToLocalChecked()).ToLocalChecked(); |
| 810 | if (v->IsFunction()) { |
| 811 | v8::Local<v8::Function> funC = v8::Local<v8::Function>::Cast(v); |
| 812 | |
| 813 | const int argc = 1; |
| 814 | v8::Local<v8::String> timeStr = FXJS_WSToJSString(pIsolate, string); |
| 815 | v8::Local<v8::Value> argv[argc] = {timeStr}; |
| 816 | v = funC->Call(context, context->Global(), argc, argv).ToLocalChecked(); |
| 817 | if (v->IsNumber()) { |
| 818 | double date = v->ToNumber(context).ToLocalChecked()->Value(); |
| 819 | if (!_isfinite(date)) |
| 820 | return date; |
| 821 | return date + _getLocalTZA() + _getDaylightSavingTA(date); |
| 822 | } |
| 823 | } |
| 824 | } |
| 825 | return 0; |
| 826 | } |
| 827 | |
| 828 | double JS_MakeDay(int nYear, int nMonth, int nDate) { |
| 829 | if (!_isfinite(nYear) || !_isfinite(nMonth) || !_isfinite(nDate)) |
| 830 | return GetNan(); |
| 831 | double y = _toInteger(nYear); |
| 832 | double m = _toInteger(nMonth); |
| 833 | double dt = _toInteger(nDate); |
| 834 | double ym = y + FXSYS_floor((double)m / 12); |
| 835 | double mn = _Mod(m, 12); |
| 836 | |
| 837 | double t = _TimeFromYearMonth((int)ym, (int)mn); |
| 838 | |
| 839 | if (_YearFromTime(t) != ym || _MonthFromTime(t) != mn || |
| 840 | _DateFromTime(t) != 1) |
| 841 | return GetNan(); |
| 842 | return _Day(t) + dt - 1; |
| 843 | } |
| 844 | |
| 845 | double JS_MakeTime(int nHour, int nMin, int nSec, int nMs) { |
| 846 | if (!_isfinite(nHour) || !_isfinite(nMin) || !_isfinite(nSec) || |
| 847 | !_isfinite(nMs)) |
| 848 | return GetNan(); |
| 849 | |
| 850 | double h = _toInteger(nHour); |
| 851 | double m = _toInteger(nMin); |
| 852 | double s = _toInteger(nSec); |
| 853 | double milli = _toInteger(nMs); |
| 854 | |
| 855 | return h * 3600000 + m * 60000 + s * 1000 + milli; |
| 856 | } |
| 857 | |
| 858 | double JS_MakeDate(double day, double time) { |
| 859 | if (!_isfinite(day) || !_isfinite(time)) |
| 860 | return GetNan(); |
| 861 | |
| 862 | return day * 86400000 + time; |
| 863 | } |
| 864 | |
| 865 | bool JS_PortIsNan(double d) { |
| 866 | return d != d; |
| 867 | } |
| 868 | |
| 869 | double JS_LocalTime(double d) { |
| 870 | return JS_GetDateTime() + _getDaylightSavingTA(d); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 871 | } |