Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(59)

Side by Side Diff: fpdfsdk/src/javascript/JS_Value.cpp

Issue 1347833002: Ensure functions in FXJS_V8 are prefixed by FXJS_. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Rebase, nits, format. Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « fpdfsdk/src/javascript/JS_Runtime.cpp ('k') | fpdfsdk/src/javascript/PublicMethods.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 PDFium Authors. All rights reserved. 1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 6
7 #include "../../include/javascript/JavaScript.h" 7 #include "../../include/javascript/JavaScript.h"
8 #include "../../include/javascript/JS_Define.h" 8 #include "../../include/javascript/JS_Define.h"
9 #include "../../include/javascript/JS_Object.h" 9 #include "../../include/javascript/JS_Object.h"
10 #include "../../include/javascript/JS_Value.h" 10 #include "../../include/javascript/JS_Value.h"
11 #include "../../include/javascript/Document.h" 11 #include "../../include/javascript/Document.h"
12 12
13 static const FX_DWORD g_nan[2] = {0, 0x7FF80000};
14 static double GetNan() {
15 return *(double*)g_nan;
16 }
17
13 /* ---------------------------- CJS_Value ---------------------------- */ 18 /* ---------------------------- CJS_Value ---------------------------- */
14 19
15 CJS_Value::CJS_Value(v8::Isolate* isolate) 20 CJS_Value::CJS_Value(v8::Isolate* isolate)
16 : m_eType(VT_unknown), m_isolate(isolate) {} 21 : m_eType(VT_unknown), m_isolate(isolate) {}
17 CJS_Value::CJS_Value(v8::Isolate* isolate, 22 CJS_Value::CJS_Value(v8::Isolate* isolate, v8::Local<v8::Value> pValue, Type t)
18 v8::Local<v8::Value> pValue, 23 : m_eType(t), m_pValue(pValue), m_isolate(isolate) {
19 FXJSVALUETYPE t) 24 }
20 : m_pValue(pValue), m_eType(t), m_isolate(isolate) {}
21 25
22 CJS_Value::CJS_Value(v8::Isolate* isolate, const int& iValue) 26 CJS_Value::CJS_Value(v8::Isolate* isolate, const int& iValue)
23 : m_isolate(isolate) { 27 : m_isolate(isolate) {
24 operator=(iValue); 28 operator=(iValue);
25 } 29 }
26 30
27 CJS_Value::CJS_Value(v8::Isolate* isolate, const bool& bValue) 31 CJS_Value::CJS_Value(v8::Isolate* isolate, const bool& bValue)
28 : m_isolate(isolate) { 32 : m_isolate(isolate) {
29 operator=(bValue); 33 operator=(bValue);
30 } 34 }
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 operator=(pStr); 70 operator=(pStr);
67 } 71 }
68 72
69 CJS_Value::CJS_Value(v8::Isolate* isolate, CJS_Array& array) 73 CJS_Value::CJS_Value(v8::Isolate* isolate, CJS_Array& array)
70 : m_isolate(isolate) { 74 : m_isolate(isolate) {
71 operator=(array); 75 operator=(array);
72 } 76 }
73 77
74 CJS_Value::~CJS_Value() {} 78 CJS_Value::~CJS_Value() {}
75 79
76 void CJS_Value::Attach(v8::Local<v8::Value> pValue, FXJSVALUETYPE t) { 80 void CJS_Value::Attach(v8::Local<v8::Value> pValue, Type t) {
77 m_pValue = pValue; 81 m_pValue = pValue;
78 m_eType = t; 82 m_eType = t;
79 } 83 }
80 84
81 void CJS_Value::Attach(CJS_Value* pValue) { 85 void CJS_Value::Attach(CJS_Value* pValue) {
82 if (pValue) 86 if (pValue)
83 Attach(pValue->ToV8Value(), pValue->GetType()); 87 Attach(pValue->ToV8Value(), pValue->GetType());
84 } 88 }
85 89
86 void CJS_Value::Detach() { 90 void CJS_Value::Detach() {
87 m_pValue = v8::Local<v8::Value>(); 91 m_pValue = v8::Local<v8::Value>();
88 m_eType = VT_unknown; 92 m_eType = VT_unknown;
89 } 93 }
90 94
91 /* ----------------------------------------------------------------------------- ----------- 95 /* ----------------------------------------------------------------------------- -----------
92 */ 96 */
93 97
94 int CJS_Value::ToInt() const { 98 int CJS_Value::ToInt() const {
95 return JS_ToInt32(m_isolate, m_pValue); 99 return FXJS_ToInt32(m_isolate, m_pValue);
96 } 100 }
97 101
98 bool CJS_Value::ToBool() const { 102 bool CJS_Value::ToBool() const {
99 return JS_ToBoolean(m_isolate, m_pValue); 103 return FXJS_ToBoolean(m_isolate, m_pValue);
100 } 104 }
101 105
102 double CJS_Value::ToDouble() const { 106 double CJS_Value::ToDouble() const {
103 return JS_ToNumber(m_isolate, m_pValue); 107 return FXJS_ToNumber(m_isolate, m_pValue);
104 } 108 }
105 109
106 float CJS_Value::ToFloat() const { 110 float CJS_Value::ToFloat() const {
107 return (float)ToDouble(); 111 return (float)ToDouble();
108 } 112 }
109 113
110 CJS_Object* CJS_Value::ToCJSObject() const { 114 CJS_Object* CJS_Value::ToCJSObject() const {
111 v8::Local<v8::Object> pObj = JS_ToObject(m_isolate, m_pValue); 115 v8::Local<v8::Object> pObj = FXJS_ToObject(m_isolate, m_pValue);
112 return (CJS_Object*)JS_GetPrivate(m_isolate, pObj); 116 return (CJS_Object*)FXJS_GetPrivate(m_isolate, pObj);
113 } 117 }
114 118
115 v8::Local<v8::Object> CJS_Value::ToV8Object() const { 119 v8::Local<v8::Object> CJS_Value::ToV8Object() const {
116 return JS_ToObject(m_isolate, m_pValue); 120 return FXJS_ToObject(m_isolate, m_pValue);
117 } 121 }
118 122
119 CFX_WideString CJS_Value::ToCFXWideString() const { 123 CFX_WideString CJS_Value::ToCFXWideString() const {
120 return JS_ToString(m_isolate, m_pValue); 124 return FXJS_ToString(m_isolate, m_pValue);
121 } 125 }
122 126
123 CFX_ByteString CJS_Value::ToCFXByteString() const { 127 CFX_ByteString CJS_Value::ToCFXByteString() const {
124 return CFX_ByteString::FromUnicode(ToCFXWideString()); 128 return CFX_ByteString::FromUnicode(ToCFXWideString());
125 } 129 }
126 130
127 v8::Local<v8::Value> CJS_Value::ToV8Value() const { 131 v8::Local<v8::Value> CJS_Value::ToV8Value() const {
128 return m_pValue; 132 return m_pValue;
129 } 133 }
130 134
131 v8::Local<v8::Array> CJS_Value::ToV8Array() const { 135 v8::Local<v8::Array> CJS_Value::ToV8Array() const {
132 if (IsArrayObject()) 136 if (IsArrayObject())
133 return v8::Local<v8::Array>::Cast(JS_ToObject(m_isolate, m_pValue)); 137 return v8::Local<v8::Array>::Cast(FXJS_ToObject(m_isolate, m_pValue));
134 return v8::Local<v8::Array>(); 138 return v8::Local<v8::Array>();
135 } 139 }
136 140
137 /* ----------------------------------------------------------------------------- ----------- 141 /* ----------------------------------------------------------------------------- -----------
138 */ 142 */
139 143
140 void CJS_Value::operator=(int iValue) { 144 void CJS_Value::operator=(int iValue) {
141 m_pValue = JS_NewNumber(m_isolate, iValue); 145 m_pValue = FXJS_NewNumber(m_isolate, iValue);
142
143 m_eType = VT_number; 146 m_eType = VT_number;
144 } 147 }
145 148
146 void CJS_Value::operator=(bool bValue) { 149 void CJS_Value::operator=(bool bValue) {
147 m_pValue = JS_NewBoolean(m_isolate, bValue); 150 m_pValue = FXJS_NewBoolean(m_isolate, bValue);
148
149 m_eType = VT_boolean; 151 m_eType = VT_boolean;
150 } 152 }
151 153
152 void CJS_Value::operator=(double dValue) { 154 void CJS_Value::operator=(double dValue) {
153 m_pValue = JS_NewNumber(m_isolate, dValue); 155 m_pValue = FXJS_NewNumber(m_isolate, dValue);
154
155 m_eType = VT_number; 156 m_eType = VT_number;
156 } 157 }
157 158
158 void CJS_Value::operator=(float fValue) { 159 void CJS_Value::operator=(float fValue) {
159 m_pValue = JS_NewNumber(m_isolate, fValue); 160 m_pValue = FXJS_NewNumber(m_isolate, fValue);
160 m_eType = VT_number; 161 m_eType = VT_number;
161 } 162 }
162 163
163 void CJS_Value::operator=(v8::Local<v8::Object> pObj) { 164 void CJS_Value::operator=(v8::Local<v8::Object> pObj) {
164 m_pValue = JS_NewObject(m_isolate, pObj); 165 m_pValue = FXJS_NewObject(m_isolate, pObj);
165
166 m_eType = VT_fxobject; 166 m_eType = VT_fxobject;
167 } 167 }
168 168
169 void CJS_Value::operator=(CJS_Object* pObj) { 169 void CJS_Value::operator=(CJS_Object* pObj) {
170 if (pObj) 170 if (pObj)
171 operator=((v8::Local<v8::Object>)*pObj); 171 operator=((v8::Local<v8::Object>)*pObj);
172 } 172 }
173 173
174 void CJS_Value::operator=(CJS_Document* pJsDoc) { 174 void CJS_Value::operator=(CJS_Document* pJsDoc) {
175 m_eType = VT_object; 175 m_eType = VT_object;
176 if (pJsDoc) { 176 if (pJsDoc) {
177 m_pValue = static_cast<v8::Local<v8::Object>>(*pJsDoc); 177 m_pValue = static_cast<v8::Local<v8::Object>>(*pJsDoc);
178 } 178 }
179 } 179 }
180 180
181 void CJS_Value::operator=(const FX_WCHAR* pWstr) { 181 void CJS_Value::operator=(const FX_WCHAR* pWstr) {
182 m_pValue = JS_NewString(m_isolate, (wchar_t*)pWstr); 182 m_pValue = FXJS_NewString(m_isolate, (wchar_t*)pWstr);
183
184 m_eType = VT_string; 183 m_eType = VT_string;
185 } 184 }
186 185
187 void CJS_Value::SetNull() { 186 void CJS_Value::SetNull() {
188 m_pValue = JS_NewNull(); 187 m_pValue = FXJS_NewNull();
189
190 m_eType = VT_null; 188 m_eType = VT_null;
191 } 189 }
192 190
193 void CJS_Value::operator=(const FX_CHAR* pStr) { 191 void CJS_Value::operator=(const FX_CHAR* pStr) {
194 operator=(CFX_WideString::FromLocal(pStr).c_str()); 192 operator=(CFX_WideString::FromLocal(pStr).c_str());
195 } 193 }
196 194
197 void CJS_Value::operator=(CJS_Array& array) { 195 void CJS_Value::operator=(CJS_Array& array) {
198 m_pValue = JS_NewObject2(m_isolate, (v8::Local<v8::Array>)array); 196 m_pValue = FXJS_NewObject2(m_isolate, (v8::Local<v8::Array>)array);
199
200 m_eType = VT_object; 197 m_eType = VT_object;
201 } 198 }
202 199
203 void CJS_Value::operator=(CJS_Date& date) { 200 void CJS_Value::operator=(CJS_Date& date) {
204 m_pValue = JS_NewDate(m_isolate, (double)date); 201 m_pValue = FXJS_NewDate(m_isolate, (double)date);
205
206 m_eType = VT_date; 202 m_eType = VT_date;
207 } 203 }
208 204
209 void CJS_Value::operator=(CJS_Value value) { 205 void CJS_Value::operator=(CJS_Value value) {
210 m_pValue = value.ToV8Value(); 206 m_pValue = value.ToV8Value();
211
212 m_eType = value.m_eType; 207 m_eType = value.m_eType;
213 m_isolate = value.m_isolate; 208 m_isolate = value.m_isolate;
214 } 209 }
215 210
216 /* ----------------------------------------------------------------------------- ----------- 211 /* ----------------------------------------------------------------------------- -----------
217 */ 212 */
218 213
219 FXJSVALUETYPE CJS_Value::GetType() const { 214 CJS_Value::Type CJS_Value::GetType() const {
220 if (m_pValue.IsEmpty()) 215 if (m_pValue.IsEmpty())
221 return VT_unknown; 216 return VT_unknown;
222 if (m_pValue->IsString()) 217 if (m_pValue->IsString())
223 return VT_string; 218 return VT_string;
224 if (m_pValue->IsNumber()) 219 if (m_pValue->IsNumber())
225 return VT_number; 220 return VT_number;
226 if (m_pValue->IsBoolean()) 221 if (m_pValue->IsBoolean())
227 return VT_boolean; 222 return VT_boolean;
228 if (m_pValue->IsDate()) 223 if (m_pValue->IsDate())
229 return VT_date; 224 return VT_date;
(...skipping 14 matching lines...) Expand all
244 239
245 FX_BOOL CJS_Value::IsDateObject() const { 240 FX_BOOL CJS_Value::IsDateObject() const {
246 if (m_pValue.IsEmpty()) 241 if (m_pValue.IsEmpty())
247 return FALSE; 242 return FALSE;
248 return m_pValue->IsDate(); 243 return m_pValue->IsDate();
249 } 244 }
250 245
251 // CJS_Value::operator CJS_Array() 246 // CJS_Value::operator CJS_Array()
252 FX_BOOL CJS_Value::ConvertToArray(CJS_Array& array) const { 247 FX_BOOL CJS_Value::ConvertToArray(CJS_Array& array) const {
253 if (IsArrayObject()) { 248 if (IsArrayObject()) {
254 array.Attach(JS_ToArray(m_isolate, m_pValue)); 249 array.Attach(FXJS_ToArray(m_isolate, m_pValue));
255 return TRUE; 250 return TRUE;
256 } 251 }
257 252
258 return FALSE; 253 return FALSE;
259 } 254 }
260 255
261 FX_BOOL CJS_Value::ConvertToDate(CJS_Date& date) const { 256 FX_BOOL CJS_Value::ConvertToDate(CJS_Date& date) const {
262 // if (GetType() == VT_date) 257 // if (GetType() == VT_date)
263 // { 258 // {
264 // date = (double)(*this); 259 // date = (double)(*this);
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
417 m_pArray = pArray; 412 m_pArray = pArray;
418 } 413 }
419 414
420 FX_BOOL CJS_Array::IsAttached() { 415 FX_BOOL CJS_Array::IsAttached() {
421 return FALSE; 416 return FALSE;
422 } 417 }
423 418
424 void CJS_Array::GetElement(unsigned index, CJS_Value& value) { 419 void CJS_Array::GetElement(unsigned index, CJS_Value& value) {
425 if (m_pArray.IsEmpty()) 420 if (m_pArray.IsEmpty())
426 return; 421 return;
427 v8::Local<v8::Value> p = JS_GetArrayElement(m_isolate, m_pArray, index); 422 v8::Local<v8::Value> p = FXJS_GetArrayElement(m_isolate, m_pArray, index);
428 value.Attach(p, VT_object); 423 value.Attach(p, CJS_Value::VT_object);
429 } 424 }
430 425
431 void CJS_Array::SetElement(unsigned index, CJS_Value value) { 426 void CJS_Array::SetElement(unsigned index, CJS_Value value) {
432 if (m_pArray.IsEmpty()) 427 if (m_pArray.IsEmpty())
433 m_pArray = JS_NewArray(m_isolate); 428 m_pArray = FXJS_NewArray(m_isolate);
434 429
435 JS_PutArrayElement(m_isolate, m_pArray, index, value.ToV8Value(), 430 FXJS_PutArrayElement(m_isolate, m_pArray, index, value.ToV8Value());
436 value.GetType());
437 } 431 }
438 432
439 int CJS_Array::GetLength() { 433 int CJS_Array::GetLength() {
440 if (m_pArray.IsEmpty()) 434 if (m_pArray.IsEmpty())
441 return 0; 435 return 0;
442 return JS_GetArrayLength(m_pArray); 436 return FXJS_GetArrayLength(m_pArray);
443 } 437 }
444 438
445 CJS_Array::operator v8::Local<v8::Array>() { 439 CJS_Array::operator v8::Local<v8::Array>() {
446 if (m_pArray.IsEmpty()) 440 if (m_pArray.IsEmpty())
447 m_pArray = JS_NewArray(m_isolate); 441 m_pArray = FXJS_NewArray(m_isolate);
448 442
449 return m_pArray; 443 return m_pArray;
450 } 444 }
451 445
452 /* ======================================== CJS_Date 446 /* ======================================== CJS_Date
453 * ========================================= */ 447 * ========================================= */
454 448
455 CJS_Date::CJS_Date(v8::Isolate* isolate) : m_isolate(isolate) {} 449 CJS_Date::CJS_Date(v8::Isolate* isolate) : m_isolate(isolate) {}
456 450
457 CJS_Date::CJS_Date(v8::Isolate* isolate, double dMsec_time) { 451 CJS_Date::CJS_Date(v8::Isolate* isolate, double dMsec_time) {
458 m_isolate = isolate; 452 m_isolate = isolate;
459 m_pDate = JS_NewDate(isolate, dMsec_time); 453 m_pDate = FXJS_NewDate(isolate, dMsec_time);
460 } 454 }
461 455
462 CJS_Date::CJS_Date(v8::Isolate* isolate, 456 CJS_Date::CJS_Date(v8::Isolate* isolate,
463 int year, 457 int year,
464 int mon, 458 int mon,
465 int day, 459 int day,
466 int hour, 460 int hour,
467 int min, 461 int min,
468 int sec) { 462 int sec) {
469 m_isolate = isolate; 463 m_isolate = isolate;
470 m_pDate = JS_NewDate(isolate, MakeDate(year, mon, day, hour, min, sec, 0)); 464 m_pDate = FXJS_NewDate(isolate, MakeDate(year, mon, day, hour, min, sec, 0));
471 } 465 }
472 466
473 double CJS_Date::MakeDate(int year, 467 double CJS_Date::MakeDate(int year,
474 int mon, 468 int mon,
475 int day, 469 int day,
476 int hour, 470 int hour,
477 int min, 471 int min,
478 int sec, 472 int sec,
479 int ms) { 473 int ms) {
480 return JS_MakeDate(JS_MakeDay(year, mon, day), 474 return JS_MakeDate(JS_MakeDay(year, mon, day),
481 JS_MakeTime(hour, min, sec, ms)); 475 JS_MakeTime(hour, min, sec, ms));
482 } 476 }
483 477
484 CJS_Date::~CJS_Date() {} 478 CJS_Date::~CJS_Date() {}
485 479
486 FX_BOOL CJS_Date::IsValidDate() { 480 FX_BOOL CJS_Date::IsValidDate() {
487 if (m_pDate.IsEmpty()) 481 if (m_pDate.IsEmpty())
488 return FALSE; 482 return FALSE;
489 return !JS_PortIsNan(JS_ToNumber(m_isolate, m_pDate)); 483 return !JS_PortIsNan(FXJS_ToNumber(m_isolate, m_pDate));
490 } 484 }
491 485
492 void CJS_Date::Attach(v8::Local<v8::Value> pDate) { 486 void CJS_Date::Attach(v8::Local<v8::Value> pDate) {
493 m_pDate = pDate; 487 m_pDate = pDate;
494 } 488 }
495 489
496 int CJS_Date::GetYear() { 490 int CJS_Date::GetYear() {
497 if (IsValidDate()) 491 if (IsValidDate())
498 return JS_GetYearFromTime(JS_LocalTime(JS_ToNumber(m_isolate, m_pDate))); 492 return JS_GetYearFromTime(JS_LocalTime(FXJS_ToNumber(m_isolate, m_pDate)));
499 493
500 return 0; 494 return 0;
501 } 495 }
502 496
503 void CJS_Date::SetYear(int iYear) { 497 void CJS_Date::SetYear(int iYear) {
504 double date = MakeDate(iYear, GetMonth(), GetDay(), GetHours(), GetMinutes(), 498 double date = MakeDate(iYear, GetMonth(), GetDay(), GetHours(), GetMinutes(),
505 GetSeconds(), 0); 499 GetSeconds(), 0);
506 JS_ValueCopy(m_pDate, JS_NewDate(m_isolate, date)); 500 FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_isolate, date));
507 } 501 }
508 502
509 int CJS_Date::GetMonth() { 503 int CJS_Date::GetMonth() {
510 if (IsValidDate()) 504 if (IsValidDate())
511 return JS_GetMonthFromTime(JS_LocalTime(JS_ToNumber(m_isolate, m_pDate))); 505 return JS_GetMonthFromTime(JS_LocalTime(FXJS_ToNumber(m_isolate, m_pDate)));
512 506
513 return 0; 507 return 0;
514 } 508 }
515 509
516 void CJS_Date::SetMonth(int iMonth) { 510 void CJS_Date::SetMonth(int iMonth) {
517 double date = MakeDate(GetYear(), iMonth, GetDay(), GetHours(), GetMinutes(), 511 double date = MakeDate(GetYear(), iMonth, GetDay(), GetHours(), GetMinutes(),
518 GetSeconds(), 0); 512 GetSeconds(), 0);
519 JS_ValueCopy(m_pDate, JS_NewDate(m_isolate, date)); 513 FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_isolate, date));
520 } 514 }
521 515
522 int CJS_Date::GetDay() { 516 int CJS_Date::GetDay() {
523 if (IsValidDate()) 517 if (IsValidDate())
524 return JS_GetDayFromTime(JS_LocalTime(JS_ToNumber(m_isolate, m_pDate))); 518 return JS_GetDayFromTime(JS_LocalTime(FXJS_ToNumber(m_isolate, m_pDate)));
525 519
526 return 0; 520 return 0;
527 } 521 }
528 522
529 void CJS_Date::SetDay(int iDay) { 523 void CJS_Date::SetDay(int iDay) {
530 double date = MakeDate(GetYear(), GetMonth(), iDay, GetHours(), GetMinutes(), 524 double date = MakeDate(GetYear(), GetMonth(), iDay, GetHours(), GetMinutes(),
531 GetSeconds(), 0); 525 GetSeconds(), 0);
532 JS_ValueCopy(m_pDate, JS_NewDate(m_isolate, date)); 526 FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_isolate, date));
533 } 527 }
534 528
535 int CJS_Date::GetHours() { 529 int CJS_Date::GetHours() {
536 if (IsValidDate()) 530 if (IsValidDate())
537 return JS_GetHourFromTime(JS_LocalTime(JS_ToNumber(m_isolate, m_pDate))); 531 return JS_GetHourFromTime(JS_LocalTime(FXJS_ToNumber(m_isolate, m_pDate)));
538 532
539 return 0; 533 return 0;
540 } 534 }
541 535
542 void CJS_Date::SetHours(int iHours) { 536 void CJS_Date::SetHours(int iHours) {
543 double date = MakeDate(GetYear(), GetMonth(), GetDay(), iHours, GetMinutes(), 537 double date = MakeDate(GetYear(), GetMonth(), GetDay(), iHours, GetMinutes(),
544 GetSeconds(), 0); 538 GetSeconds(), 0);
545 JS_ValueCopy(m_pDate, JS_NewDate(m_isolate, date)); 539 FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_isolate, date));
546 } 540 }
547 541
548 int CJS_Date::GetMinutes() { 542 int CJS_Date::GetMinutes() {
549 if (IsValidDate()) 543 if (IsValidDate())
550 return JS_GetMinFromTime(JS_LocalTime(JS_ToNumber(m_isolate, m_pDate))); 544 return JS_GetMinFromTime(JS_LocalTime(FXJS_ToNumber(m_isolate, m_pDate)));
551 545
552 return 0; 546 return 0;
553 } 547 }
554 548
555 void CJS_Date::SetMinutes(int minutes) { 549 void CJS_Date::SetMinutes(int minutes) {
556 double date = MakeDate(GetYear(), GetMonth(), GetDay(), GetHours(), minutes, 550 double date = MakeDate(GetYear(), GetMonth(), GetDay(), GetHours(), minutes,
557 GetSeconds(), 0); 551 GetSeconds(), 0);
558 JS_ValueCopy(m_pDate, JS_NewDate(m_isolate, date)); 552 FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_isolate, date));
559 } 553 }
560 554
561 int CJS_Date::GetSeconds() { 555 int CJS_Date::GetSeconds() {
562 if (IsValidDate()) 556 if (IsValidDate())
563 return JS_GetSecFromTime(JS_LocalTime(JS_ToNumber(m_isolate, m_pDate))); 557 return JS_GetSecFromTime(JS_LocalTime(FXJS_ToNumber(m_isolate, m_pDate)));
564 558
565 return 0; 559 return 0;
566 } 560 }
567 561
568 void CJS_Date::SetSeconds(int seconds) { 562 void CJS_Date::SetSeconds(int seconds) {
569 double date = MakeDate(GetYear(), GetMonth(), GetDay(), GetHours(), 563 double date = MakeDate(GetYear(), GetMonth(), GetDay(), GetHours(),
570 GetMinutes(), seconds, 0); 564 GetMinutes(), seconds, 0);
571 JS_ValueCopy(m_pDate, JS_NewDate(m_isolate, date)); 565 FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_isolate, date));
572 } 566 }
573 567
574 CJS_Date::operator v8::Local<v8::Value>() { 568 CJS_Date::operator v8::Local<v8::Value>() {
575 return m_pDate; 569 return m_pDate;
576 } 570 }
577 571
578 CJS_Date::operator double() const { 572 CJS_Date::operator double() const {
579 if (m_pDate.IsEmpty()) 573 if (m_pDate.IsEmpty())
580 return 0.0; 574 return 0.0;
581 return JS_ToNumber(m_isolate, m_pDate); 575 return FXJS_ToNumber(m_isolate, m_pDate);
582 } 576 }
583 577
584 CFX_WideString CJS_Date::ToString() const { 578 CFX_WideString CJS_Date::ToString() const {
585 if (m_pDate.IsEmpty()) 579 if (m_pDate.IsEmpty())
586 return L""; 580 return L"";
587 return JS_ToString(m_isolate, m_pDate); 581 return FXJS_ToString(m_isolate, m_pDate);
588 } 582 }
583
584 double _getLocalTZA() {
585 if (!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS))
586 return 0;
587 time_t t = 0;
588 time(&t);
589 localtime(&t);
590 #if _MSC_VER >= 1900
591 // In gcc and in Visual Studio prior to VS 2015 'timezone' is a global
592 // variable declared in time.h. That variable was deprecated and in VS 2015
593 // is removed, with _get_timezone replacing it.
594 long timezone = 0;
595 _get_timezone(&timezone);
596 #endif
597 return (double)(-(timezone * 1000));
598 }
599
600 int _getDaylightSavingTA(double d) {
601 if (!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS))
602 return 0;
603 time_t t = (time_t)(d / 1000);
604 struct tm* tmp = localtime(&t);
605 if (tmp == NULL)
606 return 0;
607 if (tmp->tm_isdst > 0)
608 // One hour.
609 return (int)60 * 60 * 1000;
610 return 0;
611 }
612
613 double _Mod(double x, double y) {
614 double r = fmod(x, y);
615 if (r < 0)
616 r += y;
617 return r;
618 }
619
620 int _isfinite(double v) {
621 #if _MSC_VER
622 return ::_finite(v);
623 #else
624 return std::fabs(v) < std::numeric_limits<double>::max();
625 #endif
626 }
627
628 double _toInteger(double n) {
629 return (n >= 0) ? FXSYS_floor(n) : -FXSYS_floor(-n);
630 }
631
632 bool _isLeapYear(int year) {
633 return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 != 0));
634 }
635
636 int _DayFromYear(int y) {
637 return (int)(365 * (y - 1970.0) + FXSYS_floor((y - 1969.0) / 4) -
638 FXSYS_floor((y - 1901.0) / 100) +
639 FXSYS_floor((y - 1601.0) / 400));
640 }
641
642 double _TimeFromYear(int y) {
643 return ((double)86400000) * _DayFromYear(y);
644 }
645
646 double _TimeFromYearMonth(int y, int m) {
647 static int daysMonth[12] = {
648 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
649 static int leapDaysMonth[12] = {
650 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335};
651 int* pMonth = daysMonth;
652 if (_isLeapYear(y))
653 pMonth = leapDaysMonth;
654 return _TimeFromYear(y) + ((double)pMonth[m]) * 86400000;
655 }
656
657 int _Day(double t) {
658 return (int)FXSYS_floor(t / 86400000);
659 }
660
661 int _YearFromTime(double t) {
662 // estimate the time.
663 int y = 1970 + (int)(t / (365.0 * 86400000));
664 if (_TimeFromYear(y) <= t) {
665 while (_TimeFromYear(y + 1) <= t)
666 y++;
667 } else
668 while (_TimeFromYear(y - 1) > t)
669 y--;
670 return y;
671 }
672
673 int _DayWithinYear(double t) {
674 int year = _YearFromTime(t);
675 int day = _Day(t);
676 return day - _DayFromYear(year);
677 }
678
679 int _MonthFromTime(double t) {
680 int day = _DayWithinYear(t);
681 int year = _YearFromTime(t);
682 if (0 <= day && day < 31)
683 return 0;
684 if (31 <= day && day < 59 + _isLeapYear(year))
685 return 1;
686 if ((59 + _isLeapYear(year)) <= day && day < (90 + _isLeapYear(year)))
687 return 2;
688 if ((90 + _isLeapYear(year)) <= day && day < (120 + _isLeapYear(year)))
689 return 3;
690 if ((120 + _isLeapYear(year)) <= day && day < (151 + _isLeapYear(year)))
691 return 4;
692 if ((151 + _isLeapYear(year)) <= day && day < (181 + _isLeapYear(year)))
693 return 5;
694 if ((181 + _isLeapYear(year)) <= day && day < (212 + _isLeapYear(year)))
695 return 6;
696 if ((212 + _isLeapYear(year)) <= day && day < (243 + _isLeapYear(year)))
697 return 7;
698 if ((243 + _isLeapYear(year)) <= day && day < (273 + _isLeapYear(year)))
699 return 8;
700 if ((273 + _isLeapYear(year)) <= day && day < (304 + _isLeapYear(year)))
701 return 9;
702 if ((304 + _isLeapYear(year)) <= day && day < (334 + _isLeapYear(year)))
703 return 10;
704 if ((334 + _isLeapYear(year)) <= day && day < (365 + _isLeapYear(year)))
705 return 11;
706
707 return -1;
708 }
709
710 int _DateFromTime(double t) {
711 int day = _DayWithinYear(t);
712 int year = _YearFromTime(t);
713 bool leap = _isLeapYear(year);
714 int month = _MonthFromTime(t);
715 switch (month) {
716 case 0:
717 return day + 1;
718 case 1:
719 return day - 30;
720 case 2:
721 return day - 58 - leap;
722 case 3:
723 return day - 89 - leap;
724 case 4:
725 return day - 119 - leap;
726 case 5:
727 return day - 150 - leap;
728 case 6:
729 return day - 180 - leap;
730 case 7:
731 return day - 211 - leap;
732 case 8:
733 return day - 242 - leap;
734 case 9:
735 return day - 272 - leap;
736 case 10:
737 return day - 303 - leap;
738 case 11:
739 return day - 333 - leap;
740 default:
741 return 0;
742 }
743 }
744
745 double JS_GetDateTime() {
746 if (!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS))
747 return 0;
748 time_t t = time(NULL);
749 struct tm* pTm = localtime(&t);
750
751 int year = pTm->tm_year + 1900;
752 double t1 = _TimeFromYear(year);
753
754 return t1 + pTm->tm_yday * 86400000.0 + pTm->tm_hour * 3600000.0 +
755 pTm->tm_min * 60000.0 + pTm->tm_sec * 1000.0;
756 }
757
758 int JS_GetYearFromTime(double dt) {
759 return _YearFromTime(dt);
760 }
761
762 int JS_GetMonthFromTime(double dt) {
763 return _MonthFromTime(dt);
764 }
765
766 int JS_GetDayFromTime(double dt) {
767 return _DateFromTime(dt);
768 }
769
770 int JS_GetHourFromTime(double dt) {
771 return (int)_Mod(FXSYS_floor((double)(dt / (60 * 60 * 1000))), 24);
772 }
773
774 int JS_GetMinFromTime(double dt) {
775 return (int)_Mod(FXSYS_floor((double)(dt / (60 * 1000))), 60);
776 }
777
778 int JS_GetSecFromTime(double dt) {
779 return (int)_Mod(FXSYS_floor((double)(dt / 1000)), 60);
780 }
781
782 double JS_DateParse(const wchar_t* string) {
783 v8::Isolate* pIsolate = v8::Isolate::GetCurrent();
784 v8::Isolate::Scope isolate_scope(pIsolate);
785 v8::HandleScope scope(pIsolate);
786
787 v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
788
789 // Use the built-in object method.
790 v8::Local<v8::Value> v =
791 context->Global()
792 ->Get(context, v8::String::NewFromUtf8(pIsolate, "Date",
793 v8::NewStringType::kNormal)
794 .ToLocalChecked())
795 .ToLocalChecked();
796 if (v->IsObject()) {
797 v8::Local<v8::Object> o = v->ToObject(context).ToLocalChecked();
798 v = o->Get(context, v8::String::NewFromUtf8(pIsolate, "parse",
799 v8::NewStringType::kNormal)
800 .ToLocalChecked()).ToLocalChecked();
801 if (v->IsFunction()) {
802 v8::Local<v8::Function> funC = v8::Local<v8::Function>::Cast(v);
803
804 const int argc = 1;
805 v8::Local<v8::String> timeStr = FXJS_WSToJSString(pIsolate, string);
806 v8::Local<v8::Value> argv[argc] = {timeStr};
807 v = funC->Call(context, context->Global(), argc, argv).ToLocalChecked();
808 if (v->IsNumber()) {
809 double date = v->ToNumber(context).ToLocalChecked()->Value();
810 if (!_isfinite(date))
811 return date;
812 return date + _getLocalTZA() + _getDaylightSavingTA(date);
813 }
814 }
815 }
816 return 0;
817 }
818
819 double JS_MakeDay(int nYear, int nMonth, int nDate) {
820 if (!_isfinite(nYear) || !_isfinite(nMonth) || !_isfinite(nDate))
821 return GetNan();
822 double y = _toInteger(nYear);
823 double m = _toInteger(nMonth);
824 double dt = _toInteger(nDate);
825 double ym = y + FXSYS_floor((double)m / 12);
826 double mn = _Mod(m, 12);
827
828 double t = _TimeFromYearMonth((int)ym, (int)mn);
829
830 if (_YearFromTime(t) != ym || _MonthFromTime(t) != mn ||
831 _DateFromTime(t) != 1)
832 return GetNan();
833 return _Day(t) + dt - 1;
834 }
835
836 double JS_MakeTime(int nHour, int nMin, int nSec, int nMs) {
837 if (!_isfinite(nHour) || !_isfinite(nMin) || !_isfinite(nSec) ||
838 !_isfinite(nMs))
839 return GetNan();
840
841 double h = _toInteger(nHour);
842 double m = _toInteger(nMin);
843 double s = _toInteger(nSec);
844 double milli = _toInteger(nMs);
845
846 return h * 3600000 + m * 60000 + s * 1000 + milli;
847 }
848
849 double JS_MakeDate(double day, double time) {
850 if (!_isfinite(day) || !_isfinite(time))
851 return GetNan();
852
853 return day * 86400000 + time;
854 }
855
856 bool JS_PortIsNan(double d) {
857 return d != d;
858 }
859
860 double JS_LocalTime(double d) {
861 return JS_GetDateTime() + _getDaylightSavingTA(d);
862 }
OLDNEW
« no previous file with comments | « fpdfsdk/src/javascript/JS_Runtime.cpp ('k') | fpdfsdk/src/javascript/PublicMethods.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698