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. |
| 4 | |
| 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
| 7 | #ifndef _JS_VALUE_H_ |
| 8 | #define _JS_VALUE_H_ |
| 9 | |
| 10 | class CJS_Array; |
| 11 | class CJS_Date; |
Tom Sepez | f79a69c | 2014-10-30 13:23:42 -0700 | [diff] [blame^] | 12 | class CJS_Document; |
| 13 | class CJS_Object; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 14 | |
| 15 | class CJS_Value |
| 16 | { |
| 17 | public: |
| 18 | CJS_Value(v8::Isolate* isolate); |
| 19 | CJS_Value(v8::Isolate* isolate, v8::Handle<v8::Value> pValue,FXJSVALUETYPE t); |
| 20 | CJS_Value(v8::Isolate* isolate, const int &iValue); |
| 21 | CJS_Value(v8::Isolate* isolate, const double &dValue); |
Tom Sepez | f79a69c | 2014-10-30 13:23:42 -0700 | [diff] [blame^] | 22 | CJS_Value(v8::Isolate* isolate, const float &fValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 23 | CJS_Value(v8::Isolate* isolate, const bool &bValue); |
| 24 | CJS_Value(v8::Isolate* isolate, JSFXObject); |
Tom Sepez | f79a69c | 2014-10-30 13:23:42 -0700 | [diff] [blame^] | 25 | CJS_Value(v8::Isolate* isolate, CJS_Object*); |
| 26 | CJS_Value(v8::Isolate* isolate, CJS_Document*); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 27 | CJS_Value(v8::Isolate* isolate, FX_LPCSTR pStr); |
| 28 | CJS_Value(v8::Isolate* isolate, FX_LPCWSTR pWstr); |
| 29 | CJS_Value(v8::Isolate* isolate, CJS_Array& array); |
Tom Sepez | f79a69c | 2014-10-30 13:23:42 -0700 | [diff] [blame^] | 30 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 31 | ~CJS_Value(); |
| 32 | |
| 33 | void SetNull(); |
| 34 | void Attach(v8::Handle<v8::Value> pValue,FXJSVALUETYPE t); |
| 35 | void Attach(CJS_Value *pValue); |
| 36 | void Detach(); |
| 37 | |
| 38 | |
| 39 | operator int() const; |
| 40 | operator bool() const; |
| 41 | operator double() const; |
| 42 | operator float() const; |
Tom Sepez | f79a69c | 2014-10-30 13:23:42 -0700 | [diff] [blame^] | 43 | operator CJS_Object*() const; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 44 | operator v8::Handle<v8::Object>() const; |
| 45 | operator v8::Handle<v8::Array>() const; |
| 46 | operator CFX_WideString() const; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 47 | operator CFX_ByteString() const; |
| 48 | v8::Handle<v8::Value> ToJSValue(); |
| 49 | |
| 50 | void operator = (int iValue); |
Tom Sepez | f79a69c | 2014-10-30 13:23:42 -0700 | [diff] [blame^] | 51 | void operator = (bool bValue); |
| 52 | void operator = (double); |
| 53 | void operator = (float); |
| 54 | void operator = (CJS_Object*); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 55 | void operator = (v8::Handle<v8::Object>); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 56 | void operator = (CJS_Array &); |
| 57 | void operator = (CJS_Date &); |
Tom Sepez | f79a69c | 2014-10-30 13:23:42 -0700 | [diff] [blame^] | 58 | void operator = (FX_LPCWSTR pWstr); |
| 59 | void operator = (FX_LPCSTR pStr); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 60 | void operator = (CJS_Value value); |
Tom Sepez | f79a69c | 2014-10-30 13:23:42 -0700 | [diff] [blame^] | 61 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 62 | FX_BOOL IsArrayObject() const; |
| 63 | FX_BOOL IsDateObject() const; |
| 64 | FXJSVALUETYPE GetType() const; |
| 65 | |
| 66 | FX_BOOL ConvertToArray(CJS_Array &) const; |
| 67 | FX_BOOL ConvertToDate(CJS_Date &) const; |
| 68 | |
| 69 | v8::Isolate* GetIsolate() {return m_isolate;} |
Tom Sepez | f79a69c | 2014-10-30 13:23:42 -0700 | [diff] [blame^] | 70 | protected: |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 71 | v8::Handle<v8::Value> m_pValue; |
| 72 | FXJSVALUETYPE m_eType; |
| 73 | v8::Isolate* m_isolate; |
| 74 | }; |
| 75 | |
| 76 | template<class TYPE> class CJS_ParametersTmpl : public CFX_ArrayTemplate<TYPE> |
| 77 | { |
| 78 | public: |
| 79 | void push_back(TYPE newElement){CFX_ArrayTemplate<TYPE>::Add(newElement);} |
| 80 | int size() const{return CFX_ArrayTemplate<TYPE>::GetSize();} |
| 81 | }; |
| 82 | typedef CJS_ParametersTmpl<CJS_Value> CJS_Parameters; |
| 83 | |
| 84 | class CJS_PropValue: public CJS_Value |
| 85 | { |
| 86 | public: |
| 87 | CJS_PropValue(const CJS_Value &); |
| 88 | CJS_PropValue(v8::Isolate* isolate); |
| 89 | ~CJS_PropValue(); |
| 90 | public: |
| 91 | FX_BOOL IsSetting(); |
| 92 | FX_BOOL IsGetting(); |
| 93 | void operator<<(int ); |
| 94 | void operator>>(int &) const; |
| 95 | void operator<<(bool); |
| 96 | void operator>>(bool &) const; |
| 97 | void operator<<(double ); |
| 98 | void operator>>(double &) const; |
| 99 | void operator<<(CJS_Object *pObj); |
| 100 | void operator>>(CJS_Object *&ppObj) const; |
| 101 | void operator<<(CFX_ByteString); |
| 102 | void operator>>(CFX_ByteString &) const; |
| 103 | void operator<<(CFX_WideString); |
| 104 | void operator>>(CFX_WideString &) const; |
| 105 | void operator<<(FX_LPCWSTR c_string); |
| 106 | |
| 107 | void operator<<(JSFXObject); |
| 108 | void operator>>(JSFXObject &) const; |
| 109 | |
| 110 | void operator>>(CJS_Array &array) const; |
| 111 | void operator<<(CJS_Array &array); |
| 112 | |
| 113 | void operator<<(CJS_Date &date); |
| 114 | void operator>>(CJS_Date &date) const; |
| 115 | |
| 116 | operator v8::Handle<v8::Value>() const; |
| 117 | |
| 118 | void StartSetting(); |
| 119 | void StartGetting(); |
| 120 | private: |
| 121 | FX_BOOL m_bIsSetting; |
| 122 | }; |
| 123 | |
| 124 | class CJS_Array |
| 125 | { |
| 126 | public: |
| 127 | CJS_Array(v8::Isolate* isolate); |
| 128 | virtual ~CJS_Array(); |
| 129 | |
| 130 | void Attach(v8::Handle<v8::Array> pArray); |
| 131 | void GetElement(unsigned index,CJS_Value &value); |
| 132 | void SetElement(unsigned index,CJS_Value value); |
| 133 | int GetLength(); |
| 134 | FX_BOOL IsAttached(); |
| 135 | operator v8::Handle<v8::Array>(); |
| 136 | |
| 137 | v8::Isolate* GetIsolate() {return m_isolate;} |
| 138 | private: |
| 139 | v8::Handle<v8::Array> m_pArray; |
| 140 | v8::Isolate* m_isolate; |
| 141 | }; |
| 142 | |
| 143 | class CJS_Date |
| 144 | { |
| 145 | friend class CJS_Value; |
| 146 | public: |
| 147 | CJS_Date(v8::Isolate* isolate); |
| 148 | CJS_Date(v8::Isolate* isolate,double dMsec_time); |
| 149 | CJS_Date(v8::Isolate* isolate,int year, int mon, int day,int hour, int min, int sec); |
| 150 | virtual ~CJS_Date(); |
| 151 | void Attach(v8::Handle<v8::Value> pDate); |
| 152 | |
| 153 | int GetYear(); |
| 154 | void SetYear(int iYear); |
| 155 | |
| 156 | int GetMonth(); |
| 157 | void SetMonth(int iMonth); |
| 158 | |
| 159 | int GetDay(); |
| 160 | void SetDay(int iDay); |
| 161 | |
| 162 | int GetHours(); |
| 163 | void SetHours(int iHours); |
| 164 | |
| 165 | int GetMinutes(); |
| 166 | void SetMinutes(int minutes); |
| 167 | |
| 168 | int GetSeconds(); |
| 169 | void SetSeconds(int seconds); |
| 170 | |
| 171 | operator v8::Handle<v8::Value>(); |
| 172 | operator double() const; |
| 173 | |
| 174 | CFX_WideString ToString() const; |
| 175 | |
| 176 | static double MakeDate(int year, int mon, int mday,int hour, int min, int sec,int ms); |
| 177 | |
| 178 | FX_BOOL IsValidDate(); |
| 179 | |
| 180 | protected: |
| 181 | v8::Handle<v8::Value> m_pDate; |
| 182 | v8::Isolate* m_isolate; |
| 183 | }; |
| 184 | |
| 185 | #endif //_JS_VALUE_H_ |
| 186 | |