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