John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1 | // 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 Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 4 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
Tom Sepez | 19922bb | 2015-05-28 13:23:12 -0700 | [diff] [blame] | 7 | #ifndef FPDFSDK_INCLUDE_JAVASCRIPT_JS_VALUE_H_ |
| 8 | #define FPDFSDK_INCLUDE_JAVASCRIPT_JS_VALUE_H_ |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 9 | |
Tom Sepez | 9a3f812 | 2015-04-07 15:35:48 -0700 | [diff] [blame] | 10 | #include "../../../core/include/fxcrt/fx_basic.h" |
Tom Sepez | 19922bb | 2015-05-28 13:23:12 -0700 | [diff] [blame] | 11 | #include "../jsapi/fxjs_v8.h" |
Tom Sepez | 9a3f812 | 2015-04-07 15:35:48 -0700 | [diff] [blame] | 12 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 13 | class CJS_Array; |
| 14 | class CJS_Date; |
Tom Sepez | f79a69c | 2014-10-30 13:23:42 -0700 | [diff] [blame] | 15 | class CJS_Document; |
| 16 | class CJS_Object; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 17 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 18 | class CJS_Value { |
| 19 | public: |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame^] | 20 | enum Type { |
| 21 | VT_unknown, |
| 22 | VT_string, |
| 23 | VT_number, |
| 24 | VT_boolean, |
| 25 | VT_date, |
| 26 | VT_object, |
| 27 | VT_fxobject, |
| 28 | VT_null, |
| 29 | VT_undefined |
| 30 | }; |
| 31 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 32 | CJS_Value(v8::Isolate* isolate); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame^] | 33 | CJS_Value(v8::Isolate* isolate, v8::Local<v8::Value> pValue, Type t); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 34 | CJS_Value(v8::Isolate* isolate, const int& iValue); |
| 35 | CJS_Value(v8::Isolate* isolate, const double& dValue); |
| 36 | CJS_Value(v8::Isolate* isolate, const float& fValue); |
| 37 | CJS_Value(v8::Isolate* isolate, const bool& bValue); |
Tom Sepez | 808a99e | 2015-09-10 12:28:37 -0700 | [diff] [blame] | 38 | CJS_Value(v8::Isolate* isolate, v8::Local<v8::Object>); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 39 | CJS_Value(v8::Isolate* isolate, CJS_Object*); |
| 40 | CJS_Value(v8::Isolate* isolate, CJS_Document*); |
| 41 | CJS_Value(v8::Isolate* isolate, const FX_CHAR* pStr); |
| 42 | CJS_Value(v8::Isolate* isolate, const FX_WCHAR* pWstr); |
| 43 | CJS_Value(v8::Isolate* isolate, CJS_Array& array); |
Tom Sepez | f79a69c | 2014-10-30 13:23:42 -0700 | [diff] [blame] | 44 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 45 | ~CJS_Value(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 46 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 47 | void SetNull(); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame^] | 48 | void Attach(v8::Local<v8::Value> pValue, Type t); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 49 | void Attach(CJS_Value* pValue); |
| 50 | void Detach(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 51 | |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame^] | 52 | Type GetType() const; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 53 | int ToInt() const; |
| 54 | bool ToBool() const; |
| 55 | double ToDouble() const; |
| 56 | float ToFloat() const; |
| 57 | CJS_Object* ToCJSObject() const; |
| 58 | CFX_WideString ToCFXWideString() const; |
| 59 | CFX_ByteString ToCFXByteString() const; |
| 60 | v8::Local<v8::Object> ToV8Object() const; |
| 61 | v8::Local<v8::Array> ToV8Array() const; |
| 62 | v8::Local<v8::Value> ToV8Value() const; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 63 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 64 | void operator=(int iValue); |
| 65 | void operator=(bool bValue); |
| 66 | void operator=(double); |
| 67 | void operator=(float); |
| 68 | void operator=(CJS_Object*); |
| 69 | void operator=(CJS_Document*); |
| 70 | void operator=(v8::Local<v8::Object>); |
| 71 | void operator=(CJS_Array&); |
| 72 | void operator=(CJS_Date&); |
| 73 | void operator=(const FX_WCHAR* pWstr); |
| 74 | void operator=(const FX_CHAR* pStr); |
| 75 | void operator=(CJS_Value value); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 76 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 77 | FX_BOOL IsArrayObject() const; |
| 78 | FX_BOOL IsDateObject() const; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 79 | FX_BOOL ConvertToArray(CJS_Array&) const; |
| 80 | FX_BOOL ConvertToDate(CJS_Date&) const; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 81 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 82 | v8::Isolate* GetIsolate() { return m_isolate; } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 83 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 84 | protected: |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame^] | 85 | Type m_eType; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 86 | v8::Local<v8::Value> m_pValue; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 87 | v8::Isolate* m_isolate; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 88 | }; |
| 89 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 90 | class CJS_Parameters : public CFX_ArrayTemplate<CJS_Value> { |
| 91 | public: |
| 92 | void push_back(const CJS_Value& newElement) { |
| 93 | CFX_ArrayTemplate<CJS_Value>::Add(newElement); |
| 94 | } |
| 95 | int size() const { return CFX_ArrayTemplate<CJS_Value>::GetSize(); } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 96 | }; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 97 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 98 | class CJS_PropValue : public CJS_Value { |
| 99 | public: |
| 100 | CJS_PropValue(const CJS_Value&); |
| 101 | CJS_PropValue(v8::Isolate* isolate); |
| 102 | ~CJS_PropValue(); |
| 103 | |
| 104 | public: |
| 105 | FX_BOOL IsSetting(); |
| 106 | FX_BOOL IsGetting(); |
| 107 | void operator<<(int); |
| 108 | void operator>>(int&) const; |
| 109 | void operator<<(bool); |
| 110 | void operator>>(bool&) const; |
| 111 | void operator<<(double); |
| 112 | void operator>>(double&) const; |
| 113 | void operator<<(CJS_Object* pObj); |
| 114 | void operator>>(CJS_Object*& ppObj) const; |
| 115 | void operator<<(CJS_Document* pJsDoc); |
| 116 | void operator>>(CJS_Document*& ppJsDoc) const; |
| 117 | void operator<<(CFX_ByteString); |
| 118 | void operator>>(CFX_ByteString&) const; |
| 119 | void operator<<(CFX_WideString); |
| 120 | void operator>>(CFX_WideString&) const; |
| 121 | void operator<<(const FX_WCHAR* c_string); |
Tom Sepez | 808a99e | 2015-09-10 12:28:37 -0700 | [diff] [blame] | 122 | void operator<<(v8::Local<v8::Object>); |
| 123 | void operator>>(v8::Local<v8::Object>&) const; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 124 | void operator>>(CJS_Array& array) const; |
| 125 | void operator<<(CJS_Array& array); |
| 126 | void operator<<(CJS_Date& date); |
| 127 | void operator>>(CJS_Date& date) const; |
| 128 | operator v8::Local<v8::Value>() const; |
| 129 | void StartSetting(); |
| 130 | void StartGetting(); |
| 131 | |
| 132 | private: |
| 133 | FX_BOOL m_bIsSetting; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 134 | }; |
| 135 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 136 | class CJS_Array { |
| 137 | public: |
| 138 | CJS_Array(v8::Isolate* isolate); |
| 139 | virtual ~CJS_Array(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 140 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 141 | void Attach(v8::Local<v8::Array> pArray); |
| 142 | void GetElement(unsigned index, CJS_Value& value); |
| 143 | void SetElement(unsigned index, CJS_Value value); |
| 144 | int GetLength(); |
| 145 | FX_BOOL IsAttached(); |
| 146 | operator v8::Local<v8::Array>(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 147 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 148 | v8::Isolate* GetIsolate() { return m_isolate; } |
| 149 | |
| 150 | private: |
| 151 | v8::Local<v8::Array> m_pArray; |
| 152 | v8::Isolate* m_isolate; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 153 | }; |
| 154 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 155 | class CJS_Date { |
| 156 | friend class CJS_Value; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 157 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 158 | public: |
| 159 | CJS_Date(v8::Isolate* isolate); |
| 160 | CJS_Date(v8::Isolate* isolate, double dMsec_time); |
| 161 | CJS_Date(v8::Isolate* isolate, |
| 162 | int year, |
| 163 | int mon, |
| 164 | int day, |
| 165 | int hour, |
| 166 | int min, |
| 167 | int sec); |
| 168 | virtual ~CJS_Date(); |
| 169 | void Attach(v8::Local<v8::Value> pDate); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 170 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 171 | int GetYear(); |
| 172 | void SetYear(int iYear); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 173 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 174 | int GetMonth(); |
| 175 | void SetMonth(int iMonth); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 176 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 177 | int GetDay(); |
| 178 | void SetDay(int iDay); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 179 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 180 | int GetHours(); |
| 181 | void SetHours(int iHours); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 182 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 183 | int GetMinutes(); |
| 184 | void SetMinutes(int minutes); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 185 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 186 | int GetSeconds(); |
| 187 | void SetSeconds(int seconds); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 188 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 189 | operator v8::Local<v8::Value>(); |
| 190 | operator double() const; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 191 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 192 | CFX_WideString ToString() const; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 193 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 194 | static double |
| 195 | MakeDate(int year, int mon, int mday, int hour, int min, int sec, int ms); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 196 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 197 | FX_BOOL IsValidDate(); |
| 198 | |
| 199 | protected: |
| 200 | v8::Local<v8::Value> m_pDate; |
| 201 | v8::Isolate* m_isolate; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 202 | }; |
| 203 | |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame^] | 204 | double JS_GetDateTime(); |
| 205 | int JS_GetYearFromTime(double dt); |
| 206 | int JS_GetMonthFromTime(double dt); |
| 207 | int JS_GetDayFromTime(double dt); |
| 208 | int JS_GetHourFromTime(double dt); |
| 209 | int JS_GetMinFromTime(double dt); |
| 210 | int JS_GetSecFromTime(double dt); |
| 211 | double JS_DateParse(const wchar_t* string); |
| 212 | double JS_MakeDay(int nYear, int nMonth, int nDay); |
| 213 | double JS_MakeTime(int nHour, int nMin, int nSec, int nMs); |
| 214 | double JS_MakeDate(double day, double time); |
| 215 | bool JS_PortIsNan(double d); |
| 216 | double JS_LocalTime(double d); |
| 217 | |
Tom Sepez | 19922bb | 2015-05-28 13:23:12 -0700 | [diff] [blame] | 218 | #endif // FPDFSDK_INCLUDE_JAVASCRIPT_JS_VALUE_H_ |