blob: 1a6d47fcea1b61c6b7f10e6915106912e273c2fb [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
dsinclaira52ab742016-09-29 13:59:29 -070012#include "core/fxcrt/fx_basic.h"
dsinclair43554682016-09-29 17:29:48 -070013#include "fxjs/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);
Dan Sinclair812e96c2017-03-13 16:43:37 -040041 CJS_Value(CJS_Runtime* pRuntime, const char* pStr);
42 CJS_Value(CJS_Runtime* pRuntime, const wchar_t* pWstr);
tsepeze5aff742016-08-08 09:49:42 -070043 CJS_Value(CJS_Runtime* pRuntime, const CJS_Array& array);
tsepezf3c88322016-08-09 07:30:38 -070044 CJS_Value(CJS_Runtime* pRuntime, const CJS_Date& date);
tsepezf3dc8c62016-08-10 06:29:29 -070045 CJS_Value(CJS_Runtime* pRuntime, const CJS_Object* object);
tsepezd7807352016-07-25 07:26:47 -070046 CJS_Value(const CJS_Value& other);
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
tsepezf3dc8c62016-08-10 06:29:29 -070050 void SetNull(CJS_Runtime* pRuntime);
51 void SetValue(const CJS_Value& other);
tsepez40faa792016-07-15 17:58:02 -070052 void Attach(v8::Local<v8::Value> pValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070053 void Detach();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070054
tsepez40faa792016-07-15 17:58:02 -070055 static Type GetValueType(v8::Local<v8::Value> value);
56 Type GetType() const { return GetValueType(m_pValue); }
tsepezf3dc8c62016-08-10 06:29:29 -070057
tsepezb4694242016-08-15 16:44:55 -070058 int ToInt(CJS_Runtime* pRuntime) const;
59 bool ToBool(CJS_Runtime* pRuntime) const;
60 double ToDouble(CJS_Runtime* pRuntime) const;
61 float ToFloat(CJS_Runtime* pRuntime) const;
62 CJS_Object* ToCJSObject(CJS_Runtime* pRuntime) const;
63 CFX_WideString ToCFXWideString(CJS_Runtime* pRuntime) const;
64 CFX_ByteString ToCFXByteString(CJS_Runtime* pRuntime) const;
65 v8::Local<v8::Object> ToV8Object(CJS_Runtime* pRuntime) const;
66 v8::Local<v8::Array> ToV8Array(CJS_Runtime* pRuntime) const;
67 v8::Local<v8::Value> ToV8Value(CJS_Runtime* pRuntime) const;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070068
Tom Sepez4246b002016-01-20 11:48:29 -080069 // Replace the current |m_pValue| with a v8::Number if possible
tsepez40faa792016-07-15 17:58:02 -070070 // to make one from the current |m_pValue|.
tsepezb4694242016-08-15 16:44:55 -070071 void MaybeCoerceToNumber(CJS_Runtime* pRuntime);
Tom Sepez4246b002016-01-20 11:48:29 -080072
tsepezf3dc8c62016-08-10 06:29:29 -070073 bool IsArrayObject() const;
74 bool IsDateObject() const;
tsepezb4694242016-08-15 16:44:55 -070075 bool ConvertToArray(CJS_Runtime* pRuntime, CJS_Array&) const;
76 bool ConvertToDate(CJS_Runtime* pRuntime, CJS_Date&) const;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070077
Nico Weber9d8ec5a2015-08-04 13:00:21 -070078 protected:
79 v8::Local<v8::Value> m_pValue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070080};
81
tsepezf3dc8c62016-08-10 06:29:29 -070082class CJS_PropValue {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070083 public:
tsepezfbf52c22016-07-25 11:17:07 -070084 explicit CJS_PropValue(CJS_Runtime* pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -070085 CJS_PropValue(CJS_Runtime* pRuntime, const CJS_Value&);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070086 ~CJS_PropValue();
87
tsepezfbf52c22016-07-25 11:17:07 -070088 void StartSetting() { m_bIsSetting = true; }
89 void StartGetting() { m_bIsSetting = false; }
90 bool IsSetting() const { return m_bIsSetting; }
91 bool IsGetting() const { return !m_bIsSetting; }
Tom Sepez797ca5c2017-05-25 12:03:18 -070092 CJS_Runtime* GetJSRuntime() const { return m_pJSRuntime.Get(); }
tsepezf3dc8c62016-08-10 06:29:29 -070093 CJS_Value* GetJSValue() { 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 Sinclair738b08c2016-03-01 14:45:20 -050096 void operator<<(int val);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070097 void operator>>(int&) const;
Dan Sinclair738b08c2016-03-01 14:45:20 -050098 void operator<<(bool val);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070099 void operator>>(bool&) const;
Dan Sinclair738b08c2016-03-01 14:45:20 -0500100 void operator<<(double val);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700101 void operator>>(double&) const;
102 void operator<<(CJS_Object* pObj);
103 void operator>>(CJS_Object*& ppObj) const;
104 void operator<<(CJS_Document* pJsDoc);
105 void operator>>(CJS_Document*& ppJsDoc) const;
106 void operator<<(CFX_ByteString);
107 void operator>>(CFX_ByteString&) const;
108 void operator<<(CFX_WideString);
109 void operator>>(CFX_WideString&) const;
Dan Sinclair812e96c2017-03-13 16:43:37 -0400110 void operator<<(const wchar_t* c_string);
Tom Sepez808a99e2015-09-10 12:28:37 -0700111 void operator<<(v8::Local<v8::Object>);
112 void operator>>(v8::Local<v8::Object>&) const;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700113 void operator>>(CJS_Array& array) const;
114 void operator<<(CJS_Array& array);
115 void operator<<(CJS_Date& date);
116 void operator>>(CJS_Date& date) const;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700117
118 private:
tsepezfbf52c22016-07-25 11:17:07 -0700119 bool m_bIsSetting;
tsepezf3dc8c62016-08-10 06:29:29 -0700120 CJS_Value m_Value;
Tom Sepez797ca5c2017-05-25 12:03:18 -0700121 CFX_UnownedPtr<CJS_Runtime> const m_pJSRuntime;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700122};
123
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700124class CJS_Array {
125 public:
tsepeze5aff742016-08-08 09:49:42 -0700126 CJS_Array();
weili625ad662016-06-15 11:21:33 -0700127 CJS_Array(const CJS_Array& other);
tsepezfbf52c22016-07-25 11:17:07 -0700128 virtual ~CJS_Array();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700129
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700130 void Attach(v8::Local<v8::Array> pArray);
tsepez19249712017-01-12 11:15:04 -0800131 int GetLength(CJS_Runtime* pRuntime) const;
132
133 // These two calls may re-enter JS (and hence invalidate objects).
tsepezb4694242016-08-15 16:44:55 -0700134 void GetElement(CJS_Runtime* pRuntime,
tsepeze5aff742016-08-08 09:49:42 -0700135 unsigned index,
136 CJS_Value& value) const;
tsepezb4694242016-08-15 16:44:55 -0700137 void SetElement(CJS_Runtime* pRuntime,
tsepeze5aff742016-08-08 09:49:42 -0700138 unsigned index,
139 const CJS_Value& value);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700140
tsepezb4694242016-08-15 16:44:55 -0700141 v8::Local<v8::Array> ToV8Array(CJS_Runtime* pRuntime) const;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700142
143 private:
tsepezfbf52c22016-07-25 11:17:07 -0700144 mutable v8::Local<v8::Array> m_pArray;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700145};
146
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700147class CJS_Date {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700148 public:
tsepezf3c88322016-08-09 07:30:38 -0700149 CJS_Date();
tsepezb4694242016-08-15 16:44:55 -0700150 CJS_Date(CJS_Runtime* pRuntime, double dMsec_time);
151 CJS_Date(CJS_Runtime* pRuntime,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700152 int year,
153 int mon,
154 int day,
155 int hour,
156 int min,
157 int sec);
158 virtual ~CJS_Date();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700159
tsepez135b9982016-08-05 09:32:50 -0700160 void Attach(v8::Local<v8::Date> pDate);
tsepezb4694242016-08-15 16:44:55 -0700161 bool IsValidDate(CJS_Runtime* pRuntime) const;
tsepezfbf52c22016-07-25 11:17:07 -0700162
tsepezb4694242016-08-15 16:44:55 -0700163 int GetYear(CJS_Runtime* pRuntime) const;
164 void SetYear(CJS_Runtime* pRuntime, int iYear);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700165
tsepezb4694242016-08-15 16:44:55 -0700166 int GetMonth(CJS_Runtime* pRuntime) const;
167 void SetMonth(CJS_Runtime* pRuntime, int iMonth);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700168
tsepezb4694242016-08-15 16:44:55 -0700169 int GetDay(CJS_Runtime* pRuntime) const;
170 void SetDay(CJS_Runtime* pRuntime, int iDay);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700171
tsepezb4694242016-08-15 16:44:55 -0700172 int GetHours(CJS_Runtime* pRuntime) const;
173 void SetHours(CJS_Runtime* pRuntime, int iHours);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700174
tsepezb4694242016-08-15 16:44:55 -0700175 int GetMinutes(CJS_Runtime* pRuntime) const;
176 void SetMinutes(CJS_Runtime* pRuntime, int minutes);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700177
tsepezb4694242016-08-15 16:44:55 -0700178 int GetSeconds(CJS_Runtime* pRuntime) const;
179 void SetSeconds(CJS_Runtime* pRuntime, int seconds);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700180
tsepezb4694242016-08-15 16:44:55 -0700181 v8::Local<v8::Date> ToV8Date(CJS_Runtime* pRuntime) const;
182 double ToDouble(CJS_Runtime* pRuntime) const;
183 CFX_WideString ToString(CJS_Runtime* pRuntime) const;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700184
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700185 protected:
tsepez135b9982016-08-05 09:32:50 -0700186 v8::Local<v8::Date> m_pDate;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700187};
188
Tom Sepez39bfe122015-09-17 15:25:23 -0700189double JS_GetDateTime();
190int JS_GetYearFromTime(double dt);
191int JS_GetMonthFromTime(double dt);
192int JS_GetDayFromTime(double dt);
193int JS_GetHourFromTime(double dt);
194int JS_GetMinFromTime(double dt);
195int JS_GetSecFromTime(double dt);
tsepez018935c2016-04-15 13:15:12 -0700196double JS_DateParse(const CFX_WideString& str);
Tom Sepez39bfe122015-09-17 15:25:23 -0700197double JS_MakeDay(int nYear, int nMonth, int nDay);
198double JS_MakeTime(int nHour, int nMin, int nSec, int nMs);
199double JS_MakeDate(double day, double time);
200bool JS_PortIsNan(double d);
201double JS_LocalTime(double d);
202
Tom Sepezbd932572016-01-29 09:10:41 -0800203// Some JS methods have the bizarre convention that they may also be called
204// with a single argument which is an object containing the actual arguments
205// as its properties. The varying arguments to this method are the property
206// names as wchar_t string literals corresponding to each positional argument.
207// The result will always contain |nKeywords| value, with unspecified ones
208// being set to type VT_unknown.
209std::vector<CJS_Value> JS_ExpandKeywordParams(
210 CJS_Runtime* pRuntime,
211 const std::vector<CJS_Value>& originals,
212 size_t nKeywords,
213 ...);
214
Dan Sinclairf766ad22016-03-14 13:51:24 -0400215#endif // FPDFSDK_JAVASCRIPT_JS_VALUE_H_