blob: 47e8cce98ade5ba0e1afdf841dbf36b6616ddc1d [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
dsinclair43554682016-09-29 17:29:48 -070012#include "fxjs/fxjs_v8.h"
Tom Sepez9a3f8122015-04-07 15:35:48 -070013
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070014class CJS_Array;
15class CJS_Date;
Tom Sepezf79a69c2014-10-30 13:23:42 -070016class CJS_Document;
17class CJS_Object;
Tom Sepez67fd5df2015-10-08 12:24:19 -070018class CJS_Runtime;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070019
Nico Weber9d8ec5a2015-08-04 13:00:21 -070020class CJS_Value {
21 public:
Tom Sepez39bfe122015-09-17 15:25:23 -070022 enum Type {
23 VT_unknown,
24 VT_string,
25 VT_number,
26 VT_boolean,
27 VT_date,
28 VT_object,
Tom Sepez39bfe122015-09-17 15:25:23 -070029 VT_null,
30 VT_undefined
31 };
32
tsepezd7807352016-07-25 07:26:47 -070033 explicit CJS_Value(CJS_Runtime* pRuntime);
tsepez40faa792016-07-15 17:58:02 -070034 CJS_Value(CJS_Runtime* pRuntime, v8::Local<v8::Value> pValue);
Tom Sepez67fd5df2015-10-08 12:24:19 -070035 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);
tsepez40faa792016-07-15 17:58:02 -070039 CJS_Value(CJS_Runtime* pRuntime, CJS_Object* pObj);
Dan Sinclair812e96c2017-03-13 16:43:37 -040040 CJS_Value(CJS_Runtime* pRuntime, const char* pStr);
41 CJS_Value(CJS_Runtime* pRuntime, const wchar_t* pWstr);
tsepeze5aff742016-08-08 09:49:42 -070042 CJS_Value(CJS_Runtime* pRuntime, const CJS_Array& array);
tsepezf3c88322016-08-09 07:30:38 -070043 CJS_Value(CJS_Runtime* pRuntime, const CJS_Date& date);
tsepezf3dc8c62016-08-10 06:29:29 -070044 CJS_Value(CJS_Runtime* pRuntime, const CJS_Object* object);
tsepezd7807352016-07-25 07:26:47 -070045 CJS_Value(const CJS_Value& other);
Tom Sepezf79a69c2014-10-30 13:23:42 -070046
Nico Weber9d8ec5a2015-08-04 13:00:21 -070047 ~CJS_Value();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070048
tsepezf3dc8c62016-08-10 06:29:29 -070049 void SetNull(CJS_Runtime* pRuntime);
50 void SetValue(const CJS_Value& other);
tsepez40faa792016-07-15 17:58:02 -070051 void Attach(v8::Local<v8::Value> pValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070052 void Detach();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070053
tsepez40faa792016-07-15 17:58:02 -070054 static Type GetValueType(v8::Local<v8::Value> value);
55 Type GetType() const { return GetValueType(m_pValue); }
tsepezf3dc8c62016-08-10 06:29:29 -070056
tsepezb4694242016-08-15 16:44:55 -070057 int ToInt(CJS_Runtime* pRuntime) const;
58 bool ToBool(CJS_Runtime* pRuntime) const;
59 double ToDouble(CJS_Runtime* pRuntime) const;
60 float ToFloat(CJS_Runtime* pRuntime) const;
61 CJS_Object* ToCJSObject(CJS_Runtime* pRuntime) const;
Ryan Harrison275e2602017-09-18 14:23:18 -040062 WideString ToCFXWideString(CJS_Runtime* pRuntime) const;
63 ByteString ToCFXByteString(CJS_Runtime* pRuntime) const;
tsepezb4694242016-08-15 16:44:55 -070064 v8::Local<v8::Object> ToV8Object(CJS_Runtime* pRuntime) const;
65 v8::Local<v8::Array> ToV8Array(CJS_Runtime* pRuntime) const;
66 v8::Local<v8::Value> ToV8Value(CJS_Runtime* pRuntime) 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
tsepez40faa792016-07-15 17:58:02 -070069 // to make one from the current |m_pValue|.
tsepezb4694242016-08-15 16:44:55 -070070 void MaybeCoerceToNumber(CJS_Runtime* pRuntime);
Tom Sepez4246b002016-01-20 11:48:29 -080071
tsepezf3dc8c62016-08-10 06:29:29 -070072 bool IsArrayObject() const;
73 bool IsDateObject() const;
tsepezb4694242016-08-15 16:44:55 -070074 bool ConvertToArray(CJS_Runtime* pRuntime, CJS_Array&) const;
75 bool ConvertToDate(CJS_Runtime* pRuntime, CJS_Date&) const;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070076
Nico Weber9d8ec5a2015-08-04 13:00:21 -070077 protected:
78 v8::Local<v8::Value> m_pValue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070079};
80
tsepezf3dc8c62016-08-10 06:29:29 -070081class CJS_PropValue {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070082 public:
tsepezfbf52c22016-07-25 11:17:07 -070083 explicit CJS_PropValue(CJS_Runtime* pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -070084 CJS_PropValue(CJS_Runtime* pRuntime, const CJS_Value&);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070085 ~CJS_PropValue();
86
tsepezfbf52c22016-07-25 11:17:07 -070087 void StartSetting() { m_bIsSetting = true; }
88 void StartGetting() { m_bIsSetting = false; }
89 bool IsSetting() const { return m_bIsSetting; }
90 bool IsGetting() const { return !m_bIsSetting; }
Tom Sepez797ca5c2017-05-25 12:03:18 -070091 CJS_Runtime* GetJSRuntime() const { return m_pJSRuntime.Get(); }
tsepezf3dc8c62016-08-10 06:29:29 -070092 CJS_Value* GetJSValue() { return &m_Value; }
dan sinclaircbe23db2017-10-19 14:29:33 -040093 const CJS_Value* GetJSValue() const { return &m_Value; }
Tom Sepez67fd5df2015-10-08 12:24:19 -070094
tsepez19249712017-01-12 11:15:04 -080095 // These calls may re-enter JS (and hence invalidate objects).
dan sinclaircbe23db2017-10-19 14:29:33 -040096 void Set(int val);
97 int ToInt() const;
98
99 void Set(bool val);
100 bool ToBool() const;
101
102 void Set(double val);
103 double ToDouble() const;
104
105 void Set(CJS_Object* pObj);
106 CJS_Object* ToObject() const;
107
108 void Set(CJS_Document* pJsDoc);
109 CJS_Document* ToDocument() const;
110
111 void Set(const ByteString&);
112 ByteString ToByteString() const;
113
114 void Set(const WideString&);
115 void Set(const wchar_t* c_string);
116 WideString ToWideString() const;
117
118 void Set(v8::Local<v8::Object>);
119 v8::Local<v8::Object> ToV8Object() const;
120
121 void Set(const CJS_Array& array);
122 CJS_Array ToArray() const;
123
124 void Set(const CJS_Date& date);
125 CJS_Date ToDate() const;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700126
127 private:
tsepezfbf52c22016-07-25 11:17:07 -0700128 bool m_bIsSetting;
tsepezf3dc8c62016-08-10 06:29:29 -0700129 CJS_Value m_Value;
Dan Sinclairaee0db02017-09-21 16:53:58 -0400130 UnownedPtr<CJS_Runtime> const m_pJSRuntime;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700131};
132
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700133class CJS_Array {
134 public:
tsepeze5aff742016-08-08 09:49:42 -0700135 CJS_Array();
weili625ad662016-06-15 11:21:33 -0700136 CJS_Array(const CJS_Array& other);
tsepezfbf52c22016-07-25 11:17:07 -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);
tsepez19249712017-01-12 11:15:04 -0800140 int GetLength(CJS_Runtime* pRuntime) const;
141
142 // These two calls may re-enter JS (and hence invalidate objects).
tsepezb4694242016-08-15 16:44:55 -0700143 void GetElement(CJS_Runtime* pRuntime,
tsepeze5aff742016-08-08 09:49:42 -0700144 unsigned index,
145 CJS_Value& value) const;
tsepezb4694242016-08-15 16:44:55 -0700146 void SetElement(CJS_Runtime* pRuntime,
tsepeze5aff742016-08-08 09:49:42 -0700147 unsigned index,
148 const CJS_Value& value);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700149
tsepezb4694242016-08-15 16:44:55 -0700150 v8::Local<v8::Array> ToV8Array(CJS_Runtime* pRuntime) const;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700151
152 private:
tsepezfbf52c22016-07-25 11:17:07 -0700153 mutable v8::Local<v8::Array> m_pArray;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700154};
155
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700156class CJS_Date {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700157 public:
tsepezf3c88322016-08-09 07:30:38 -0700158 CJS_Date();
tsepezb4694242016-08-15 16:44:55 -0700159 CJS_Date(CJS_Runtime* pRuntime, double dMsec_time);
160 CJS_Date(CJS_Runtime* pRuntime,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700161 int year,
162 int mon,
163 int day,
164 int hour,
165 int min,
166 int sec);
dan sinclaircbe23db2017-10-19 14:29:33 -0400167 CJS_Date(const CJS_Date&);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700168 virtual ~CJS_Date();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700169
tsepez135b9982016-08-05 09:32:50 -0700170 void Attach(v8::Local<v8::Date> pDate);
tsepezb4694242016-08-15 16:44:55 -0700171 bool IsValidDate(CJS_Runtime* pRuntime) const;
tsepezfbf52c22016-07-25 11:17:07 -0700172
tsepezb4694242016-08-15 16:44:55 -0700173 int GetYear(CJS_Runtime* pRuntime) const;
174 void SetYear(CJS_Runtime* pRuntime, int iYear);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700175
tsepezb4694242016-08-15 16:44:55 -0700176 int GetMonth(CJS_Runtime* pRuntime) const;
177 void SetMonth(CJS_Runtime* pRuntime, int iMonth);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700178
tsepezb4694242016-08-15 16:44:55 -0700179 int GetDay(CJS_Runtime* pRuntime) const;
180 void SetDay(CJS_Runtime* pRuntime, int iDay);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700181
tsepezb4694242016-08-15 16:44:55 -0700182 int GetHours(CJS_Runtime* pRuntime) const;
183 void SetHours(CJS_Runtime* pRuntime, int iHours);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700184
tsepezb4694242016-08-15 16:44:55 -0700185 int GetMinutes(CJS_Runtime* pRuntime) const;
186 void SetMinutes(CJS_Runtime* pRuntime, int minutes);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700187
tsepezb4694242016-08-15 16:44:55 -0700188 int GetSeconds(CJS_Runtime* pRuntime) const;
189 void SetSeconds(CJS_Runtime* pRuntime, int seconds);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700190
tsepezb4694242016-08-15 16:44:55 -0700191 v8::Local<v8::Date> ToV8Date(CJS_Runtime* pRuntime) const;
192 double ToDouble(CJS_Runtime* pRuntime) const;
Ryan Harrison275e2602017-09-18 14:23:18 -0400193 WideString ToString(CJS_Runtime* pRuntime) const;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700194
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700195 protected:
tsepez135b9982016-08-05 09:32:50 -0700196 v8::Local<v8::Date> m_pDate;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700197};
198
Tom Sepez39bfe122015-09-17 15:25:23 -0700199double JS_GetDateTime();
200int JS_GetYearFromTime(double dt);
201int JS_GetMonthFromTime(double dt);
202int JS_GetDayFromTime(double dt);
203int JS_GetHourFromTime(double dt);
204int JS_GetMinFromTime(double dt);
205int JS_GetSecFromTime(double dt);
Ryan Harrison275e2602017-09-18 14:23:18 -0400206double JS_DateParse(const WideString& str);
Tom Sepez39bfe122015-09-17 15:25:23 -0700207double JS_MakeDay(int nYear, int nMonth, int nDay);
208double JS_MakeTime(int nHour, int nMin, int nSec, int nMs);
209double JS_MakeDate(double day, double time);
Tom Sepez39bfe122015-09-17 15:25:23 -0700210double JS_LocalTime(double d);
211
Tom Sepezbd932572016-01-29 09:10:41 -0800212// Some JS methods have the bizarre convention that they may also be called
213// with a single argument which is an object containing the actual arguments
214// as its properties. The varying arguments to this method are the property
215// names as wchar_t string literals corresponding to each positional argument.
216// The result will always contain |nKeywords| value, with unspecified ones
217// being set to type VT_unknown.
218std::vector<CJS_Value> JS_ExpandKeywordParams(
219 CJS_Runtime* pRuntime,
220 const std::vector<CJS_Value>& originals,
221 size_t nKeywords,
222 ...);
223
Dan Sinclairf766ad22016-03-14 13:51:24 -0400224#endif // FPDFSDK_JAVASCRIPT_JS_VALUE_H_