blob: c567a7cbf2088abfbd38de0835dc2354f58a0bf1 [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);
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; }
tsepezf3dc8c62016-08-10 06:29:29 -070092 CJS_Runtime* GetJSRuntime() const { return m_pJSRuntime; }
93 CJS_Value* GetJSValue() { return &m_Value; }
Tom Sepez67fd5df2015-10-08 12:24:19 -070094
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;
105 void operator<<(CFX_ByteString);
106 void operator>>(CFX_ByteString&) const;
107 void operator<<(CFX_WideString);
108 void operator>>(CFX_WideString&) const;
109 void operator<<(const FX_WCHAR* 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;
120 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);
tsepezb4694242016-08-15 16:44:55 -0700130 void GetElement(CJS_Runtime* pRuntime,
tsepeze5aff742016-08-08 09:49:42 -0700131 unsigned index,
132 CJS_Value& value) const;
tsepezb4694242016-08-15 16:44:55 -0700133 void SetElement(CJS_Runtime* pRuntime,
tsepeze5aff742016-08-08 09:49:42 -0700134 unsigned index,
135 const CJS_Value& value);
tsepezb4694242016-08-15 16:44:55 -0700136 int GetLength(CJS_Runtime* pRuntime) const;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700137
tsepezb4694242016-08-15 16:44:55 -0700138 v8::Local<v8::Array> ToV8Array(CJS_Runtime* pRuntime) const;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700139
140 private:
tsepezfbf52c22016-07-25 11:17:07 -0700141 mutable v8::Local<v8::Array> m_pArray;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700142};
143
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700144class CJS_Date {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700145 public:
tsepezf3c88322016-08-09 07:30:38 -0700146 CJS_Date();
tsepezb4694242016-08-15 16:44:55 -0700147 CJS_Date(CJS_Runtime* pRuntime, double dMsec_time);
148 CJS_Date(CJS_Runtime* pRuntime,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700149 int year,
150 int mon,
151 int day,
152 int hour,
153 int min,
154 int sec);
155 virtual ~CJS_Date();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700156
tsepez135b9982016-08-05 09:32:50 -0700157 void Attach(v8::Local<v8::Date> pDate);
tsepezb4694242016-08-15 16:44:55 -0700158 bool IsValidDate(CJS_Runtime* pRuntime) const;
tsepezfbf52c22016-07-25 11:17:07 -0700159
tsepezb4694242016-08-15 16:44:55 -0700160 int GetYear(CJS_Runtime* pRuntime) const;
161 void SetYear(CJS_Runtime* pRuntime, int iYear);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700162
tsepezb4694242016-08-15 16:44:55 -0700163 int GetMonth(CJS_Runtime* pRuntime) const;
164 void SetMonth(CJS_Runtime* pRuntime, int iMonth);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700165
tsepezb4694242016-08-15 16:44:55 -0700166 int GetDay(CJS_Runtime* pRuntime) const;
167 void SetDay(CJS_Runtime* pRuntime, int iDay);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700168
tsepezb4694242016-08-15 16:44:55 -0700169 int GetHours(CJS_Runtime* pRuntime) const;
170 void SetHours(CJS_Runtime* pRuntime, int iHours);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700171
tsepezb4694242016-08-15 16:44:55 -0700172 int GetMinutes(CJS_Runtime* pRuntime) const;
173 void SetMinutes(CJS_Runtime* pRuntime, int minutes);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700174
tsepezb4694242016-08-15 16:44:55 -0700175 int GetSeconds(CJS_Runtime* pRuntime) const;
176 void SetSeconds(CJS_Runtime* pRuntime, int seconds);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700177
tsepezb4694242016-08-15 16:44:55 -0700178 v8::Local<v8::Date> ToV8Date(CJS_Runtime* pRuntime) const;
179 double ToDouble(CJS_Runtime* pRuntime) const;
180 CFX_WideString ToString(CJS_Runtime* pRuntime) const;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700181
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700182 protected:
tsepez135b9982016-08-05 09:32:50 -0700183 v8::Local<v8::Date> m_pDate;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700184};
185
Tom Sepez39bfe122015-09-17 15:25:23 -0700186double JS_GetDateTime();
187int JS_GetYearFromTime(double dt);
188int JS_GetMonthFromTime(double dt);
189int JS_GetDayFromTime(double dt);
190int JS_GetHourFromTime(double dt);
191int JS_GetMinFromTime(double dt);
192int JS_GetSecFromTime(double dt);
tsepez018935c2016-04-15 13:15:12 -0700193double JS_DateParse(const CFX_WideString& str);
Tom Sepez39bfe122015-09-17 15:25:23 -0700194double JS_MakeDay(int nYear, int nMonth, int nDay);
195double JS_MakeTime(int nHour, int nMin, int nSec, int nMs);
196double JS_MakeDate(double day, double time);
197bool JS_PortIsNan(double d);
198double JS_LocalTime(double d);
199
Tom Sepezbd932572016-01-29 09:10:41 -0800200// Some JS methods have the bizarre convention that they may also be called
201// with a single argument which is an object containing the actual arguments
202// as its properties. The varying arguments to this method are the property
203// names as wchar_t string literals corresponding to each positional argument.
204// The result will always contain |nKeywords| value, with unspecified ones
205// being set to type VT_unknown.
206std::vector<CJS_Value> JS_ExpandKeywordParams(
207 CJS_Runtime* pRuntime,
208 const std::vector<CJS_Value>& originals,
209 size_t nKeywords,
210 ...);
211
Dan Sinclairf766ad22016-03-14 13:51:24 -0400212#endif // FPDFSDK_JAVASCRIPT_JS_VALUE_H_