blob: a07c61f108e3ad5be3d1c70743ced0dffa23eac2 [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// 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 Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Tom Sepez39bfe122015-09-17 15:25:23 -07007#include <time.h>
8#include <cmath>
9#include <limits>
10
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070011#include "../../include/javascript/JavaScript.h"
12#include "../../include/javascript/JS_Define.h"
13#include "../../include/javascript/JS_Object.h"
14#include "../../include/javascript/JS_Value.h"
Tom Sepezf79a69c2014-10-30 13:23:42 -070015#include "../../include/javascript/Document.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070016
Tom Sepez39bfe122015-09-17 15:25:23 -070017static const FX_DWORD g_nan[2] = {0, 0x7FF80000};
18static double GetNan() {
19 return *(double*)g_nan;
20}
21
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070022/* ---------------------------- CJS_Value ---------------------------- */
23
Nico Weber9d8ec5a2015-08-04 13:00:21 -070024CJS_Value::CJS_Value(v8::Isolate* isolate)
25 : m_eType(VT_unknown), m_isolate(isolate) {}
Tom Sepez39bfe122015-09-17 15:25:23 -070026CJS_Value::CJS_Value(v8::Isolate* isolate, v8::Local<v8::Value> pValue, Type t)
27 : m_eType(t), m_pValue(pValue), m_isolate(isolate) {
28}
Nico Weber9d8ec5a2015-08-04 13:00:21 -070029
30CJS_Value::CJS_Value(v8::Isolate* isolate, const int& iValue)
31 : m_isolate(isolate) {
32 operator=(iValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070033}
34
Nico Weber9d8ec5a2015-08-04 13:00:21 -070035CJS_Value::CJS_Value(v8::Isolate* isolate, const bool& bValue)
36 : m_isolate(isolate) {
37 operator=(bValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070038}
39
Nico Weber9d8ec5a2015-08-04 13:00:21 -070040CJS_Value::CJS_Value(v8::Isolate* isolate, const float& fValue)
41 : m_isolate(isolate) {
42 operator=(fValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070043}
44
Nico Weber9d8ec5a2015-08-04 13:00:21 -070045CJS_Value::CJS_Value(v8::Isolate* isolate, const double& dValue)
46 : m_isolate(isolate) {
47 operator=(dValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070048}
49
Tom Sepez808a99e2015-09-10 12:28:37 -070050CJS_Value::CJS_Value(v8::Isolate* isolate, v8::Local<v8::Object> pJsObj)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070051 : m_isolate(isolate) {
52 operator=(pJsObj);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070053}
54
Nico Weber9d8ec5a2015-08-04 13:00:21 -070055CJS_Value::CJS_Value(v8::Isolate* isolate, CJS_Object* pJsObj)
56 : m_isolate(isolate) {
57 operator=(pJsObj);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070058}
59
Nico Weber9d8ec5a2015-08-04 13:00:21 -070060CJS_Value::CJS_Value(v8::Isolate* isolate, CJS_Document* pJsDoc)
61 : m_isolate(isolate) {
62 m_eType = VT_object;
63 if (pJsDoc)
Tom Sepez116e4ad2015-09-21 09:22:05 -070064 m_pValue = pJsDoc->ToV8Object();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070065}
66
Nico Weber9d8ec5a2015-08-04 13:00:21 -070067CJS_Value::CJS_Value(v8::Isolate* isolate, const FX_WCHAR* pWstr)
68 : m_isolate(isolate) {
69 operator=(pWstr);
Tom Sepezf79a69c2014-10-30 13:23:42 -070070}
71
Nico Weber9d8ec5a2015-08-04 13:00:21 -070072CJS_Value::CJS_Value(v8::Isolate* isolate, const FX_CHAR* pStr)
73 : m_isolate(isolate) {
74 operator=(pStr);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070075}
76
Nico Weber9d8ec5a2015-08-04 13:00:21 -070077CJS_Value::CJS_Value(v8::Isolate* isolate, CJS_Array& array)
78 : m_isolate(isolate) {
79 operator=(array);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070080}
81
Nico Weber9d8ec5a2015-08-04 13:00:21 -070082CJS_Value::~CJS_Value() {}
83
Tom Sepez39bfe122015-09-17 15:25:23 -070084void CJS_Value::Attach(v8::Local<v8::Value> pValue, Type t) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070085 m_pValue = pValue;
86 m_eType = t;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070087}
88
Nico Weber9d8ec5a2015-08-04 13:00:21 -070089void CJS_Value::Attach(CJS_Value* pValue) {
90 if (pValue)
91 Attach(pValue->ToV8Value(), pValue->GetType());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070092}
93
Nico Weber9d8ec5a2015-08-04 13:00:21 -070094void CJS_Value::Detach() {
95 m_pValue = v8::Local<v8::Value>();
96 m_eType = VT_unknown;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070097}
98
Nico Weber9d8ec5a2015-08-04 13:00:21 -070099/* ----------------------------------------------------------------------------------------
100 */
101
102int CJS_Value::ToInt() const {
Tom Sepez39bfe122015-09-17 15:25:23 -0700103 return FXJS_ToInt32(m_isolate, m_pValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700104}
105
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700106bool CJS_Value::ToBool() const {
Tom Sepez39bfe122015-09-17 15:25:23 -0700107 return FXJS_ToBoolean(m_isolate, m_pValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700108}
109
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700110double CJS_Value::ToDouble() const {
Tom Sepez39bfe122015-09-17 15:25:23 -0700111 return FXJS_ToNumber(m_isolate, m_pValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700112}
113
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700114float CJS_Value::ToFloat() const {
115 return (float)ToDouble();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700116}
117
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700118CJS_Object* CJS_Value::ToCJSObject() const {
Tom Sepez39bfe122015-09-17 15:25:23 -0700119 v8::Local<v8::Object> pObj = FXJS_ToObject(m_isolate, m_pValue);
120 return (CJS_Object*)FXJS_GetPrivate(m_isolate, pObj);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700121}
122
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700123v8::Local<v8::Object> CJS_Value::ToV8Object() const {
Tom Sepez39bfe122015-09-17 15:25:23 -0700124 return FXJS_ToObject(m_isolate, m_pValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700125}
126
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700127CFX_WideString CJS_Value::ToCFXWideString() const {
Tom Sepez39bfe122015-09-17 15:25:23 -0700128 return FXJS_ToString(m_isolate, m_pValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700129}
130
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700131CFX_ByteString CJS_Value::ToCFXByteString() const {
132 return CFX_ByteString::FromUnicode(ToCFXWideString());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700133}
134
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700135v8::Local<v8::Value> CJS_Value::ToV8Value() const {
136 return m_pValue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700137}
138
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700139v8::Local<v8::Array> CJS_Value::ToV8Array() const {
140 if (IsArrayObject())
Tom Sepez39bfe122015-09-17 15:25:23 -0700141 return v8::Local<v8::Array>::Cast(FXJS_ToObject(m_isolate, m_pValue));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700142 return v8::Local<v8::Array>();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700143}
144
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700145/* ----------------------------------------------------------------------------------------
146 */
147
148void CJS_Value::operator=(int iValue) {
Tom Sepez39bfe122015-09-17 15:25:23 -0700149 m_pValue = FXJS_NewNumber(m_isolate, iValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700150 m_eType = VT_number;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700151}
152
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700153void CJS_Value::operator=(bool bValue) {
Tom Sepez39bfe122015-09-17 15:25:23 -0700154 m_pValue = FXJS_NewBoolean(m_isolate, bValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700155 m_eType = VT_boolean;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700156}
157
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700158void CJS_Value::operator=(double dValue) {
Tom Sepez39bfe122015-09-17 15:25:23 -0700159 m_pValue = FXJS_NewNumber(m_isolate, dValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700160 m_eType = VT_number;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700161}
162
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700163void CJS_Value::operator=(float fValue) {
Tom Sepez39bfe122015-09-17 15:25:23 -0700164 m_pValue = FXJS_NewNumber(m_isolate, fValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700165 m_eType = VT_number;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700166}
167
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700168void CJS_Value::operator=(v8::Local<v8::Object> pObj) {
Tom Sepez39bfe122015-09-17 15:25:23 -0700169 m_pValue = FXJS_NewObject(m_isolate, pObj);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700170 m_eType = VT_fxobject;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700171}
172
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700173void CJS_Value::operator=(CJS_Object* pObj) {
174 if (pObj)
Tom Sepez116e4ad2015-09-21 09:22:05 -0700175 operator=(pObj->ToV8Object());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700176}
177
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700178void CJS_Value::operator=(CJS_Document* pJsDoc) {
179 m_eType = VT_object;
180 if (pJsDoc) {
Tom Sepez116e4ad2015-09-21 09:22:05 -0700181 m_pValue = pJsDoc->ToV8Object();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700182 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700183}
184
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700185void CJS_Value::operator=(const FX_WCHAR* pWstr) {
Tom Sepez39bfe122015-09-17 15:25:23 -0700186 m_pValue = FXJS_NewString(m_isolate, (wchar_t*)pWstr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700187 m_eType = VT_string;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700188}
189
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700190void CJS_Value::SetNull() {
Tom Sepez39bfe122015-09-17 15:25:23 -0700191 m_pValue = FXJS_NewNull();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700192 m_eType = VT_null;
JUN FANG33f6f0d2015-04-06 12:39:51 -0700193}
194
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700195void CJS_Value::operator=(const FX_CHAR* pStr) {
196 operator=(CFX_WideString::FromLocal(pStr).c_str());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700197}
198
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700199void CJS_Value::operator=(CJS_Array& array) {
Tom Sepez39bfe122015-09-17 15:25:23 -0700200 m_pValue = FXJS_NewObject2(m_isolate, (v8::Local<v8::Array>)array);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700201 m_eType = VT_object;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700202}
203
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700204void CJS_Value::operator=(CJS_Date& date) {
Tom Sepez39bfe122015-09-17 15:25:23 -0700205 m_pValue = FXJS_NewDate(m_isolate, (double)date);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700206 m_eType = VT_date;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700207}
208
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700209void CJS_Value::operator=(CJS_Value value) {
210 m_pValue = value.ToV8Value();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700211 m_eType = value.m_eType;
212 m_isolate = value.m_isolate;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700213}
214
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700215/* ----------------------------------------------------------------------------------------
216 */
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700217
Tom Sepez39bfe122015-09-17 15:25:23 -0700218CJS_Value::Type CJS_Value::GetType() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700219 if (m_pValue.IsEmpty())
220 return VT_unknown;
221 if (m_pValue->IsString())
222 return VT_string;
223 if (m_pValue->IsNumber())
224 return VT_number;
225 if (m_pValue->IsBoolean())
226 return VT_boolean;
227 if (m_pValue->IsDate())
228 return VT_date;
229 if (m_pValue->IsObject())
230 return VT_object;
231 if (m_pValue->IsNull())
232 return VT_null;
233 if (m_pValue->IsUndefined())
234 return VT_undefined;
235 return VT_unknown;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700236}
237
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700238FX_BOOL CJS_Value::IsArrayObject() const {
239 if (m_pValue.IsEmpty())
240 return FALSE;
241 return m_pValue->IsArray();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700242}
243
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700244FX_BOOL CJS_Value::IsDateObject() const {
245 if (m_pValue.IsEmpty())
246 return FALSE;
247 return m_pValue->IsDate();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700248}
249
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700250// CJS_Value::operator CJS_Array()
251FX_BOOL CJS_Value::ConvertToArray(CJS_Array& array) const {
252 if (IsArrayObject()) {
Tom Sepez39bfe122015-09-17 15:25:23 -0700253 array.Attach(FXJS_ToArray(m_isolate, m_pValue));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700254 return TRUE;
255 }
256
257 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700258}
259
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700260FX_BOOL CJS_Value::ConvertToDate(CJS_Date& date) const {
261 // if (GetType() == VT_date)
262 // {
263 // date = (double)(*this);
264 // return TRUE;
265 // }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700266
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700267 if (IsDateObject()) {
268 date.Attach(m_pValue);
269 return TRUE;
270 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700271
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700272 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700273}
274
275/* ---------------------------- CJS_PropValue ---------------------------- */
276
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700277CJS_PropValue::CJS_PropValue(const CJS_Value& value)
278 : CJS_Value(value), m_bIsSetting(0) {}
279
280CJS_PropValue::CJS_PropValue(v8::Isolate* isolate)
281 : CJS_Value(isolate), m_bIsSetting(0) {}
282
283CJS_PropValue::~CJS_PropValue() {}
284
285FX_BOOL CJS_PropValue::IsSetting() {
286 return m_bIsSetting;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700287}
288
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700289FX_BOOL CJS_PropValue::IsGetting() {
290 return !m_bIsSetting;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700291}
292
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700293void CJS_PropValue::operator<<(int iValue) {
294 ASSERT(!m_bIsSetting);
295 CJS_Value::operator=(iValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700296}
297
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700298void CJS_PropValue::operator>>(int& iValue) const {
299 ASSERT(m_bIsSetting);
300 iValue = CJS_Value::ToInt();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700301}
302
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700303void CJS_PropValue::operator<<(bool bValue) {
304 ASSERT(!m_bIsSetting);
305 CJS_Value::operator=(bValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700306}
307
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700308void CJS_PropValue::operator>>(bool& bValue) const {
309 ASSERT(m_bIsSetting);
310 bValue = CJS_Value::ToBool();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700311}
312
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700313void CJS_PropValue::operator<<(double dValue) {
314 ASSERT(!m_bIsSetting);
315 CJS_Value::operator=(dValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700316}
317
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700318void CJS_PropValue::operator>>(double& dValue) const {
319 ASSERT(m_bIsSetting);
320 dValue = CJS_Value::ToDouble();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700321}
322
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700323void CJS_PropValue::operator<<(CJS_Object* pObj) {
324 ASSERT(!m_bIsSetting);
325 CJS_Value::operator=(pObj);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700326}
327
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700328void CJS_PropValue::operator>>(CJS_Object*& ppObj) const {
329 ASSERT(m_bIsSetting);
330 ppObj = CJS_Value::ToCJSObject();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700331}
332
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700333void CJS_PropValue::operator<<(CJS_Document* pJsDoc) {
334 ASSERT(!m_bIsSetting);
335 CJS_Value::operator=(pJsDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700336}
337
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700338void CJS_PropValue::operator>>(CJS_Document*& ppJsDoc) const {
339 ASSERT(m_bIsSetting);
340 ppJsDoc = static_cast<CJS_Document*>(CJS_Value::ToCJSObject());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700341}
342
Tom Sepez808a99e2015-09-10 12:28:37 -0700343void CJS_PropValue::operator<<(v8::Local<v8::Object> pObj) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700344 ASSERT(!m_bIsSetting);
345 CJS_Value::operator=(pObj);
JUN FANG33f6f0d2015-04-06 12:39:51 -0700346}
347
Tom Sepez808a99e2015-09-10 12:28:37 -0700348void CJS_PropValue::operator>>(v8::Local<v8::Object>& ppObj) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700349 ASSERT(m_bIsSetting);
350 ppObj = CJS_Value::ToV8Object();
JUN FANG33f6f0d2015-04-06 12:39:51 -0700351}
352
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700353void CJS_PropValue::StartSetting() {
354 m_bIsSetting = 1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700355}
356
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700357void CJS_PropValue::StartGetting() {
358 m_bIsSetting = 0;
359}
360void CJS_PropValue::operator<<(CFX_ByteString string) {
361 ASSERT(!m_bIsSetting);
362 CJS_Value::operator=(string.c_str());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700363}
364
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700365void CJS_PropValue::operator>>(CFX_ByteString& string) const {
366 ASSERT(m_bIsSetting);
367 string = CJS_Value::ToCFXByteString();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700368}
369
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700370void CJS_PropValue::operator<<(const FX_WCHAR* c_string) {
371 ASSERT(!m_bIsSetting);
372 CJS_Value::operator=(c_string);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700373}
374
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700375void CJS_PropValue::operator>>(CFX_WideString& wide_string) const {
376 ASSERT(m_bIsSetting);
377 wide_string = CJS_Value::ToCFXWideString();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700378}
379
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700380void CJS_PropValue::operator<<(CFX_WideString wide_string) {
381 ASSERT(!m_bIsSetting);
382 CJS_Value::operator=(wide_string.c_str());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700383}
384
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700385void CJS_PropValue::operator>>(CJS_Array& array) const {
386 ASSERT(m_bIsSetting);
387 ConvertToArray(array);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700388}
389
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700390void CJS_PropValue::operator<<(CJS_Array& array) {
391 ASSERT(!m_bIsSetting);
392 CJS_Value::operator=(array);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700393}
394
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700395void CJS_PropValue::operator>>(CJS_Date& date) const {
396 ASSERT(m_bIsSetting);
397 ConvertToDate(date);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700398}
399
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700400void CJS_PropValue::operator<<(CJS_Date& date) {
401 ASSERT(!m_bIsSetting);
402 CJS_Value::operator=(date);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700403}
404
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700405CJS_PropValue::operator v8::Local<v8::Value>() const {
406 return m_pValue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700407}
408
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700409/* ======================================== CJS_Array
410 * ========================================= */
411CJS_Array::CJS_Array(v8::Isolate* isolate) : m_isolate(isolate) {}
412
413CJS_Array::~CJS_Array() {}
414
415void CJS_Array::Attach(v8::Local<v8::Array> pArray) {
416 m_pArray = pArray;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700417}
418
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700419FX_BOOL CJS_Array::IsAttached() {
420 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700421}
422
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700423void CJS_Array::GetElement(unsigned index, CJS_Value& value) {
424 if (m_pArray.IsEmpty())
425 return;
Tom Sepez39bfe122015-09-17 15:25:23 -0700426 v8::Local<v8::Value> p = FXJS_GetArrayElement(m_isolate, m_pArray, index);
427 value.Attach(p, CJS_Value::VT_object);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700428}
429
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700430void CJS_Array::SetElement(unsigned index, CJS_Value value) {
431 if (m_pArray.IsEmpty())
Tom Sepez39bfe122015-09-17 15:25:23 -0700432 m_pArray = FXJS_NewArray(m_isolate);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700433
Tom Sepez39bfe122015-09-17 15:25:23 -0700434 FXJS_PutArrayElement(m_isolate, m_pArray, index, value.ToV8Value());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700435}
436
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700437int CJS_Array::GetLength() {
438 if (m_pArray.IsEmpty())
439 return 0;
Tom Sepez39bfe122015-09-17 15:25:23 -0700440 return FXJS_GetArrayLength(m_pArray);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700441}
442
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700443CJS_Array::operator v8::Local<v8::Array>() {
444 if (m_pArray.IsEmpty())
Tom Sepez39bfe122015-09-17 15:25:23 -0700445 m_pArray = FXJS_NewArray(m_isolate);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700446
447 return m_pArray;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700448}
449
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700450/* ======================================== CJS_Date
451 * ========================================= */
452
453CJS_Date::CJS_Date(v8::Isolate* isolate) : m_isolate(isolate) {}
454
455CJS_Date::CJS_Date(v8::Isolate* isolate, double dMsec_time) {
456 m_isolate = isolate;
Tom Sepez39bfe122015-09-17 15:25:23 -0700457 m_pDate = FXJS_NewDate(isolate, dMsec_time);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700458}
459
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700460CJS_Date::CJS_Date(v8::Isolate* isolate,
461 int year,
462 int mon,
463 int day,
464 int hour,
465 int min,
466 int sec) {
467 m_isolate = isolate;
Tom Sepez39bfe122015-09-17 15:25:23 -0700468 m_pDate = FXJS_NewDate(isolate, MakeDate(year, mon, day, hour, min, sec, 0));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700469}
470
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700471double CJS_Date::MakeDate(int year,
472 int mon,
473 int day,
474 int hour,
475 int min,
476 int sec,
477 int ms) {
478 return JS_MakeDate(JS_MakeDay(year, mon, day),
479 JS_MakeTime(hour, min, sec, ms));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700480}
481
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700482CJS_Date::~CJS_Date() {}
483
484FX_BOOL CJS_Date::IsValidDate() {
485 if (m_pDate.IsEmpty())
486 return FALSE;
Tom Sepez39bfe122015-09-17 15:25:23 -0700487 return !JS_PortIsNan(FXJS_ToNumber(m_isolate, m_pDate));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700488}
489
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700490void CJS_Date::Attach(v8::Local<v8::Value> pDate) {
491 m_pDate = pDate;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700492}
493
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700494int CJS_Date::GetYear() {
495 if (IsValidDate())
Tom Sepez39bfe122015-09-17 15:25:23 -0700496 return JS_GetYearFromTime(JS_LocalTime(FXJS_ToNumber(m_isolate, m_pDate)));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700497
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700498 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700499}
500
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700501void CJS_Date::SetYear(int iYear) {
502 double date = MakeDate(iYear, GetMonth(), GetDay(), GetHours(), GetMinutes(),
503 GetSeconds(), 0);
Tom Sepez39bfe122015-09-17 15:25:23 -0700504 FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_isolate, date));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700505}
506
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700507int CJS_Date::GetMonth() {
508 if (IsValidDate())
Tom Sepez39bfe122015-09-17 15:25:23 -0700509 return JS_GetMonthFromTime(JS_LocalTime(FXJS_ToNumber(m_isolate, m_pDate)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700510
511 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700512}
513
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700514void CJS_Date::SetMonth(int iMonth) {
515 double date = MakeDate(GetYear(), iMonth, GetDay(), GetHours(), GetMinutes(),
516 GetSeconds(), 0);
Tom Sepez39bfe122015-09-17 15:25:23 -0700517 FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_isolate, date));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700518}
519
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700520int CJS_Date::GetDay() {
521 if (IsValidDate())
Tom Sepez39bfe122015-09-17 15:25:23 -0700522 return JS_GetDayFromTime(JS_LocalTime(FXJS_ToNumber(m_isolate, m_pDate)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700523
524 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700525}
526
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700527void CJS_Date::SetDay(int iDay) {
528 double date = MakeDate(GetYear(), GetMonth(), iDay, GetHours(), GetMinutes(),
529 GetSeconds(), 0);
Tom Sepez39bfe122015-09-17 15:25:23 -0700530 FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_isolate, date));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700531}
532
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700533int CJS_Date::GetHours() {
534 if (IsValidDate())
Tom Sepez39bfe122015-09-17 15:25:23 -0700535 return JS_GetHourFromTime(JS_LocalTime(FXJS_ToNumber(m_isolate, m_pDate)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700536
537 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700538}
539
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700540void CJS_Date::SetHours(int iHours) {
541 double date = MakeDate(GetYear(), GetMonth(), GetDay(), iHours, GetMinutes(),
542 GetSeconds(), 0);
Tom Sepez39bfe122015-09-17 15:25:23 -0700543 FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_isolate, date));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700544}
545
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700546int CJS_Date::GetMinutes() {
547 if (IsValidDate())
Tom Sepez39bfe122015-09-17 15:25:23 -0700548 return JS_GetMinFromTime(JS_LocalTime(FXJS_ToNumber(m_isolate, m_pDate)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700549
550 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700551}
552
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700553void CJS_Date::SetMinutes(int minutes) {
554 double date = MakeDate(GetYear(), GetMonth(), GetDay(), GetHours(), minutes,
555 GetSeconds(), 0);
Tom Sepez39bfe122015-09-17 15:25:23 -0700556 FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_isolate, date));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700557}
558
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700559int CJS_Date::GetSeconds() {
560 if (IsValidDate())
Tom Sepez39bfe122015-09-17 15:25:23 -0700561 return JS_GetSecFromTime(JS_LocalTime(FXJS_ToNumber(m_isolate, m_pDate)));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700562
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700563 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700564}
565
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700566void CJS_Date::SetSeconds(int seconds) {
567 double date = MakeDate(GetYear(), GetMonth(), GetDay(), GetHours(),
568 GetMinutes(), seconds, 0);
Tom Sepez39bfe122015-09-17 15:25:23 -0700569 FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_isolate, date));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700570}
571
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700572CJS_Date::operator v8::Local<v8::Value>() {
573 return m_pDate;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700574}
575
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700576CJS_Date::operator double() const {
577 if (m_pDate.IsEmpty())
578 return 0.0;
Tom Sepez39bfe122015-09-17 15:25:23 -0700579 return FXJS_ToNumber(m_isolate, m_pDate);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700580}
581
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700582CFX_WideString CJS_Date::ToString() const {
583 if (m_pDate.IsEmpty())
584 return L"";
Tom Sepez39bfe122015-09-17 15:25:23 -0700585 return FXJS_ToString(m_isolate, m_pDate);
586}
587
588double _getLocalTZA() {
589 if (!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS))
590 return 0;
591 time_t t = 0;
592 time(&t);
593 localtime(&t);
594#if _MSC_VER >= 1900
595 // In gcc and in Visual Studio prior to VS 2015 'timezone' is a global
596 // variable declared in time.h. That variable was deprecated and in VS 2015
597 // is removed, with _get_timezone replacing it.
598 long timezone = 0;
599 _get_timezone(&timezone);
600#endif
601 return (double)(-(timezone * 1000));
602}
603
604int _getDaylightSavingTA(double d) {
605 if (!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS))
606 return 0;
607 time_t t = (time_t)(d / 1000);
608 struct tm* tmp = localtime(&t);
609 if (tmp == NULL)
610 return 0;
611 if (tmp->tm_isdst > 0)
612 // One hour.
613 return (int)60 * 60 * 1000;
614 return 0;
615}
616
617double _Mod(double x, double y) {
618 double r = fmod(x, y);
619 if (r < 0)
620 r += y;
621 return r;
622}
623
624int _isfinite(double v) {
625#if _MSC_VER
626 return ::_finite(v);
627#else
628 return std::fabs(v) < std::numeric_limits<double>::max();
629#endif
630}
631
632double _toInteger(double n) {
633 return (n >= 0) ? FXSYS_floor(n) : -FXSYS_floor(-n);
634}
635
636bool _isLeapYear(int year) {
637 return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 != 0));
638}
639
640int _DayFromYear(int y) {
641 return (int)(365 * (y - 1970.0) + FXSYS_floor((y - 1969.0) / 4) -
642 FXSYS_floor((y - 1901.0) / 100) +
643 FXSYS_floor((y - 1601.0) / 400));
644}
645
646double _TimeFromYear(int y) {
647 return ((double)86400000) * _DayFromYear(y);
648}
649
650double _TimeFromYearMonth(int y, int m) {
651 static int daysMonth[12] = {
652 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
653 static int leapDaysMonth[12] = {
654 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335};
655 int* pMonth = daysMonth;
656 if (_isLeapYear(y))
657 pMonth = leapDaysMonth;
658 return _TimeFromYear(y) + ((double)pMonth[m]) * 86400000;
659}
660
661int _Day(double t) {
662 return (int)FXSYS_floor(t / 86400000);
663}
664
665int _YearFromTime(double t) {
666 // estimate the time.
667 int y = 1970 + (int)(t / (365.0 * 86400000));
668 if (_TimeFromYear(y) <= t) {
669 while (_TimeFromYear(y + 1) <= t)
670 y++;
671 } else
672 while (_TimeFromYear(y - 1) > t)
673 y--;
674 return y;
675}
676
677int _DayWithinYear(double t) {
678 int year = _YearFromTime(t);
679 int day = _Day(t);
680 return day - _DayFromYear(year);
681}
682
683int _MonthFromTime(double t) {
684 int day = _DayWithinYear(t);
685 int year = _YearFromTime(t);
686 if (0 <= day && day < 31)
687 return 0;
688 if (31 <= day && day < 59 + _isLeapYear(year))
689 return 1;
690 if ((59 + _isLeapYear(year)) <= day && day < (90 + _isLeapYear(year)))
691 return 2;
692 if ((90 + _isLeapYear(year)) <= day && day < (120 + _isLeapYear(year)))
693 return 3;
694 if ((120 + _isLeapYear(year)) <= day && day < (151 + _isLeapYear(year)))
695 return 4;
696 if ((151 + _isLeapYear(year)) <= day && day < (181 + _isLeapYear(year)))
697 return 5;
698 if ((181 + _isLeapYear(year)) <= day && day < (212 + _isLeapYear(year)))
699 return 6;
700 if ((212 + _isLeapYear(year)) <= day && day < (243 + _isLeapYear(year)))
701 return 7;
702 if ((243 + _isLeapYear(year)) <= day && day < (273 + _isLeapYear(year)))
703 return 8;
704 if ((273 + _isLeapYear(year)) <= day && day < (304 + _isLeapYear(year)))
705 return 9;
706 if ((304 + _isLeapYear(year)) <= day && day < (334 + _isLeapYear(year)))
707 return 10;
708 if ((334 + _isLeapYear(year)) <= day && day < (365 + _isLeapYear(year)))
709 return 11;
710
711 return -1;
712}
713
714int _DateFromTime(double t) {
715 int day = _DayWithinYear(t);
716 int year = _YearFromTime(t);
717 bool leap = _isLeapYear(year);
718 int month = _MonthFromTime(t);
719 switch (month) {
720 case 0:
721 return day + 1;
722 case 1:
723 return day - 30;
724 case 2:
725 return day - 58 - leap;
726 case 3:
727 return day - 89 - leap;
728 case 4:
729 return day - 119 - leap;
730 case 5:
731 return day - 150 - leap;
732 case 6:
733 return day - 180 - leap;
734 case 7:
735 return day - 211 - leap;
736 case 8:
737 return day - 242 - leap;
738 case 9:
739 return day - 272 - leap;
740 case 10:
741 return day - 303 - leap;
742 case 11:
743 return day - 333 - leap;
744 default:
745 return 0;
746 }
747}
748
749double JS_GetDateTime() {
750 if (!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS))
751 return 0;
752 time_t t = time(NULL);
753 struct tm* pTm = localtime(&t);
754
755 int year = pTm->tm_year + 1900;
756 double t1 = _TimeFromYear(year);
757
758 return t1 + pTm->tm_yday * 86400000.0 + pTm->tm_hour * 3600000.0 +
759 pTm->tm_min * 60000.0 + pTm->tm_sec * 1000.0;
760}
761
762int JS_GetYearFromTime(double dt) {
763 return _YearFromTime(dt);
764}
765
766int JS_GetMonthFromTime(double dt) {
767 return _MonthFromTime(dt);
768}
769
770int JS_GetDayFromTime(double dt) {
771 return _DateFromTime(dt);
772}
773
774int JS_GetHourFromTime(double dt) {
775 return (int)_Mod(FXSYS_floor((double)(dt / (60 * 60 * 1000))), 24);
776}
777
778int JS_GetMinFromTime(double dt) {
779 return (int)_Mod(FXSYS_floor((double)(dt / (60 * 1000))), 60);
780}
781
782int JS_GetSecFromTime(double dt) {
783 return (int)_Mod(FXSYS_floor((double)(dt / 1000)), 60);
784}
785
786double JS_DateParse(const wchar_t* string) {
787 v8::Isolate* pIsolate = v8::Isolate::GetCurrent();
788 v8::Isolate::Scope isolate_scope(pIsolate);
789 v8::HandleScope scope(pIsolate);
790
791 v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
792
793 // Use the built-in object method.
794 v8::Local<v8::Value> v =
795 context->Global()
796 ->Get(context, v8::String::NewFromUtf8(pIsolate, "Date",
797 v8::NewStringType::kNormal)
798 .ToLocalChecked())
799 .ToLocalChecked();
800 if (v->IsObject()) {
801 v8::Local<v8::Object> o = v->ToObject(context).ToLocalChecked();
802 v = o->Get(context, v8::String::NewFromUtf8(pIsolate, "parse",
803 v8::NewStringType::kNormal)
804 .ToLocalChecked()).ToLocalChecked();
805 if (v->IsFunction()) {
806 v8::Local<v8::Function> funC = v8::Local<v8::Function>::Cast(v);
807
808 const int argc = 1;
809 v8::Local<v8::String> timeStr = FXJS_WSToJSString(pIsolate, string);
810 v8::Local<v8::Value> argv[argc] = {timeStr};
811 v = funC->Call(context, context->Global(), argc, argv).ToLocalChecked();
812 if (v->IsNumber()) {
813 double date = v->ToNumber(context).ToLocalChecked()->Value();
814 if (!_isfinite(date))
815 return date;
816 return date + _getLocalTZA() + _getDaylightSavingTA(date);
817 }
818 }
819 }
820 return 0;
821}
822
823double JS_MakeDay(int nYear, int nMonth, int nDate) {
824 if (!_isfinite(nYear) || !_isfinite(nMonth) || !_isfinite(nDate))
825 return GetNan();
826 double y = _toInteger(nYear);
827 double m = _toInteger(nMonth);
828 double dt = _toInteger(nDate);
829 double ym = y + FXSYS_floor((double)m / 12);
830 double mn = _Mod(m, 12);
831
832 double t = _TimeFromYearMonth((int)ym, (int)mn);
833
834 if (_YearFromTime(t) != ym || _MonthFromTime(t) != mn ||
835 _DateFromTime(t) != 1)
836 return GetNan();
837 return _Day(t) + dt - 1;
838}
839
840double JS_MakeTime(int nHour, int nMin, int nSec, int nMs) {
841 if (!_isfinite(nHour) || !_isfinite(nMin) || !_isfinite(nSec) ||
842 !_isfinite(nMs))
843 return GetNan();
844
845 double h = _toInteger(nHour);
846 double m = _toInteger(nMin);
847 double s = _toInteger(nSec);
848 double milli = _toInteger(nMs);
849
850 return h * 3600000 + m * 60000 + s * 1000 + milli;
851}
852
853double JS_MakeDate(double day, double time) {
854 if (!_isfinite(day) || !_isfinite(time))
855 return GetNan();
856
857 return day * 86400000 + time;
858}
859
860bool JS_PortIsNan(double d) {
861 return d != d;
862}
863
864double JS_LocalTime(double d) {
865 return JS_GetDateTime() + _getDaylightSavingTA(d);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700866}