blob: 8c94ca742a42cbe433c965f6dcc1f09d72643f1d [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; }
Tom Sepez67fd5df2015-10-08 12:24:19 -070093
tsepez19249712017-01-12 11:15:04 -080094 // These calls may re-enter JS (and hence invalidate objects).
Dan Sinclair738b08c2016-03-01 14:45:20 -050095 void operator<<(int val);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070096 void operator>>(int&) const;
Dan Sinclair738b08c2016-03-01 14:45:20 -050097 void operator<<(bool val);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070098 void operator>>(bool&) const;
Dan Sinclair738b08c2016-03-01 14:45:20 -050099 void operator<<(double val);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700100 void operator>>(double&) const;
101 void operator<<(CJS_Object* pObj);
102 void operator>>(CJS_Object*& ppObj) const;
103 void operator<<(CJS_Document* pJsDoc);
104 void operator>>(CJS_Document*& ppJsDoc) const;
Ryan Harrison275e2602017-09-18 14:23:18 -0400105 void operator<<(ByteString);
106 void operator>>(ByteString&) const;
107 void operator<<(WideString);
108 void operator>>(WideString&) const;
Dan Sinclair812e96c2017-03-13 16:43:37 -0400109 void operator<<(const wchar_t* c_string);
Tom Sepez808a99e2015-09-10 12:28:37 -0700110 void operator<<(v8::Local<v8::Object>);
111 void operator>>(v8::Local<v8::Object>&) const;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700112 void operator>>(CJS_Array& array) const;
113 void operator<<(CJS_Array& array);
114 void operator<<(CJS_Date& date);
115 void operator>>(CJS_Date& date) const;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700116
117 private:
tsepezfbf52c22016-07-25 11:17:07 -0700118 bool m_bIsSetting;
tsepezf3dc8c62016-08-10 06:29:29 -0700119 CJS_Value m_Value;
Dan Sinclairaee0db02017-09-21 16:53:58 -0400120 UnownedPtr<CJS_Runtime> const m_pJSRuntime;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700121};
122
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700123class CJS_Array {
124 public:
tsepeze5aff742016-08-08 09:49:42 -0700125 CJS_Array();
weili625ad662016-06-15 11:21:33 -0700126 CJS_Array(const CJS_Array& other);
tsepezfbf52c22016-07-25 11:17:07 -0700127 virtual ~CJS_Array();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700128
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700129 void Attach(v8::Local<v8::Array> pArray);
tsepez19249712017-01-12 11:15:04 -0800130 int GetLength(CJS_Runtime* pRuntime) const;
131
132 // These two calls may re-enter JS (and hence invalidate objects).
tsepezb4694242016-08-15 16:44:55 -0700133 void GetElement(CJS_Runtime* pRuntime,
tsepeze5aff742016-08-08 09:49:42 -0700134 unsigned index,
135 CJS_Value& value) const;
tsepezb4694242016-08-15 16:44:55 -0700136 void SetElement(CJS_Runtime* pRuntime,
tsepeze5aff742016-08-08 09:49:42 -0700137 unsigned index,
138 const CJS_Value& value);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700139
tsepezb4694242016-08-15 16:44:55 -0700140 v8::Local<v8::Array> ToV8Array(CJS_Runtime* pRuntime) const;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700141
142 private:
tsepezfbf52c22016-07-25 11:17:07 -0700143 mutable v8::Local<v8::Array> m_pArray;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700144};
145
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700146class CJS_Date {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700147 public:
tsepezf3c88322016-08-09 07:30:38 -0700148 CJS_Date();
tsepezb4694242016-08-15 16:44:55 -0700149 CJS_Date(CJS_Runtime* pRuntime, double dMsec_time);
150 CJS_Date(CJS_Runtime* pRuntime,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700151 int year,
152 int mon,
153 int day,
154 int hour,
155 int min,
156 int sec);
157 virtual ~CJS_Date();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700158
tsepez135b9982016-08-05 09:32:50 -0700159 void Attach(v8::Local<v8::Date> pDate);
tsepezb4694242016-08-15 16:44:55 -0700160 bool IsValidDate(CJS_Runtime* pRuntime) const;
tsepezfbf52c22016-07-25 11:17:07 -0700161
tsepezb4694242016-08-15 16:44:55 -0700162 int GetYear(CJS_Runtime* pRuntime) const;
163 void SetYear(CJS_Runtime* pRuntime, int iYear);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700164
tsepezb4694242016-08-15 16:44:55 -0700165 int GetMonth(CJS_Runtime* pRuntime) const;
166 void SetMonth(CJS_Runtime* pRuntime, int iMonth);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700167
tsepezb4694242016-08-15 16:44:55 -0700168 int GetDay(CJS_Runtime* pRuntime) const;
169 void SetDay(CJS_Runtime* pRuntime, int iDay);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700170
tsepezb4694242016-08-15 16:44:55 -0700171 int GetHours(CJS_Runtime* pRuntime) const;
172 void SetHours(CJS_Runtime* pRuntime, int iHours);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700173
tsepezb4694242016-08-15 16:44:55 -0700174 int GetMinutes(CJS_Runtime* pRuntime) const;
175 void SetMinutes(CJS_Runtime* pRuntime, int minutes);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700176
tsepezb4694242016-08-15 16:44:55 -0700177 int GetSeconds(CJS_Runtime* pRuntime) const;
178 void SetSeconds(CJS_Runtime* pRuntime, int seconds);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700179
tsepezb4694242016-08-15 16:44:55 -0700180 v8::Local<v8::Date> ToV8Date(CJS_Runtime* pRuntime) const;
181 double ToDouble(CJS_Runtime* pRuntime) const;
Ryan Harrison275e2602017-09-18 14:23:18 -0400182 WideString ToString(CJS_Runtime* pRuntime) const;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700183
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700184 protected:
tsepez135b9982016-08-05 09:32:50 -0700185 v8::Local<v8::Date> m_pDate;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700186};
187
Tom Sepez39bfe122015-09-17 15:25:23 -0700188double JS_GetDateTime();
189int JS_GetYearFromTime(double dt);
190int JS_GetMonthFromTime(double dt);
191int JS_GetDayFromTime(double dt);
192int JS_GetHourFromTime(double dt);
193int JS_GetMinFromTime(double dt);
194int JS_GetSecFromTime(double dt);
Ryan Harrison275e2602017-09-18 14:23:18 -0400195double JS_DateParse(const WideString& str);
Tom Sepez39bfe122015-09-17 15:25:23 -0700196double JS_MakeDay(int nYear, int nMonth, int nDay);
197double JS_MakeTime(int nHour, int nMin, int nSec, int nMs);
198double JS_MakeDate(double day, double time);
Tom Sepez39bfe122015-09-17 15:25:23 -0700199double JS_LocalTime(double d);
200
Tom Sepezbd932572016-01-29 09:10:41 -0800201// Some JS methods have the bizarre convention that they may also be called
202// with a single argument which is an object containing the actual arguments
203// as its properties. The varying arguments to this method are the property
204// names as wchar_t string literals corresponding to each positional argument.
205// The result will always contain |nKeywords| value, with unspecified ones
206// being set to type VT_unknown.
207std::vector<CJS_Value> JS_ExpandKeywordParams(
208 CJS_Runtime* pRuntime,
209 const std::vector<CJS_Value>& originals,
210 size_t nKeywords,
211 ...);
212
Dan Sinclairf766ad22016-03-14 13:51:24 -0400213#endif // FPDFSDK_JAVASCRIPT_JS_VALUE_H_