blob: 6c6a03d265ec450805bc8291aba9c8df9626d211 [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
Tom Sepez37458412015-10-06 11:33:46 -07007#ifndef FPDFSDK_SRC_JAVASCRIPT_JS_VALUE_H_
8#define FPDFSDK_SRC_JAVASCRIPT_JS_VALUE_H_
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07009
Lei Zhanga688a042015-11-09 13:57:49 -080010#include "core/include/fxcrt/fx_basic.h"
Lei Zhangbde53d22015-11-12 22:21:30 -080011#include "fpdfsdk/include/jsapi/fxjs_v8.h"
Tom Sepez9a3f8122015-04-07 15:35:48 -070012
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070013class CJS_Array;
14class CJS_Date;
Tom Sepezf79a69c2014-10-30 13:23:42 -070015class CJS_Document;
16class CJS_Object;
Tom Sepez67fd5df2015-10-08 12:24:19 -070017class CJS_Runtime;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070018
Nico Weber9d8ec5a2015-08-04 13:00:21 -070019class CJS_Value {
20 public:
Tom Sepez39bfe122015-09-17 15:25:23 -070021 enum Type {
22 VT_unknown,
23 VT_string,
24 VT_number,
25 VT_boolean,
26 VT_date,
27 VT_object,
28 VT_fxobject,
29 VT_null,
30 VT_undefined
31 };
32
Tom Sepez67fd5df2015-10-08 12:24:19 -070033 CJS_Value(CJS_Runtime* pRuntime);
34 CJS_Value(CJS_Runtime* pRuntime, v8::Local<v8::Value> pValue, Type t);
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);
39 CJS_Value(CJS_Runtime* pRuntime, v8::Local<v8::Object>);
40 CJS_Value(CJS_Runtime* pRuntime, CJS_Object*);
41 CJS_Value(CJS_Runtime* pRuntime, CJS_Document*);
42 CJS_Value(CJS_Runtime* pRuntime, const FX_CHAR* pStr);
43 CJS_Value(CJS_Runtime* pRuntime, const FX_WCHAR* pWstr);
44 CJS_Value(CJS_Runtime* pRuntime, CJS_Array& array);
Tom Sepezf79a69c2014-10-30 13:23:42 -070045
Nico Weber9d8ec5a2015-08-04 13:00:21 -070046 ~CJS_Value();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070047
Nico Weber9d8ec5a2015-08-04 13:00:21 -070048 void SetNull();
Tom Sepez39bfe122015-09-17 15:25:23 -070049 void Attach(v8::Local<v8::Value> pValue, Type t);
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
Tom Sepez39bfe122015-09-17 15:25:23 -070053 Type GetType() const;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070054 int ToInt() const;
55 bool ToBool() const;
56 double ToDouble() const;
57 float ToFloat() const;
58 CJS_Object* ToCJSObject() const;
59 CFX_WideString ToCFXWideString() const;
60 CFX_ByteString ToCFXByteString() const;
61 v8::Local<v8::Object> ToV8Object() const;
62 v8::Local<v8::Array> ToV8Array() const;
63 v8::Local<v8::Value> ToV8Value() const;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070064
Tom Sepez4246b002016-01-20 11:48:29 -080065 // Replace the current |m_pValue| with a v8::Number if possible
66 // to make one from the current |m_pValue|, updating |m_eType|
67 // as appropriate to indicate the result.
68 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);
75 void operator=(CJS_Document* val);
76 void operator=(v8::Local<v8::Object> val);
77 void operator=(CJS_Array& val);
78 void operator=(CJS_Date& val);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070079 void operator=(const FX_WCHAR* pWstr);
80 void operator=(const FX_CHAR* pStr);
81 void operator=(CJS_Value value);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070082
Nico Weber9d8ec5a2015-08-04 13:00:21 -070083 FX_BOOL IsArrayObject() const;
84 FX_BOOL IsDateObject() const;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070085 FX_BOOL ConvertToArray(CJS_Array&) const;
86 FX_BOOL ConvertToDate(CJS_Date&) const;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070087
Tom Sepez67fd5df2015-10-08 12:24:19 -070088 CJS_Runtime* GetJSRuntime() const { return m_pJSRuntime; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070089
Nico Weber9d8ec5a2015-08-04 13:00:21 -070090 protected:
Tom Sepez39bfe122015-09-17 15:25:23 -070091 Type m_eType;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070092 v8::Local<v8::Value> m_pValue;
Tom Sepez67fd5df2015-10-08 12:24:19 -070093 CJS_Runtime* m_pJSRuntime;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070094};
95
Nico Weber9d8ec5a2015-08-04 13:00:21 -070096class CJS_PropValue : public CJS_Value {
97 public:
98 CJS_PropValue(const CJS_Value&);
Tom Sepez67fd5df2015-10-08 12:24:19 -070099 CJS_PropValue(CJS_Runtime* pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700100 ~CJS_PropValue();
101
Tom Sepez67fd5df2015-10-08 12:24:19 -0700102 FX_BOOL IsSetting() const { return m_bIsSetting; }
103 FX_BOOL IsGetting() const { return !m_bIsSetting; }
104
Dan Sinclair738b08c2016-03-01 14:45:20 -0500105 void operator<<(int val);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700106 void operator>>(int&) const;
Dan Sinclair738b08c2016-03-01 14:45:20 -0500107 void operator<<(bool val);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700108 void operator>>(bool&) const;
Dan Sinclair738b08c2016-03-01 14:45:20 -0500109 void operator<<(double val);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700110 void operator>>(double&) const;
111 void operator<<(CJS_Object* pObj);
112 void operator>>(CJS_Object*& ppObj) const;
113 void operator<<(CJS_Document* pJsDoc);
114 void operator>>(CJS_Document*& ppJsDoc) const;
115 void operator<<(CFX_ByteString);
116 void operator>>(CFX_ByteString&) const;
117 void operator<<(CFX_WideString);
118 void operator>>(CFX_WideString&) const;
119 void operator<<(const FX_WCHAR* c_string);
Tom Sepez808a99e2015-09-10 12:28:37 -0700120 void operator<<(v8::Local<v8::Object>);
121 void operator>>(v8::Local<v8::Object>&) const;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700122 void operator>>(CJS_Array& array) const;
123 void operator<<(CJS_Array& array);
124 void operator<<(CJS_Date& date);
125 void operator>>(CJS_Date& date) const;
126 operator v8::Local<v8::Value>() const;
127 void StartSetting();
128 void StartGetting();
129
130 private:
131 FX_BOOL m_bIsSetting;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700132};
133
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700134class CJS_Array {
135 public:
Tom Sepez67fd5df2015-10-08 12:24:19 -0700136 CJS_Array(CJS_Runtime* pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700137 virtual ~CJS_Array();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700138
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700139 void Attach(v8::Local<v8::Array> pArray);
140 void GetElement(unsigned index, CJS_Value& value);
141 void SetElement(unsigned index, CJS_Value value);
142 int GetLength();
143 FX_BOOL IsAttached();
144 operator v8::Local<v8::Array>();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700145
Tom Sepez67fd5df2015-10-08 12:24:19 -0700146 CJS_Runtime* GetJSRuntime() const { return m_pJSRuntime; }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700147
148 private:
149 v8::Local<v8::Array> m_pArray;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700150 CJS_Runtime* m_pJSRuntime;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700151};
152
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700153class CJS_Date {
154 friend class CJS_Value;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700155
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700156 public:
Tom Sepez67fd5df2015-10-08 12:24:19 -0700157 CJS_Date(CJS_Runtime* pRuntime);
158 CJS_Date(CJS_Runtime* pRuntime, double dMsec_time);
159 CJS_Date(CJS_Runtime* pRuntime,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700160 int year,
161 int mon,
162 int day,
163 int hour,
164 int min,
165 int sec);
166 virtual ~CJS_Date();
167 void Attach(v8::Local<v8::Value> pDate);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700168
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700169 int GetYear();
170 void SetYear(int iYear);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700171
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700172 int GetMonth();
173 void SetMonth(int iMonth);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700174
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700175 int GetDay();
176 void SetDay(int iDay);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700177
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700178 int GetHours();
179 void SetHours(int iHours);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700180
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700181 int GetMinutes();
182 void SetMinutes(int minutes);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700183
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700184 int GetSeconds();
185 void SetSeconds(int seconds);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700186
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700187 operator v8::Local<v8::Value>();
188 operator double() const;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700189
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700190 CFX_WideString ToString() const;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700191
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700192 static double
193 MakeDate(int year, int mon, int mday, int hour, int min, int sec, int ms);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700194
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700195 FX_BOOL IsValidDate();
196
197 protected:
198 v8::Local<v8::Value> m_pDate;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700199 CJS_Runtime* m_pJSRuntime;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700200};
201
Tom Sepez39bfe122015-09-17 15:25:23 -0700202double JS_GetDateTime();
203int JS_GetYearFromTime(double dt);
204int JS_GetMonthFromTime(double dt);
205int JS_GetDayFromTime(double dt);
206int JS_GetHourFromTime(double dt);
207int JS_GetMinFromTime(double dt);
208int JS_GetSecFromTime(double dt);
209double JS_DateParse(const wchar_t* string);
210double JS_MakeDay(int nYear, int nMonth, int nDay);
211double JS_MakeTime(int nHour, int nMin, int nSec, int nMs);
212double JS_MakeDate(double day, double time);
213bool JS_PortIsNan(double d);
214double JS_LocalTime(double d);
215
Tom Sepezbd932572016-01-29 09:10:41 -0800216// Some JS methods have the bizarre convention that they may also be called
217// with a single argument which is an object containing the actual arguments
218// as its properties. The varying arguments to this method are the property
219// names as wchar_t string literals corresponding to each positional argument.
220// The result will always contain |nKeywords| value, with unspecified ones
221// being set to type VT_unknown.
222std::vector<CJS_Value> JS_ExpandKeywordParams(
223 CJS_Runtime* pRuntime,
224 const std::vector<CJS_Value>& originals,
225 size_t nKeywords,
226 ...);
227
Tom Sepez37458412015-10-06 11:33:46 -0700228#endif // FPDFSDK_SRC_JAVASCRIPT_JS_VALUE_H_