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