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 | 3745841 | 2015-10-06 11:33:46 -0700 | [diff] [blame] | 7 | #ifndef FPDFSDK_SRC_JAVASCRIPT_JS_VALUE_H_ |
| 8 | #define FPDFSDK_SRC_JAVASCRIPT_JS_VALUE_H_ |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 9 | |
Lei Zhang | a688a04 | 2015-11-09 13:57:49 -0800 | [diff] [blame] | 10 | #include "core/include/fxcrt/fx_basic.h" |
Lei Zhang | bde53d2 | 2015-11-12 22:21:30 -0800 | [diff] [blame] | 11 | #include "fpdfsdk/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; |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 17 | class CJS_Runtime; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 18 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 19 | class CJS_Value { |
| 20 | public: |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 21 | enum Type { |
| 22 | VT_unknown, |
| 23 | VT_string, |
| 24 | VT_number, |
| 25 | VT_boolean, |
| 26 | VT_date, |
| 27 | VT_object, |
| 28 | VT_fxobject, |
| 29 | VT_null, |
| 30 | VT_undefined |
| 31 | }; |
| 32 | |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 33 | CJS_Value(CJS_Runtime* pRuntime); |
| 34 | CJS_Value(CJS_Runtime* pRuntime, v8::Local<v8::Value> pValue, Type t); |
| 35 | CJS_Value(CJS_Runtime* pRuntime, const int& iValue); |
| 36 | CJS_Value(CJS_Runtime* pRuntime, const double& dValue); |
| 37 | CJS_Value(CJS_Runtime* pRuntime, const float& fValue); |
| 38 | CJS_Value(CJS_Runtime* pRuntime, const bool& bValue); |
| 39 | CJS_Value(CJS_Runtime* pRuntime, v8::Local<v8::Object>); |
| 40 | CJS_Value(CJS_Runtime* pRuntime, CJS_Object*); |
| 41 | CJS_Value(CJS_Runtime* pRuntime, CJS_Document*); |
| 42 | CJS_Value(CJS_Runtime* pRuntime, const FX_CHAR* pStr); |
| 43 | CJS_Value(CJS_Runtime* pRuntime, const FX_WCHAR* pWstr); |
| 44 | CJS_Value(CJS_Runtime* pRuntime, CJS_Array& array); |
Tom Sepez | f79a69c | 2014-10-30 13:23:42 -0700 | [diff] [blame] | 45 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 46 | ~CJS_Value(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 47 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 48 | void SetNull(); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 49 | void Attach(v8::Local<v8::Value> pValue, Type t); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 50 | void Attach(CJS_Value* pValue); |
| 51 | void Detach(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 52 | |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 53 | Type GetType() const; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 54 | int ToInt() const; |
| 55 | bool ToBool() const; |
| 56 | double ToDouble() const; |
| 57 | float ToFloat() const; |
| 58 | CJS_Object* ToCJSObject() const; |
| 59 | CFX_WideString ToCFXWideString() const; |
| 60 | CFX_ByteString ToCFXByteString() const; |
| 61 | v8::Local<v8::Object> ToV8Object() const; |
| 62 | v8::Local<v8::Array> ToV8Array() const; |
| 63 | v8::Local<v8::Value> ToV8Value() const; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 64 | |
Tom Sepez | 4246b00 | 2016-01-20 11:48:29 -0800 | [diff] [blame] | 65 | // Replace the current |m_pValue| with a v8::Number if possible |
| 66 | // to make one from the current |m_pValue|, updating |m_eType| |
| 67 | // as appropriate to indicate the result. |
| 68 | void MaybeCoerceToNumber(); |
| 69 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 70 | void operator=(int iValue); |
| 71 | void operator=(bool bValue); |
Dan Sinclair | 738b08c | 2016-03-01 14:45:20 -0500 | [diff] [blame^] | 72 | void operator=(double val); |
| 73 | void operator=(float val); |
| 74 | void operator=(CJS_Object* val); |
| 75 | void operator=(CJS_Document* val); |
| 76 | void operator=(v8::Local<v8::Object> val); |
| 77 | void operator=(CJS_Array& val); |
| 78 | void operator=(CJS_Date& val); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 79 | void operator=(const FX_WCHAR* pWstr); |
| 80 | void operator=(const FX_CHAR* pStr); |
| 81 | void operator=(CJS_Value value); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 82 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 83 | FX_BOOL IsArrayObject() const; |
| 84 | FX_BOOL IsDateObject() const; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 85 | FX_BOOL ConvertToArray(CJS_Array&) const; |
| 86 | FX_BOOL ConvertToDate(CJS_Date&) const; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 87 | |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 88 | CJS_Runtime* GetJSRuntime() const { return m_pJSRuntime; } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 89 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 90 | protected: |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 91 | Type m_eType; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 92 | v8::Local<v8::Value> m_pValue; |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 93 | CJS_Runtime* m_pJSRuntime; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 94 | }; |
| 95 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 96 | class CJS_PropValue : public CJS_Value { |
| 97 | public: |
| 98 | CJS_PropValue(const CJS_Value&); |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 99 | CJS_PropValue(CJS_Runtime* pRuntime); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 100 | ~CJS_PropValue(); |
| 101 | |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 102 | FX_BOOL IsSetting() const { return m_bIsSetting; } |
| 103 | FX_BOOL IsGetting() const { return !m_bIsSetting; } |
| 104 | |
Dan Sinclair | 738b08c | 2016-03-01 14:45:20 -0500 | [diff] [blame^] | 105 | void operator<<(int val); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 106 | void operator>>(int&) const; |
Dan Sinclair | 738b08c | 2016-03-01 14:45:20 -0500 | [diff] [blame^] | 107 | void operator<<(bool val); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 108 | void operator>>(bool&) const; |
Dan Sinclair | 738b08c | 2016-03-01 14:45:20 -0500 | [diff] [blame^] | 109 | void operator<<(double val); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 110 | void operator>>(double&) const; |
| 111 | void operator<<(CJS_Object* pObj); |
| 112 | void operator>>(CJS_Object*& ppObj) const; |
| 113 | void operator<<(CJS_Document* pJsDoc); |
| 114 | void operator>>(CJS_Document*& ppJsDoc) const; |
| 115 | void operator<<(CFX_ByteString); |
| 116 | void operator>>(CFX_ByteString&) const; |
| 117 | void operator<<(CFX_WideString); |
| 118 | void operator>>(CFX_WideString&) const; |
| 119 | void operator<<(const FX_WCHAR* c_string); |
Tom Sepez | 808a99e | 2015-09-10 12:28:37 -0700 | [diff] [blame] | 120 | void operator<<(v8::Local<v8::Object>); |
| 121 | void operator>>(v8::Local<v8::Object>&) const; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 122 | void operator>>(CJS_Array& array) const; |
| 123 | void operator<<(CJS_Array& array); |
| 124 | void operator<<(CJS_Date& date); |
| 125 | void operator>>(CJS_Date& date) const; |
| 126 | operator v8::Local<v8::Value>() const; |
| 127 | void StartSetting(); |
| 128 | void StartGetting(); |
| 129 | |
| 130 | private: |
| 131 | FX_BOOL m_bIsSetting; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 132 | }; |
| 133 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 134 | class CJS_Array { |
| 135 | public: |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 136 | CJS_Array(CJS_Runtime* pRuntime); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 137 | virtual ~CJS_Array(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 138 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 139 | void Attach(v8::Local<v8::Array> pArray); |
| 140 | void GetElement(unsigned index, CJS_Value& value); |
| 141 | void SetElement(unsigned index, CJS_Value value); |
| 142 | int GetLength(); |
| 143 | FX_BOOL IsAttached(); |
| 144 | operator v8::Local<v8::Array>(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 145 | |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 146 | CJS_Runtime* GetJSRuntime() const { return m_pJSRuntime; } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 147 | |
| 148 | private: |
| 149 | v8::Local<v8::Array> m_pArray; |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 150 | CJS_Runtime* m_pJSRuntime; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 151 | }; |
| 152 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 153 | class CJS_Date { |
| 154 | friend class CJS_Value; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 155 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 156 | public: |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 157 | CJS_Date(CJS_Runtime* pRuntime); |
| 158 | CJS_Date(CJS_Runtime* pRuntime, double dMsec_time); |
| 159 | CJS_Date(CJS_Runtime* pRuntime, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 160 | int year, |
| 161 | int mon, |
| 162 | int day, |
| 163 | int hour, |
| 164 | int min, |
| 165 | int sec); |
| 166 | virtual ~CJS_Date(); |
| 167 | void Attach(v8::Local<v8::Value> pDate); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 168 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 169 | int GetYear(); |
| 170 | void SetYear(int iYear); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 171 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 172 | int GetMonth(); |
| 173 | void SetMonth(int iMonth); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 174 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 175 | int GetDay(); |
| 176 | void SetDay(int iDay); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 177 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 178 | int GetHours(); |
| 179 | void SetHours(int iHours); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 180 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 181 | int GetMinutes(); |
| 182 | void SetMinutes(int minutes); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 183 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 184 | int GetSeconds(); |
| 185 | void SetSeconds(int seconds); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 186 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 187 | operator v8::Local<v8::Value>(); |
| 188 | operator double() const; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 189 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 190 | CFX_WideString ToString() 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 | static double |
| 193 | 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] | 194 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 195 | FX_BOOL IsValidDate(); |
| 196 | |
| 197 | protected: |
| 198 | v8::Local<v8::Value> m_pDate; |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 199 | CJS_Runtime* m_pJSRuntime; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 200 | }; |
| 201 | |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 202 | double JS_GetDateTime(); |
| 203 | int JS_GetYearFromTime(double dt); |
| 204 | int JS_GetMonthFromTime(double dt); |
| 205 | int JS_GetDayFromTime(double dt); |
| 206 | int JS_GetHourFromTime(double dt); |
| 207 | int JS_GetMinFromTime(double dt); |
| 208 | int JS_GetSecFromTime(double dt); |
| 209 | double JS_DateParse(const wchar_t* string); |
| 210 | double JS_MakeDay(int nYear, int nMonth, int nDay); |
| 211 | double JS_MakeTime(int nHour, int nMin, int nSec, int nMs); |
| 212 | double JS_MakeDate(double day, double time); |
| 213 | bool JS_PortIsNan(double d); |
| 214 | double JS_LocalTime(double d); |
| 215 | |
Tom Sepez | bd93257 | 2016-01-29 09:10:41 -0800 | [diff] [blame] | 216 | // Some JS methods have the bizarre convention that they may also be called |
| 217 | // with a single argument which is an object containing the actual arguments |
| 218 | // as its properties. The varying arguments to this method are the property |
| 219 | // names as wchar_t string literals corresponding to each positional argument. |
| 220 | // The result will always contain |nKeywords| value, with unspecified ones |
| 221 | // being set to type VT_unknown. |
| 222 | std::vector<CJS_Value> JS_ExpandKeywordParams( |
| 223 | CJS_Runtime* pRuntime, |
| 224 | const std::vector<CJS_Value>& originals, |
| 225 | size_t nKeywords, |
| 226 | ...); |
| 227 | |
Tom Sepez | 3745841 | 2015-10-06 11:33:46 -0700 | [diff] [blame] | 228 | #endif // FPDFSDK_SRC_JAVASCRIPT_JS_VALUE_H_ |