blob: 8e791253c61fd3af64312f7b7c2c27ac4b4ce4b5 [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// 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 Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Dan Sinclairf766ad22016-03-14 13:51:24 -04007#ifndef FPDFSDK_JAVASCRIPT_JS_VALUE_H_
8#define FPDFSDK_JAVASCRIPT_JS_VALUE_H_
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07009
Dan Sinclair3ebd1212016-03-09 09:59:23 -050010#include <vector>
11
Dan Sinclaira8a28e02016-03-23 15:41:39 -040012#include "core/fxcrt/include/fx_basic.h"
dsinclairb3f24672016-07-12 10:42:14 -070013#include "fxjs/include/fxjs_v8.h"
Tom Sepez9a3f8122015-04-07 15:35:48 -070014
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070015class CJS_Array;
16class CJS_Date;
Tom Sepezf79a69c2014-10-30 13:23:42 -070017class CJS_Document;
18class CJS_Object;
Tom Sepez67fd5df2015-10-08 12:24:19 -070019class CJS_Runtime;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070020
Nico Weber9d8ec5a2015-08-04 13:00:21 -070021class CJS_Value {
22 public:
Tom Sepez39bfe122015-09-17 15:25:23 -070023 enum Type {
24 VT_unknown,
25 VT_string,
26 VT_number,
27 VT_boolean,
28 VT_date,
29 VT_object,
Tom Sepez39bfe122015-09-17 15:25:23 -070030 VT_null,
31 VT_undefined
32 };
33
Tom Sepez67fd5df2015-10-08 12:24:19 -070034 CJS_Value(CJS_Runtime* pRuntime);
tsepez40faa792016-07-15 17:58:02 -070035 CJS_Value(CJS_Runtime* pRuntime, v8::Local<v8::Value> pValue);
Tom Sepez67fd5df2015-10-08 12:24:19 -070036 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);
tsepez40faa792016-07-15 17:58:02 -070040 CJS_Value(CJS_Runtime* pRuntime, CJS_Object* pObj);
Tom Sepez67fd5df2015-10-08 12:24:19 -070041 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);
Tom Sepezf79a69c2014-10-30 13:23:42 -070044
Nico Weber9d8ec5a2015-08-04 13:00:21 -070045 ~CJS_Value();
weili625ad662016-06-15 11:21:33 -070046 CJS_Value(const CJS_Value& other);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070047
Nico Weber9d8ec5a2015-08-04 13:00:21 -070048 void SetNull();
tsepez40faa792016-07-15 17:58:02 -070049 void Attach(v8::Local<v8::Value> pValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070050 void Attach(CJS_Value* pValue);
51 void Detach();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070052
tsepez40faa792016-07-15 17:58:02 -070053 static Type GetValueType(v8::Local<v8::Value> value);
54 Type GetType() const { return GetValueType(m_pValue); }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070055 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-Malek3f3b45c2014-05-23 17:28:10 -070065
Tom Sepez4246b002016-01-20 11:48:29 -080066 // Replace the current |m_pValue| with a v8::Number if possible
tsepez40faa792016-07-15 17:58:02 -070067 // to make one from the current |m_pValue|.
Tom Sepez4246b002016-01-20 11:48:29 -080068 void MaybeCoerceToNumber();
69
Nico Weber9d8ec5a2015-08-04 13:00:21 -070070 void operator=(int iValue);
71 void operator=(bool bValue);
Dan Sinclair738b08c2016-03-01 14:45:20 -050072 void operator=(double val);
73 void operator=(float val);
74 void operator=(CJS_Object* val);
Dan Sinclair738b08c2016-03-01 14:45:20 -050075 void operator=(v8::Local<v8::Object> val);
76 void operator=(CJS_Array& val);
77 void operator=(CJS_Date& val);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070078 void operator=(const FX_WCHAR* pWstr);
79 void operator=(const FX_CHAR* pStr);
80 void operator=(CJS_Value value);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070081
Nico Weber9d8ec5a2015-08-04 13:00:21 -070082 FX_BOOL IsArrayObject() const;
83 FX_BOOL IsDateObject() const;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070084 FX_BOOL ConvertToArray(CJS_Array&) const;
85 FX_BOOL ConvertToDate(CJS_Date&) const;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070086
Tom Sepez67fd5df2015-10-08 12:24:19 -070087 CJS_Runtime* GetJSRuntime() const { return m_pJSRuntime; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070088
Nico Weber9d8ec5a2015-08-04 13:00:21 -070089 protected:
90 v8::Local<v8::Value> m_pValue;
Tom Sepez67fd5df2015-10-08 12:24:19 -070091 CJS_Runtime* m_pJSRuntime;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070092};
93
Nico Weber9d8ec5a2015-08-04 13:00:21 -070094class CJS_PropValue : public CJS_Value {
95 public:
96 CJS_PropValue(const CJS_Value&);
Tom Sepez67fd5df2015-10-08 12:24:19 -070097 CJS_PropValue(CJS_Runtime* pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070098 ~CJS_PropValue();
99
Tom Sepez67fd5df2015-10-08 12:24:19 -0700100 FX_BOOL IsSetting() const { return m_bIsSetting; }
101 FX_BOOL IsGetting() const { return !m_bIsSetting; }
102
Dan Sinclair738b08c2016-03-01 14:45:20 -0500103 void operator<<(int val);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700104 void operator>>(int&) const;
Dan Sinclair738b08c2016-03-01 14:45:20 -0500105 void operator<<(bool val);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700106 void operator>>(bool&) const;
Dan Sinclair738b08c2016-03-01 14:45:20 -0500107 void operator<<(double val);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700108 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 Sepez808a99e2015-09-10 12:28:37 -0700118 void operator<<(v8::Local<v8::Object>);
119 void operator>>(v8::Local<v8::Object>&) const;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700120 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;
124 operator v8::Local<v8::Value>() const;
125 void StartSetting();
126 void StartGetting();
127
128 private:
129 FX_BOOL m_bIsSetting;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700130};
131
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700132class CJS_Array {
133 public:
Tom Sepez67fd5df2015-10-08 12:24:19 -0700134 CJS_Array(CJS_Runtime* pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700135 virtual ~CJS_Array();
weili625ad662016-06-15 11:21:33 -0700136 CJS_Array(const CJS_Array& other);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700137
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700138 void Attach(v8::Local<v8::Array> pArray);
139 void GetElement(unsigned index, CJS_Value& value);
140 void SetElement(unsigned index, CJS_Value value);
141 int GetLength();
142 FX_BOOL IsAttached();
143 operator v8::Local<v8::Array>();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700144
Tom Sepez67fd5df2015-10-08 12:24:19 -0700145 CJS_Runtime* GetJSRuntime() const { return m_pJSRuntime; }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700146
147 private:
148 v8::Local<v8::Array> m_pArray;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700149 CJS_Runtime* m_pJSRuntime;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700150};
151
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700152class CJS_Date {
153 friend class CJS_Value;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700154
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700155 public:
Tom Sepez67fd5df2015-10-08 12:24:19 -0700156 CJS_Date(CJS_Runtime* pRuntime);
157 CJS_Date(CJS_Runtime* pRuntime, double dMsec_time);
158 CJS_Date(CJS_Runtime* pRuntime,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700159 int year,
160 int mon,
161 int day,
162 int hour,
163 int min,
164 int sec);
165 virtual ~CJS_Date();
166 void Attach(v8::Local<v8::Value> pDate);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700167
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700168 int GetYear();
169 void SetYear(int iYear);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700170
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700171 int GetMonth();
172 void SetMonth(int iMonth);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700173
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700174 int GetDay();
175 void SetDay(int iDay);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700176
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700177 int GetHours();
178 void SetHours(int iHours);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700179
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700180 int GetMinutes();
181 void SetMinutes(int minutes);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700182
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700183 int GetSeconds();
184 void SetSeconds(int seconds);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700185
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700186 operator v8::Local<v8::Value>();
187 operator double() const;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700188
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700189 CFX_WideString ToString() const;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700190
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700191 static double
192 MakeDate(int year, int mon, int mday, int hour, int min, int sec, int ms);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700193
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700194 FX_BOOL IsValidDate();
195
196 protected:
197 v8::Local<v8::Value> m_pDate;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700198 CJS_Runtime* m_pJSRuntime;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700199};
200
Tom Sepez39bfe122015-09-17 15:25:23 -0700201double JS_GetDateTime();
202int JS_GetYearFromTime(double dt);
203int JS_GetMonthFromTime(double dt);
204int JS_GetDayFromTime(double dt);
205int JS_GetHourFromTime(double dt);
206int JS_GetMinFromTime(double dt);
207int JS_GetSecFromTime(double dt);
tsepez018935c2016-04-15 13:15:12 -0700208double JS_DateParse(const CFX_WideString& str);
Tom Sepez39bfe122015-09-17 15:25:23 -0700209double JS_MakeDay(int nYear, int nMonth, int nDay);
210double JS_MakeTime(int nHour, int nMin, int nSec, int nMs);
211double JS_MakeDate(double day, double time);
212bool JS_PortIsNan(double d);
213double JS_LocalTime(double d);
214
Tom Sepezbd932572016-01-29 09:10:41 -0800215// Some JS methods have the bizarre convention that they may also be called
216// with a single argument which is an object containing the actual arguments
217// as its properties. The varying arguments to this method are the property
218// names as wchar_t string literals corresponding to each positional argument.
219// The result will always contain |nKeywords| value, with unspecified ones
220// being set to type VT_unknown.
221std::vector<CJS_Value> JS_ExpandKeywordParams(
222 CJS_Runtime* pRuntime,
223 const std::vector<CJS_Value>& originals,
224 size_t nKeywords,
225 ...);
226
Dan Sinclairf766ad22016-03-14 13:51:24 -0400227#endif // FPDFSDK_JAVASCRIPT_JS_VALUE_H_