blob: df7bdf47463fda6292422bdf2039aecee09ced16 [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
tsepezfbf52c22016-07-25 11:17:07 -070020namespace {
21
22const uint32_t g_nan[2] = {0, 0x7FF80000};
23
24double GetNan() {
Tom Sepez39bfe122015-09-17 15:25:23 -070025 return *(double*)g_nan;
26}
27
tsepezfbf52c22016-07-25 11:17:07 -070028double
29MakeDate(int year, int mon, int day, int hour, int min, int sec, int ms) {
30 return JS_MakeDate(JS_MakeDay(year, mon, day),
31 JS_MakeTime(hour, min, sec, ms));
32}
33
34} // namespace
35
tsepez40faa792016-07-15 17:58:02 -070036CJS_Value::CJS_Value(CJS_Runtime* pRuntime) : m_pJSRuntime(pRuntime) {}
Nico Weber9d8ec5a2015-08-04 13:00:21 -070037
tsepez40faa792016-07-15 17:58:02 -070038CJS_Value::CJS_Value(CJS_Runtime* pRuntime, v8::Local<v8::Value> pValue)
39 : m_pValue(pValue), m_pJSRuntime(pRuntime) {}
Tom Sepez67fd5df2015-10-08 12:24:19 -070040
41CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const int& iValue)
42 : m_pJSRuntime(pRuntime) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070043 operator=(iValue);
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 bool& bValue)
47 : m_pJSRuntime(pRuntime) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070048 operator=(bValue);
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, const float& fValue)
52 : m_pJSRuntime(pRuntime) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070053 operator=(fValue);
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, const double& dValue)
57 : m_pJSRuntime(pRuntime) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070058 operator=(dValue);
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_Object* pJsObj)
62 : m_pJSRuntime(pRuntime) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070063 operator=(pJsObj);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070064}
65
Tom Sepez67fd5df2015-10-08 12:24:19 -070066CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const FX_WCHAR* pWstr)
67 : m_pJSRuntime(pRuntime) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070068 operator=(pWstr);
Tom Sepezf79a69c2014-10-30 13:23:42 -070069}
70
Tom Sepez67fd5df2015-10-08 12:24:19 -070071CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const FX_CHAR* pStr)
72 : m_pJSRuntime(pRuntime) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070073 operator=(pStr);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070074}
75
tsepeze5aff742016-08-08 09:49:42 -070076CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const CJS_Array& array)
77 : m_pValue(array.ToV8Array(pRuntime->GetIsolate())),
78 m_pJSRuntime(pRuntime) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070079
Nico Weber9d8ec5a2015-08-04 13:00:21 -070080CJS_Value::~CJS_Value() {}
81
weili625ad662016-06-15 11:21:33 -070082CJS_Value::CJS_Value(const CJS_Value& other) = default;
83
tsepez40faa792016-07-15 17:58:02 -070084void CJS_Value::Attach(v8::Local<v8::Value> pValue) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070085 m_pValue = pValue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070086}
87
Nico Weber9d8ec5a2015-08-04 13:00:21 -070088void CJS_Value::Detach() {
89 m_pValue = v8::Local<v8::Value>();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070090}
91
Nico Weber9d8ec5a2015-08-04 13:00:21 -070092int CJS_Value::ToInt() const {
Tom Sepez67fd5df2015-10-08 12:24:19 -070093 return FXJS_ToInt32(m_pJSRuntime->GetIsolate(), m_pValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070094}
95
Nico Weber9d8ec5a2015-08-04 13:00:21 -070096bool CJS_Value::ToBool() const {
Tom Sepez67fd5df2015-10-08 12:24:19 -070097 return FXJS_ToBoolean(m_pJSRuntime->GetIsolate(), m_pValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070098}
99
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700100double CJS_Value::ToDouble() const {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700101 return FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700102}
103
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700104float CJS_Value::ToFloat() const {
105 return (float)ToDouble();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700106}
107
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700108CJS_Object* CJS_Value::ToCJSObject() const {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700109 v8::Local<v8::Object> pObj =
110 FXJS_ToObject(m_pJSRuntime->GetIsolate(), m_pValue);
111 return (CJS_Object*)FXJS_GetPrivate(m_pJSRuntime->GetIsolate(), pObj);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700112}
113
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700114v8::Local<v8::Object> CJS_Value::ToV8Object() const {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700115 return FXJS_ToObject(m_pJSRuntime->GetIsolate(), m_pValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700116}
117
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700118CFX_WideString CJS_Value::ToCFXWideString() const {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700119 return FXJS_ToString(m_pJSRuntime->GetIsolate(), m_pValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700120}
121
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700122CFX_ByteString CJS_Value::ToCFXByteString() const {
123 return CFX_ByteString::FromUnicode(ToCFXWideString());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700124}
125
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700126v8::Local<v8::Value> CJS_Value::ToV8Value() const {
127 return m_pValue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700128}
129
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700130v8::Local<v8::Array> CJS_Value::ToV8Array() const {
131 if (IsArrayObject())
Tom Sepez67fd5df2015-10-08 12:24:19 -0700132 return v8::Local<v8::Array>::Cast(
133 FXJS_ToObject(m_pJSRuntime->GetIsolate(), m_pValue));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700134 return v8::Local<v8::Array>();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700135}
136
Tom Sepez4246b002016-01-20 11:48:29 -0800137void CJS_Value::MaybeCoerceToNumber() {
138 bool bAllowNaN = false;
tsepez40faa792016-07-15 17:58:02 -0700139 if (GetType() == VT_string) {
Tom Sepez4246b002016-01-20 11:48:29 -0800140 CFX_ByteString bstr = ToCFXByteString();
141 if (bstr.GetLength() == 0)
142 return;
143 if (bstr == "NaN")
144 bAllowNaN = true;
145 }
etiennebb2f6f912016-04-27 09:10:09 -0700146 v8::TryCatch try_catch(m_pJSRuntime->GetIsolate());
Tom Sepez4246b002016-01-20 11:48:29 -0800147 v8::MaybeLocal<v8::Number> maybeNum =
148 m_pValue->ToNumber(m_pJSRuntime->GetIsolate()->GetCurrentContext());
149 if (maybeNum.IsEmpty())
150 return;
151 v8::Local<v8::Number> num = maybeNum.ToLocalChecked();
152 if (std::isnan(num->Value()) && !bAllowNaN)
153 return;
154 m_pValue = num;
Tom Sepez4246b002016-01-20 11:48:29 -0800155}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700156
157void CJS_Value::operator=(int iValue) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700158 m_pValue = FXJS_NewNumber(m_pJSRuntime->GetIsolate(), iValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700159}
160
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700161void CJS_Value::operator=(bool bValue) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700162 m_pValue = FXJS_NewBoolean(m_pJSRuntime->GetIsolate(), bValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700163}
164
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700165void CJS_Value::operator=(double dValue) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700166 m_pValue = FXJS_NewNumber(m_pJSRuntime->GetIsolate(), dValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700167}
168
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700169void CJS_Value::operator=(float fValue) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700170 m_pValue = FXJS_NewNumber(m_pJSRuntime->GetIsolate(), fValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700171}
172
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700173void CJS_Value::operator=(v8::Local<v8::Object> pObj) {
tsepez88b66862016-07-14 12:07:48 -0700174 m_pValue = pObj;
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=(const FX_WCHAR* pWstr) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700183 m_pValue = FXJS_NewString(m_pJSRuntime->GetIsolate(), (wchar_t*)pWstr);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700184}
185
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700186void CJS_Value::SetNull() {
tsepez88b66862016-07-14 12:07:48 -0700187 m_pValue = FXJS_NewNull(m_pJSRuntime->GetIsolate());
JUN FANG33f6f0d2015-04-06 12:39:51 -0700188}
189
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700190void CJS_Value::operator=(const FX_CHAR* pStr) {
191 operator=(CFX_WideString::FromLocal(pStr).c_str());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700192}
193
tsepezfbf52c22016-07-25 11:17:07 -0700194void CJS_Value::operator=(const CJS_Value& value) {
195 ASSERT(m_pJSRuntime == value.m_pJSRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700196 m_pValue = value.ToV8Value();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700197}
198
tsepez40faa792016-07-15 17:58:02 -0700199// static
200CJS_Value::Type CJS_Value::GetValueType(v8::Local<v8::Value> value) {
201 if (value.IsEmpty())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700202 return VT_unknown;
tsepez40faa792016-07-15 17:58:02 -0700203 if (value->IsString())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700204 return VT_string;
tsepez40faa792016-07-15 17:58:02 -0700205 if (value->IsNumber())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700206 return VT_number;
tsepez40faa792016-07-15 17:58:02 -0700207 if (value->IsBoolean())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700208 return VT_boolean;
tsepez40faa792016-07-15 17:58:02 -0700209 if (value->IsDate())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700210 return VT_date;
tsepez40faa792016-07-15 17:58:02 -0700211 if (value->IsObject())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700212 return VT_object;
tsepez40faa792016-07-15 17:58:02 -0700213 if (value->IsNull())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700214 return VT_null;
tsepez40faa792016-07-15 17:58:02 -0700215 if (value->IsUndefined())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700216 return VT_undefined;
217 return VT_unknown;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700218}
219
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700220FX_BOOL CJS_Value::IsArrayObject() const {
221 if (m_pValue.IsEmpty())
222 return FALSE;
223 return m_pValue->IsArray();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700224}
225
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700226FX_BOOL CJS_Value::IsDateObject() const {
227 if (m_pValue.IsEmpty())
228 return FALSE;
229 return m_pValue->IsDate();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700230}
231
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700232// CJS_Value::operator CJS_Array()
233FX_BOOL CJS_Value::ConvertToArray(CJS_Array& array) const {
234 if (IsArrayObject()) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700235 array.Attach(FXJS_ToArray(m_pJSRuntime->GetIsolate(), m_pValue));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700236 return TRUE;
237 }
238
239 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700240}
241
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700242FX_BOOL CJS_Value::ConvertToDate(CJS_Date& date) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700243 if (IsDateObject()) {
tsepez135b9982016-08-05 09:32:50 -0700244 v8::Local<v8::Value> mutable_value = m_pValue;
245 date.Attach(mutable_value.As<v8::Date>());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700246 return TRUE;
247 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700248
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700249 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700250}
251
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700252CJS_PropValue::CJS_PropValue(const CJS_Value& value)
253 : CJS_Value(value), m_bIsSetting(0) {}
254
Tom Sepez67fd5df2015-10-08 12:24:19 -0700255CJS_PropValue::CJS_PropValue(CJS_Runtime* pRuntime)
Dan Sinclairf766ad22016-03-14 13:51:24 -0400256 : CJS_Value(pRuntime), m_bIsSetting(0) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700257
Dan Sinclairf766ad22016-03-14 13:51:24 -0400258CJS_PropValue::~CJS_PropValue() {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700259
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700260void CJS_PropValue::operator<<(int iValue) {
261 ASSERT(!m_bIsSetting);
262 CJS_Value::operator=(iValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700263}
264
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700265void CJS_PropValue::operator>>(int& iValue) const {
266 ASSERT(m_bIsSetting);
267 iValue = CJS_Value::ToInt();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700268}
269
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700270void CJS_PropValue::operator<<(bool bValue) {
271 ASSERT(!m_bIsSetting);
272 CJS_Value::operator=(bValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700273}
274
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700275void CJS_PropValue::operator>>(bool& bValue) const {
276 ASSERT(m_bIsSetting);
277 bValue = CJS_Value::ToBool();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700278}
279
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700280void CJS_PropValue::operator<<(double dValue) {
281 ASSERT(!m_bIsSetting);
282 CJS_Value::operator=(dValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700283}
284
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700285void CJS_PropValue::operator>>(double& dValue) const {
286 ASSERT(m_bIsSetting);
287 dValue = CJS_Value::ToDouble();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700288}
289
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700290void CJS_PropValue::operator<<(CJS_Object* pObj) {
291 ASSERT(!m_bIsSetting);
292 CJS_Value::operator=(pObj);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700293}
294
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700295void CJS_PropValue::operator>>(CJS_Object*& ppObj) const {
296 ASSERT(m_bIsSetting);
297 ppObj = CJS_Value::ToCJSObject();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700298}
299
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700300void CJS_PropValue::operator<<(CJS_Document* pJsDoc) {
301 ASSERT(!m_bIsSetting);
302 CJS_Value::operator=(pJsDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700303}
304
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700305void CJS_PropValue::operator>>(CJS_Document*& ppJsDoc) const {
306 ASSERT(m_bIsSetting);
307 ppJsDoc = static_cast<CJS_Document*>(CJS_Value::ToCJSObject());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700308}
309
Tom Sepez808a99e2015-09-10 12:28:37 -0700310void CJS_PropValue::operator<<(v8::Local<v8::Object> pObj) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700311 ASSERT(!m_bIsSetting);
312 CJS_Value::operator=(pObj);
JUN FANG33f6f0d2015-04-06 12:39:51 -0700313}
314
Tom Sepez808a99e2015-09-10 12:28:37 -0700315void CJS_PropValue::operator>>(v8::Local<v8::Object>& ppObj) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700316 ASSERT(m_bIsSetting);
317 ppObj = CJS_Value::ToV8Object();
JUN FANG33f6f0d2015-04-06 12:39:51 -0700318}
319
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500320void CJS_PropValue::operator<<(CFX_ByteString str) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700321 ASSERT(!m_bIsSetting);
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500322 CJS_Value::operator=(str.c_str());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700323}
324
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500325void CJS_PropValue::operator>>(CFX_ByteString& str) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700326 ASSERT(m_bIsSetting);
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500327 str = CJS_Value::ToCFXByteString();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700328}
329
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700330void CJS_PropValue::operator<<(const FX_WCHAR* c_string) {
331 ASSERT(!m_bIsSetting);
332 CJS_Value::operator=(c_string);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700333}
334
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700335void CJS_PropValue::operator>>(CFX_WideString& wide_string) const {
336 ASSERT(m_bIsSetting);
337 wide_string = CJS_Value::ToCFXWideString();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700338}
339
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700340void CJS_PropValue::operator<<(CFX_WideString wide_string) {
341 ASSERT(!m_bIsSetting);
342 CJS_Value::operator=(wide_string.c_str());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700343}
344
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700345void CJS_PropValue::operator>>(CJS_Array& array) const {
346 ASSERT(m_bIsSetting);
347 ConvertToArray(array);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700348}
349
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700350void CJS_PropValue::operator<<(CJS_Array& array) {
351 ASSERT(!m_bIsSetting);
tsepeze5aff742016-08-08 09:49:42 -0700352 m_pValue = array.ToV8Array(m_pJSRuntime->GetIsolate());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700353}
354
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700355void CJS_PropValue::operator>>(CJS_Date& date) const {
356 ASSERT(m_bIsSetting);
357 ConvertToDate(date);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700358}
359
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700360void CJS_PropValue::operator<<(CJS_Date& date) {
361 ASSERT(!m_bIsSetting);
tsepezf3c88322016-08-09 07:30:38 -0700362 m_pValue = date.ToV8Date(m_pJSRuntime->GetIsolate());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700363}
364
tsepeze5aff742016-08-08 09:49:42 -0700365CJS_Array::CJS_Array() {}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700366
weili625ad662016-06-15 11:21:33 -0700367CJS_Array::CJS_Array(const CJS_Array& other) = default;
368
tsepeze5aff742016-08-08 09:49:42 -0700369CJS_Array::~CJS_Array() {}
370
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700371void CJS_Array::Attach(v8::Local<v8::Array> pArray) {
372 m_pArray = pArray;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700373}
374
tsepeze5aff742016-08-08 09:49:42 -0700375void CJS_Array::GetElement(v8::Isolate* pIsolate,
376 unsigned index,
377 CJS_Value& value) const {
378 if (!m_pArray.IsEmpty())
379 value.Attach(FXJS_GetArrayElement(pIsolate, m_pArray, index));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700380}
381
tsepeze5aff742016-08-08 09:49:42 -0700382void CJS_Array::SetElement(v8::Isolate* pIsolate,
383 unsigned index,
384 const CJS_Value& value) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700385 if (m_pArray.IsEmpty())
tsepeze5aff742016-08-08 09:49:42 -0700386 m_pArray = FXJS_NewArray(pIsolate);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700387
tsepeze5aff742016-08-08 09:49:42 -0700388 FXJS_PutArrayElement(pIsolate, m_pArray, index, value.ToV8Value());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700389}
390
tsepezfbf52c22016-07-25 11:17:07 -0700391int CJS_Array::GetLength() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700392 if (m_pArray.IsEmpty())
393 return 0;
Tom Sepez39bfe122015-09-17 15:25:23 -0700394 return FXJS_GetArrayLength(m_pArray);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700395}
396
tsepeze5aff742016-08-08 09:49:42 -0700397v8::Local<v8::Array> CJS_Array::ToV8Array(v8::Isolate* pIsolate) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700398 if (m_pArray.IsEmpty())
tsepeze5aff742016-08-08 09:49:42 -0700399 m_pArray = FXJS_NewArray(pIsolate);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700400
401 return m_pArray;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700402}
403
tsepezf3c88322016-08-09 07:30:38 -0700404CJS_Date::CJS_Date() {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700405
tsepezf3c88322016-08-09 07:30:38 -0700406CJS_Date::CJS_Date(v8::Isolate* pIsolate, double dMsecTime)
407 : m_pDate(FXJS_NewDate(pIsolate, dMsecTime)) {}
Tom Sepez67fd5df2015-10-08 12:24:19 -0700408
tsepezf3c88322016-08-09 07:30:38 -0700409CJS_Date::CJS_Date(v8::Isolate* pIsolate,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700410 int year,
411 int mon,
412 int day,
413 int hour,
414 int min,
Tom Sepez67fd5df2015-10-08 12:24:19 -0700415 int sec)
tsepezf3c88322016-08-09 07:30:38 -0700416 : m_pDate(FXJS_NewDate(pIsolate,
417 MakeDate(year, mon, day, hour, min, sec, 0))) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700418
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700419CJS_Date::~CJS_Date() {}
420
tsepezf3c88322016-08-09 07:30:38 -0700421bool CJS_Date::IsValidDate(v8::Isolate* pIsolate) const {
422 return !m_pDate.IsEmpty() && !JS_PortIsNan(FXJS_ToNumber(pIsolate, m_pDate));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700423}
424
tsepez135b9982016-08-05 09:32:50 -0700425void CJS_Date::Attach(v8::Local<v8::Date> pDate) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700426 m_pDate = pDate;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700427}
428
tsepezf3c88322016-08-09 07:30:38 -0700429int CJS_Date::GetYear(v8::Isolate* pIsolate) const {
430 if (!IsValidDate(pIsolate))
tsepezfbf52c22016-07-25 11:17:07 -0700431 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700432
tsepezf3c88322016-08-09 07:30:38 -0700433 return JS_GetYearFromTime(JS_LocalTime(FXJS_ToNumber(pIsolate, m_pDate)));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700434}
435
tsepezf3c88322016-08-09 07:30:38 -0700436void CJS_Date::SetYear(v8::Isolate* pIsolate, int iYear) {
437 m_pDate = FXJS_NewDate(
438 pIsolate,
439 MakeDate(iYear, GetMonth(pIsolate), GetDay(pIsolate), GetHours(pIsolate),
440 GetMinutes(pIsolate), GetSeconds(pIsolate), 0));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700441}
442
tsepezf3c88322016-08-09 07:30:38 -0700443int CJS_Date::GetMonth(v8::Isolate* pIsolate) const {
444 if (!IsValidDate(pIsolate))
tsepezfbf52c22016-07-25 11:17:07 -0700445 return 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700446
tsepezf3c88322016-08-09 07:30:38 -0700447 return JS_GetMonthFromTime(JS_LocalTime(FXJS_ToNumber(pIsolate, m_pDate)));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700448}
449
tsepezf3c88322016-08-09 07:30:38 -0700450void CJS_Date::SetMonth(v8::Isolate* pIsolate, int iMonth) {
451 m_pDate = FXJS_NewDate(
452 pIsolate,
453 MakeDate(GetYear(pIsolate), iMonth, GetDay(pIsolate), GetHours(pIsolate),
454 GetMinutes(pIsolate), GetSeconds(pIsolate), 0));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700455}
456
tsepezf3c88322016-08-09 07:30:38 -0700457int CJS_Date::GetDay(v8::Isolate* pIsolate) const {
458 if (!IsValidDate(pIsolate))
tsepezfbf52c22016-07-25 11:17:07 -0700459 return 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700460
tsepezf3c88322016-08-09 07:30:38 -0700461 return JS_GetDayFromTime(JS_LocalTime(FXJS_ToNumber(pIsolate, m_pDate)));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700462}
463
tsepezf3c88322016-08-09 07:30:38 -0700464void CJS_Date::SetDay(v8::Isolate* pIsolate, int iDay) {
465 m_pDate = FXJS_NewDate(
466 pIsolate,
467 MakeDate(GetYear(pIsolate), GetMonth(pIsolate), iDay, GetHours(pIsolate),
468 GetMinutes(pIsolate), GetSeconds(pIsolate), 0));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700469}
470
tsepezf3c88322016-08-09 07:30:38 -0700471int CJS_Date::GetHours(v8::Isolate* pIsolate) const {
472 if (!IsValidDate(pIsolate))
tsepezfbf52c22016-07-25 11:17:07 -0700473 return 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700474
tsepezf3c88322016-08-09 07:30:38 -0700475 return JS_GetHourFromTime(JS_LocalTime(FXJS_ToNumber(pIsolate, m_pDate)));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700476}
477
tsepezf3c88322016-08-09 07:30:38 -0700478void CJS_Date::SetHours(v8::Isolate* pIsolate, int iHours) {
479 m_pDate = FXJS_NewDate(
480 pIsolate,
481 MakeDate(GetYear(pIsolate), GetMonth(pIsolate), GetDay(pIsolate), iHours,
482 GetMinutes(pIsolate), GetSeconds(pIsolate), 0));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700483}
484
tsepezf3c88322016-08-09 07:30:38 -0700485int CJS_Date::GetMinutes(v8::Isolate* pIsolate) const {
486 if (!IsValidDate(pIsolate))
tsepezfbf52c22016-07-25 11:17:07 -0700487 return 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700488
tsepezf3c88322016-08-09 07:30:38 -0700489 return JS_GetMinFromTime(JS_LocalTime(FXJS_ToNumber(pIsolate, m_pDate)));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700490}
491
tsepezf3c88322016-08-09 07:30:38 -0700492void CJS_Date::SetMinutes(v8::Isolate* pIsolate, int minutes) {
493 m_pDate =
494 FXJS_NewDate(pIsolate, MakeDate(GetYear(pIsolate), GetMonth(pIsolate),
495 GetDay(pIsolate), GetHours(pIsolate),
496 minutes, GetSeconds(pIsolate), 0));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700497}
498
tsepezf3c88322016-08-09 07:30:38 -0700499int CJS_Date::GetSeconds(v8::Isolate* pIsolate) const {
500 if (!IsValidDate(pIsolate))
tsepezfbf52c22016-07-25 11:17:07 -0700501 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700502
tsepezf3c88322016-08-09 07:30:38 -0700503 return JS_GetSecFromTime(JS_LocalTime(FXJS_ToNumber(pIsolate, m_pDate)));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700504}
505
tsepezf3c88322016-08-09 07:30:38 -0700506void CJS_Date::SetSeconds(v8::Isolate* pIsolate, int seconds) {
507 m_pDate =
508 FXJS_NewDate(pIsolate, MakeDate(GetYear(pIsolate), GetMonth(pIsolate),
509 GetDay(pIsolate), GetHours(pIsolate),
510 GetMinutes(pIsolate), seconds, 0));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700511}
512
tsepezf3c88322016-08-09 07:30:38 -0700513double CJS_Date::ToDouble(v8::Isolate* pIsolate) const {
514 return !m_pDate.IsEmpty() ? FXJS_ToNumber(pIsolate, m_pDate) : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700515}
516
tsepezf3c88322016-08-09 07:30:38 -0700517CFX_WideString CJS_Date::ToString(v8::Isolate* pIsolate) const {
518 return !m_pDate.IsEmpty() ? FXJS_ToString(pIsolate, m_pDate)
519 : CFX_WideString();
520}
tsepezfbf52c22016-07-25 11:17:07 -0700521
tsepezf3c88322016-08-09 07:30:38 -0700522v8::Local<v8::Date> CJS_Date::ToV8Date(v8::Isolate* pIsolate) const {
523 return m_pDate;
Tom Sepez39bfe122015-09-17 15:25:23 -0700524}
525
526double _getLocalTZA() {
527 if (!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS))
528 return 0;
529 time_t t = 0;
530 time(&t);
531 localtime(&t);
532#if _MSC_VER >= 1900
533 // In gcc and in Visual Studio prior to VS 2015 'timezone' is a global
534 // variable declared in time.h. That variable was deprecated and in VS 2015
535 // is removed, with _get_timezone replacing it.
536 long timezone = 0;
537 _get_timezone(&timezone);
538#endif
539 return (double)(-(timezone * 1000));
540}
541
542int _getDaylightSavingTA(double d) {
543 if (!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS))
544 return 0;
545 time_t t = (time_t)(d / 1000);
546 struct tm* tmp = localtime(&t);
Lei Zhang412e9082015-12-14 18:34:00 -0800547 if (!tmp)
Tom Sepez39bfe122015-09-17 15:25:23 -0700548 return 0;
549 if (tmp->tm_isdst > 0)
550 // One hour.
551 return (int)60 * 60 * 1000;
552 return 0;
553}
554
555double _Mod(double x, double y) {
556 double r = fmod(x, y);
557 if (r < 0)
558 r += y;
559 return r;
560}
561
562int _isfinite(double v) {
563#if _MSC_VER
564 return ::_finite(v);
565#else
566 return std::fabs(v) < std::numeric_limits<double>::max();
567#endif
568}
569
570double _toInteger(double n) {
571 return (n >= 0) ? FXSYS_floor(n) : -FXSYS_floor(-n);
572}
573
574bool _isLeapYear(int year) {
575 return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 != 0));
576}
577
578int _DayFromYear(int y) {
579 return (int)(365 * (y - 1970.0) + FXSYS_floor((y - 1969.0) / 4) -
580 FXSYS_floor((y - 1901.0) / 100) +
581 FXSYS_floor((y - 1601.0) / 400));
582}
583
584double _TimeFromYear(int y) {
Lei Zhang1ac47eb2015-12-21 11:04:44 -0800585 return 86400000.0 * _DayFromYear(y);
Tom Sepez39bfe122015-09-17 15:25:23 -0700586}
587
Tom Sepez4161c5c2016-03-21 12:26:54 -0700588static const uint16_t daysMonth[12] = {0, 31, 59, 90, 120, 151,
589 181, 212, 243, 273, 304, 334};
590static const uint16_t leapDaysMonth[12] = {0, 31, 60, 91, 121, 152,
591 182, 213, 244, 274, 305, 335};
592
Tom Sepez39bfe122015-09-17 15:25:23 -0700593double _TimeFromYearMonth(int y, int m) {
Tom Sepez4161c5c2016-03-21 12:26:54 -0700594 const uint16_t* pMonth = _isLeapYear(y) ? leapDaysMonth : daysMonth;
Tom Sepez39bfe122015-09-17 15:25:23 -0700595 return _TimeFromYear(y) + ((double)pMonth[m]) * 86400000;
596}
597
598int _Day(double t) {
599 return (int)FXSYS_floor(t / 86400000);
600}
601
602int _YearFromTime(double t) {
603 // estimate the time.
Lei Zhang1ac47eb2015-12-21 11:04:44 -0800604 int y = 1970 + static_cast<int>(t / (365.2425 * 86400000));
Tom Sepez39bfe122015-09-17 15:25:23 -0700605 if (_TimeFromYear(y) <= t) {
606 while (_TimeFromYear(y + 1) <= t)
607 y++;
Dan Sinclair738b08c2016-03-01 14:45:20 -0500608 } else {
Lei Zhang1ac47eb2015-12-21 11:04:44 -0800609 while (_TimeFromYear(y) > t)
Tom Sepez39bfe122015-09-17 15:25:23 -0700610 y--;
Dan Sinclair738b08c2016-03-01 14:45:20 -0500611 }
Tom Sepez39bfe122015-09-17 15:25:23 -0700612 return y;
613}
614
615int _DayWithinYear(double t) {
616 int year = _YearFromTime(t);
617 int day = _Day(t);
618 return day - _DayFromYear(year);
619}
620
621int _MonthFromTime(double t) {
622 int day = _DayWithinYear(t);
623 int year = _YearFromTime(t);
624 if (0 <= day && day < 31)
625 return 0;
626 if (31 <= day && day < 59 + _isLeapYear(year))
627 return 1;
628 if ((59 + _isLeapYear(year)) <= day && day < (90 + _isLeapYear(year)))
629 return 2;
630 if ((90 + _isLeapYear(year)) <= day && day < (120 + _isLeapYear(year)))
631 return 3;
632 if ((120 + _isLeapYear(year)) <= day && day < (151 + _isLeapYear(year)))
633 return 4;
634 if ((151 + _isLeapYear(year)) <= day && day < (181 + _isLeapYear(year)))
635 return 5;
636 if ((181 + _isLeapYear(year)) <= day && day < (212 + _isLeapYear(year)))
637 return 6;
638 if ((212 + _isLeapYear(year)) <= day && day < (243 + _isLeapYear(year)))
639 return 7;
640 if ((243 + _isLeapYear(year)) <= day && day < (273 + _isLeapYear(year)))
641 return 8;
642 if ((273 + _isLeapYear(year)) <= day && day < (304 + _isLeapYear(year)))
643 return 9;
644 if ((304 + _isLeapYear(year)) <= day && day < (334 + _isLeapYear(year)))
645 return 10;
646 if ((334 + _isLeapYear(year)) <= day && day < (365 + _isLeapYear(year)))
647 return 11;
648
649 return -1;
650}
651
652int _DateFromTime(double t) {
653 int day = _DayWithinYear(t);
654 int year = _YearFromTime(t);
weili12367cb2016-06-03 11:22:16 -0700655 int leap = _isLeapYear(year);
Tom Sepez39bfe122015-09-17 15:25:23 -0700656 int month = _MonthFromTime(t);
657 switch (month) {
658 case 0:
659 return day + 1;
660 case 1:
661 return day - 30;
662 case 2:
663 return day - 58 - leap;
664 case 3:
665 return day - 89 - leap;
666 case 4:
667 return day - 119 - leap;
668 case 5:
669 return day - 150 - leap;
670 case 6:
671 return day - 180 - leap;
672 case 7:
673 return day - 211 - leap;
674 case 8:
675 return day - 242 - leap;
676 case 9:
677 return day - 272 - leap;
678 case 10:
679 return day - 303 - leap;
680 case 11:
681 return day - 333 - leap;
682 default:
683 return 0;
684 }
685}
686
687double JS_GetDateTime() {
688 if (!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS))
689 return 0;
thestig1cd352e2016-06-07 17:53:06 -0700690 time_t t = time(nullptr);
Tom Sepez39bfe122015-09-17 15:25:23 -0700691 struct tm* pTm = localtime(&t);
692
693 int year = pTm->tm_year + 1900;
694 double t1 = _TimeFromYear(year);
695
696 return t1 + pTm->tm_yday * 86400000.0 + pTm->tm_hour * 3600000.0 +
697 pTm->tm_min * 60000.0 + pTm->tm_sec * 1000.0;
698}
699
700int JS_GetYearFromTime(double dt) {
701 return _YearFromTime(dt);
702}
703
704int JS_GetMonthFromTime(double dt) {
705 return _MonthFromTime(dt);
706}
707
708int JS_GetDayFromTime(double dt) {
709 return _DateFromTime(dt);
710}
711
712int JS_GetHourFromTime(double dt) {
tsepez86a61dc2016-03-25 10:00:11 -0700713 return (int)_Mod(floor(dt / (60 * 60 * 1000)), 24);
Tom Sepez39bfe122015-09-17 15:25:23 -0700714}
715
716int JS_GetMinFromTime(double dt) {
tsepez86a61dc2016-03-25 10:00:11 -0700717 return (int)_Mod(floor(dt / (60 * 1000)), 60);
Tom Sepez39bfe122015-09-17 15:25:23 -0700718}
719
720int JS_GetSecFromTime(double dt) {
tsepez86a61dc2016-03-25 10:00:11 -0700721 return (int)_Mod(floor(dt / 1000), 60);
Tom Sepez39bfe122015-09-17 15:25:23 -0700722}
723
tsepez018935c2016-04-15 13:15:12 -0700724double JS_DateParse(const CFX_WideString& str) {
Tom Sepez39bfe122015-09-17 15:25:23 -0700725 v8::Isolate* pIsolate = v8::Isolate::GetCurrent();
726 v8::Isolate::Scope isolate_scope(pIsolate);
727 v8::HandleScope scope(pIsolate);
728
729 v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
730
731 // Use the built-in object method.
732 v8::Local<v8::Value> v =
733 context->Global()
734 ->Get(context, v8::String::NewFromUtf8(pIsolate, "Date",
735 v8::NewStringType::kNormal)
736 .ToLocalChecked())
737 .ToLocalChecked();
738 if (v->IsObject()) {
739 v8::Local<v8::Object> o = v->ToObject(context).ToLocalChecked();
740 v = o->Get(context, v8::String::NewFromUtf8(pIsolate, "parse",
741 v8::NewStringType::kNormal)
Dan Sinclairf766ad22016-03-14 13:51:24 -0400742 .ToLocalChecked())
743 .ToLocalChecked();
Tom Sepez39bfe122015-09-17 15:25:23 -0700744 if (v->IsFunction()) {
745 v8::Local<v8::Function> funC = v8::Local<v8::Function>::Cast(v);
Tom Sepez39bfe122015-09-17 15:25:23 -0700746 const int argc = 1;
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500747 v8::Local<v8::String> timeStr = FXJS_WSToJSString(pIsolate, str);
Tom Sepez39bfe122015-09-17 15:25:23 -0700748 v8::Local<v8::Value> argv[argc] = {timeStr};
749 v = funC->Call(context, context->Global(), argc, argv).ToLocalChecked();
750 if (v->IsNumber()) {
751 double date = v->ToNumber(context).ToLocalChecked()->Value();
752 if (!_isfinite(date))
753 return date;
tsepez86a61dc2016-03-25 10:00:11 -0700754 return JS_LocalTime(date);
Tom Sepez39bfe122015-09-17 15:25:23 -0700755 }
756 }
757 }
758 return 0;
759}
760
761double JS_MakeDay(int nYear, int nMonth, int nDate) {
762 if (!_isfinite(nYear) || !_isfinite(nMonth) || !_isfinite(nDate))
763 return GetNan();
764 double y = _toInteger(nYear);
765 double m = _toInteger(nMonth);
766 double dt = _toInteger(nDate);
767 double ym = y + FXSYS_floor((double)m / 12);
768 double mn = _Mod(m, 12);
769
770 double t = _TimeFromYearMonth((int)ym, (int)mn);
771
772 if (_YearFromTime(t) != ym || _MonthFromTime(t) != mn ||
773 _DateFromTime(t) != 1)
774 return GetNan();
775 return _Day(t) + dt - 1;
776}
777
778double JS_MakeTime(int nHour, int nMin, int nSec, int nMs) {
779 if (!_isfinite(nHour) || !_isfinite(nMin) || !_isfinite(nSec) ||
780 !_isfinite(nMs))
781 return GetNan();
782
783 double h = _toInteger(nHour);
784 double m = _toInteger(nMin);
785 double s = _toInteger(nSec);
786 double milli = _toInteger(nMs);
787
788 return h * 3600000 + m * 60000 + s * 1000 + milli;
789}
790
791double JS_MakeDate(double day, double time) {
792 if (!_isfinite(day) || !_isfinite(time))
793 return GetNan();
794
795 return day * 86400000 + time;
796}
797
798bool JS_PortIsNan(double d) {
799 return d != d;
800}
801
802double JS_LocalTime(double d) {
tsepez86a61dc2016-03-25 10:00:11 -0700803 return d + _getLocalTZA() + _getDaylightSavingTA(d);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700804}
Tom Sepezbd932572016-01-29 09:10:41 -0800805
806std::vector<CJS_Value> JS_ExpandKeywordParams(
807 CJS_Runtime* pRuntime,
808 const std::vector<CJS_Value>& originals,
809 size_t nKeywords,
810 ...) {
811 ASSERT(nKeywords);
812
813 std::vector<CJS_Value> result(nKeywords, CJS_Value(pRuntime));
814 size_t size = std::min(originals.size(), nKeywords);
815 for (size_t i = 0; i < size; ++i)
816 result[i] = originals[i];
817
818 if (originals.size() != 1 || originals[0].GetType() != CJS_Value::VT_object ||
819 originals[0].IsArrayObject()) {
820 return result;
821 }
822 v8::Local<v8::Object> pObj = originals[0].ToV8Object();
823 result[0] = CJS_Value(pRuntime); // Make unknown.
824
825 va_list ap;
826 va_start(ap, nKeywords);
Wei Li89409932016-03-28 10:33:33 -0700827 for (size_t i = 0; i < nKeywords; ++i) {
Tom Sepezbd932572016-01-29 09:10:41 -0800828 const wchar_t* property = va_arg(ap, const wchar_t*);
829 v8::Local<v8::Value> v8Value =
830 FXJS_GetObjectElement(pRuntime->GetIsolate(), pObj, property);
831 if (!v8Value->IsUndefined())
tsepez40faa792016-07-15 17:58:02 -0700832 result[i] = CJS_Value(pRuntime, v8Value);
Tom Sepezbd932572016-01-29 09:10:41 -0800833 }
834 va_end(ap);
835 return result;
836}