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

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

Issue 2172813002: Tidy up JS_Value.h (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: rebse Created 4 years, 5 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
« fpdfsdk/javascript/JS_Value.h ('K') | « fpdfsdk/javascript/JS_Value.h ('k') | no next file » | 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 "fpdfsdk/javascript/JS_Value.h" 7 #include "fpdfsdk/javascript/JS_Value.h"
8 8
9 #include <time.h> 9 #include <time.h>
10 10
11 #include <algorithm> 11 #include <algorithm>
12 #include <cmath> 12 #include <cmath>
13 #include <limits> 13 #include <limits>
14 #include <vector> 14 #include <vector>
15 15
16 #include "fpdfsdk/javascript/Document.h" 16 #include "fpdfsdk/javascript/Document.h"
17 #include "fpdfsdk/javascript/JS_Define.h" 17 #include "fpdfsdk/javascript/JS_Define.h"
18 #include "fpdfsdk/javascript/JS_Object.h" 18 #include "fpdfsdk/javascript/JS_Object.h"
19 19
20 static const uint32_t g_nan[2] = {0, 0x7FF80000}; 20 namespace {
21 static double GetNan() { 21
22 const uint32_t g_nan[2] = {0, 0x7FF80000};
23
24 double GetNan() {
22 return *(double*)g_nan; 25 return *(double*)g_nan;
23 } 26 }
24 27
28 double
29 MakeDate(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
25 CJS_Value::CJS_Value(CJS_Runtime* pRuntime) : m_pJSRuntime(pRuntime) {} 36 CJS_Value::CJS_Value(CJS_Runtime* pRuntime) : m_pJSRuntime(pRuntime) {}
26 37
27 CJS_Value::CJS_Value(CJS_Runtime* pRuntime, v8::Local<v8::Value> pValue) 38 CJS_Value::CJS_Value(CJS_Runtime* pRuntime, v8::Local<v8::Value> pValue)
28 : m_pValue(pValue), m_pJSRuntime(pRuntime) {} 39 : m_pValue(pValue), m_pJSRuntime(pRuntime) {}
29 40
30 CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const int& iValue) 41 CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const int& iValue)
31 : m_pJSRuntime(pRuntime) { 42 : m_pJSRuntime(pRuntime) {
32 operator=(iValue); 43 operator=(iValue);
33 } 44 }
34 45
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 } 185 }
175 186
176 void CJS_Value::SetNull() { 187 void CJS_Value::SetNull() {
177 m_pValue = FXJS_NewNull(m_pJSRuntime->GetIsolate()); 188 m_pValue = FXJS_NewNull(m_pJSRuntime->GetIsolate());
178 } 189 }
179 190
180 void CJS_Value::operator=(const FX_CHAR* pStr) { 191 void CJS_Value::operator=(const FX_CHAR* pStr) {
181 operator=(CFX_WideString::FromLocal(pStr).c_str()); 192 operator=(CFX_WideString::FromLocal(pStr).c_str());
182 } 193 }
183 194
184 void CJS_Value::operator=(CJS_Array& array) { 195 void CJS_Value::operator=(const CJS_Array& array) {
185 m_pValue = static_cast<v8::Local<v8::Array>>(array); 196 ASSERT(m_pJSRuntime == array.GetJSRuntime());
197 m_pValue = array.ToV8Array();
186 } 198 }
187 199
188 void CJS_Value::operator=(CJS_Date& date) { 200 void CJS_Value::operator=(const CJS_Date& date) {
189 m_pValue = FXJS_NewDate(m_pJSRuntime->GetIsolate(), (double)date); 201 ASSERT(m_pJSRuntime == date.GetJSRuntime());
202 m_pValue = FXJS_NewDate(m_pJSRuntime->GetIsolate(), date.ToDouble());
190 } 203 }
191 204
192 void CJS_Value::operator=(CJS_Value value) { 205 void CJS_Value::operator=(const CJS_Value& value) {
206 ASSERT(m_pJSRuntime == value.m_pJSRuntime);
193 m_pValue = value.ToV8Value(); 207 m_pValue = value.ToV8Value();
194 m_pJSRuntime = value.m_pJSRuntime;
195 } 208 }
196 209
197 // static 210 // static
198 CJS_Value::Type CJS_Value::GetValueType(v8::Local<v8::Value> value) { 211 CJS_Value::Type CJS_Value::GetValueType(v8::Local<v8::Value> value) {
199 if (value.IsEmpty()) 212 if (value.IsEmpty())
200 return VT_unknown; 213 return VT_unknown;
201 if (value->IsString()) 214 if (value->IsString())
202 return VT_string; 215 return VT_string;
203 if (value->IsNumber()) 216 if (value->IsNumber())
204 return VT_number; 217 return VT_number;
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
307 void CJS_PropValue::operator<<(v8::Local<v8::Object> pObj) { 320 void CJS_PropValue::operator<<(v8::Local<v8::Object> pObj) {
308 ASSERT(!m_bIsSetting); 321 ASSERT(!m_bIsSetting);
309 CJS_Value::operator=(pObj); 322 CJS_Value::operator=(pObj);
310 } 323 }
311 324
312 void CJS_PropValue::operator>>(v8::Local<v8::Object>& ppObj) const { 325 void CJS_PropValue::operator>>(v8::Local<v8::Object>& ppObj) const {
313 ASSERT(m_bIsSetting); 326 ASSERT(m_bIsSetting);
314 ppObj = CJS_Value::ToV8Object(); 327 ppObj = CJS_Value::ToV8Object();
315 } 328 }
316 329
317 void CJS_PropValue::StartSetting() {
318 m_bIsSetting = 1;
319 }
320
321 void CJS_PropValue::StartGetting() {
322 m_bIsSetting = 0;
323 }
324 void CJS_PropValue::operator<<(CFX_ByteString str) { 330 void CJS_PropValue::operator<<(CFX_ByteString str) {
325 ASSERT(!m_bIsSetting); 331 ASSERT(!m_bIsSetting);
326 CJS_Value::operator=(str.c_str()); 332 CJS_Value::operator=(str.c_str());
327 } 333 }
328 334
329 void CJS_PropValue::operator>>(CFX_ByteString& str) const { 335 void CJS_PropValue::operator>>(CFX_ByteString& str) const {
330 ASSERT(m_bIsSetting); 336 ASSERT(m_bIsSetting);
331 str = CJS_Value::ToCFXByteString(); 337 str = CJS_Value::ToCFXByteString();
332 } 338 }
333 339
(...skipping 25 matching lines...) Expand all
359 void CJS_PropValue::operator>>(CJS_Date& date) const { 365 void CJS_PropValue::operator>>(CJS_Date& date) const {
360 ASSERT(m_bIsSetting); 366 ASSERT(m_bIsSetting);
361 ConvertToDate(date); 367 ConvertToDate(date);
362 } 368 }
363 369
364 void CJS_PropValue::operator<<(CJS_Date& date) { 370 void CJS_PropValue::operator<<(CJS_Date& date) {
365 ASSERT(!m_bIsSetting); 371 ASSERT(!m_bIsSetting);
366 CJS_Value::operator=(date); 372 CJS_Value::operator=(date);
367 } 373 }
368 374
369 CJS_PropValue::operator v8::Local<v8::Value>() const {
370 return m_pValue;
371 }
372
373 CJS_Array::CJS_Array(CJS_Runtime* pRuntime) : m_pJSRuntime(pRuntime) {} 375 CJS_Array::CJS_Array(CJS_Runtime* pRuntime) : m_pJSRuntime(pRuntime) {}
374 376
375 CJS_Array::~CJS_Array() {} 377 CJS_Array::~CJS_Array() {}
376 378
377 CJS_Array::CJS_Array(const CJS_Array& other) = default; 379 CJS_Array::CJS_Array(const CJS_Array& other) = default;
378 380
379 void CJS_Array::Attach(v8::Local<v8::Array> pArray) { 381 void CJS_Array::Attach(v8::Local<v8::Array> pArray) {
380 m_pArray = pArray; 382 m_pArray = pArray;
381 } 383 }
382 384
383 FX_BOOL CJS_Array::IsAttached() { 385 void CJS_Array::GetElement(unsigned index, CJS_Value& value) const {
384 return FALSE;
385 }
386
387 void CJS_Array::GetElement(unsigned index, CJS_Value& value) {
388 if (m_pArray.IsEmpty()) 386 if (m_pArray.IsEmpty())
389 return; 387 return;
390 v8::Local<v8::Value> p = 388 v8::Local<v8::Value> p =
391 FXJS_GetArrayElement(m_pJSRuntime->GetIsolate(), m_pArray, index); 389 FXJS_GetArrayElement(m_pJSRuntime->GetIsolate(), m_pArray, index);
392 value.Attach(p); 390 value.Attach(p);
393 } 391 }
394 392
395 void CJS_Array::SetElement(unsigned index, CJS_Value value) { 393 void CJS_Array::SetElement(unsigned index, CJS_Value value) {
396 if (m_pArray.IsEmpty()) 394 if (m_pArray.IsEmpty())
397 m_pArray = FXJS_NewArray(m_pJSRuntime->GetIsolate()); 395 m_pArray = FXJS_NewArray(m_pJSRuntime->GetIsolate());
398 396
399 FXJS_PutArrayElement(m_pJSRuntime->GetIsolate(), m_pArray, index, 397 FXJS_PutArrayElement(m_pJSRuntime->GetIsolate(), m_pArray, index,
400 value.ToV8Value()); 398 value.ToV8Value());
401 } 399 }
402 400
403 int CJS_Array::GetLength() { 401 int CJS_Array::GetLength() const {
404 if (m_pArray.IsEmpty()) 402 if (m_pArray.IsEmpty())
405 return 0; 403 return 0;
406 return FXJS_GetArrayLength(m_pArray); 404 return FXJS_GetArrayLength(m_pArray);
407 } 405 }
408 406
409 CJS_Array::operator v8::Local<v8::Array>() { 407 v8::Local<v8::Array> CJS_Array::ToV8Array() const {
410 if (m_pArray.IsEmpty()) 408 if (m_pArray.IsEmpty())
411 m_pArray = FXJS_NewArray(m_pJSRuntime->GetIsolate()); 409 m_pArray = FXJS_NewArray(m_pJSRuntime->GetIsolate());
412 410
413 return m_pArray; 411 return m_pArray;
414 } 412 }
415 413
416 CJS_Date::CJS_Date(CJS_Runtime* pRuntime) : m_pJSRuntime(pRuntime) {} 414 CJS_Date::CJS_Date(CJS_Runtime* pRuntime) : m_pJSRuntime(pRuntime) {}
417 415
418 CJS_Date::CJS_Date(CJS_Runtime* pRuntime, double dMsecTime) 416 CJS_Date::CJS_Date(CJS_Runtime* pRuntime, double dMsecTime)
419 : m_pJSRuntime(pRuntime) { 417 : m_pJSRuntime(pRuntime) {
420 m_pDate = FXJS_NewDate(pRuntime->GetIsolate(), dMsecTime); 418 m_pDate = FXJS_NewDate(pRuntime->GetIsolate(), dMsecTime);
421 } 419 }
422 420
423 CJS_Date::CJS_Date(CJS_Runtime* pRuntime, 421 CJS_Date::CJS_Date(CJS_Runtime* pRuntime,
424 int year, 422 int year,
425 int mon, 423 int mon,
426 int day, 424 int day,
427 int hour, 425 int hour,
428 int min, 426 int min,
429 int sec) 427 int sec)
430 : m_pJSRuntime(pRuntime) { 428 : m_pJSRuntime(pRuntime) {
431 m_pDate = FXJS_NewDate(pRuntime->GetIsolate(), 429 m_pDate = FXJS_NewDate(pRuntime->GetIsolate(),
432 MakeDate(year, mon, day, hour, min, sec, 0)); 430 MakeDate(year, mon, day, hour, min, sec, 0));
433 } 431 }
434 432
435 double CJS_Date::MakeDate(int year,
436 int mon,
437 int day,
438 int hour,
439 int min,
440 int sec,
441 int ms) {
442 return JS_MakeDate(JS_MakeDay(year, mon, day),
443 JS_MakeTime(hour, min, sec, ms));
444 }
445
446 CJS_Date::~CJS_Date() {} 433 CJS_Date::~CJS_Date() {}
447 434
448 FX_BOOL CJS_Date::IsValidDate() { 435 bool CJS_Date::IsValidDate() const {
449 if (m_pDate.IsEmpty()) 436 return !m_pDate.IsEmpty() &&
450 return FALSE; 437 !JS_PortIsNan(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate));
451 return !JS_PortIsNan(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate));
452 } 438 }
453 439
454 void CJS_Date::Attach(v8::Local<v8::Value> pDate) { 440 void CJS_Date::Attach(v8::Local<v8::Value> pDate) {
455 m_pDate = pDate; 441 m_pDate = pDate;
456 } 442 }
457 443
458 int CJS_Date::GetYear() { 444 int CJS_Date::GetYear() const {
459 if (IsValidDate()) 445 if (!IsValidDate())
460 return JS_GetYearFromTime( 446 return 0;
461 JS_LocalTime(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate)));
462 447
463 return 0; 448 return JS_GetYearFromTime(
449 JS_LocalTime(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate)));
464 } 450 }
465 451
466 void CJS_Date::SetYear(int iYear) { 452 void CJS_Date::SetYear(int iYear) {
467 double date = MakeDate(iYear, GetMonth(), GetDay(), GetHours(), GetMinutes(), 453 double date = MakeDate(iYear, GetMonth(), GetDay(), GetHours(), GetMinutes(),
468 GetSeconds(), 0); 454 GetSeconds(), 0);
469 FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_pJSRuntime->GetIsolate(), date)); 455 FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_pJSRuntime->GetIsolate(), date));
470 } 456 }
471 457
472 int CJS_Date::GetMonth() { 458 int CJS_Date::GetMonth() const {
473 if (IsValidDate()) 459 if (!IsValidDate())
474 return JS_GetMonthFromTime( 460 return 0;
475 JS_LocalTime(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate)));
476 461
477 return 0; 462 return JS_GetMonthFromTime(
463 JS_LocalTime(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate)));
478 } 464 }
479 465
480 void CJS_Date::SetMonth(int iMonth) { 466 void CJS_Date::SetMonth(int iMonth) {
481 double date = MakeDate(GetYear(), iMonth, GetDay(), GetHours(), GetMinutes(), 467 double date = MakeDate(GetYear(), iMonth, GetDay(), GetHours(), GetMinutes(),
482 GetSeconds(), 0); 468 GetSeconds(), 0);
483 FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_pJSRuntime->GetIsolate(), date)); 469 FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_pJSRuntime->GetIsolate(), date));
484 } 470 }
485 471
486 int CJS_Date::GetDay() { 472 int CJS_Date::GetDay() const {
487 if (IsValidDate()) 473 if (!IsValidDate())
488 return JS_GetDayFromTime( 474 return 0;
489 JS_LocalTime(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate)));
490 475
491 return 0; 476 return JS_GetDayFromTime(
477 JS_LocalTime(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate)));
492 } 478 }
493 479
494 void CJS_Date::SetDay(int iDay) { 480 void CJS_Date::SetDay(int iDay) {
495 double date = MakeDate(GetYear(), GetMonth(), iDay, GetHours(), GetMinutes(), 481 double date = MakeDate(GetYear(), GetMonth(), iDay, GetHours(), GetMinutes(),
496 GetSeconds(), 0); 482 GetSeconds(), 0);
497 FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_pJSRuntime->GetIsolate(), date)); 483 FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_pJSRuntime->GetIsolate(), date));
498 } 484 }
499 485
500 int CJS_Date::GetHours() { 486 int CJS_Date::GetHours() const {
501 if (IsValidDate()) 487 if (!IsValidDate())
502 return JS_GetHourFromTime( 488 return 0;
503 JS_LocalTime(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate)));
504 489
505 return 0; 490 return JS_GetHourFromTime(
491 JS_LocalTime(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate)));
506 } 492 }
507 493
508 void CJS_Date::SetHours(int iHours) { 494 void CJS_Date::SetHours(int iHours) {
509 double date = MakeDate(GetYear(), GetMonth(), GetDay(), iHours, GetMinutes(), 495 double date = MakeDate(GetYear(), GetMonth(), GetDay(), iHours, GetMinutes(),
510 GetSeconds(), 0); 496 GetSeconds(), 0);
511 FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_pJSRuntime->GetIsolate(), date)); 497 FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_pJSRuntime->GetIsolate(), date));
512 } 498 }
513 499
514 int CJS_Date::GetMinutes() { 500 int CJS_Date::GetMinutes() const {
515 if (IsValidDate()) 501 if (!IsValidDate())
516 return JS_GetMinFromTime( 502 return 0;
517 JS_LocalTime(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate)));
518 503
519 return 0; 504 return JS_GetMinFromTime(
505 JS_LocalTime(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate)));
520 } 506 }
521 507
522 void CJS_Date::SetMinutes(int minutes) { 508 void CJS_Date::SetMinutes(int minutes) {
523 double date = MakeDate(GetYear(), GetMonth(), GetDay(), GetHours(), minutes, 509 double date = MakeDate(GetYear(), GetMonth(), GetDay(), GetHours(), minutes,
524 GetSeconds(), 0); 510 GetSeconds(), 0);
525 FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_pJSRuntime->GetIsolate(), date)); 511 FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_pJSRuntime->GetIsolate(), date));
526 } 512 }
527 513
528 int CJS_Date::GetSeconds() { 514 int CJS_Date::GetSeconds() const {
529 if (IsValidDate()) 515 if (!IsValidDate())
530 return JS_GetSecFromTime( 516 return 0;
531 JS_LocalTime(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate)));
532 517
533 return 0; 518 return JS_GetSecFromTime(
519 JS_LocalTime(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate)));
534 } 520 }
535 521
536 void CJS_Date::SetSeconds(int seconds) { 522 void CJS_Date::SetSeconds(int seconds) {
537 double date = MakeDate(GetYear(), GetMonth(), GetDay(), GetHours(), 523 double date = MakeDate(GetYear(), GetMonth(), GetDay(), GetHours(),
538 GetMinutes(), seconds, 0); 524 GetMinutes(), seconds, 0);
539 FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_pJSRuntime->GetIsolate(), date)); 525 FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_pJSRuntime->GetIsolate(), date));
540 } 526 }
541 527
542 CJS_Date::operator v8::Local<v8::Value>() { 528 double CJS_Date::ToDouble() const {
543 return m_pDate;
544 }
545
546 CJS_Date::operator double() const {
547 if (m_pDate.IsEmpty()) 529 if (m_pDate.IsEmpty())
548 return 0.0; 530 return 0.0;
531
549 return FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate); 532 return FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate);
550 } 533 }
551 534
552 CFX_WideString CJS_Date::ToString() const { 535 CFX_WideString CJS_Date::ToString() const {
553 if (m_pDate.IsEmpty()) 536 if (m_pDate.IsEmpty())
554 return L""; 537 return L"";
538
555 return FXJS_ToString(m_pJSRuntime->GetIsolate(), m_pDate); 539 return FXJS_ToString(m_pJSRuntime->GetIsolate(), m_pDate);
556 } 540 }
557 541
558 double _getLocalTZA() { 542 double _getLocalTZA() {
559 if (!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS)) 543 if (!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS))
560 return 0; 544 return 0;
561 time_t t = 0; 545 time_t t = 0;
562 time(&t); 546 time(&t);
563 localtime(&t); 547 localtime(&t);
564 #if _MSC_VER >= 1900 548 #if _MSC_VER >= 1900
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after
859 for (size_t i = 0; i < nKeywords; ++i) { 843 for (size_t i = 0; i < nKeywords; ++i) {
860 const wchar_t* property = va_arg(ap, const wchar_t*); 844 const wchar_t* property = va_arg(ap, const wchar_t*);
861 v8::Local<v8::Value> v8Value = 845 v8::Local<v8::Value> v8Value =
862 FXJS_GetObjectElement(pRuntime->GetIsolate(), pObj, property); 846 FXJS_GetObjectElement(pRuntime->GetIsolate(), pObj, property);
863 if (!v8Value->IsUndefined()) 847 if (!v8Value->IsUndefined())
864 result[i] = CJS_Value(pRuntime, v8Value); 848 result[i] = CJS_Value(pRuntime, v8Value);
865 } 849 }
866 va_end(ap); 850 va_end(ap);
867 return result; 851 return result;
868 } 852 }
OLDNEW
« fpdfsdk/javascript/JS_Value.h ('K') | « fpdfsdk/javascript/JS_Value.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698