blob: c63963a110fd2ef9dadafd7b3334298aee4187c6 [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>
Tom Sepezbd932572016-01-29 09:10:41 -080010#include <algorithm>
Tom Sepez39bfe122015-09-17 15:25:23 -070011#include <cmath>
12#include <limits>
13
Tom Sepez37458412015-10-06 11:33:46 -070014#include "Document.h"
15#include "JS_Define.h"
16#include "JS_Object.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070017
Tom Sepez39bfe122015-09-17 15:25:23 -070018static const FX_DWORD g_nan[2] = {0, 0x7FF80000};
19static double GetNan() {
20 return *(double*)g_nan;
21}
22
Tom Sepez67fd5df2015-10-08 12:24:19 -070023CJS_Value::CJS_Value(CJS_Runtime* pRuntime)
24 : m_eType(VT_unknown), m_pJSRuntime(pRuntime) {
Tom Sepez39bfe122015-09-17 15:25:23 -070025}
Nico Weber9d8ec5a2015-08-04 13:00:21 -070026
Tom Sepez67fd5df2015-10-08 12:24:19 -070027CJS_Value::CJS_Value(CJS_Runtime* pRuntime, v8::Local<v8::Value> pValue, Type t)
28 : m_eType(t), m_pValue(pValue), m_pJSRuntime(pRuntime) {
29}
30
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 }
154 v8::TryCatch(m_pJSRuntime->GetIsolate());
155 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 {
277 // if (GetType() == VT_date)
278 // {
279 // date = (double)(*this);
280 // return TRUE;
281 // }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700282
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700283 if (IsDateObject()) {
284 date.Attach(m_pValue);
285 return TRUE;
286 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700287
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700288 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700289}
290
291/* ---------------------------- CJS_PropValue ---------------------------- */
292
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700293CJS_PropValue::CJS_PropValue(const CJS_Value& value)
294 : CJS_Value(value), m_bIsSetting(0) {}
295
Tom Sepez67fd5df2015-10-08 12:24:19 -0700296CJS_PropValue::CJS_PropValue(CJS_Runtime* pRuntime)
297 : CJS_Value(pRuntime), m_bIsSetting(0) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700298}
299
Tom Sepez67fd5df2015-10-08 12:24:19 -0700300CJS_PropValue::~CJS_PropValue() {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700301}
302
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700303void CJS_PropValue::operator<<(int iValue) {
304 ASSERT(!m_bIsSetting);
305 CJS_Value::operator=(iValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700306}
307
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700308void CJS_PropValue::operator>>(int& iValue) const {
309 ASSERT(m_bIsSetting);
310 iValue = CJS_Value::ToInt();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700311}
312
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700313void CJS_PropValue::operator<<(bool bValue) {
314 ASSERT(!m_bIsSetting);
315 CJS_Value::operator=(bValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700316}
317
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700318void CJS_PropValue::operator>>(bool& bValue) const {
319 ASSERT(m_bIsSetting);
320 bValue = CJS_Value::ToBool();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700321}
322
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700323void CJS_PropValue::operator<<(double dValue) {
324 ASSERT(!m_bIsSetting);
325 CJS_Value::operator=(dValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700326}
327
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700328void CJS_PropValue::operator>>(double& dValue) const {
329 ASSERT(m_bIsSetting);
330 dValue = CJS_Value::ToDouble();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700331}
332
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700333void CJS_PropValue::operator<<(CJS_Object* pObj) {
334 ASSERT(!m_bIsSetting);
335 CJS_Value::operator=(pObj);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700336}
337
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700338void CJS_PropValue::operator>>(CJS_Object*& ppObj) const {
339 ASSERT(m_bIsSetting);
340 ppObj = CJS_Value::ToCJSObject();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700341}
342
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700343void CJS_PropValue::operator<<(CJS_Document* pJsDoc) {
344 ASSERT(!m_bIsSetting);
345 CJS_Value::operator=(pJsDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700346}
347
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700348void CJS_PropValue::operator>>(CJS_Document*& ppJsDoc) const {
349 ASSERT(m_bIsSetting);
350 ppJsDoc = static_cast<CJS_Document*>(CJS_Value::ToCJSObject());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700351}
352
Tom Sepez808a99e2015-09-10 12:28:37 -0700353void CJS_PropValue::operator<<(v8::Local<v8::Object> pObj) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700354 ASSERT(!m_bIsSetting);
355 CJS_Value::operator=(pObj);
JUN FANG33f6f0d2015-04-06 12:39:51 -0700356}
357
Tom Sepez808a99e2015-09-10 12:28:37 -0700358void CJS_PropValue::operator>>(v8::Local<v8::Object>& ppObj) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700359 ASSERT(m_bIsSetting);
360 ppObj = CJS_Value::ToV8Object();
JUN FANG33f6f0d2015-04-06 12:39:51 -0700361}
362
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700363void CJS_PropValue::StartSetting() {
364 m_bIsSetting = 1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700365}
366
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700367void CJS_PropValue::StartGetting() {
368 m_bIsSetting = 0;
369}
370void CJS_PropValue::operator<<(CFX_ByteString string) {
371 ASSERT(!m_bIsSetting);
372 CJS_Value::operator=(string.c_str());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700373}
374
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700375void CJS_PropValue::operator>>(CFX_ByteString& string) const {
376 ASSERT(m_bIsSetting);
377 string = CJS_Value::ToCFXByteString();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700378}
379
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700380void CJS_PropValue::operator<<(const FX_WCHAR* c_string) {
381 ASSERT(!m_bIsSetting);
382 CJS_Value::operator=(c_string);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700383}
384
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700385void CJS_PropValue::operator>>(CFX_WideString& wide_string) const {
386 ASSERT(m_bIsSetting);
387 wide_string = CJS_Value::ToCFXWideString();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700388}
389
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700390void CJS_PropValue::operator<<(CFX_WideString wide_string) {
391 ASSERT(!m_bIsSetting);
392 CJS_Value::operator=(wide_string.c_str());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700393}
394
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700395void CJS_PropValue::operator>>(CJS_Array& array) const {
396 ASSERT(m_bIsSetting);
397 ConvertToArray(array);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700398}
399
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700400void CJS_PropValue::operator<<(CJS_Array& array) {
401 ASSERT(!m_bIsSetting);
402 CJS_Value::operator=(array);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700403}
404
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700405void CJS_PropValue::operator>>(CJS_Date& date) const {
406 ASSERT(m_bIsSetting);
407 ConvertToDate(date);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700408}
409
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700410void CJS_PropValue::operator<<(CJS_Date& date) {
411 ASSERT(!m_bIsSetting);
412 CJS_Value::operator=(date);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700413}
414
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700415CJS_PropValue::operator v8::Local<v8::Value>() const {
416 return m_pValue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700417}
418
Tom Sepez67fd5df2015-10-08 12:24:19 -0700419CJS_Array::CJS_Array(CJS_Runtime* pRuntime) : m_pJSRuntime(pRuntime) {
420}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700421
422CJS_Array::~CJS_Array() {}
423
424void CJS_Array::Attach(v8::Local<v8::Array> pArray) {
425 m_pArray = pArray;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700426}
427
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700428FX_BOOL CJS_Array::IsAttached() {
429 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700430}
431
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700432void CJS_Array::GetElement(unsigned index, CJS_Value& value) {
433 if (m_pArray.IsEmpty())
434 return;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700435 v8::Local<v8::Value> p =
436 FXJS_GetArrayElement(m_pJSRuntime->GetIsolate(), m_pArray, index);
Tom Sepez39bfe122015-09-17 15:25:23 -0700437 value.Attach(p, CJS_Value::VT_object);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700438}
439
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700440void CJS_Array::SetElement(unsigned index, CJS_Value value) {
441 if (m_pArray.IsEmpty())
Tom Sepez67fd5df2015-10-08 12:24:19 -0700442 m_pArray = FXJS_NewArray(m_pJSRuntime->GetIsolate());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700443
Tom Sepez67fd5df2015-10-08 12:24:19 -0700444 FXJS_PutArrayElement(m_pJSRuntime->GetIsolate(), m_pArray, index,
445 value.ToV8Value());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700446}
447
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700448int CJS_Array::GetLength() {
449 if (m_pArray.IsEmpty())
450 return 0;
Tom Sepez39bfe122015-09-17 15:25:23 -0700451 return FXJS_GetArrayLength(m_pArray);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700452}
453
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700454CJS_Array::operator v8::Local<v8::Array>() {
455 if (m_pArray.IsEmpty())
Tom Sepez67fd5df2015-10-08 12:24:19 -0700456 m_pArray = FXJS_NewArray(m_pJSRuntime->GetIsolate());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700457
458 return m_pArray;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700459}
460
Tom Sepez67fd5df2015-10-08 12:24:19 -0700461CJS_Date::CJS_Date(CJS_Runtime* pRuntime) : m_pJSRuntime(pRuntime) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700462}
463
Tom Sepez67fd5df2015-10-08 12:24:19 -0700464CJS_Date::CJS_Date(CJS_Runtime* pRuntime, double dMsecTime)
465 : m_pJSRuntime(pRuntime) {
466 m_pDate = FXJS_NewDate(pRuntime->GetIsolate(), dMsecTime);
467}
468
469CJS_Date::CJS_Date(CJS_Runtime* pRuntime,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700470 int year,
471 int mon,
472 int day,
473 int hour,
474 int min,
Tom Sepez67fd5df2015-10-08 12:24:19 -0700475 int sec)
476 : m_pJSRuntime(pRuntime) {
477 m_pDate = FXJS_NewDate(pRuntime->GetIsolate(),
478 MakeDate(year, mon, day, hour, min, sec, 0));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700479}
480
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700481double CJS_Date::MakeDate(int year,
482 int mon,
483 int day,
484 int hour,
485 int min,
486 int sec,
487 int ms) {
488 return JS_MakeDate(JS_MakeDay(year, mon, day),
489 JS_MakeTime(hour, min, sec, ms));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700490}
491
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700492CJS_Date::~CJS_Date() {}
493
494FX_BOOL CJS_Date::IsValidDate() {
495 if (m_pDate.IsEmpty())
496 return FALSE;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700497 return !JS_PortIsNan(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700498}
499
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700500void CJS_Date::Attach(v8::Local<v8::Value> pDate) {
501 m_pDate = pDate;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700502}
503
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700504int CJS_Date::GetYear() {
505 if (IsValidDate())
Tom Sepez67fd5df2015-10-08 12:24:19 -0700506 return JS_GetYearFromTime(
507 JS_LocalTime(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate)));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700508
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700509 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700510}
511
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700512void CJS_Date::SetYear(int iYear) {
513 double date = MakeDate(iYear, GetMonth(), GetDay(), GetHours(), GetMinutes(),
514 GetSeconds(), 0);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700515 FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_pJSRuntime->GetIsolate(), date));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700516}
517
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700518int CJS_Date::GetMonth() {
519 if (IsValidDate())
Tom Sepez67fd5df2015-10-08 12:24:19 -0700520 return JS_GetMonthFromTime(
521 JS_LocalTime(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700522
523 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700524}
525
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700526void CJS_Date::SetMonth(int iMonth) {
527 double date = MakeDate(GetYear(), iMonth, GetDay(), GetHours(), GetMinutes(),
528 GetSeconds(), 0);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700529 FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_pJSRuntime->GetIsolate(), date));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700530}
531
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700532int CJS_Date::GetDay() {
533 if (IsValidDate())
Tom Sepez67fd5df2015-10-08 12:24:19 -0700534 return JS_GetDayFromTime(
535 JS_LocalTime(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700536
537 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700538}
539
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700540void CJS_Date::SetDay(int iDay) {
541 double date = MakeDate(GetYear(), GetMonth(), iDay, GetHours(), GetMinutes(),
542 GetSeconds(), 0);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700543 FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_pJSRuntime->GetIsolate(), date));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700544}
545
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700546int CJS_Date::GetHours() {
547 if (IsValidDate())
Tom Sepez67fd5df2015-10-08 12:24:19 -0700548 return JS_GetHourFromTime(
549 JS_LocalTime(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700550
551 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700552}
553
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700554void CJS_Date::SetHours(int iHours) {
555 double date = MakeDate(GetYear(), GetMonth(), GetDay(), iHours, GetMinutes(),
556 GetSeconds(), 0);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700557 FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_pJSRuntime->GetIsolate(), date));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700558}
559
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700560int CJS_Date::GetMinutes() {
561 if (IsValidDate())
Tom Sepez67fd5df2015-10-08 12:24:19 -0700562 return JS_GetMinFromTime(
563 JS_LocalTime(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700564
565 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700566}
567
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700568void CJS_Date::SetMinutes(int minutes) {
569 double date = MakeDate(GetYear(), GetMonth(), GetDay(), GetHours(), minutes,
570 GetSeconds(), 0);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700571 FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_pJSRuntime->GetIsolate(), date));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700572}
573
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700574int CJS_Date::GetSeconds() {
575 if (IsValidDate())
Tom Sepez67fd5df2015-10-08 12:24:19 -0700576 return JS_GetSecFromTime(
577 JS_LocalTime(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate)));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700578
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700579 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700580}
581
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700582void CJS_Date::SetSeconds(int seconds) {
583 double date = MakeDate(GetYear(), GetMonth(), GetDay(), GetHours(),
584 GetMinutes(), seconds, 0);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700585 FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_pJSRuntime->GetIsolate(), date));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700586}
587
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700588CJS_Date::operator v8::Local<v8::Value>() {
589 return m_pDate;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700590}
591
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700592CJS_Date::operator double() const {
593 if (m_pDate.IsEmpty())
594 return 0.0;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700595 return FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700596}
597
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700598CFX_WideString CJS_Date::ToString() const {
599 if (m_pDate.IsEmpty())
600 return L"";
Tom Sepez67fd5df2015-10-08 12:24:19 -0700601 return FXJS_ToString(m_pJSRuntime->GetIsolate(), m_pDate);
Tom Sepez39bfe122015-09-17 15:25:23 -0700602}
603
604double _getLocalTZA() {
605 if (!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS))
606 return 0;
607 time_t t = 0;
608 time(&t);
609 localtime(&t);
610#if _MSC_VER >= 1900
611 // In gcc and in Visual Studio prior to VS 2015 'timezone' is a global
612 // variable declared in time.h. That variable was deprecated and in VS 2015
613 // is removed, with _get_timezone replacing it.
614 long timezone = 0;
615 _get_timezone(&timezone);
616#endif
617 return (double)(-(timezone * 1000));
618}
619
620int _getDaylightSavingTA(double d) {
621 if (!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS))
622 return 0;
623 time_t t = (time_t)(d / 1000);
624 struct tm* tmp = localtime(&t);
Lei Zhang412e9082015-12-14 18:34:00 -0800625 if (!tmp)
Tom Sepez39bfe122015-09-17 15:25:23 -0700626 return 0;
627 if (tmp->tm_isdst > 0)
628 // One hour.
629 return (int)60 * 60 * 1000;
630 return 0;
631}
632
633double _Mod(double x, double y) {
634 double r = fmod(x, y);
635 if (r < 0)
636 r += y;
637 return r;
638}
639
640int _isfinite(double v) {
641#if _MSC_VER
642 return ::_finite(v);
643#else
644 return std::fabs(v) < std::numeric_limits<double>::max();
645#endif
646}
647
648double _toInteger(double n) {
649 return (n >= 0) ? FXSYS_floor(n) : -FXSYS_floor(-n);
650}
651
652bool _isLeapYear(int year) {
653 return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 != 0));
654}
655
656int _DayFromYear(int y) {
657 return (int)(365 * (y - 1970.0) + FXSYS_floor((y - 1969.0) / 4) -
658 FXSYS_floor((y - 1901.0) / 100) +
659 FXSYS_floor((y - 1601.0) / 400));
660}
661
662double _TimeFromYear(int y) {
Lei Zhang1ac47eb2015-12-21 11:04:44 -0800663 return 86400000.0 * _DayFromYear(y);
Tom Sepez39bfe122015-09-17 15:25:23 -0700664}
665
666double _TimeFromYearMonth(int y, int m) {
667 static int daysMonth[12] = {
668 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
669 static int leapDaysMonth[12] = {
670 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335};
671 int* pMonth = daysMonth;
672 if (_isLeapYear(y))
673 pMonth = leapDaysMonth;
674 return _TimeFromYear(y) + ((double)pMonth[m]) * 86400000;
675}
676
677int _Day(double t) {
678 return (int)FXSYS_floor(t / 86400000);
679}
680
681int _YearFromTime(double t) {
682 // estimate the time.
Lei Zhang1ac47eb2015-12-21 11:04:44 -0800683 int y = 1970 + static_cast<int>(t / (365.2425 * 86400000));
Tom Sepez39bfe122015-09-17 15:25:23 -0700684 if (_TimeFromYear(y) <= t) {
685 while (_TimeFromYear(y + 1) <= t)
686 y++;
687 } else
Lei Zhang1ac47eb2015-12-21 11:04:44 -0800688 while (_TimeFromYear(y) > t)
Tom Sepez39bfe122015-09-17 15:25:23 -0700689 y--;
690 return y;
691}
692
693int _DayWithinYear(double t) {
694 int year = _YearFromTime(t);
695 int day = _Day(t);
696 return day - _DayFromYear(year);
697}
698
699int _MonthFromTime(double t) {
700 int day = _DayWithinYear(t);
701 int year = _YearFromTime(t);
702 if (0 <= day && day < 31)
703 return 0;
704 if (31 <= day && day < 59 + _isLeapYear(year))
705 return 1;
706 if ((59 + _isLeapYear(year)) <= day && day < (90 + _isLeapYear(year)))
707 return 2;
708 if ((90 + _isLeapYear(year)) <= day && day < (120 + _isLeapYear(year)))
709 return 3;
710 if ((120 + _isLeapYear(year)) <= day && day < (151 + _isLeapYear(year)))
711 return 4;
712 if ((151 + _isLeapYear(year)) <= day && day < (181 + _isLeapYear(year)))
713 return 5;
714 if ((181 + _isLeapYear(year)) <= day && day < (212 + _isLeapYear(year)))
715 return 6;
716 if ((212 + _isLeapYear(year)) <= day && day < (243 + _isLeapYear(year)))
717 return 7;
718 if ((243 + _isLeapYear(year)) <= day && day < (273 + _isLeapYear(year)))
719 return 8;
720 if ((273 + _isLeapYear(year)) <= day && day < (304 + _isLeapYear(year)))
721 return 9;
722 if ((304 + _isLeapYear(year)) <= day && day < (334 + _isLeapYear(year)))
723 return 10;
724 if ((334 + _isLeapYear(year)) <= day && day < (365 + _isLeapYear(year)))
725 return 11;
726
727 return -1;
728}
729
730int _DateFromTime(double t) {
731 int day = _DayWithinYear(t);
732 int year = _YearFromTime(t);
733 bool leap = _isLeapYear(year);
734 int month = _MonthFromTime(t);
735 switch (month) {
736 case 0:
737 return day + 1;
738 case 1:
739 return day - 30;
740 case 2:
741 return day - 58 - leap;
742 case 3:
743 return day - 89 - leap;
744 case 4:
745 return day - 119 - leap;
746 case 5:
747 return day - 150 - leap;
748 case 6:
749 return day - 180 - leap;
750 case 7:
751 return day - 211 - leap;
752 case 8:
753 return day - 242 - leap;
754 case 9:
755 return day - 272 - leap;
756 case 10:
757 return day - 303 - leap;
758 case 11:
759 return day - 333 - leap;
760 default:
761 return 0;
762 }
763}
764
765double JS_GetDateTime() {
766 if (!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS))
767 return 0;
768 time_t t = time(NULL);
769 struct tm* pTm = localtime(&t);
770
771 int year = pTm->tm_year + 1900;
772 double t1 = _TimeFromYear(year);
773
774 return t1 + pTm->tm_yday * 86400000.0 + pTm->tm_hour * 3600000.0 +
775 pTm->tm_min * 60000.0 + pTm->tm_sec * 1000.0;
776}
777
778int JS_GetYearFromTime(double dt) {
779 return _YearFromTime(dt);
780}
781
782int JS_GetMonthFromTime(double dt) {
783 return _MonthFromTime(dt);
784}
785
786int JS_GetDayFromTime(double dt) {
787 return _DateFromTime(dt);
788}
789
790int JS_GetHourFromTime(double dt) {
791 return (int)_Mod(FXSYS_floor((double)(dt / (60 * 60 * 1000))), 24);
792}
793
794int JS_GetMinFromTime(double dt) {
795 return (int)_Mod(FXSYS_floor((double)(dt / (60 * 1000))), 60);
796}
797
798int JS_GetSecFromTime(double dt) {
799 return (int)_Mod(FXSYS_floor((double)(dt / 1000)), 60);
800}
801
802double JS_DateParse(const wchar_t* string) {
803 v8::Isolate* pIsolate = v8::Isolate::GetCurrent();
804 v8::Isolate::Scope isolate_scope(pIsolate);
805 v8::HandleScope scope(pIsolate);
806
807 v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
808
809 // Use the built-in object method.
810 v8::Local<v8::Value> v =
811 context->Global()
812 ->Get(context, v8::String::NewFromUtf8(pIsolate, "Date",
813 v8::NewStringType::kNormal)
814 .ToLocalChecked())
815 .ToLocalChecked();
816 if (v->IsObject()) {
817 v8::Local<v8::Object> o = v->ToObject(context).ToLocalChecked();
818 v = o->Get(context, v8::String::NewFromUtf8(pIsolate, "parse",
819 v8::NewStringType::kNormal)
820 .ToLocalChecked()).ToLocalChecked();
821 if (v->IsFunction()) {
822 v8::Local<v8::Function> funC = v8::Local<v8::Function>::Cast(v);
823
824 const int argc = 1;
825 v8::Local<v8::String> timeStr = FXJS_WSToJSString(pIsolate, string);
826 v8::Local<v8::Value> argv[argc] = {timeStr};
827 v = funC->Call(context, context->Global(), argc, argv).ToLocalChecked();
828 if (v->IsNumber()) {
829 double date = v->ToNumber(context).ToLocalChecked()->Value();
830 if (!_isfinite(date))
831 return date;
832 return date + _getLocalTZA() + _getDaylightSavingTA(date);
833 }
834 }
835 }
836 return 0;
837}
838
839double JS_MakeDay(int nYear, int nMonth, int nDate) {
840 if (!_isfinite(nYear) || !_isfinite(nMonth) || !_isfinite(nDate))
841 return GetNan();
842 double y = _toInteger(nYear);
843 double m = _toInteger(nMonth);
844 double dt = _toInteger(nDate);
845 double ym = y + FXSYS_floor((double)m / 12);
846 double mn = _Mod(m, 12);
847
848 double t = _TimeFromYearMonth((int)ym, (int)mn);
849
850 if (_YearFromTime(t) != ym || _MonthFromTime(t) != mn ||
851 _DateFromTime(t) != 1)
852 return GetNan();
853 return _Day(t) + dt - 1;
854}
855
856double JS_MakeTime(int nHour, int nMin, int nSec, int nMs) {
857 if (!_isfinite(nHour) || !_isfinite(nMin) || !_isfinite(nSec) ||
858 !_isfinite(nMs))
859 return GetNan();
860
861 double h = _toInteger(nHour);
862 double m = _toInteger(nMin);
863 double s = _toInteger(nSec);
864 double milli = _toInteger(nMs);
865
866 return h * 3600000 + m * 60000 + s * 1000 + milli;
867}
868
869double JS_MakeDate(double day, double time) {
870 if (!_isfinite(day) || !_isfinite(time))
871 return GetNan();
872
873 return day * 86400000 + time;
874}
875
876bool JS_PortIsNan(double d) {
877 return d != d;
878}
879
880double JS_LocalTime(double d) {
881 return JS_GetDateTime() + _getDaylightSavingTA(d);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700882}
Tom Sepezbd932572016-01-29 09:10:41 -0800883
884std::vector<CJS_Value> JS_ExpandKeywordParams(
885 CJS_Runtime* pRuntime,
886 const std::vector<CJS_Value>& originals,
887 size_t nKeywords,
888 ...) {
889 ASSERT(nKeywords);
890
891 std::vector<CJS_Value> result(nKeywords, CJS_Value(pRuntime));
892 size_t size = std::min(originals.size(), nKeywords);
893 for (size_t i = 0; i < size; ++i)
894 result[i] = originals[i];
895
896 if (originals.size() != 1 || originals[0].GetType() != CJS_Value::VT_object ||
897 originals[0].IsArrayObject()) {
898 return result;
899 }
900 v8::Local<v8::Object> pObj = originals[0].ToV8Object();
901 result[0] = CJS_Value(pRuntime); // Make unknown.
902
903 va_list ap;
904 va_start(ap, nKeywords);
905 for (int i = 0; i < nKeywords; ++i) {
906 const wchar_t* property = va_arg(ap, const wchar_t*);
907 v8::Local<v8::Value> v8Value =
908 FXJS_GetObjectElement(pRuntime->GetIsolate(), pObj, property);
909 if (!v8Value->IsUndefined())
910 result[i] = CJS_Value(pRuntime, v8Value, CJS_Value::VT_unknown);
911 }
912 va_end(ap);
913 return result;
914}