From fbf52c2231bd148bcf1fcd6ac1e6a5ed5e311b8c Mon Sep 17 00:00:00 2001 From: tsepez Date: Mon, 25 Jul 2016 11:17:07 -0700 Subject: Tidy up JS_Value.h Use ToV8Object() instead of CJS_Value cast operator. Add some missing consts / explicits. Move code into empty namespace. Review-Url: https://codereview.chromium.org/2172813002 --- fpdfsdk/javascript/JS_Define.h | 4 +- fpdfsdk/javascript/JS_Value.cpp | 136 ++++++++++++++++++---------------------- fpdfsdk/javascript/JS_Value.h | 69 +++++++++----------- 3 files changed, 93 insertions(+), 116 deletions(-) (limited to 'fpdfsdk/javascript') diff --git a/fpdfsdk/javascript/JS_Define.h b/fpdfsdk/javascript/JS_Define.h index 6aa1dec768..e120758c60 100644 --- a/fpdfsdk/javascript/JS_Define.h +++ b/fpdfsdk/javascript/JS_Define.h @@ -92,7 +92,7 @@ void JSPropGetter(const char* prop_name_string, sError)); return; } - info.GetReturnValue().Set((v8::Local)value); + info.GetReturnValue().Set(value.ToV8Value()); } template )value); + info.GetReturnValue().Set(value.ToV8Value()); } template diff --git a/fpdfsdk/javascript/JS_Value.cpp b/fpdfsdk/javascript/JS_Value.cpp index c13d1d405e..fa1f7d95c5 100644 --- a/fpdfsdk/javascript/JS_Value.cpp +++ b/fpdfsdk/javascript/JS_Value.cpp @@ -17,11 +17,22 @@ #include "fpdfsdk/javascript/JS_Define.h" #include "fpdfsdk/javascript/JS_Object.h" -static const uint32_t g_nan[2] = {0, 0x7FF80000}; -static double GetNan() { +namespace { + +const uint32_t g_nan[2] = {0, 0x7FF80000}; + +double GetNan() { return *(double*)g_nan; } +double +MakeDate(int year, int mon, int day, int hour, int min, int sec, int ms) { + return JS_MakeDate(JS_MakeDay(year, mon, day), + JS_MakeTime(hour, min, sec, ms)); +} + +} // namespace + CJS_Value::CJS_Value(CJS_Runtime* pRuntime) : m_pJSRuntime(pRuntime) {} CJS_Value::CJS_Value(CJS_Runtime* pRuntime, v8::Local pValue) @@ -181,17 +192,19 @@ void CJS_Value::operator=(const FX_CHAR* pStr) { operator=(CFX_WideString::FromLocal(pStr).c_str()); } -void CJS_Value::operator=(CJS_Array& array) { - m_pValue = static_cast>(array); +void CJS_Value::operator=(const CJS_Array& array) { + ASSERT(m_pJSRuntime == array.GetJSRuntime()); + m_pValue = array.ToV8Array(); } -void CJS_Value::operator=(CJS_Date& date) { - m_pValue = FXJS_NewDate(m_pJSRuntime->GetIsolate(), (double)date); +void CJS_Value::operator=(const CJS_Date& date) { + ASSERT(m_pJSRuntime == date.GetJSRuntime()); + m_pValue = FXJS_NewDate(m_pJSRuntime->GetIsolate(), date.ToDouble()); } -void CJS_Value::operator=(CJS_Value value) { +void CJS_Value::operator=(const CJS_Value& value) { + ASSERT(m_pJSRuntime == value.m_pJSRuntime); m_pValue = value.ToV8Value(); - m_pJSRuntime = value.m_pJSRuntime; } // static @@ -314,13 +327,6 @@ void CJS_PropValue::operator>>(v8::Local& ppObj) const { ppObj = CJS_Value::ToV8Object(); } -void CJS_PropValue::StartSetting() { - m_bIsSetting = 1; -} - -void CJS_PropValue::StartGetting() { - m_bIsSetting = 0; -} void CJS_PropValue::operator<<(CFX_ByteString str) { ASSERT(!m_bIsSetting); CJS_Value::operator=(str.c_str()); @@ -366,10 +372,6 @@ void CJS_PropValue::operator<<(CJS_Date& date) { CJS_Value::operator=(date); } -CJS_PropValue::operator v8::Local() const { - return m_pValue; -} - CJS_Array::CJS_Array(CJS_Runtime* pRuntime) : m_pJSRuntime(pRuntime) {} CJS_Array::~CJS_Array() {} @@ -380,11 +382,7 @@ void CJS_Array::Attach(v8::Local pArray) { m_pArray = pArray; } -FX_BOOL CJS_Array::IsAttached() { - return FALSE; -} - -void CJS_Array::GetElement(unsigned index, CJS_Value& value) { +void CJS_Array::GetElement(unsigned index, CJS_Value& value) const { if (m_pArray.IsEmpty()) return; v8::Local p = @@ -400,13 +398,13 @@ void CJS_Array::SetElement(unsigned index, CJS_Value value) { value.ToV8Value()); } -int CJS_Array::GetLength() { +int CJS_Array::GetLength() const { if (m_pArray.IsEmpty()) return 0; return FXJS_GetArrayLength(m_pArray); } -CJS_Array::operator v8::Local() { +v8::Local CJS_Array::ToV8Array() const { if (m_pArray.IsEmpty()) m_pArray = FXJS_NewArray(m_pJSRuntime->GetIsolate()); @@ -432,35 +430,23 @@ CJS_Date::CJS_Date(CJS_Runtime* pRuntime, MakeDate(year, mon, day, hour, min, sec, 0)); } -double CJS_Date::MakeDate(int year, - int mon, - int day, - int hour, - int min, - int sec, - int ms) { - return JS_MakeDate(JS_MakeDay(year, mon, day), - JS_MakeTime(hour, min, sec, ms)); -} - CJS_Date::~CJS_Date() {} -FX_BOOL CJS_Date::IsValidDate() { - if (m_pDate.IsEmpty()) - return FALSE; - return !JS_PortIsNan(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate)); +bool CJS_Date::IsValidDate() const { + return !m_pDate.IsEmpty() && + !JS_PortIsNan(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate)); } void CJS_Date::Attach(v8::Local pDate) { m_pDate = pDate; } -int CJS_Date::GetYear() { - if (IsValidDate()) - return JS_GetYearFromTime( - JS_LocalTime(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate))); +int CJS_Date::GetYear() const { + if (!IsValidDate()) + return 0; - return 0; + return JS_GetYearFromTime( + JS_LocalTime(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate))); } void CJS_Date::SetYear(int iYear) { @@ -469,12 +455,12 @@ void CJS_Date::SetYear(int iYear) { FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_pJSRuntime->GetIsolate(), date)); } -int CJS_Date::GetMonth() { - if (IsValidDate()) - return JS_GetMonthFromTime( - JS_LocalTime(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate))); +int CJS_Date::GetMonth() const { + if (!IsValidDate()) + return 0; - return 0; + return JS_GetMonthFromTime( + JS_LocalTime(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate))); } void CJS_Date::SetMonth(int iMonth) { @@ -483,12 +469,12 @@ void CJS_Date::SetMonth(int iMonth) { FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_pJSRuntime->GetIsolate(), date)); } -int CJS_Date::GetDay() { - if (IsValidDate()) - return JS_GetDayFromTime( - JS_LocalTime(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate))); +int CJS_Date::GetDay() const { + if (!IsValidDate()) + return 0; - return 0; + return JS_GetDayFromTime( + JS_LocalTime(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate))); } void CJS_Date::SetDay(int iDay) { @@ -497,12 +483,12 @@ void CJS_Date::SetDay(int iDay) { FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_pJSRuntime->GetIsolate(), date)); } -int CJS_Date::GetHours() { - if (IsValidDate()) - return JS_GetHourFromTime( - JS_LocalTime(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate))); +int CJS_Date::GetHours() const { + if (!IsValidDate()) + return 0; - return 0; + return JS_GetHourFromTime( + JS_LocalTime(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate))); } void CJS_Date::SetHours(int iHours) { @@ -511,12 +497,12 @@ void CJS_Date::SetHours(int iHours) { FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_pJSRuntime->GetIsolate(), date)); } -int CJS_Date::GetMinutes() { - if (IsValidDate()) - return JS_GetMinFromTime( - JS_LocalTime(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate))); +int CJS_Date::GetMinutes() const { + if (!IsValidDate()) + return 0; - return 0; + return JS_GetMinFromTime( + JS_LocalTime(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate))); } void CJS_Date::SetMinutes(int minutes) { @@ -525,12 +511,12 @@ void CJS_Date::SetMinutes(int minutes) { FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_pJSRuntime->GetIsolate(), date)); } -int CJS_Date::GetSeconds() { - if (IsValidDate()) - return JS_GetSecFromTime( - JS_LocalTime(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate))); +int CJS_Date::GetSeconds() const { + if (!IsValidDate()) + return 0; - return 0; + return JS_GetSecFromTime( + JS_LocalTime(FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate))); } void CJS_Date::SetSeconds(int seconds) { @@ -539,19 +525,17 @@ void CJS_Date::SetSeconds(int seconds) { FXJS_ValueCopy(m_pDate, FXJS_NewDate(m_pJSRuntime->GetIsolate(), date)); } -CJS_Date::operator v8::Local() { - return m_pDate; -} - -CJS_Date::operator double() const { +double CJS_Date::ToDouble() const { if (m_pDate.IsEmpty()) return 0.0; + return FXJS_ToNumber(m_pJSRuntime->GetIsolate(), m_pDate); } CFX_WideString CJS_Date::ToString() const { if (m_pDate.IsEmpty()) return L""; + return FXJS_ToString(m_pJSRuntime->GetIsolate(), m_pDate); } diff --git a/fpdfsdk/javascript/JS_Value.h b/fpdfsdk/javascript/JS_Value.h index 2abc42521d..5943aaf888 100644 --- a/fpdfsdk/javascript/JS_Value.h +++ b/fpdfsdk/javascript/JS_Value.h @@ -72,11 +72,11 @@ class CJS_Value { void operator=(float val); void operator=(CJS_Object* val); void operator=(v8::Local val); - void operator=(CJS_Array& val); - void operator=(CJS_Date& val); - void operator=(const FX_WCHAR* pWstr); + void operator=(const CJS_Array& val); + void operator=(const CJS_Date& val); + void operator=(const CJS_Value& value); void operator=(const FX_CHAR* pStr); - void operator=(CJS_Value value); + void operator=(const FX_WCHAR* pWstr); FX_BOOL IsArrayObject() const; FX_BOOL IsDateObject() const; @@ -87,17 +87,19 @@ class CJS_Value { protected: v8::Local m_pValue; - CJS_Runtime* m_pJSRuntime; + CJS_Runtime* const m_pJSRuntime; }; class CJS_PropValue : public CJS_Value { public: + explicit CJS_PropValue(CJS_Runtime* pRuntime); CJS_PropValue(const CJS_Value&); - CJS_PropValue(CJS_Runtime* pRuntime); ~CJS_PropValue(); - FX_BOOL IsSetting() const { return m_bIsSetting; } - FX_BOOL IsGetting() const { return !m_bIsSetting; } + void StartSetting() { m_bIsSetting = true; } + void StartGetting() { m_bIsSetting = false; } + bool IsSetting() const { return m_bIsSetting; } + bool IsGetting() const { return !m_bIsSetting; } void operator<<(int val); void operator>>(int&) const; @@ -120,39 +122,33 @@ class CJS_PropValue : public CJS_Value { void operator<<(CJS_Array& array); void operator<<(CJS_Date& date); void operator>>(CJS_Date& date) const; - operator v8::Local() const; - void StartSetting(); - void StartGetting(); private: - FX_BOOL m_bIsSetting; + bool m_bIsSetting; }; class CJS_Array { public: - CJS_Array(CJS_Runtime* pRuntime); - virtual ~CJS_Array(); + explicit CJS_Array(CJS_Runtime* pRuntime); CJS_Array(const CJS_Array& other); + virtual ~CJS_Array(); void Attach(v8::Local pArray); - void GetElement(unsigned index, CJS_Value& value); + void GetElement(unsigned index, CJS_Value& value) const; void SetElement(unsigned index, CJS_Value value); - int GetLength(); - FX_BOOL IsAttached(); - operator v8::Local(); + int GetLength() const; + v8::Local ToV8Array() const; CJS_Runtime* GetJSRuntime() const { return m_pJSRuntime; } private: - v8::Local m_pArray; - CJS_Runtime* m_pJSRuntime; + mutable v8::Local m_pArray; + CJS_Runtime* const m_pJSRuntime; }; class CJS_Date { - friend class CJS_Value; - public: - CJS_Date(CJS_Runtime* pRuntime); + explicit CJS_Date(CJS_Runtime* pRuntime); CJS_Date(CJS_Runtime* pRuntime, double dMsec_time); CJS_Date(CJS_Runtime* pRuntime, int year, @@ -162,39 +158,36 @@ class CJS_Date { int min, int sec); virtual ~CJS_Date(); + void Attach(v8::Local pDate); + bool IsValidDate() const; - int GetYear(); + int GetYear() const; void SetYear(int iYear); - int GetMonth(); + int GetMonth() const; void SetMonth(int iMonth); - int GetDay(); + int GetDay() const; void SetDay(int iDay); - int GetHours(); + int GetHours() const; void SetHours(int iHours); - int GetMinutes(); + int GetMinutes() const; void SetMinutes(int minutes); - int GetSeconds(); + int GetSeconds() const; void SetSeconds(int seconds); - operator v8::Local(); - operator double() const; - + CJS_Runtime* GetJSRuntime() const { return m_pJSRuntime; } + v8::Local ToV8Value() const { return m_pDate; } + double ToDouble() const; CFX_WideString ToString() const; - static double - MakeDate(int year, int mon, int mday, int hour, int min, int sec, int ms); - - FX_BOOL IsValidDate(); - protected: v8::Local m_pDate; - CJS_Runtime* m_pJSRuntime; + CJS_Runtime* const m_pJSRuntime; }; double JS_GetDateTime(); -- cgit v1.2.3