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