Merge XFA to PDFium master at 4dc95e7 on 10/28/2014
[pdfium.git] / xfa / src / fxjse / src / value.h
1 // Copyright 2014 PDFium Authors. All rights reserved.\r
2 // Use of this source code is governed by a BSD-style license that can be\r
3 // found in the LICENSE file.\r
4 \r
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com\r
6 \r
7 #ifndef FXJSE_VALUE_H_\r
8 #define FXJSE_VALUE_H_\r
9 #include "scope_inline.h"\r
10 class CFXJSE_Value : public CFX_Object\r
11 {\r
12 public:\r
13     CFXJSE_Value(v8::Isolate* pIsolate) : m_pIsolate(pIsolate) {}\r
14 protected:\r
15     CFXJSE_Value();\r
16     CFXJSE_Value(const CFXJSE_Value&);\r
17     CFXJSE_Value& operator = (const CFXJSE_Value&);\r
18 public:\r
19     V8_INLINE FX_BOOL IsUndefined()     const\r
20     {\r
21         if (m_hValue.IsEmpty()) {\r
22             return FALSE;\r
23         }\r
24         CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);\r
25         v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue);\r
26         return hValue->IsUndefined();\r
27     }\r
28     V8_INLINE FX_BOOL IsNull()          const\r
29     {\r
30         if (m_hValue.IsEmpty()) {\r
31             return FALSE;\r
32         }\r
33         CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);\r
34         v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue);\r
35         return hValue->IsNull();\r
36     }\r
37     V8_INLINE FX_BOOL IsBoolean()       const\r
38     {\r
39         if (m_hValue.IsEmpty()) {\r
40             return FALSE;\r
41         }\r
42         CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);\r
43         v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue);\r
44         return hValue->IsBoolean();\r
45     }\r
46     V8_INLINE FX_BOOL IsString()        const\r
47     {\r
48         if (m_hValue.IsEmpty()) {\r
49             return FALSE;\r
50         }\r
51         CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);\r
52         v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue);\r
53         return hValue->IsString();\r
54     }\r
55     V8_INLINE FX_BOOL IsNumber()        const\r
56     {\r
57         if (m_hValue.IsEmpty()) {\r
58             return FALSE;\r
59         }\r
60         CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);\r
61         v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue);\r
62         return hValue->IsNumber();\r
63     }\r
64     V8_INLINE FX_BOOL IsInteger()       const\r
65     {\r
66         if (m_hValue.IsEmpty()) {\r
67             return FALSE;\r
68         }\r
69         CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);\r
70         v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue);\r
71         return hValue->IsInt32();\r
72     }\r
73     V8_INLINE FX_BOOL IsObject()        const\r
74     {\r
75         if (m_hValue.IsEmpty()) {\r
76             return FALSE;\r
77         }\r
78         CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);\r
79         v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue);\r
80         return hValue->IsObject();\r
81     }\r
82     V8_INLINE FX_BOOL IsArray()         const\r
83     {\r
84         if (m_hValue.IsEmpty()) {\r
85             return FALSE;\r
86         }\r
87         CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);\r
88         v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue);\r
89         return hValue->IsArray();\r
90     }\r
91     V8_INLINE FX_BOOL IsFunction()      const\r
92     {\r
93         if (m_hValue.IsEmpty()) {\r
94             return FALSE;\r
95         }\r
96         CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);\r
97         v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue);\r
98         return hValue->IsFunction();\r
99     }\r
100     V8_INLINE FX_BOOL IsDate()          const\r
101     {\r
102         if (m_hValue.IsEmpty()) {\r
103             return FALSE;\r
104         }\r
105         CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);\r
106         v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue);\r
107         return hValue->IsDate();\r
108     }\r
109 public:\r
110     V8_INLINE FX_BOOL           ToBoolean() const\r
111     {\r
112         ASSERT(!m_hValue.IsEmpty());\r
113         CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);\r
114         v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue);\r
115         return static_cast<FX_BOOL>(hValue->BooleanValue());\r
116     }\r
117     V8_INLINE FX_FLOAT          ToFloat()       const\r
118     {\r
119         ASSERT(!m_hValue.IsEmpty());\r
120         CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);\r
121         v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue);\r
122         return static_cast<FX_FLOAT>(hValue->NumberValue());\r
123     }\r
124     V8_INLINE FXJSE_DOUBLE      ToDouble()      const\r
125     {\r
126         ASSERT(!m_hValue.IsEmpty());\r
127         CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);\r
128         v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue);\r
129         return static_cast<FXJSE_DOUBLE>(hValue->NumberValue());\r
130     }\r
131     V8_INLINE FX_INT32          ToInteger()     const\r
132     {\r
133         ASSERT(!m_hValue.IsEmpty());\r
134         CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);\r
135         v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue);\r
136         return static_cast<FX_INT32>(hValue->NumberValue());\r
137     }\r
138     V8_INLINE void                      ToString(CFX_ByteString& szStrOutput) const\r
139     {\r
140         ASSERT(!m_hValue.IsEmpty());\r
141         CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);\r
142         v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue);\r
143         v8::Local<v8::String> hString = hValue->ToString();\r
144         v8::String::Utf8Value hStringVal(hString);\r
145         szStrOutput = *hStringVal;\r
146     }\r
147     FX_LPVOID                           ToObject(CFXJSE_Class*   lpClass) const;\r
148 public:\r
149     V8_INLINE void SetUndefined()\r
150     {\r
151         CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);\r
152         v8::Local<v8::Value> hValue = v8::Undefined(m_pIsolate);\r
153         m_hValue.Reset(m_pIsolate, hValue);\r
154     }\r
155     V8_INLINE void SetNull()\r
156     {\r
157         CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);\r
158         v8::Local<v8::Value> hValue = v8::Null(m_pIsolate);\r
159         m_hValue.Reset(m_pIsolate, hValue);\r
160     }\r
161     V8_INLINE void SetBoolean(FX_BOOL bBoolean)\r
162     {\r
163         CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);\r
164         v8::Local<v8::Value> hValue = v8::Boolean::New(m_pIsolate, bBoolean != FALSE);\r
165         m_hValue.Reset(m_pIsolate, hValue);\r
166     }\r
167     V8_INLINE void SetInteger(FX_INT32 nInteger)\r
168     {\r
169         CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);\r
170         v8::Local<v8::Value> hValue = v8::Integer::New(m_pIsolate, nInteger);\r
171         m_hValue.Reset(m_pIsolate, hValue);\r
172     }\r
173     V8_INLINE void SetDouble(FXJSE_DOUBLE dDouble)\r
174     {\r
175         CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);\r
176         v8::Local<v8::Value> hValue = v8::Number::New(m_pIsolate, dDouble);\r
177         m_hValue.Reset(m_pIsolate, hValue);\r
178     }\r
179     V8_INLINE void SetString(FX_BSTR szString)\r
180     {\r
181         CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);\r
182         v8::Local<v8::Value> hValue = v8::String::NewFromUtf8(m_pIsolate,\r
183                                       reinterpret_cast<const char*>(szString.GetPtr()),\r
184                                       v8::String::kNormalString, szString.GetLength());\r
185         m_hValue.Reset(m_pIsolate, hValue);\r
186     }\r
187     V8_INLINE void SetFloat(FX_FLOAT fFloat);\r
188     V8_INLINE void SetJSObject()\r
189     {\r
190         CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);\r
191         v8::Local<v8::Value> hValue = v8::Object::New(m_pIsolate);\r
192         m_hValue.Reset(m_pIsolate, hValue);\r
193     }\r
194     void SetHostObject(FX_LPVOID lpObject, CFXJSE_Class* lpClass);\r
195     void SetArray(FX_UINT32 uValueCount, CFXJSE_Value** rgValues);\r
196     void SetDate(FXJSE_DOUBLE dDouble);\r
197 public:\r
198     FX_BOOL GetObjectProperty(FX_BSTR   szPropName,     CFXJSE_Value* lpPropValue);\r
199     FX_BOOL SetObjectProperty(FX_BSTR   szPropName,     CFXJSE_Value* lpPropValue);\r
200     FX_BOOL GetObjectProperty(FX_UINT32 uPropIdx,       CFXJSE_Value* lpPropValue);\r
201     FX_BOOL SetObjectProperty(FX_UINT32 uPropIdx,       CFXJSE_Value* lpPropValue);\r
202     FX_BOOL DeleteObjectProperty(FX_BSTR szPropName);\r
203     FX_BOOL HasObjectOwnProperty(FX_BSTR   szPropName, FX_BOOL  bUseTypeGetter);\r
204     FX_BOOL SetObjectOwnProperty(FX_BSTR   szPropName, CFXJSE_Value* lpPropValue);\r
205     FX_BOOL     SetFunctionBind(CFXJSE_Value* lpOldFunction, CFXJSE_Value* lpNewThis);\r
206     FX_BOOL Call(CFXJSE_Value* lpReceiver, CFXJSE_Value* lpRetValue, FX_UINT32 nArgCount, FXJSE_HVALUE* lpArgs);\r
207 public:\r
208     V8_INLINE v8::Isolate* GetIsolate() const\r
209     {\r
210         return m_pIsolate;\r
211     }\r
212     V8_INLINE const v8::Persistent<v8::Value>& DirectGetValue() const\r
213     {\r
214         return m_hValue;\r
215     }\r
216     V8_INLINE void ForceSetValue(v8::Local<v8::Value> hValue)\r
217     {\r
218         m_hValue.Reset(m_pIsolate, hValue);\r
219     }\r
220     V8_INLINE void Assign(const CFXJSE_Value* lpValue)\r
221     {\r
222         if(lpValue) {\r
223             m_hValue.Reset(m_pIsolate, lpValue->m_hValue);\r
224         } else {\r
225             m_hValue.Reset();\r
226         }\r
227     }\r
228 public:\r
229     static CFXJSE_Value* Create(v8::Isolate* pIsolate);\r
230 protected:\r
231     v8::Isolate*                                m_pIsolate;\r
232     v8::Persistent<v8::Value>   m_hValue;\r
233     friend class CFXJSE_Context;\r
234     friend class CFXJSE_Class;\r
235 };\r
236 #endif\r