blob: ab02f02cfb0250e0b6b6cd6aef096490a8a3092c [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
Dan Sinclairf766ad22016-03-14 13:51:24 -04007#include "fpdfsdk/javascript/JS_Value.h"
Tom Sepez37458412015-10-06 11:33:46 -07008
Tom Sepez39bfe122015-09-17 15:25:23 -07009#include <time.h>
Dan Sinclair3ebd1212016-03-09 09:59:23 -050010
Tom Sepezbd932572016-01-29 09:10:41 -080011#include <algorithm>
Tom Sepez39bfe122015-09-17 15:25:23 -070012#include <cmath>
13#include <limits>
Dan Sinclair3ebd1212016-03-09 09:59:23 -050014#include <vector>
Tom Sepez39bfe122015-09-17 15:25:23 -070015
Dan Sinclairf766ad22016-03-14 13:51:24 -040016#include "fpdfsdk/javascript/Document.h"
17#include "fpdfsdk/javascript/JS_Define.h"
18#include "fpdfsdk/javascript/JS_Object.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070019
tsepezc3255f52016-03-25 14:52:27 -070020static const uint32_t g_nan[2] = {0, 0x7FF80000};
Tom Sepez39bfe122015-09-17 15:25:23 -070021static double GetNan() {
22 return *(double*)g_nan;
23}
24
Tom Sepez67fd5df2015-10-08 12:24:19 -070025CJS_Value::CJS_Value(CJS_Runtime* pRuntime)
Dan Sinclairf766ad22016-03-14 13:51:24 -040026 : m_eType(VT_unknown), m_pJSRuntime(pRuntime) {}
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)
Dan Sinclairf766ad22016-03-14 13:51:24 -040029 : m_eType(t), m_pValue(pValue), m_pJSRuntime(pRuntime) {}
Tom Sepez67fd5df2015-10-08 12:24:19 -070030
31CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const int& iValue)
32 : m_pJSRuntime(pRuntime) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070033 operator=(iValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070034}
35
Tom Sepez67fd5df2015-10-08 12:24:19 -070036CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const bool& bValue)
37 : m_pJSRuntime(pRuntime) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070038 operator=(bValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070039}
40
Tom Sepez67fd5df2015-10-08 12:24:19 -070041CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const float& fValue)
42 : m_pJSRuntime(pRuntime) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070043 operator=(fValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070044}
45
Tom Sepez67fd5df2015-10-08 12:24:19 -070046CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const double& dValue)
47 : m_pJSRuntime(pRuntime) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070048 operator=(dValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070049}
50
Tom Sepez67fd5df2015-10-08 12:24:19 -070051CJS_Value::CJS_Value(CJS_Runtime* pRuntime, v8::Local<v8::Object> pJsObj)
52 : m_pJSRuntime(pRuntime) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070053 operator=(pJsObj);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070054}
55
Tom Sepez67fd5df2015-10-08 12:24:19 -070056CJS_Value::CJS_Value(CJS_Runtime* pRuntime, CJS_Object* pJsObj)
57 : m_pJSRuntime(pRuntime) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070058 operator=(pJsObj);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070059}
60
Tom Sepez67fd5df2015-10-08 12:24:19 -070061CJS_Value::CJS_Value(CJS_Runtime* pRuntime, CJS_Document* pJsDoc)
62 : m_pJSRuntime(pRuntime) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070063 m_eType = VT_object;
64 if (pJsDoc)
Tom Sepez116e4ad2015-09-21 09:22:05 -070065 m_pValue = pJsDoc->ToV8Object();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070066}
67
Tom Sepez67fd5df2015-10-08 12:24:19 -070068CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const FX_WCHAR* pWstr)
69 : m_pJSRuntime(pRuntime) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070070 operator=(pWstr);
Tom Sepezf79a69c2014-10-30 13:23:42 -070071}
72
Tom Sepez67fd5df2015-10-08 12:24:19 -070073CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const FX_CHAR* pStr)
74 : m_pJSRuntime(pRuntime) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070075 operator=(pStr);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070076}
77
Tom Sepez67fd5df2015-10-08 12:24:19 -070078CJS_Value::CJS_Value(CJS_Runtime* pRuntime, CJS_Array& array)
79 : m_pJSRuntime(pRuntime) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070080 operator=(array);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070081}
82
Nico Weber9d8ec5a2015-08-04 13:00:21 -070083CJS_Value::~CJS_Value() {}
84
Tom Sepez39bfe122015-09-17 15:25:23 -070085void CJS_Value::Attach(v8::Local<v8::Value> pValue, Type t) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070086 m_pValue = pValue;
87 m_eType = t;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070088}
89
Nico Weber9d8ec5a2015-08-04 13:00:21 -070090void CJS_Value::Attach(CJS_Value* pValue) {
91 if (pValue)
92 Attach(pValue->ToV8Value(), pValue->GetType());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070093}
94
Nico Weber9d8ec5a2015-08-04 13:00:21 -070095void CJS_Value::Detach() {
96 m_pValue = v8::Local<v8::Value>();
97 m_eType = VT_unknown;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070098}
99
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700100int CJS_Value::ToInt() const {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700101 return FXJS_ToInt32(m_pJSRuntime->GetIsolate(), m_pValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700102}
103
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700104bool CJS_Value::ToBool() const {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700105 return FXJS_ToBoolean(m_pJSRuntime->GetIsolate(), m_pValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700106}
107
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700108double CJS_Value::ToDouble() const {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700109 return FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700110}
111
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700112float CJS_Value::ToFloat() const {
113 return (float)ToDouble();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700114}
115
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700116CJS_Object* CJS_Value::ToCJSObject() const {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700117 v8::Local<v8::Object> pObj =
118 FXJS_ToObject(m_pJSRuntime->GetIsolate(), m_pValue);
119 return (CJS_Object*)FXJS_GetPrivate(m_pJSRuntime->GetIsolate(), pObj);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700120}
121
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700122v8::Local<v8::Object> CJS_Value::ToV8Object() const {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700123 return FXJS_ToObject(m_pJSRuntime->GetIsolate(), m_pValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700124}
125
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700126CFX_WideString CJS_Value::ToCFXWideString() const {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700127 return FXJS_ToString(m_pJSRuntime->GetIsolate(), m_pValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700128}
129
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700130CFX_ByteString CJS_Value::ToCFXByteString() const {
131 return CFX_ByteString::FromUnicode(ToCFXWideString());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700132}
133
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700134v8::Local<v8::Value> CJS_Value::ToV8Value() const {
135 return m_pValue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700136}
137
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700138v8::Local<v8::Array> CJS_Value::ToV8Array() const {
139 if (IsArrayObject())
Tom Sepez67fd5df2015-10-08 12:24:19 -0700140 return v8::Local<v8::Array>::Cast(
141 FXJS_ToObject(m_pJSRuntime->GetIsolate(), 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
Tom Sepez4246b002016-01-20 11:48:29 -0800145void CJS_Value::MaybeCoerceToNumber() {
146 bool bAllowNaN = false;
147 if (m_eType == VT_string) {
148 CFX_ByteString bstr = ToCFXByteString();
149 if (bstr.GetLength() == 0)
150 return;
151 if (bstr == "NaN")
152 bAllowNaN = true;
153 }
etiennebb2f6f912016-04-27 09:10:09 -0700154 v8::TryCatch try_catch(m_pJSRuntime->GetIsolate());
Tom Sepez4246b002016-01-20 11:48:29 -0800155 v8::MaybeLocal<v8::Number> maybeNum =
156 m_pValue->ToNumber(m_pJSRuntime->GetIsolate()->GetCurrentContext());
157 if (maybeNum.IsEmpty())
158 return;
159 v8::Local<v8::Number> num = maybeNum.ToLocalChecked();
160 if (std::isnan(num->Value()) && !bAllowNaN)
161 return;
162 m_pValue = num;
163 m_eType = VT_number;
164}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700165
166void CJS_Value::operator=(int iValue) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700167 m_pValue = FXJS_NewNumber(m_pJSRuntime->GetIsolate(), iValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700168 m_eType = VT_number;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700169}
170
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700171void CJS_Value::operator=(bool bValue) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700172 m_pValue = FXJS_NewBoolean(m_pJSRuntime->GetIsolate(), bValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700173 m_eType = VT_boolean;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700174}
175
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700176void CJS_Value::operator=(double dValue) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700177 m_pValue = FXJS_NewNumber(m_pJSRuntime->GetIsolate(), dValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700178 m_eType = VT_number;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700179}
180
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700181void CJS_Value::operator=(float fValue) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700182 m_pValue = FXJS_NewNumber(m_pJSRuntime->GetIsolate(), fValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700183 m_eType = VT_number;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700184}
185
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700186void CJS_Value::operator=(v8::Local<v8::Object> pObj) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700187 m_pValue = FXJS_NewObject(m_pJSRuntime->GetIsolate(), pObj);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700188 m_eType = VT_fxobject;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700189}
190
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700191void CJS_Value::operator=(CJS_Object* pObj) {
192 if (pObj)
Tom Sepez116e4ad2015-09-21 09:22:05 -0700193 operator=(pObj->ToV8Object());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700194}
195
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700196void CJS_Value::operator=(CJS_Document* pJsDoc) {
197 m_eType = VT_object;
198 if (pJsDoc) {
Tom Sepez116e4ad2015-09-21 09:22:05 -0700199 m_pValue = pJsDoc->ToV8Object();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700200 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700201}
202
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700203void CJS_Value::operator=(const FX_WCHAR* pWstr) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700204 m_pValue = FXJS_NewString(m_pJSRuntime->GetIsolate(), (wchar_t*)pWstr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700205 m_eType = VT_string;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700206}
207
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700208void CJS_Value::SetNull() {
Tom Sepez39bfe122015-09-17 15:25:23 -0700209 m_pValue = FXJS_NewNull();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700210 m_eType = VT_null;
JUN FANG33f6f0d2015-04-06 12:39:51 -0700211}
212
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700213void CJS_Value::operator=(const FX_CHAR* pStr) {
214 operator=(CFX_WideString::FromLocal(pStr).c_str());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700215}
216
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700217void CJS_Value::operator=(CJS_Array& array) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700218 m_pValue =
219 FXJS_NewObject2(m_pJSRuntime->GetIsolate(), (v8::Local<v8::Array>)array);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700220 m_eType = VT_object;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700221}
222
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700223void CJS_Value::operator=(CJS_Date& date) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700224 m_pValue = FXJS_NewDate(m_pJSRuntime->GetIsolate(), (double)date);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700225 m_eType = VT_date;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700226}
227
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700228void CJS_Value::operator=(CJS_Value value) {
229 m_pValue = value.ToV8Value();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700230 m_eType = value.m_eType;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700231 m_pJSRuntime = value.m_pJSRuntime;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700232}
233
Tom Sepez39bfe122015-09-17 15:25:23 -0700234CJS_Value::Type CJS_Value::GetType() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700235 if (m_pValue.IsEmpty())
236 return VT_unknown;
237 if (m_pValue->IsString())
238 return VT_string;
239 if (m_pValue->IsNumber())
240 return VT_number;
241 if (m_pValue->IsBoolean())
242 return VT_boolean;
243 if (m_pValue->IsDate())
244 return VT_date;
245 if (m_pValue->IsObject())
246 return VT_object;
247 if (m_pValue->IsNull())
248 return VT_null;
249 if (m_pValue->IsUndefined())
250 return VT_undefined;
251 return VT_unknown;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700252}
253
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700254FX_BOOL CJS_Value::IsArrayObject() const {
255 if (m_pValue.IsEmpty())
256 return FALSE;
257 return m_pValue->IsArray();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700258}
259
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700260FX_BOOL CJS_Value::IsDateObject() const {
261 if (m_pValue.IsEmpty())
262 return FALSE;
263 return m_pValue->IsDate();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700264}
265
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700266// CJS_Value::operator CJS_Array()
267FX_BOOL CJS_Value::ConvertToArray(CJS_Array& array) const {
268 if (IsArrayObject()) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700269 array.Attach(FXJS_ToArray(m_pJSRuntime->GetIsolate(), m_pValue));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700270 return TRUE;
271 }
272
273 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700274}
275
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700276FX_BOOL CJS_Value::ConvertToDate(CJS_Date& date) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700277 if (IsDateObject()) {
278 date.Attach(m_pValue);
279 return TRUE;
280 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700281
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700282 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700283}
284
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700285CJS_PropValue::CJS_PropValue(const CJS_Value& value)
286 : CJS_Value(value), m_bIsSetting(0) {}
287
Tom Sepez67fd5df2015-10-08 12:24:19 -0700288CJS_PropValue::CJS_PropValue(CJS_Runtime* pRuntime)
Dan Sinclairf766ad22016-03-14 13:51:24 -0400289 : CJS_Value(pRuntime), m_bIsSetting(0) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700290
Dan Sinclairf766ad22016-03-14 13:51:24 -0400291CJS_PropValue::~CJS_PropValue() {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700292
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}
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500360void CJS_PropValue::operator<<(CFX_ByteString str) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700361 ASSERT(!m_bIsSetting);
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500362 CJS_Value::operator=(str.c_str());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700363}
364
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500365void CJS_PropValue::operator>>(CFX_ByteString& str) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700366 ASSERT(m_bIsSetting);
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500367 str = 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
Dan Sinclairf766ad22016-03-14 13:51:24 -0400409CJS_Array::CJS_Array(CJS_Runtime* pRuntime) : m_pJSRuntime(pRuntime) {}
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
Dan Sinclairf766ad22016-03-14 13:51:24 -0400450CJS_Date::CJS_Date(CJS_Runtime* pRuntime) : m_pJSRuntime(pRuntime) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700451
Tom Sepez67fd5df2015-10-08 12:24:19 -0700452CJS_Date::CJS_Date(CJS_Runtime* pRuntime, double dMsecTime)
453 : m_pJSRuntime(pRuntime) {
454 m_pDate = FXJS_NewDate(pRuntime->GetIsolate(), dMsecTime);
455}
456
457CJS_Date::CJS_Date(CJS_Runtime* pRuntime,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700458 int year,
459 int mon,
460 int day,
461 int hour,
462 int min,
Tom Sepez67fd5df2015-10-08 12:24:19 -0700463 int sec)
464 : m_pJSRuntime(pRuntime) {
465 m_pDate = FXJS_NewDate(pRuntime->GetIsolate(),
466 MakeDate(year, mon, day, hour, min, sec, 0));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700467}
468
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700469double CJS_Date::MakeDate(int year,
470 int mon,
471 int day,
472 int hour,
473 int min,
474 int sec,
475 int ms) {
476 return JS_MakeDate(JS_MakeDay(year, mon, day),
477 JS_MakeTime(hour, min, sec, ms));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700478}
479
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700480CJS_Date::~CJS_Date() {}
481
482FX_BOOL CJS_Date::IsValidDate() {
483 if (m_pDate.IsEmpty())
484 return FALSE;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700485 return !JS_PortIsNan(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700486}
487
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700488void CJS_Date::Attach(v8::Local<v8::Value> pDate) {
489 m_pDate = pDate;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700490}
491
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700492int CJS_Date::GetYear() {
493 if (IsValidDate())
Tom Sepez67fd5df2015-10-08 12:24:19 -0700494 return JS_GetYearFromTime(
495 JS_LocalTime(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate)));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700496
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700497 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700498}
499
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700500void CJS_Date::SetYear(int iYear) {
501 double date = MakeDate(iYear, GetMonth(), GetDay(), GetHours(), GetMinutes(),
502 GetSeconds(), 0);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700503 FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_pJSRuntime->GetIsolate(), date));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700504}
505
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700506int CJS_Date::GetMonth() {
507 if (IsValidDate())
Tom Sepez67fd5df2015-10-08 12:24:19 -0700508 return JS_GetMonthFromTime(
509 JS_LocalTime(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), 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 Sepez67fd5df2015-10-08 12:24:19 -0700517 FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_pJSRuntime->GetIsolate(), 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 Sepez67fd5df2015-10-08 12:24:19 -0700522 return JS_GetDayFromTime(
523 JS_LocalTime(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700524
525 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700526}
527
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700528void CJS_Date::SetDay(int iDay) {
529 double date = MakeDate(GetYear(), GetMonth(), iDay, GetHours(), GetMinutes(),
530 GetSeconds(), 0);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700531 FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_pJSRuntime->GetIsolate(), date));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700532}
533
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700534int CJS_Date::GetHours() {
535 if (IsValidDate())
Tom Sepez67fd5df2015-10-08 12:24:19 -0700536 return JS_GetHourFromTime(
537 JS_LocalTime(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700538
539 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700540}
541
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700542void CJS_Date::SetHours(int iHours) {
543 double date = MakeDate(GetYear(), GetMonth(), GetDay(), iHours, GetMinutes(),
544 GetSeconds(), 0);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700545 FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_pJSRuntime->GetIsolate(), date));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700546}
547
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700548int CJS_Date::GetMinutes() {
549 if (IsValidDate())
Tom Sepez67fd5df2015-10-08 12:24:19 -0700550 return JS_GetMinFromTime(
551 JS_LocalTime(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700552
553 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700554}
555
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700556void CJS_Date::SetMinutes(int minutes) {
557 double date = MakeDate(GetYear(), GetMonth(), GetDay(), GetHours(), minutes,
558 GetSeconds(), 0);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700559 FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_pJSRuntime->GetIsolate(), date));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700560}
561
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700562int CJS_Date::GetSeconds() {
563 if (IsValidDate())
Tom Sepez67fd5df2015-10-08 12:24:19 -0700564 return JS_GetSecFromTime(
565 JS_LocalTime(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate)));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700566
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700567 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700568}
569
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700570void CJS_Date::SetSeconds(int seconds) {
571 double date = MakeDate(GetYear(), GetMonth(), GetDay(), GetHours(),
572 GetMinutes(), seconds, 0);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700573 FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_pJSRuntime->GetIsolate(), date));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700574}
575
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700576CJS_Date::operator v8::Local<v8::Value>() {
577 return m_pDate;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700578}
579
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700580CJS_Date::operator double() const {
581 if (m_pDate.IsEmpty())
582 return 0.0;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700583 return FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700584}
585
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700586CFX_WideString CJS_Date::ToString() const {
587 if (m_pDate.IsEmpty())
588 return L"";
Tom Sepez67fd5df2015-10-08 12:24:19 -0700589 return FXJS_ToString(m_pJSRuntime->GetIsolate(), m_pDate);
Tom Sepez39bfe122015-09-17 15:25:23 -0700590}
591
592double _getLocalTZA() {
593 if (!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS))
594 return 0;
595 time_t t = 0;
596 time(&t);
597 localtime(&t);
598#if _MSC_VER >= 1900
599 // In gcc and in Visual Studio prior to VS 2015 'timezone' is a global
600 // variable declared in time.h. That variable was deprecated and in VS 2015
601 // is removed, with _get_timezone replacing it.
602 long timezone = 0;
603 _get_timezone(&timezone);
604#endif
605 return (double)(-(timezone * 1000));
606}
607
608int _getDaylightSavingTA(double d) {
609 if (!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS))
610 return 0;
611 time_t t = (time_t)(d / 1000);
612 struct tm* tmp = localtime(&t);
Lei Zhang412e9082015-12-14 18:34:00 -0800613 if (!tmp)
Tom Sepez39bfe122015-09-17 15:25:23 -0700614 return 0;
615 if (tmp->tm_isdst > 0)
616 // One hour.
617 return (int)60 * 60 * 1000;
618 return 0;
619}
620
621double _Mod(double x, double y) {
622 double r = fmod(x, y);
623 if (r < 0)
624 r += y;
625 return r;
626}
627
628int _isfinite(double v) {
629#if _MSC_VER
630 return ::_finite(v);
631#else
632 return std::fabs(v) < std::numeric_limits<double>::max();
633#endif
634}
635
636double _toInteger(double n) {
637 return (n >= 0) ? FXSYS_floor(n) : -FXSYS_floor(-n);
638}
639
640bool _isLeapYear(int year) {
641 return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 != 0));
642}
643
644int _DayFromYear(int y) {
645 return (int)(365 * (y - 1970.0) + FXSYS_floor((y - 1969.0) / 4) -
646 FXSYS_floor((y - 1901.0) / 100) +
647 FXSYS_floor((y - 1601.0) / 400));
648}
649
650double _TimeFromYear(int y) {
Lei Zhang1ac47eb2015-12-21 11:04:44 -0800651 return 86400000.0 * _DayFromYear(y);
Tom Sepez39bfe122015-09-17 15:25:23 -0700652}
653
Tom Sepez4161c5c2016-03-21 12:26:54 -0700654static const uint16_t daysMonth[12] = {0, 31, 59, 90, 120, 151,
655 181, 212, 243, 273, 304, 334};
656static const uint16_t leapDaysMonth[12] = {0, 31, 60, 91, 121, 152,
657 182, 213, 244, 274, 305, 335};
658
Tom Sepez39bfe122015-09-17 15:25:23 -0700659double _TimeFromYearMonth(int y, int m) {
Tom Sepez4161c5c2016-03-21 12:26:54 -0700660 const uint16_t* pMonth = _isLeapYear(y) ? leapDaysMonth : daysMonth;
Tom Sepez39bfe122015-09-17 15:25:23 -0700661 return _TimeFromYear(y) + ((double)pMonth[m]) * 86400000;
662}
663
664int _Day(double t) {
665 return (int)FXSYS_floor(t / 86400000);
666}
667
668int _YearFromTime(double t) {
669 // estimate the time.
Lei Zhang1ac47eb2015-12-21 11:04:44 -0800670 int y = 1970 + static_cast<int>(t / (365.2425 * 86400000));
Tom Sepez39bfe122015-09-17 15:25:23 -0700671 if (_TimeFromYear(y) <= t) {
672 while (_TimeFromYear(y + 1) <= t)
673 y++;
Dan Sinclair738b08c2016-03-01 14:45:20 -0500674 } else {
Lei Zhang1ac47eb2015-12-21 11:04:44 -0800675 while (_TimeFromYear(y) > t)
Tom Sepez39bfe122015-09-17 15:25:23 -0700676 y--;
Dan Sinclair738b08c2016-03-01 14:45:20 -0500677 }
Tom Sepez39bfe122015-09-17 15:25:23 -0700678 return y;
679}
680
681int _DayWithinYear(double t) {
682 int year = _YearFromTime(t);
683 int day = _Day(t);
684 return day - _DayFromYear(year);
685}
686
687int _MonthFromTime(double t) {
688 int day = _DayWithinYear(t);
689 int year = _YearFromTime(t);
690 if (0 <= day && day < 31)
691 return 0;
692 if (31 <= day && day < 59 + _isLeapYear(year))
693 return 1;
694 if ((59 + _isLeapYear(year)) <= day && day < (90 + _isLeapYear(year)))
695 return 2;
696 if ((90 + _isLeapYear(year)) <= day && day < (120 + _isLeapYear(year)))
697 return 3;
698 if ((120 + _isLeapYear(year)) <= day && day < (151 + _isLeapYear(year)))
699 return 4;
700 if ((151 + _isLeapYear(year)) <= day && day < (181 + _isLeapYear(year)))
701 return 5;
702 if ((181 + _isLeapYear(year)) <= day && day < (212 + _isLeapYear(year)))
703 return 6;
704 if ((212 + _isLeapYear(year)) <= day && day < (243 + _isLeapYear(year)))
705 return 7;
706 if ((243 + _isLeapYear(year)) <= day && day < (273 + _isLeapYear(year)))
707 return 8;
708 if ((273 + _isLeapYear(year)) <= day && day < (304 + _isLeapYear(year)))
709 return 9;
710 if ((304 + _isLeapYear(year)) <= day && day < (334 + _isLeapYear(year)))
711 return 10;
712 if ((334 + _isLeapYear(year)) <= day && day < (365 + _isLeapYear(year)))
713 return 11;
714
715 return -1;
716}
717
718int _DateFromTime(double t) {
719 int day = _DayWithinYear(t);
720 int year = _YearFromTime(t);
721 bool leap = _isLeapYear(year);
722 int month = _MonthFromTime(t);
723 switch (month) {
724 case 0:
725 return day + 1;
726 case 1:
727 return day - 30;
728 case 2:
729 return day - 58 - leap;
730 case 3:
731 return day - 89 - leap;
732 case 4:
733 return day - 119 - leap;
734 case 5:
735 return day - 150 - leap;
736 case 6:
737 return day - 180 - leap;
738 case 7:
739 return day - 211 - leap;
740 case 8:
741 return day - 242 - leap;
742 case 9:
743 return day - 272 - leap;
744 case 10:
745 return day - 303 - leap;
746 case 11:
747 return day - 333 - leap;
748 default:
749 return 0;
750 }
751}
752
753double JS_GetDateTime() {
754 if (!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS))
755 return 0;
756 time_t t = time(NULL);
757 struct tm* pTm = localtime(&t);
758
759 int year = pTm->tm_year + 1900;
760 double t1 = _TimeFromYear(year);
761
762 return t1 + pTm->tm_yday * 86400000.0 + pTm->tm_hour * 3600000.0 +
763 pTm->tm_min * 60000.0 + pTm->tm_sec * 1000.0;
764}
765
766int JS_GetYearFromTime(double dt) {
767 return _YearFromTime(dt);
768}
769
770int JS_GetMonthFromTime(double dt) {
771 return _MonthFromTime(dt);
772}
773
774int JS_GetDayFromTime(double dt) {
775 return _DateFromTime(dt);
776}
777
778int JS_GetHourFromTime(double dt) {
tsepez86a61dc2016-03-25 10:00:11 -0700779 return (int)_Mod(floor(dt / (60 * 60 * 1000)), 24);
Tom Sepez39bfe122015-09-17 15:25:23 -0700780}
781
782int JS_GetMinFromTime(double dt) {
tsepez86a61dc2016-03-25 10:00:11 -0700783 return (int)_Mod(floor(dt / (60 * 1000)), 60);
Tom Sepez39bfe122015-09-17 15:25:23 -0700784}
785
786int JS_GetSecFromTime(double dt) {
tsepez86a61dc2016-03-25 10:00:11 -0700787 return (int)_Mod(floor(dt / 1000), 60);
Tom Sepez39bfe122015-09-17 15:25:23 -0700788}
789
tsepez018935c2016-04-15 13:15:12 -0700790double JS_DateParse(const CFX_WideString& str) {
Tom Sepez39bfe122015-09-17 15:25:23 -0700791 v8::Isolate* pIsolate = v8::Isolate::GetCurrent();
792 v8::Isolate::Scope isolate_scope(pIsolate);
793 v8::HandleScope scope(pIsolate);
794
795 v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
796
797 // Use the built-in object method.
798 v8::Local<v8::Value> v =
799 context->Global()
800 ->Get(context, v8::String::NewFromUtf8(pIsolate, "Date",
801 v8::NewStringType::kNormal)
802 .ToLocalChecked())
803 .ToLocalChecked();
804 if (v->IsObject()) {
805 v8::Local<v8::Object> o = v->ToObject(context).ToLocalChecked();
806 v = o->Get(context, v8::String::NewFromUtf8(pIsolate, "parse",
807 v8::NewStringType::kNormal)
Dan Sinclairf766ad22016-03-14 13:51:24 -0400808 .ToLocalChecked())
809 .ToLocalChecked();
Tom Sepez39bfe122015-09-17 15:25:23 -0700810 if (v->IsFunction()) {
811 v8::Local<v8::Function> funC = v8::Local<v8::Function>::Cast(v);
Tom Sepez39bfe122015-09-17 15:25:23 -0700812 const int argc = 1;
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500813 v8::Local<v8::String> timeStr = FXJS_WSToJSString(pIsolate, str);
Tom Sepez39bfe122015-09-17 15:25:23 -0700814 v8::Local<v8::Value> argv[argc] = {timeStr};
815 v = funC->Call(context, context->Global(), argc, argv).ToLocalChecked();
816 if (v->IsNumber()) {
817 double date = v->ToNumber(context).ToLocalChecked()->Value();
818 if (!_isfinite(date))
819 return date;
tsepez86a61dc2016-03-25 10:00:11 -0700820 return JS_LocalTime(date);
Tom Sepez39bfe122015-09-17 15:25:23 -0700821 }
822 }
823 }
824 return 0;
825}
826
827double JS_MakeDay(int nYear, int nMonth, int nDate) {
828 if (!_isfinite(nYear) || !_isfinite(nMonth) || !_isfinite(nDate))
829 return GetNan();
830 double y = _toInteger(nYear);
831 double m = _toInteger(nMonth);
832 double dt = _toInteger(nDate);
833 double ym = y + FXSYS_floor((double)m / 12);
834 double mn = _Mod(m, 12);
835
836 double t = _TimeFromYearMonth((int)ym, (int)mn);
837
838 if (_YearFromTime(t) != ym || _MonthFromTime(t) != mn ||
839 _DateFromTime(t) != 1)
840 return GetNan();
841 return _Day(t) + dt - 1;
842}
843
844double JS_MakeTime(int nHour, int nMin, int nSec, int nMs) {
845 if (!_isfinite(nHour) || !_isfinite(nMin) || !_isfinite(nSec) ||
846 !_isfinite(nMs))
847 return GetNan();
848
849 double h = _toInteger(nHour);
850 double m = _toInteger(nMin);
851 double s = _toInteger(nSec);
852 double milli = _toInteger(nMs);
853
854 return h * 3600000 + m * 60000 + s * 1000 + milli;
855}
856
857double JS_MakeDate(double day, double time) {
858 if (!_isfinite(day) || !_isfinite(time))
859 return GetNan();
860
861 return day * 86400000 + time;
862}
863
864bool JS_PortIsNan(double d) {
865 return d != d;
866}
867
868double JS_LocalTime(double d) {
tsepez86a61dc2016-03-25 10:00:11 -0700869 return d + _getLocalTZA() + _getDaylightSavingTA(d);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700870}
Tom Sepezbd932572016-01-29 09:10:41 -0800871
872std::vector<CJS_Value> JS_ExpandKeywordParams(
873 CJS_Runtime* pRuntime,
874 const std::vector<CJS_Value>& originals,
875 size_t nKeywords,
876 ...) {
877 ASSERT(nKeywords);
878
879 std::vector<CJS_Value> result(nKeywords, CJS_Value(pRuntime));
880 size_t size = std::min(originals.size(), nKeywords);
881 for (size_t i = 0; i < size; ++i)
882 result[i] = originals[i];
883
884 if (originals.size() != 1 || originals[0].GetType() != CJS_Value::VT_object ||
885 originals[0].IsArrayObject()) {
886 return result;
887 }
888 v8::Local<v8::Object> pObj = originals[0].ToV8Object();
889 result[0] = CJS_Value(pRuntime); // Make unknown.
890
891 va_list ap;
892 va_start(ap, nKeywords);
Wei Li89409932016-03-28 10:33:33 -0700893 for (size_t i = 0; i < nKeywords; ++i) {
Tom Sepezbd932572016-01-29 09:10:41 -0800894 const wchar_t* property = va_arg(ap, const wchar_t*);
895 v8::Local<v8::Value> v8Value =
896 FXJS_GetObjectElement(pRuntime->GetIsolate(), pObj, property);
897 if (!v8Value->IsUndefined())
898 result[i] = CJS_Value(pRuntime, v8Value, CJS_Value::VT_unknown);
899 }
900 va_end(ap);
901 return result;
902}