blob: e8544504d20e28393fbe80eff74b4490a8b24f76 [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,
30 VT_fxobject,
31 VT_null,
32 VT_undefined
33 };
34
Tom Sepez67fd5df2015-10-08 12:24:19 -070035 CJS_Value(CJS_Runtime* pRuntime);
36 CJS_Value(CJS_Runtime* pRuntime, v8::Local<v8::Value> pValue, Type t);
37 CJS_Value(CJS_Runtime* pRuntime, const int& iValue);
38 CJS_Value(CJS_Runtime* pRuntime, const double& dValue);
39 CJS_Value(CJS_Runtime* pRuntime, const float& fValue);
40 CJS_Value(CJS_Runtime* pRuntime, const bool& bValue);
41 CJS_Value(CJS_Runtime* pRuntime, v8::Local<v8::Object>);
42 CJS_Value(CJS_Runtime* pRuntime, CJS_Object*);
43 CJS_Value(CJS_Runtime* pRuntime, CJS_Document*);
44 CJS_Value(CJS_Runtime* pRuntime, const FX_CHAR* pStr);
45 CJS_Value(CJS_Runtime* pRuntime, const FX_WCHAR* pWstr);
46 CJS_Value(CJS_Runtime* pRuntime, CJS_Array& array);
Tom Sepezf79a69c2014-10-30 13:23:42 -070047
Nico Weber9d8ec5a2015-08-04 13:00:21 -070048 ~CJS_Value();
weili625ad662016-06-15 11:21:33 -070049 CJS_Value(const CJS_Value& other);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070050
Nico Weber9d8ec5a2015-08-04 13:00:21 -070051 void SetNull();
Tom Sepez39bfe122015-09-17 15:25:23 -070052 void Attach(v8::Local<v8::Value> pValue, Type t);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070053 void Attach(CJS_Value* pValue);
54 void Detach();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070055
Tom Sepez39bfe122015-09-17 15:25:23 -070056 Type GetType() const;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070057 int ToInt() const;
58 bool ToBool() const;
59 double ToDouble() const;
60 float ToFloat() const;
61 CJS_Object* ToCJSObject() const;
62 CFX_WideString ToCFXWideString() const;
63 CFX_ByteString ToCFXByteString() const;
64 v8::Local<v8::Object> ToV8Object() const;
65 v8::Local<v8::Array> ToV8Array() const;
66 v8::Local<v8::Value> ToV8Value() const;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070067
Tom Sepez4246b002016-01-20 11:48:29 -080068 // Replace the current |m_pValue| with a v8::Number if possible
69 // to make one from the current |m_pValue|, updating |m_eType|
70 // as appropriate to indicate the result.
71 void MaybeCoerceToNumber();
72
Nico Weber9d8ec5a2015-08-04 13:00:21 -070073 void operator=(int iValue);
74 void operator=(bool bValue);
Dan Sinclair738b08c2016-03-01 14:45:20 -050075 void operator=(double val);
76 void operator=(float val);
77 void operator=(CJS_Object* val);
78 void operator=(CJS_Document* val);
79 void operator=(v8::Local<v8::Object> val);
80 void operator=(CJS_Array& val);
81 void operator=(CJS_Date& val);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070082 void operator=(const FX_WCHAR* pWstr);
83 void operator=(const FX_CHAR* pStr);
84 void operator=(CJS_Value value);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070085
Nico Weber9d8ec5a2015-08-04 13:00:21 -070086 FX_BOOL IsArrayObject() const;
87 FX_BOOL IsDateObject() const;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070088 FX_BOOL ConvertToArray(CJS_Array&) const;
89 FX_BOOL ConvertToDate(CJS_Date&) const;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070090
Tom Sepez67fd5df2015-10-08 12:24:19 -070091 CJS_Runtime* GetJSRuntime() const { return m_pJSRuntime; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070092
Nico Weber9d8ec5a2015-08-04 13:00:21 -070093 protected:
Tom Sepez39bfe122015-09-17 15:25:23 -070094 Type m_eType;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070095 v8::Local<v8::Value> m_pValue;
Tom Sepez67fd5df2015-10-08 12:24:19 -070096 CJS_Runtime* m_pJSRuntime;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070097};
98
Nico Weber9d8ec5a2015-08-04 13:00:21 -070099class CJS_PropValue : public CJS_Value {
100 public:
101 CJS_PropValue(const CJS_Value&);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700102 CJS_PropValue(CJS_Runtime* pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700103 ~CJS_PropValue();
104
Tom Sepez67fd5df2015-10-08 12:24:19 -0700105 FX_BOOL IsSetting() const { return m_bIsSetting; }
106 FX_BOOL IsGetting() const { return !m_bIsSetting; }
107
Dan Sinclair738b08c2016-03-01 14:45:20 -0500108 void operator<<(int val);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700109 void operator>>(int&) const;
Dan Sinclair738b08c2016-03-01 14:45:20 -0500110 void operator<<(bool val);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700111 void operator>>(bool&) const;
Dan Sinclair738b08c2016-03-01 14:45:20 -0500112 void operator<<(double val);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700113 void operator>>(double&) const;
114 void operator<<(CJS_Object* pObj);
115 void operator>>(CJS_Object*& ppObj) const;
116 void operator<<(CJS_Document* pJsDoc);
117 void operator>>(CJS_Document*& ppJsDoc) const;
118 void operator<<(CFX_ByteString);
119 void operator>>(CFX_ByteString&) const;
120 void operator<<(CFX_WideString);
121 void operator>>(CFX_WideString&) const;
122 void operator<<(const FX_WCHAR* c_string);
Tom Sepez808a99e2015-09-10 12:28:37 -0700123 void operator<<(v8::Local<v8::Object>);
124 void operator>>(v8::Local<v8::Object>&) const;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700125 void operator>>(CJS_Array& array) const;
126 void operator<<(CJS_Array& array);
127 void operator<<(CJS_Date& date);
128 void operator>>(CJS_Date& date) const;
129 operator v8::Local<v8::Value>() const;
130 void StartSetting();
131 void StartGetting();
132
133 private:
134 FX_BOOL m_bIsSetting;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700135};
136
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700137class CJS_Array {
138 public:
Tom Sepez67fd5df2015-10-08 12:24:19 -0700139 CJS_Array(CJS_Runtime* pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700140 virtual ~CJS_Array();
weili625ad662016-06-15 11:21:33 -0700141 CJS_Array(const CJS_Array& other);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700142
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700143 void Attach(v8::Local<v8::Array> pArray);
144 void GetElement(unsigned index, CJS_Value& value);
145 void SetElement(unsigned index, CJS_Value value);
146 int GetLength();
147 FX_BOOL IsAttached();
148 operator v8::Local<v8::Array>();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700149
Tom Sepez67fd5df2015-10-08 12:24:19 -0700150 CJS_Runtime* GetJSRuntime() const { return m_pJSRuntime; }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700151
152 private:
153 v8::Local<v8::Array> m_pArray;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700154 CJS_Runtime* m_pJSRuntime;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700155};
156
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700157class CJS_Date {
158 friend class CJS_Value;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700159
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700160 public:
Tom Sepez67fd5df2015-10-08 12:24:19 -0700161 CJS_Date(CJS_Runtime* pRuntime);
162 CJS_Date(CJS_Runtime* pRuntime, double dMsec_time);
163 CJS_Date(CJS_Runtime* pRuntime,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700164 int year,
165 int mon,
166 int day,
167 int hour,
168 int min,
169 int sec);
170 virtual ~CJS_Date();
171 void Attach(v8::Local<v8::Value> pDate);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700172
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700173 int GetYear();
174 void SetYear(int iYear);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700175
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700176 int GetMonth();
177 void SetMonth(int iMonth);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700178
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700179 int GetDay();
180 void SetDay(int iDay);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700181
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700182 int GetHours();
183 void SetHours(int iHours);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700184
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700185 int GetMinutes();
186 void SetMinutes(int minutes);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700187
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700188 int GetSeconds();
189 void SetSeconds(int seconds);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700190
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700191 operator v8::Local<v8::Value>();
192 operator double() const;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700193
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700194 CFX_WideString ToString() const;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700195
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700196 static double
197 MakeDate(int year, int mon, int mday, int hour, int min, int sec, int ms);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700198
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700199 FX_BOOL IsValidDate();
200
201 protected:
202 v8::Local<v8::Value> m_pDate;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700203 CJS_Runtime* m_pJSRuntime;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700204};
205
Tom Sepez39bfe122015-09-17 15:25:23 -0700206double JS_GetDateTime();
207int JS_GetYearFromTime(double dt);
208int JS_GetMonthFromTime(double dt);
209int JS_GetDayFromTime(double dt);
210int JS_GetHourFromTime(double dt);
211int JS_GetMinFromTime(double dt);
212int JS_GetSecFromTime(double dt);
tsepez018935c2016-04-15 13:15:12 -0700213double JS_DateParse(const CFX_WideString& str);
Tom Sepez39bfe122015-09-17 15:25:23 -0700214double JS_MakeDay(int nYear, int nMonth, int nDay);
215double JS_MakeTime(int nHour, int nMin, int nSec, int nMs);
216double JS_MakeDate(double day, double time);
217bool JS_PortIsNan(double d);
218double JS_LocalTime(double d);
219
Tom Sepezbd932572016-01-29 09:10:41 -0800220// Some JS methods have the bizarre convention that they may also be called
221// with a single argument which is an object containing the actual arguments
222// as its properties. The varying arguments to this method are the property
223// names as wchar_t string literals corresponding to each positional argument.
224// The result will always contain |nKeywords| value, with unspecified ones
225// being set to type VT_unknown.
226std::vector<CJS_Value> JS_ExpandKeywordParams(
227 CJS_Runtime* pRuntime,
228 const std::vector<CJS_Value>& originals,
229 size_t nKeywords,
230 ...);
231
Dan Sinclairf766ad22016-03-14 13:51:24 -0400232#endif // FPDFSDK_JAVASCRIPT_JS_VALUE_H_