blob: f6c1c360ca459eedcf45f91ce4ebe335e60fff8c [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
Dan Sinclair3ebd1212016-03-09 09:59:23 -050010#include <vector>
11
Lei Zhanga688a042015-11-09 13:57:49 -080012#include "core/include/fxcrt/fx_basic.h"
Lei Zhangbde53d22015-11-12 22:21:30 -080013#include "fpdfsdk/include/jsapi/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();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070049
Nico Weber9d8ec5a2015-08-04 13:00:21 -070050 void SetNull();
Tom Sepez39bfe122015-09-17 15:25:23 -070051 void Attach(v8::Local<v8::Value> pValue, Type t);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070052 void Attach(CJS_Value* pValue);
53 void Detach();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070054
Tom Sepez39bfe122015-09-17 15:25:23 -070055 Type GetType() const;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070056 int ToInt() const;
57 bool ToBool() const;
58 double ToDouble() const;
59 float ToFloat() const;
60 CJS_Object* ToCJSObject() const;
61 CFX_WideString ToCFXWideString() const;
62 CFX_ByteString ToCFXByteString() const;
63 v8::Local<v8::Object> ToV8Object() const;
64 v8::Local<v8::Array> ToV8Array() const;
65 v8::Local<v8::Value> ToV8Value() const;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070066
Tom Sepez4246b002016-01-20 11:48:29 -080067 // Replace the current |m_pValue| with a v8::Number if possible
68 // to make one from the current |m_pValue|, updating |m_eType|
69 // as appropriate to indicate the result.
70 void MaybeCoerceToNumber();
71
Nico Weber9d8ec5a2015-08-04 13:00:21 -070072 void operator=(int iValue);
73 void operator=(bool bValue);
Dan Sinclair738b08c2016-03-01 14:45:20 -050074 void operator=(double val);
75 void operator=(float val);
76 void operator=(CJS_Object* val);
77 void operator=(CJS_Document* val);
78 void operator=(v8::Local<v8::Object> val);
79 void operator=(CJS_Array& val);
80 void operator=(CJS_Date& val);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070081 void operator=(const FX_WCHAR* pWstr);
82 void operator=(const FX_CHAR* pStr);
83 void operator=(CJS_Value value);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070084
Nico Weber9d8ec5a2015-08-04 13:00:21 -070085 FX_BOOL IsArrayObject() const;
86 FX_BOOL IsDateObject() const;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070087 FX_BOOL ConvertToArray(CJS_Array&) const;
88 FX_BOOL ConvertToDate(CJS_Date&) const;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070089
Tom Sepez67fd5df2015-10-08 12:24:19 -070090 CJS_Runtime* GetJSRuntime() const { return m_pJSRuntime; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070091
Nico Weber9d8ec5a2015-08-04 13:00:21 -070092 protected:
Tom Sepez39bfe122015-09-17 15:25:23 -070093 Type m_eType;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070094 v8::Local<v8::Value> m_pValue;
Tom Sepez67fd5df2015-10-08 12:24:19 -070095 CJS_Runtime* m_pJSRuntime;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070096};
97
Nico Weber9d8ec5a2015-08-04 13:00:21 -070098class CJS_PropValue : public CJS_Value {
99 public:
100 CJS_PropValue(const CJS_Value&);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700101 CJS_PropValue(CJS_Runtime* pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700102 ~CJS_PropValue();
103
Tom Sepez67fd5df2015-10-08 12:24:19 -0700104 FX_BOOL IsSetting() const { return m_bIsSetting; }
105 FX_BOOL IsGetting() const { return !m_bIsSetting; }
106
Dan Sinclair738b08c2016-03-01 14:45:20 -0500107 void operator<<(int val);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700108 void operator>>(int&) const;
Dan Sinclair738b08c2016-03-01 14:45:20 -0500109 void operator<<(bool val);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700110 void operator>>(bool&) const;
Dan Sinclair738b08c2016-03-01 14:45:20 -0500111 void operator<<(double val);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700112 void operator>>(double&) const;
113 void operator<<(CJS_Object* pObj);
114 void operator>>(CJS_Object*& ppObj) const;
115 void operator<<(CJS_Document* pJsDoc);
116 void operator>>(CJS_Document*& ppJsDoc) const;
117 void operator<<(CFX_ByteString);
118 void operator>>(CFX_ByteString&) const;
119 void operator<<(CFX_WideString);
120 void operator>>(CFX_WideString&) const;
121 void operator<<(const FX_WCHAR* c_string);
Tom Sepez808a99e2015-09-10 12:28:37 -0700122 void operator<<(v8::Local<v8::Object>);
123 void operator>>(v8::Local<v8::Object>&) const;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700124 void operator>>(CJS_Array& array) const;
125 void operator<<(CJS_Array& array);
126 void operator<<(CJS_Date& date);
127 void operator>>(CJS_Date& date) const;
128 operator v8::Local<v8::Value>() const;
129 void StartSetting();
130 void StartGetting();
131
132 private:
133 FX_BOOL m_bIsSetting;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700134};
135
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700136class CJS_Array {
137 public:
Tom Sepez67fd5df2015-10-08 12:24:19 -0700138 CJS_Array(CJS_Runtime* pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700139 virtual ~CJS_Array();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700140
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700141 void Attach(v8::Local<v8::Array> pArray);
142 void GetElement(unsigned index, CJS_Value& value);
143 void SetElement(unsigned index, CJS_Value value);
144 int GetLength();
145 FX_BOOL IsAttached();
146 operator v8::Local<v8::Array>();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700147
Tom Sepez67fd5df2015-10-08 12:24:19 -0700148 CJS_Runtime* GetJSRuntime() const { return m_pJSRuntime; }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700149
150 private:
151 v8::Local<v8::Array> m_pArray;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700152 CJS_Runtime* m_pJSRuntime;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700153};
154
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700155class CJS_Date {
156 friend class CJS_Value;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700157
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700158 public:
Tom Sepez67fd5df2015-10-08 12:24:19 -0700159 CJS_Date(CJS_Runtime* pRuntime);
160 CJS_Date(CJS_Runtime* pRuntime, double dMsec_time);
161 CJS_Date(CJS_Runtime* pRuntime,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700162 int year,
163 int mon,
164 int day,
165 int hour,
166 int min,
167 int sec);
168 virtual ~CJS_Date();
169 void Attach(v8::Local<v8::Value> pDate);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700170
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700171 int GetYear();
172 void SetYear(int iYear);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700173
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700174 int GetMonth();
175 void SetMonth(int iMonth);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700176
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700177 int GetDay();
178 void SetDay(int iDay);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700179
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700180 int GetHours();
181 void SetHours(int iHours);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700182
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700183 int GetMinutes();
184 void SetMinutes(int minutes);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700185
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700186 int GetSeconds();
187 void SetSeconds(int seconds);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700188
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700189 operator v8::Local<v8::Value>();
190 operator double() const;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700191
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700192 CFX_WideString ToString() const;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700193
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700194 static double
195 MakeDate(int year, int mon, int mday, int hour, int min, int sec, int ms);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700196
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700197 FX_BOOL IsValidDate();
198
199 protected:
200 v8::Local<v8::Value> m_pDate;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700201 CJS_Runtime* m_pJSRuntime;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700202};
203
Tom Sepez39bfe122015-09-17 15:25:23 -0700204double JS_GetDateTime();
205int JS_GetYearFromTime(double dt);
206int JS_GetMonthFromTime(double dt);
207int JS_GetDayFromTime(double dt);
208int JS_GetHourFromTime(double dt);
209int JS_GetMinFromTime(double dt);
210int JS_GetSecFromTime(double dt);
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500211double JS_DateParse(const wchar_t* str);
Tom Sepez39bfe122015-09-17 15:25:23 -0700212double JS_MakeDay(int nYear, int nMonth, int nDay);
213double JS_MakeTime(int nHour, int nMin, int nSec, int nMs);
214double JS_MakeDate(double day, double time);
215bool JS_PortIsNan(double d);
216double JS_LocalTime(double d);
217
Tom Sepezbd932572016-01-29 09:10:41 -0800218// Some JS methods have the bizarre convention that they may also be called
219// with a single argument which is an object containing the actual arguments
220// as its properties. The varying arguments to this method are the property
221// names as wchar_t string literals corresponding to each positional argument.
222// The result will always contain |nKeywords| value, with unspecified ones
223// being set to type VT_unknown.
224std::vector<CJS_Value> JS_ExpandKeywordParams(
225 CJS_Runtime* pRuntime,
226 const std::vector<CJS_Value>& originals,
227 size_t nKeywords,
228 ...);
229
Tom Sepez37458412015-10-06 11:33:46 -0700230#endif // FPDFSDK_SRC_JAVASCRIPT_JS_VALUE_H_