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