blob: bd00adcf4966333b3dc1fd89543af48f81683fac [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 Sepez37458412015-10-06 11:33:46 -07007#include "JS_Value.h"
8
Tom Sepez39bfe122015-09-17 15:25:23 -07009#include <time.h>
10#include <cmath>
11#include <limits>
12
Tom Sepez37458412015-10-06 11:33:46 -070013#include "Document.h"
14#include "JS_Define.h"
15#include "JS_Object.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
Tom Sepez67fd5df2015-10-08 12:24:19 -070024CJS_Value::CJS_Value(CJS_Runtime* pRuntime)
25 : m_eType(VT_unknown), m_pJSRuntime(pRuntime) {
Tom Sepez39bfe122015-09-17 15:25:23 -070026}
Nico Weber9d8ec5a2015-08-04 13:00:21 -070027
Tom Sepez67fd5df2015-10-08 12:24:19 -070028CJS_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
32CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const int& iValue)
33 : m_pJSRuntime(pRuntime) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070034 operator=(iValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070035}
36
Tom Sepez67fd5df2015-10-08 12:24:19 -070037CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const bool& bValue)
38 : m_pJSRuntime(pRuntime) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070039 operator=(bValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070040}
41
Tom Sepez67fd5df2015-10-08 12:24:19 -070042CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const float& fValue)
43 : m_pJSRuntime(pRuntime) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070044 operator=(fValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070045}
46
Tom Sepez67fd5df2015-10-08 12:24:19 -070047CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const double& dValue)
48 : m_pJSRuntime(pRuntime) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070049 operator=(dValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070050}
51
Tom Sepez67fd5df2015-10-08 12:24:19 -070052CJS_Value::CJS_Value(CJS_Runtime* pRuntime, v8::Local<v8::Object> pJsObj)
53 : m_pJSRuntime(pRuntime) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070054 operator=(pJsObj);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070055}
56
Tom Sepez67fd5df2015-10-08 12:24:19 -070057CJS_Value::CJS_Value(CJS_Runtime* pRuntime, CJS_Object* pJsObj)
58 : m_pJSRuntime(pRuntime) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070059 operator=(pJsObj);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070060}
61
Tom Sepez67fd5df2015-10-08 12:24:19 -070062CJS_Value::CJS_Value(CJS_Runtime* pRuntime, CJS_Document* pJsDoc)
63 : m_pJSRuntime(pRuntime) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070064 m_eType = VT_object;
65 if (pJsDoc)
Tom Sepez116e4ad2015-09-21 09:22:05 -070066 m_pValue = pJsDoc->ToV8Object();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070067}
68
Tom Sepez67fd5df2015-10-08 12:24:19 -070069CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const FX_WCHAR* pWstr)
70 : m_pJSRuntime(pRuntime) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070071 operator=(pWstr);
Tom Sepezf79a69c2014-10-30 13:23:42 -070072}
73
Tom Sepez67fd5df2015-10-08 12:24:19 -070074CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const FX_CHAR* pStr)
75 : m_pJSRuntime(pRuntime) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070076 operator=(pStr);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070077}
78
Tom Sepez67fd5df2015-10-08 12:24:19 -070079CJS_Value::CJS_Value(CJS_Runtime* pRuntime, CJS_Array& array)
80 : m_pJSRuntime(pRuntime) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070081 operator=(array);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070082}
83
Nico Weber9d8ec5a2015-08-04 13:00:21 -070084CJS_Value::~CJS_Value() {}
85
Tom Sepez39bfe122015-09-17 15:25:23 -070086void CJS_Value::Attach(v8::Local<v8::Value> pValue, Type t) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070087 m_pValue = pValue;
88 m_eType = t;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070089}
90
Nico Weber9d8ec5a2015-08-04 13:00:21 -070091void CJS_Value::Attach(CJS_Value* pValue) {
92 if (pValue)
93 Attach(pValue->ToV8Value(), pValue->GetType());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070094}
95
Nico Weber9d8ec5a2015-08-04 13:00:21 -070096void CJS_Value::Detach() {
97 m_pValue = v8::Local<v8::Value>();
98 m_eType = VT_unknown;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070099}
100
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700101/* ----------------------------------------------------------------------------------------
102 */
103
104int CJS_Value::ToInt() const {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700105 return FXJS_ToInt32(m_pJSRuntime->GetIsolate(), m_pValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700106}
107
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700108bool CJS_Value::ToBool() const {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700109 return FXJS_ToBoolean(m_pJSRuntime->GetIsolate(), m_pValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700110}
111
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700112double CJS_Value::ToDouble() const {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700113 return FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700114}
115
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700116float CJS_Value::ToFloat() const {
117 return (float)ToDouble();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700118}
119
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700120CJS_Object* CJS_Value::ToCJSObject() const {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700121 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-Malek3f3b45c2014-05-23 17:28:10 -0700124}
125
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700126v8::Local<v8::Object> CJS_Value::ToV8Object() const {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700127 return FXJS_ToObject(m_pJSRuntime->GetIsolate(), m_pValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700128}
129
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700130CFX_WideString CJS_Value::ToCFXWideString() const {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700131 return FXJS_ToString(m_pJSRuntime->GetIsolate(), m_pValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700132}
133
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700134CFX_ByteString CJS_Value::ToCFXByteString() const {
135 return CFX_ByteString::FromUnicode(ToCFXWideString());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700136}
137
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700138v8::Local<v8::Value> CJS_Value::ToV8Value() const {
139 return m_pValue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700140}
141
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700142v8::Local<v8::Array> CJS_Value::ToV8Array() const {
143 if (IsArrayObject())
Tom Sepez67fd5df2015-10-08 12:24:19 -0700144 return v8::Local<v8::Array>::Cast(
145 FXJS_ToObject(m_pJSRuntime->GetIsolate(), m_pValue));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700146 return v8::Local<v8::Array>();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700147}
148
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700149/* ----------------------------------------------------------------------------------------
150 */
151
152void CJS_Value::operator=(int iValue) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700153 m_pValue = FXJS_NewNumber(m_pJSRuntime->GetIsolate(), iValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700154 m_eType = VT_number;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700155}
156
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700157void CJS_Value::operator=(bool bValue) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700158 m_pValue = FXJS_NewBoolean(m_pJSRuntime->GetIsolate(), bValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700159 m_eType = VT_boolean;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700160}
161
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700162void CJS_Value::operator=(double dValue) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700163 m_pValue = FXJS_NewNumber(m_pJSRuntime->GetIsolate(), dValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700164 m_eType = VT_number;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700165}
166
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700167void CJS_Value::operator=(float fValue) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700168 m_pValue = FXJS_NewNumber(m_pJSRuntime->GetIsolate(), fValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700169 m_eType = VT_number;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700170}
171
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700172void CJS_Value::operator=(v8::Local<v8::Object> pObj) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700173 m_pValue = FXJS_NewObject(m_pJSRuntime->GetIsolate(), pObj);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700174 m_eType = VT_fxobject;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700175}
176
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700177void CJS_Value::operator=(CJS_Object* pObj) {
178 if (pObj)
Tom Sepez116e4ad2015-09-21 09:22:05 -0700179 operator=(pObj->ToV8Object());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700180}
181
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700182void CJS_Value::operator=(CJS_Document* pJsDoc) {
183 m_eType = VT_object;
184 if (pJsDoc) {
Tom Sepez116e4ad2015-09-21 09:22:05 -0700185 m_pValue = pJsDoc->ToV8Object();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700186 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700187}
188
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700189void CJS_Value::operator=(const FX_WCHAR* pWstr) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700190 m_pValue = FXJS_NewString(m_pJSRuntime->GetIsolate(), (wchar_t*)pWstr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700191 m_eType = VT_string;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700192}
193
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700194void CJS_Value::SetNull() {
Tom Sepez39bfe122015-09-17 15:25:23 -0700195 m_pValue = FXJS_NewNull();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700196 m_eType = VT_null;
JUN FANG33f6f0d2015-04-06 12:39:51 -0700197}
198
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700199void CJS_Value::operator=(const FX_CHAR* pStr) {
200 operator=(CFX_WideString::FromLocal(pStr).c_str());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700201}
202
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700203void CJS_Value::operator=(CJS_Array& array) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700204 m_pValue =
205 FXJS_NewObject2(m_pJSRuntime->GetIsolate(), (v8::Local<v8::Array>)array);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700206 m_eType = VT_object;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700207}
208
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700209void CJS_Value::operator=(CJS_Date& date) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700210 m_pValue = FXJS_NewDate(m_pJSRuntime->GetIsolate(), (double)date);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700211 m_eType = VT_date;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700212}
213
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700214void CJS_Value::operator=(CJS_Value value) {
215 m_pValue = value.ToV8Value();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700216 m_eType = value.m_eType;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700217 m_pJSRuntime = value.m_pJSRuntime;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700218}
219
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700220/* ----------------------------------------------------------------------------------------
221 */
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700222
Tom Sepez39bfe122015-09-17 15:25:23 -0700223CJS_Value::Type CJS_Value::GetType() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700224 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-Malek3f3b45c2014-05-23 17:28:10 -0700241}
242
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700243FX_BOOL CJS_Value::IsArrayObject() const {
244 if (m_pValue.IsEmpty())
245 return FALSE;
246 return m_pValue->IsArray();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700247}
248
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700249FX_BOOL CJS_Value::IsDateObject() const {
250 if (m_pValue.IsEmpty())
251 return FALSE;
252 return m_pValue->IsDate();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700253}
254
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700255// CJS_Value::operator CJS_Array()
256FX_BOOL CJS_Value::ConvertToArray(CJS_Array& array) const {
257 if (IsArrayObject()) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700258 array.Attach(FXJS_ToArray(m_pJSRuntime->GetIsolate(), m_pValue));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700259 return TRUE;
260 }
261
262 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700263}
264
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700265FX_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-Malek3f3b45c2014-05-23 17:28:10 -0700271
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700272 if (IsDateObject()) {
273 date.Attach(m_pValue);
274 return TRUE;
275 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700276
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700277 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700278}
279
280/* ---------------------------- CJS_PropValue ---------------------------- */
281
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700282CJS_PropValue::CJS_PropValue(const CJS_Value& value)
283 : CJS_Value(value), m_bIsSetting(0) {}
284
Tom Sepez67fd5df2015-10-08 12:24:19 -0700285CJS_PropValue::CJS_PropValue(CJS_Runtime* pRuntime)
286 : CJS_Value(pRuntime), m_bIsSetting(0) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700287}
288
Tom Sepez67fd5df2015-10-08 12:24:19 -0700289CJS_PropValue::~CJS_PropValue() {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700290}
291
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700292void CJS_PropValue::operator<<(int iValue) {
293 ASSERT(!m_bIsSetting);
294 CJS_Value::operator=(iValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700295}
296
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700297void CJS_PropValue::operator>>(int& iValue) const {
298 ASSERT(m_bIsSetting);
299 iValue = CJS_Value::ToInt();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700300}
301
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700302void CJS_PropValue::operator<<(bool bValue) {
303 ASSERT(!m_bIsSetting);
304 CJS_Value::operator=(bValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700305}
306
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700307void CJS_PropValue::operator>>(bool& bValue) const {
308 ASSERT(m_bIsSetting);
309 bValue = CJS_Value::ToBool();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700310}
311
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700312void CJS_PropValue::operator<<(double dValue) {
313 ASSERT(!m_bIsSetting);
314 CJS_Value::operator=(dValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700315}
316
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700317void CJS_PropValue::operator>>(double& dValue) const {
318 ASSERT(m_bIsSetting);
319 dValue = CJS_Value::ToDouble();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700320}
321
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700322void CJS_PropValue::operator<<(CJS_Object* pObj) {
323 ASSERT(!m_bIsSetting);
324 CJS_Value::operator=(pObj);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700325}
326
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700327void CJS_PropValue::operator>>(CJS_Object*& ppObj) const {
328 ASSERT(m_bIsSetting);
329 ppObj = CJS_Value::ToCJSObject();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700330}
331
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700332void CJS_PropValue::operator<<(CJS_Document* pJsDoc) {
333 ASSERT(!m_bIsSetting);
334 CJS_Value::operator=(pJsDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700335}
336
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700337void CJS_PropValue::operator>>(CJS_Document*& ppJsDoc) const {
338 ASSERT(m_bIsSetting);
339 ppJsDoc = static_cast<CJS_Document*>(CJS_Value::ToCJSObject());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700340}
341
Tom Sepez808a99e2015-09-10 12:28:37 -0700342void CJS_PropValue::operator<<(v8::Local<v8::Object> pObj) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700343 ASSERT(!m_bIsSetting);
344 CJS_Value::operator=(pObj);
JUN FANG33f6f0d2015-04-06 12:39:51 -0700345}
346
Tom Sepez808a99e2015-09-10 12:28:37 -0700347void CJS_PropValue::operator>>(v8::Local<v8::Object>& ppObj) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700348 ASSERT(m_bIsSetting);
349 ppObj = CJS_Value::ToV8Object();
JUN FANG33f6f0d2015-04-06 12:39:51 -0700350}
351
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700352void CJS_PropValue::StartSetting() {
353 m_bIsSetting = 1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700354}
355
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700356void CJS_PropValue::StartGetting() {
357 m_bIsSetting = 0;
358}
359void CJS_PropValue::operator<<(CFX_ByteString string) {
360 ASSERT(!m_bIsSetting);
361 CJS_Value::operator=(string.c_str());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700362}
363
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700364void CJS_PropValue::operator>>(CFX_ByteString& string) const {
365 ASSERT(m_bIsSetting);
366 string = CJS_Value::ToCFXByteString();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700367}
368
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700369void CJS_PropValue::operator<<(const FX_WCHAR* c_string) {
370 ASSERT(!m_bIsSetting);
371 CJS_Value::operator=(c_string);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700372}
373
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700374void CJS_PropValue::operator>>(CFX_WideString& wide_string) const {
375 ASSERT(m_bIsSetting);
376 wide_string = CJS_Value::ToCFXWideString();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700377}
378
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700379void CJS_PropValue::operator<<(CFX_WideString wide_string) {
380 ASSERT(!m_bIsSetting);
381 CJS_Value::operator=(wide_string.c_str());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700382}
383
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700384void CJS_PropValue::operator>>(CJS_Array& array) const {
385 ASSERT(m_bIsSetting);
386 ConvertToArray(array);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700387}
388
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700389void CJS_PropValue::operator<<(CJS_Array& array) {
390 ASSERT(!m_bIsSetting);
391 CJS_Value::operator=(array);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700392}
393
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700394void CJS_PropValue::operator>>(CJS_Date& date) const {
395 ASSERT(m_bIsSetting);
396 ConvertToDate(date);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700397}
398
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700399void CJS_PropValue::operator<<(CJS_Date& date) {
400 ASSERT(!m_bIsSetting);
401 CJS_Value::operator=(date);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700402}
403
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700404CJS_PropValue::operator v8::Local<v8::Value>() const {
405 return m_pValue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700406}
407
Tom Sepez67fd5df2015-10-08 12:24:19 -0700408CJS_Array::CJS_Array(CJS_Runtime* pRuntime) : m_pJSRuntime(pRuntime) {
409}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700410
411CJS_Array::~CJS_Array() {}
412
413void CJS_Array::Attach(v8::Local<v8::Array> pArray) {
414 m_pArray = pArray;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700415}
416
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700417FX_BOOL CJS_Array::IsAttached() {
418 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700419}
420
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700421void CJS_Array::GetElement(unsigned index, CJS_Value& value) {
422 if (m_pArray.IsEmpty())
423 return;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700424 v8::Local<v8::Value> p =
425 FXJS_GetArrayElement(m_pJSRuntime->GetIsolate(), m_pArray, index);
Tom Sepez39bfe122015-09-17 15:25:23 -0700426 value.Attach(p, CJS_Value::VT_object);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700427}
428
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700429void CJS_Array::SetElement(unsigned index, CJS_Value value) {
430 if (m_pArray.IsEmpty())
Tom Sepez67fd5df2015-10-08 12:24:19 -0700431 m_pArray = FXJS_NewArray(m_pJSRuntime->GetIsolate());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700432
Tom Sepez67fd5df2015-10-08 12:24:19 -0700433 FXJS_PutArrayElement(m_pJSRuntime->GetIsolate(), m_pArray, index,
434 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 Sepez67fd5df2015-10-08 12:24:19 -0700445 m_pArray = FXJS_NewArray(m_pJSRuntime->GetIsolate());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700446
447 return m_pArray;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700448}
449
Tom Sepez67fd5df2015-10-08 12:24:19 -0700450CJS_Date::CJS_Date(CJS_Runtime* pRuntime) : m_pJSRuntime(pRuntime) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700451}
452
Tom Sepez67fd5df2015-10-08 12:24:19 -0700453CJS_Date::CJS_Date(CJS_Runtime* pRuntime, double dMsecTime)
454 : m_pJSRuntime(pRuntime) {
455 m_pDate = FXJS_NewDate(pRuntime->GetIsolate(), dMsecTime);
456}
457
458CJS_Date::CJS_Date(CJS_Runtime* pRuntime,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700459 int year,
460 int mon,
461 int day,
462 int hour,
463 int min,
Tom Sepez67fd5df2015-10-08 12:24:19 -0700464 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-Malek3f3b45c2014-05-23 17:28:10 -0700468}
469
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700470double 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-Malek3f3b45c2014-05-23 17:28:10 -0700479}
480
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700481CJS_Date::~CJS_Date() {}
482
483FX_BOOL CJS_Date::IsValidDate() {
484 if (m_pDate.IsEmpty())
485 return FALSE;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700486 return !JS_PortIsNan(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700487}
488
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700489void CJS_Date::Attach(v8::Local<v8::Value> pDate) {
490 m_pDate = pDate;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700491}
492
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700493int CJS_Date::GetYear() {
494 if (IsValidDate())
Tom Sepez67fd5df2015-10-08 12:24:19 -0700495 return JS_GetYearFromTime(
496 JS_LocalTime(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), 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 Sepez67fd5df2015-10-08 12:24:19 -0700504 FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_pJSRuntime->GetIsolate(), 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 Sepez67fd5df2015-10-08 12:24:19 -0700509 return JS_GetMonthFromTime(
510 JS_LocalTime(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700511
512 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700513}
514
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700515void CJS_Date::SetMonth(int iMonth) {
516 double date = MakeDate(GetYear(), iMonth, GetDay(), GetHours(), GetMinutes(),
517 GetSeconds(), 0);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700518 FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_pJSRuntime->GetIsolate(), date));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700519}
520
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700521int CJS_Date::GetDay() {
522 if (IsValidDate())
Tom Sepez67fd5df2015-10-08 12:24:19 -0700523 return JS_GetDayFromTime(
524 JS_LocalTime(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700525
526 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700527}
528
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700529void CJS_Date::SetDay(int iDay) {
530 double date = MakeDate(GetYear(), GetMonth(), iDay, GetHours(), GetMinutes(),
531 GetSeconds(), 0);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700532 FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_pJSRuntime->GetIsolate(), date));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700533}
534
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700535int CJS_Date::GetHours() {
536 if (IsValidDate())
Tom Sepez67fd5df2015-10-08 12:24:19 -0700537 return JS_GetHourFromTime(
538 JS_LocalTime(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700539
540 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700541}
542
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700543void CJS_Date::SetHours(int iHours) {
544 double date = MakeDate(GetYear(), GetMonth(), GetDay(), iHours, GetMinutes(),
545 GetSeconds(), 0);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700546 FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_pJSRuntime->GetIsolate(), date));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700547}
548
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700549int CJS_Date::GetMinutes() {
550 if (IsValidDate())
Tom Sepez67fd5df2015-10-08 12:24:19 -0700551 return JS_GetMinFromTime(
552 JS_LocalTime(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700553
554 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700555}
556
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700557void CJS_Date::SetMinutes(int minutes) {
558 double date = MakeDate(GetYear(), GetMonth(), GetDay(), GetHours(), minutes,
559 GetSeconds(), 0);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700560 FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_pJSRuntime->GetIsolate(), date));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700561}
562
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700563int CJS_Date::GetSeconds() {
564 if (IsValidDate())
Tom Sepez67fd5df2015-10-08 12:24:19 -0700565 return JS_GetSecFromTime(
566 JS_LocalTime(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate)));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700567
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700568 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700569}
570
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700571void CJS_Date::SetSeconds(int seconds) {
572 double date = MakeDate(GetYear(), GetMonth(), GetDay(), GetHours(),
573 GetMinutes(), seconds, 0);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700574 FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_pJSRuntime->GetIsolate(), date));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700575}
576
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700577CJS_Date::operator v8::Local<v8::Value>() {
578 return m_pDate;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700579}
580
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700581CJS_Date::operator double() const {
582 if (m_pDate.IsEmpty())
583 return 0.0;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700584 return FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700585}
586
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700587CFX_WideString CJS_Date::ToString() const {
588 if (m_pDate.IsEmpty())
589 return L"";
Tom Sepez67fd5df2015-10-08 12:24:19 -0700590 return FXJS_ToString(m_pJSRuntime->GetIsolate(), m_pDate);
Tom Sepez39bfe122015-09-17 15:25:23 -0700591}
592
593double _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
609int _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 Zhang412e9082015-12-14 18:34:00 -0800614 if (!tmp)
Tom Sepez39bfe122015-09-17 15:25:23 -0700615 return 0;
616 if (tmp->tm_isdst > 0)
617 // One hour.
618 return (int)60 * 60 * 1000;
619 return 0;
620}
621
622double _Mod(double x, double y) {
623 double r = fmod(x, y);
624 if (r < 0)
625 r += y;
626 return r;
627}
628
629int _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
637double _toInteger(double n) {
638 return (n >= 0) ? FXSYS_floor(n) : -FXSYS_floor(-n);
639}
640
641bool _isLeapYear(int year) {
642 return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 != 0));
643}
644
645int _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
651double _TimeFromYear(int y) {
Lei Zhang1ac47eb2015-12-21 11:04:44 -0800652 return 86400000.0 * _DayFromYear(y);
Tom Sepez39bfe122015-09-17 15:25:23 -0700653}
654
655double _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
666int _Day(double t) {
667 return (int)FXSYS_floor(t / 86400000);
668}
669
670int _YearFromTime(double t) {
671 // estimate the time.
Lei Zhang1ac47eb2015-12-21 11:04:44 -0800672 int y = 1970 + static_cast<int>(t / (365.2425 * 86400000));
Tom Sepez39bfe122015-09-17 15:25:23 -0700673 if (_TimeFromYear(y) <= t) {
674 while (_TimeFromYear(y + 1) <= t)
675 y++;
676 } else
Lei Zhang1ac47eb2015-12-21 11:04:44 -0800677 while (_TimeFromYear(y) > t)
Tom Sepez39bfe122015-09-17 15:25:23 -0700678 y--;
679 return y;
680}
681
682int _DayWithinYear(double t) {
683 int year = _YearFromTime(t);
684 int day = _Day(t);
685 return day - _DayFromYear(year);
686}
687
688int _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
719int _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
754double 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
767int JS_GetYearFromTime(double dt) {
768 return _YearFromTime(dt);
769}
770
771int JS_GetMonthFromTime(double dt) {
772 return _MonthFromTime(dt);
773}
774
775int JS_GetDayFromTime(double dt) {
776 return _DateFromTime(dt);
777}
778
779int JS_GetHourFromTime(double dt) {
780 return (int)_Mod(FXSYS_floor((double)(dt / (60 * 60 * 1000))), 24);
781}
782
783int JS_GetMinFromTime(double dt) {
784 return (int)_Mod(FXSYS_floor((double)(dt / (60 * 1000))), 60);
785}
786
787int JS_GetSecFromTime(double dt) {
788 return (int)_Mod(FXSYS_floor((double)(dt / 1000)), 60);
789}
790
791double 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
828double 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
845double 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
858double JS_MakeDate(double day, double time) {
859 if (!_isfinite(day) || !_isfinite(time))
860 return GetNan();
861
862 return day * 86400000 + time;
863}
864
865bool JS_PortIsNan(double d) {
866 return d != d;
867}
868
869double JS_LocalTime(double d) {
870 return JS_GetDateTime() + _getDaylightSavingTA(d);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700871}