blob: 75c40a60a17303dd8c2133d55d615e2c941a416c [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
tsepezd7807352016-07-25 07:26:47 -070034 explicit 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);
tsepeze5aff742016-08-08 09:49:42 -070043 CJS_Value(CJS_Runtime* pRuntime, const CJS_Array& array);
tsepezd7807352016-07-25 07:26:47 -070044 CJS_Value(const CJS_Value& other);
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();
tsepez40faa792016-07-15 17:58:02 -070049 void Attach(v8::Local<v8::Value> pValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070050 void Detach();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070051
tsepez40faa792016-07-15 17:58:02 -070052 static Type GetValueType(v8::Local<v8::Value> value);
53 Type GetType() const { return GetValueType(m_pValue); }
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
tsepez40faa792016-07-15 17:58:02 -070066 // to make one from the current |m_pValue|.
Tom Sepez4246b002016-01-20 11:48:29 -080067 void MaybeCoerceToNumber();
68
Nico Weber9d8ec5a2015-08-04 13:00:21 -070069 void operator=(int iValue);
70 void operator=(bool bValue);
Dan Sinclair738b08c2016-03-01 14:45:20 -050071 void operator=(double val);
72 void operator=(float val);
73 void operator=(CJS_Object* val);
Dan Sinclair738b08c2016-03-01 14:45:20 -050074 void operator=(v8::Local<v8::Object> val);
tsepezfbf52c22016-07-25 11:17:07 -070075 void operator=(const CJS_Array& val);
76 void operator=(const CJS_Date& val);
77 void operator=(const CJS_Value& value);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070078 void operator=(const FX_CHAR* pStr);
tsepezfbf52c22016-07-25 11:17:07 -070079 void operator=(const FX_WCHAR* pWstr);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070080
Nico Weber9d8ec5a2015-08-04 13:00:21 -070081 FX_BOOL IsArrayObject() const;
82 FX_BOOL IsDateObject() const;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070083 FX_BOOL ConvertToArray(CJS_Array&) const;
84 FX_BOOL ConvertToDate(CJS_Date&) const;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070085
Tom Sepez67fd5df2015-10-08 12:24:19 -070086 CJS_Runtime* GetJSRuntime() const { return m_pJSRuntime; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070087
Nico Weber9d8ec5a2015-08-04 13:00:21 -070088 protected:
89 v8::Local<v8::Value> m_pValue;
tsepezfbf52c22016-07-25 11:17:07 -070090 CJS_Runtime* const m_pJSRuntime;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070091};
92
Nico Weber9d8ec5a2015-08-04 13:00:21 -070093class CJS_PropValue : public CJS_Value {
94 public:
tsepezfbf52c22016-07-25 11:17:07 -070095 explicit CJS_PropValue(CJS_Runtime* pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070096 CJS_PropValue(const CJS_Value&);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070097 ~CJS_PropValue();
98
tsepezfbf52c22016-07-25 11:17:07 -070099 void StartSetting() { m_bIsSetting = true; }
100 void StartGetting() { m_bIsSetting = false; }
101 bool IsSetting() const { return m_bIsSetting; }
102 bool IsGetting() const { return !m_bIsSetting; }
Tom Sepez67fd5df2015-10-08 12:24:19 -0700103
Dan Sinclair738b08c2016-03-01 14:45:20 -0500104 void operator<<(int val);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700105 void operator>>(int&) const;
Dan Sinclair738b08c2016-03-01 14:45:20 -0500106 void operator<<(bool val);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700107 void operator>>(bool&) const;
Dan Sinclair738b08c2016-03-01 14:45:20 -0500108 void operator<<(double val);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700109 void operator>>(double&) const;
110 void operator<<(CJS_Object* pObj);
111 void operator>>(CJS_Object*& ppObj) const;
112 void operator<<(CJS_Document* pJsDoc);
113 void operator>>(CJS_Document*& ppJsDoc) const;
114 void operator<<(CFX_ByteString);
115 void operator>>(CFX_ByteString&) const;
116 void operator<<(CFX_WideString);
117 void operator>>(CFX_WideString&) const;
118 void operator<<(const FX_WCHAR* c_string);
Tom Sepez808a99e2015-09-10 12:28:37 -0700119 void operator<<(v8::Local<v8::Object>);
120 void operator>>(v8::Local<v8::Object>&) const;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700121 void operator>>(CJS_Array& array) const;
122 void operator<<(CJS_Array& array);
123 void operator<<(CJS_Date& date);
124 void operator>>(CJS_Date& date) const;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700125
126 private:
tsepezfbf52c22016-07-25 11:17:07 -0700127 bool m_bIsSetting;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700128};
129
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700130class CJS_Array {
131 public:
tsepeze5aff742016-08-08 09:49:42 -0700132 CJS_Array();
weili625ad662016-06-15 11:21:33 -0700133 CJS_Array(const CJS_Array& other);
tsepezfbf52c22016-07-25 11:17:07 -0700134 virtual ~CJS_Array();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700135
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700136 void Attach(v8::Local<v8::Array> pArray);
tsepeze5aff742016-08-08 09:49:42 -0700137 void GetElement(v8::Isolate* pIsolate,
138 unsigned index,
139 CJS_Value& value) const;
140 void SetElement(v8::Isolate* pIsolate,
141 unsigned index,
142 const CJS_Value& value);
tsepezfbf52c22016-07-25 11:17:07 -0700143 int GetLength() const;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700144
tsepeze5aff742016-08-08 09:49:42 -0700145 v8::Local<v8::Array> ToV8Array(v8::Isolate* pIsolate) const;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700146
147 private:
tsepezfbf52c22016-07-25 11:17:07 -0700148 mutable v8::Local<v8::Array> m_pArray;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700149};
150
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700151class CJS_Date {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700152 public:
tsepezfbf52c22016-07-25 11:17:07 -0700153 explicit CJS_Date(CJS_Runtime* pRuntime);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700154 CJS_Date(CJS_Runtime* pRuntime, double dMsec_time);
155 CJS_Date(CJS_Runtime* pRuntime,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700156 int year,
157 int mon,
158 int day,
159 int hour,
160 int min,
161 int sec);
162 virtual ~CJS_Date();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700163
tsepez135b9982016-08-05 09:32:50 -0700164 void Attach(v8::Local<v8::Date> pDate);
tsepezfbf52c22016-07-25 11:17:07 -0700165 bool IsValidDate() const;
166
167 int GetYear() const;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700168 void SetYear(int iYear);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700169
tsepezfbf52c22016-07-25 11:17:07 -0700170 int GetMonth() const;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700171 void SetMonth(int iMonth);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700172
tsepezfbf52c22016-07-25 11:17:07 -0700173 int GetDay() const;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700174 void SetDay(int iDay);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700175
tsepezfbf52c22016-07-25 11:17:07 -0700176 int GetHours() const;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700177 void SetHours(int iHours);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700178
tsepezfbf52c22016-07-25 11:17:07 -0700179 int GetMinutes() const;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700180 void SetMinutes(int minutes);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700181
tsepezfbf52c22016-07-25 11:17:07 -0700182 int GetSeconds() const;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700183 void SetSeconds(int seconds);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700184
tsepezfbf52c22016-07-25 11:17:07 -0700185 CJS_Runtime* GetJSRuntime() const { return m_pJSRuntime; }
186 v8::Local<v8::Value> ToV8Value() const { return m_pDate; }
187 double ToDouble() const;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700188 CFX_WideString ToString() const;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700189
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700190 protected:
tsepez135b9982016-08-05 09:32:50 -0700191 v8::Local<v8::Date> m_pDate;
tsepezfbf52c22016-07-25 11:17:07 -0700192 CJS_Runtime* const m_pJSRuntime;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700193};
194
Tom Sepez39bfe122015-09-17 15:25:23 -0700195double JS_GetDateTime();
196int JS_GetYearFromTime(double dt);
197int JS_GetMonthFromTime(double dt);
198int JS_GetDayFromTime(double dt);
199int JS_GetHourFromTime(double dt);
200int JS_GetMinFromTime(double dt);
201int JS_GetSecFromTime(double dt);
tsepez018935c2016-04-15 13:15:12 -0700202double JS_DateParse(const CFX_WideString& str);
Tom Sepez39bfe122015-09-17 15:25:23 -0700203double JS_MakeDay(int nYear, int nMonth, int nDay);
204double JS_MakeTime(int nHour, int nMin, int nSec, int nMs);
205double JS_MakeDate(double day, double time);
206bool JS_PortIsNan(double d);
207double JS_LocalTime(double d);
208
Tom Sepezbd932572016-01-29 09:10:41 -0800209// Some JS methods have the bizarre convention that they may also be called
210// with a single argument which is an object containing the actual arguments
211// as its properties. The varying arguments to this method are the property
212// names as wchar_t string literals corresponding to each positional argument.
213// The result will always contain |nKeywords| value, with unspecified ones
214// being set to type VT_unknown.
215std::vector<CJS_Value> JS_ExpandKeywordParams(
216 CJS_Runtime* pRuntime,
217 const std::vector<CJS_Value>& originals,
218 size_t nKeywords,
219 ...);
220
Dan Sinclairf766ad22016-03-14 13:51:24 -0400221#endif // FPDFSDK_JAVASCRIPT_JS_VALUE_H_