1 // 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.
4 
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #ifndef FPDFSDK_SRC_JAVASCRIPT_JS_VALUE_H_
8 #define FPDFSDK_SRC_JAVASCRIPT_JS_VALUE_H_
9 
10 #include "core/include/fxcrt/fx_basic.h"
11 #include "fpdfsdk/include/jsapi/fxjs_v8.h"
12 
13 class CJS_Array;
14 class CJS_Date;
15 class CJS_Document;
16 class CJS_Object;
17 class CJS_Runtime;
18 
19 class CJS_Value {
20  public:
21   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 
33   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);
45 
46   ~CJS_Value();
47 
48   void SetNull();
49   void Attach(v8::Local<v8::Value> pValue, Type t);
50   void Attach(CJS_Value* pValue);
51   void Detach();
52 
53   Type GetType() const;
54   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;
64 
65   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);
77 
78   FX_BOOL IsArrayObject() const;
79   FX_BOOL IsDateObject() const;
80   FX_BOOL ConvertToArray(CJS_Array&) const;
81   FX_BOOL ConvertToDate(CJS_Date&) const;
82 
GetJSRuntime()83   CJS_Runtime* GetJSRuntime() const { return m_pJSRuntime; }
84 
85  protected:
86   Type m_eType;
87   v8::Local<v8::Value> m_pValue;
88   CJS_Runtime* m_pJSRuntime;
89 };
90 
91 class CJS_PropValue : public CJS_Value {
92  public:
93   CJS_PropValue(const CJS_Value&);
94   CJS_PropValue(CJS_Runtime* pRuntime);
95   ~CJS_PropValue();
96 
IsSetting()97   FX_BOOL IsSetting() const { return m_bIsSetting; }
IsGetting()98   FX_BOOL IsGetting() const { return !m_bIsSetting; }
99 
100   void operator<<(int);
101   void operator>>(int&) const;
102   void operator<<(bool);
103   void operator>>(bool&) const;
104   void operator<<(double);
105   void operator>>(double&) const;
106   void operator<<(CJS_Object* pObj);
107   void operator>>(CJS_Object*& ppObj) const;
108   void operator<<(CJS_Document* pJsDoc);
109   void operator>>(CJS_Document*& ppJsDoc) const;
110   void operator<<(CFX_ByteString);
111   void operator>>(CFX_ByteString&) const;
112   void operator<<(CFX_WideString);
113   void operator>>(CFX_WideString&) const;
114   void operator<<(const FX_WCHAR* c_string);
115   void operator<<(v8::Local<v8::Object>);
116   void operator>>(v8::Local<v8::Object>&) const;
117   void operator>>(CJS_Array& array) const;
118   void operator<<(CJS_Array& array);
119   void operator<<(CJS_Date& date);
120   void operator>>(CJS_Date& date) const;
121   operator v8::Local<v8::Value>() const;
122   void StartSetting();
123   void StartGetting();
124 
125  private:
126   FX_BOOL m_bIsSetting;
127 };
128 
129 class CJS_Array {
130  public:
131   CJS_Array(CJS_Runtime* pRuntime);
132   virtual ~CJS_Array();
133 
134   void Attach(v8::Local<v8::Array> pArray);
135   void GetElement(unsigned index, CJS_Value& value);
136   void SetElement(unsigned index, CJS_Value value);
137   int GetLength();
138   FX_BOOL IsAttached();
139   operator v8::Local<v8::Array>();
140 
GetJSRuntime()141   CJS_Runtime* GetJSRuntime() const { return m_pJSRuntime; }
142 
143  private:
144   v8::Local<v8::Array> m_pArray;
145   CJS_Runtime* m_pJSRuntime;
146 };
147 
148 class CJS_Date {
149   friend class CJS_Value;
150 
151  public:
152   CJS_Date(CJS_Runtime* pRuntime);
153   CJS_Date(CJS_Runtime* pRuntime, double dMsec_time);
154   CJS_Date(CJS_Runtime* pRuntime,
155            int year,
156            int mon,
157            int day,
158            int hour,
159            int min,
160            int sec);
161   virtual ~CJS_Date();
162   void Attach(v8::Local<v8::Value> pDate);
163 
164   int GetYear();
165   void SetYear(int iYear);
166 
167   int GetMonth();
168   void SetMonth(int iMonth);
169 
170   int GetDay();
171   void SetDay(int iDay);
172 
173   int GetHours();
174   void SetHours(int iHours);
175 
176   int GetMinutes();
177   void SetMinutes(int minutes);
178 
179   int GetSeconds();
180   void SetSeconds(int seconds);
181 
182   operator v8::Local<v8::Value>();
183   operator double() const;
184 
185   CFX_WideString ToString() const;
186 
187   static double
188   MakeDate(int year, int mon, int mday, int hour, int min, int sec, int ms);
189 
190   FX_BOOL IsValidDate();
191 
192  protected:
193   v8::Local<v8::Value> m_pDate;
194   CJS_Runtime* m_pJSRuntime;
195 };
196 
197 double JS_GetDateTime();
198 int JS_GetYearFromTime(double dt);
199 int JS_GetMonthFromTime(double dt);
200 int JS_GetDayFromTime(double dt);
201 int JS_GetHourFromTime(double dt);
202 int JS_GetMinFromTime(double dt);
203 int JS_GetSecFromTime(double dt);
204 double JS_DateParse(const wchar_t* string);
205 double JS_MakeDay(int nYear, int nMonth, int nDay);
206 double JS_MakeTime(int nHour, int nMin, int nSec, int nMs);
207 double JS_MakeDate(double day, double time);
208 bool JS_PortIsNan(double d);
209 double JS_LocalTime(double d);
210 
211 #endif  // FPDFSDK_SRC_JAVASCRIPT_JS_VALUE_H_
212