blob: d64fdc6a115f88964e923ab076d7ed8c4340a8bf [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
Tom Sepez37458412015-10-06 11:33:46 -07007#ifndef FPDFSDK_SRC_JAVASCRIPT_JS_VALUE_H_
8#define FPDFSDK_SRC_JAVASCRIPT_JS_VALUE_H_
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07009
Tom Sepez9a3f8122015-04-07 15:35:48 -070010#include "../../../core/include/fxcrt/fx_basic.h"
Tom Sepez37458412015-10-06 11:33:46 -070011#include "../../include/jsapi/fxjs_v8.h"
Tom Sepez9a3f8122015-04-07 15:35:48 -070012
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070013class CJS_Array;
14class CJS_Date;
Tom Sepezf79a69c2014-10-30 13:23:42 -070015class CJS_Document;
16class CJS_Object;
Tom Sepez67fd5df2015-10-08 12:24:19 -070017class CJS_Runtime;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070018
Nico Weber9d8ec5a2015-08-04 13:00:21 -070019class CJS_Value {
20 public:
Tom Sepez39bfe122015-09-17 15:25:23 -070021 enum Type {
22 VT_unknown,
23 VT_string,
24 VT_number,
25 VT_boolean,
26 VT_date,
27 VT_object,
28 VT_fxobject,
29 VT_null,
30 VT_undefined
31 };
32
Tom Sepez67fd5df2015-10-08 12:24:19 -070033 CJS_Value(CJS_Runtime* pRuntime);
34 CJS_Value(CJS_Runtime* pRuntime, v8::Local<v8::Value> pValue, Type t);
35 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);
39 CJS_Value(CJS_Runtime* pRuntime, v8::Local<v8::Object>);
40 CJS_Value(CJS_Runtime* pRuntime, CJS_Object*);
41 CJS_Value(CJS_Runtime* pRuntime, CJS_Document*);
42 CJS_Value(CJS_Runtime* pRuntime, const FX_CHAR* pStr);
43 CJS_Value(CJS_Runtime* pRuntime, const FX_WCHAR* pWstr);
44 CJS_Value(CJS_Runtime* pRuntime, CJS_Array& array);
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();
Tom Sepez39bfe122015-09-17 15:25:23 -070049 void Attach(v8::Local<v8::Value> pValue, Type t);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070050 void Attach(CJS_Value* pValue);
51 void Detach();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070052
Tom Sepez39bfe122015-09-17 15:25:23 -070053 Type GetType() const;
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
Nico Weber9d8ec5a2015-08-04 13:00:21 -070065 void operator=(int iValue);
66 void operator=(bool bValue);
67 void operator=(double);
68 void operator=(float);
69 void operator=(CJS_Object*);
70 void operator=(CJS_Document*);
71 void operator=(v8::Local<v8::Object>);
72 void operator=(CJS_Array&);
73 void operator=(CJS_Date&);
74 void operator=(const FX_WCHAR* pWstr);
75 void operator=(const FX_CHAR* pStr);
76 void operator=(CJS_Value value);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070077
Nico Weber9d8ec5a2015-08-04 13:00:21 -070078 FX_BOOL IsArrayObject() const;
79 FX_BOOL IsDateObject() const;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070080 FX_BOOL ConvertToArray(CJS_Array&) const;
81 FX_BOOL ConvertToDate(CJS_Date&) const;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070082
Tom Sepez67fd5df2015-10-08 12:24:19 -070083 CJS_Runtime* GetJSRuntime() const { return m_pJSRuntime; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070084
Nico Weber9d8ec5a2015-08-04 13:00:21 -070085 protected:
Tom Sepez39bfe122015-09-17 15:25:23 -070086 Type m_eType;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070087 v8::Local<v8::Value> m_pValue;
Tom Sepez67fd5df2015-10-08 12:24:19 -070088 CJS_Runtime* m_pJSRuntime;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070089};
90
Nico Weber9d8ec5a2015-08-04 13:00:21 -070091class CJS_Parameters : public CFX_ArrayTemplate<CJS_Value> {
92 public:
93 void push_back(const CJS_Value& newElement) {
94 CFX_ArrayTemplate<CJS_Value>::Add(newElement);
95 }
96 int size() const { return CFX_ArrayTemplate<CJS_Value>::GetSize(); }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070097};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070098
Nico Weber9d8ec5a2015-08-04 13:00:21 -070099class CJS_PropValue : public CJS_Value {
100 public:
101 CJS_PropValue(const CJS_Value&);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700102 CJS_PropValue(CJS_Runtime* pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700103 ~CJS_PropValue();
104
Tom Sepez67fd5df2015-10-08 12:24:19 -0700105 FX_BOOL IsSetting() const { return m_bIsSetting; }
106 FX_BOOL IsGetting() const { return !m_bIsSetting; }
107
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700108 void operator<<(int);
109 void operator>>(int&) const;
110 void operator<<(bool);
111 void operator>>(bool&) const;
112 void operator<<(double);
113 void operator>>(double&) const;
114 void operator<<(CJS_Object* pObj);
115 void operator>>(CJS_Object*& ppObj) const;
116 void operator<<(CJS_Document* pJsDoc);
117 void operator>>(CJS_Document*& ppJsDoc) const;
118 void operator<<(CFX_ByteString);
119 void operator>>(CFX_ByteString&) const;
120 void operator<<(CFX_WideString);
121 void operator>>(CFX_WideString&) const;
122 void operator<<(const FX_WCHAR* c_string);
Tom Sepez808a99e2015-09-10 12:28:37 -0700123 void operator<<(v8::Local<v8::Object>);
124 void operator>>(v8::Local<v8::Object>&) const;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700125 void operator>>(CJS_Array& array) const;
126 void operator<<(CJS_Array& array);
127 void operator<<(CJS_Date& date);
128 void operator>>(CJS_Date& date) const;
129 operator v8::Local<v8::Value>() const;
130 void StartSetting();
131 void StartGetting();
132
133 private:
134 FX_BOOL m_bIsSetting;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700135};
136
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700137class CJS_Array {
138 public:
Tom Sepez67fd5df2015-10-08 12:24:19 -0700139 CJS_Array(CJS_Runtime* pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700140 virtual ~CJS_Array();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700141
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700142 void Attach(v8::Local<v8::Array> pArray);
143 void GetElement(unsigned index, CJS_Value& value);
144 void SetElement(unsigned index, CJS_Value value);
145 int GetLength();
146 FX_BOOL IsAttached();
147 operator v8::Local<v8::Array>();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700148
Tom Sepez67fd5df2015-10-08 12:24:19 -0700149 CJS_Runtime* GetJSRuntime() const { return m_pJSRuntime; }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700150
151 private:
152 v8::Local<v8::Array> m_pArray;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700153 CJS_Runtime* m_pJSRuntime;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700154};
155
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700156class CJS_Date {
157 friend class CJS_Value;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700158
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700159 public:
Tom Sepez67fd5df2015-10-08 12:24:19 -0700160 CJS_Date(CJS_Runtime* pRuntime);
161 CJS_Date(CJS_Runtime* pRuntime, double dMsec_time);
162 CJS_Date(CJS_Runtime* pRuntime,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700163 int year,
164 int mon,
165 int day,
166 int hour,
167 int min,
168 int sec);
169 virtual ~CJS_Date();
170 void Attach(v8::Local<v8::Value> pDate);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700171
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700172 int GetYear();
173 void SetYear(int iYear);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700174
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700175 int GetMonth();
176 void SetMonth(int iMonth);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700177
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700178 int GetDay();
179 void SetDay(int iDay);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700180
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700181 int GetHours();
182 void SetHours(int iHours);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700183
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700184 int GetMinutes();
185 void SetMinutes(int minutes);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700186
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700187 int GetSeconds();
188 void SetSeconds(int seconds);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700189
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700190 operator v8::Local<v8::Value>();
191 operator double() const;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700192
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700193 CFX_WideString ToString() const;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700194
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700195 static double
196 MakeDate(int year, int mon, int mday, int hour, int min, int sec, int ms);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700197
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700198 FX_BOOL IsValidDate();
199
200 protected:
201 v8::Local<v8::Value> m_pDate;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700202 CJS_Runtime* m_pJSRuntime;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700203};
204
Tom Sepez39bfe122015-09-17 15:25:23 -0700205double JS_GetDateTime();
206int JS_GetYearFromTime(double dt);
207int JS_GetMonthFromTime(double dt);
208int JS_GetDayFromTime(double dt);
209int JS_GetHourFromTime(double dt);
210int JS_GetMinFromTime(double dt);
211int JS_GetSecFromTime(double dt);
212double JS_DateParse(const wchar_t* string);
213double JS_MakeDay(int nYear, int nMonth, int nDay);
214double JS_MakeTime(int nHour, int nMin, int nSec, int nMs);
215double JS_MakeDate(double day, double time);
216bool JS_PortIsNan(double d);
217double JS_LocalTime(double d);
218
Tom Sepez37458412015-10-06 11:33:46 -0700219#endif // FPDFSDK_SRC_JAVASCRIPT_JS_VALUE_H_