OLD | NEW |
| (Empty) |
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 #include "xfa/src/fxjse/class.h" | |
8 | |
9 #include "xfa/src/fxjse/context.h" | |
10 #include "xfa/src/fxjse/scope_inline.h" | |
11 #include "xfa/src/fxjse/util_inline.h" | |
12 #include "xfa/src/fxjse/value.h" | |
13 | |
14 static void FXJSE_V8ConstructorCallback_Wrapper( | |
15 const v8::FunctionCallbackInfo<v8::Value>& info); | |
16 static void FXJSE_V8FunctionCallback_Wrapper( | |
17 const v8::FunctionCallbackInfo<v8::Value>& info); | |
18 static void FXJSE_V8GetterCallback_Wrapper( | |
19 v8::Local<v8::String> property, | |
20 const v8::PropertyCallbackInfo<v8::Value>& info); | |
21 static void FXJSE_V8SetterCallback_Wrapper( | |
22 v8::Local<v8::String> property, | |
23 v8::Local<v8::Value> value, | |
24 const v8::PropertyCallbackInfo<void>& info); | |
25 | |
26 void FXJSE_DefineFunctions(FXJSE_HCONTEXT hContext, | |
27 const FXJSE_FUNCTION* lpFunctions, | |
28 int nNum) { | |
29 CFXJSE_Context* lpContext = reinterpret_cast<CFXJSE_Context*>(hContext); | |
30 ASSERT(lpContext); | |
31 CFXJSE_ScopeUtil_IsolateHandleContext scope(lpContext); | |
32 v8::Isolate* pIsolate = lpContext->GetRuntime(); | |
33 v8::Local<v8::Object> hGlobalObject = | |
34 FXJSE_GetGlobalObjectFromContext(scope.GetLocalContext()); | |
35 for (int32_t i = 0; i < nNum; i++) { | |
36 v8::Maybe<bool> maybe_success = hGlobalObject->DefineOwnProperty( | |
37 scope.GetLocalContext(), | |
38 v8::String::NewFromUtf8(pIsolate, lpFunctions[i].name), | |
39 v8::Function::New( | |
40 pIsolate, FXJSE_V8FunctionCallback_Wrapper, | |
41 v8::External::New(pIsolate, | |
42 const_cast<FXJSE_FUNCTION*>(lpFunctions + i))), | |
43 static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete)); | |
44 if (!maybe_success.FromMaybe(false)) | |
45 return; | |
46 } | |
47 } | |
48 | |
49 FXJSE_HCLASS FXJSE_DefineClass(FXJSE_HCONTEXT hContext, | |
50 const FXJSE_CLASS* lpClass) { | |
51 CFXJSE_Context* lpContext = reinterpret_cast<CFXJSE_Context*>(hContext); | |
52 ASSERT(lpContext); | |
53 return reinterpret_cast<FXJSE_HCLASS>( | |
54 CFXJSE_Class::Create(lpContext, lpClass, FALSE)); | |
55 } | |
56 | |
57 FXJSE_HCLASS FXJSE_GetClass(FXJSE_HCONTEXT hContext, | |
58 const CFX_ByteStringC& szName) { | |
59 return reinterpret_cast<FXJSE_HCLASS>(CFXJSE_Class::GetClassFromContext( | |
60 reinterpret_cast<CFXJSE_Context*>(hContext), szName)); | |
61 } | |
62 | |
63 static void FXJSE_V8FunctionCallback_Wrapper( | |
64 const v8::FunctionCallbackInfo<v8::Value>& info) { | |
65 const FXJSE_FUNCTION* lpFunctionInfo = | |
66 static_cast<FXJSE_FUNCTION*>(info.Data().As<v8::External>()->Value()); | |
67 if (!lpFunctionInfo) { | |
68 return; | |
69 } | |
70 CFX_ByteStringC szFunctionName(lpFunctionInfo->name); | |
71 CFXJSE_Value* lpThisValue = CFXJSE_Value::Create(info.GetIsolate()); | |
72 lpThisValue->ForceSetValue(info.This()); | |
73 CFXJSE_Value* lpRetValue = CFXJSE_Value::Create(info.GetIsolate()); | |
74 CFXJSE_ArgumentsImpl impl = {&info, lpRetValue}; | |
75 lpFunctionInfo->callbackProc(reinterpret_cast<FXJSE_HOBJECT>(lpThisValue), | |
76 szFunctionName, | |
77 reinterpret_cast<CFXJSE_Arguments&>(impl)); | |
78 if (!lpRetValue->DirectGetValue().IsEmpty()) { | |
79 info.GetReturnValue().Set(lpRetValue->DirectGetValue()); | |
80 } | |
81 delete lpRetValue; | |
82 lpRetValue = NULL; | |
83 delete lpThisValue; | |
84 lpThisValue = NULL; | |
85 } | |
86 | |
87 static void FXJSE_V8ClassGlobalConstructorCallback_Wrapper( | |
88 const v8::FunctionCallbackInfo<v8::Value>& info) { | |
89 const FXJSE_CLASS* lpClassDefinition = | |
90 static_cast<FXJSE_CLASS*>(info.Data().As<v8::External>()->Value()); | |
91 if (!lpClassDefinition) { | |
92 return; | |
93 } | |
94 CFX_ByteStringC szFunctionName(lpClassDefinition->name); | |
95 CFXJSE_Value* lpThisValue = CFXJSE_Value::Create(info.GetIsolate()); | |
96 lpThisValue->ForceSetValue(info.This()); | |
97 CFXJSE_Value* lpRetValue = CFXJSE_Value::Create(info.GetIsolate()); | |
98 CFXJSE_ArgumentsImpl impl = {&info, lpRetValue}; | |
99 lpClassDefinition->constructor(reinterpret_cast<FXJSE_HOBJECT>(lpThisValue), | |
100 szFunctionName, | |
101 reinterpret_cast<CFXJSE_Arguments&>(impl)); | |
102 if (!lpRetValue->DirectGetValue().IsEmpty()) { | |
103 info.GetReturnValue().Set(lpRetValue->DirectGetValue()); | |
104 } | |
105 delete lpRetValue; | |
106 lpRetValue = NULL; | |
107 delete lpThisValue; | |
108 lpThisValue = NULL; | |
109 } | |
110 | |
111 static void FXJSE_V8GetterCallback_Wrapper( | |
112 v8::Local<v8::String> property, | |
113 const v8::PropertyCallbackInfo<v8::Value>& info) { | |
114 const FXJSE_PROPERTY* lpPropertyInfo = | |
115 static_cast<FXJSE_PROPERTY*>(info.Data().As<v8::External>()->Value()); | |
116 if (!lpPropertyInfo) { | |
117 return; | |
118 } | |
119 CFX_ByteStringC szPropertyName(lpPropertyInfo->name); | |
120 CFXJSE_Value* lpThisValue = CFXJSE_Value::Create(info.GetIsolate()); | |
121 CFXJSE_Value* lpPropValue = CFXJSE_Value::Create(info.GetIsolate()); | |
122 lpThisValue->ForceSetValue(info.This()); | |
123 lpPropertyInfo->getProc(reinterpret_cast<FXJSE_HOBJECT>(lpThisValue), | |
124 szPropertyName, | |
125 reinterpret_cast<FXJSE_HVALUE>(lpPropValue)); | |
126 info.GetReturnValue().Set(lpPropValue->DirectGetValue()); | |
127 delete lpThisValue; | |
128 lpThisValue = NULL; | |
129 delete lpPropValue; | |
130 lpPropValue = NULL; | |
131 } | |
132 | |
133 static void FXJSE_V8SetterCallback_Wrapper( | |
134 v8::Local<v8::String> property, | |
135 v8::Local<v8::Value> value, | |
136 const v8::PropertyCallbackInfo<void>& info) { | |
137 const FXJSE_PROPERTY* lpPropertyInfo = | |
138 static_cast<FXJSE_PROPERTY*>(info.Data().As<v8::External>()->Value()); | |
139 if (!lpPropertyInfo) { | |
140 return; | |
141 } | |
142 CFX_ByteStringC szPropertyName(lpPropertyInfo->name); | |
143 CFXJSE_Value* lpThisValue = CFXJSE_Value::Create(info.GetIsolate()); | |
144 CFXJSE_Value* lpPropValue = CFXJSE_Value::Create(info.GetIsolate()); | |
145 lpThisValue->ForceSetValue(info.This()); | |
146 lpPropValue->ForceSetValue(value); | |
147 lpPropertyInfo->setProc(reinterpret_cast<FXJSE_HOBJECT>(lpThisValue), | |
148 szPropertyName, | |
149 reinterpret_cast<FXJSE_HVALUE>(lpPropValue)); | |
150 delete lpThisValue; | |
151 lpThisValue = NULL; | |
152 delete lpPropValue; | |
153 lpPropValue = NULL; | |
154 } | |
155 | |
156 static void FXJSE_V8ConstructorCallback_Wrapper( | |
157 const v8::FunctionCallbackInfo<v8::Value>& info) { | |
158 const FXJSE_CLASS* lpClassDefinition = | |
159 static_cast<FXJSE_CLASS*>(info.Data().As<v8::External>()->Value()); | |
160 if (!lpClassDefinition) { | |
161 return; | |
162 } | |
163 FXSYS_assert(info.This()->InternalFieldCount()); | |
164 info.This()->SetAlignedPointerInInternalField(0, NULL); | |
165 } | |
166 | |
167 FXJSE_HRUNTIME CFXJSE_Arguments::GetRuntime() const { | |
168 const CFXJSE_ArgumentsImpl* lpArguments = | |
169 reinterpret_cast<const CFXJSE_ArgumentsImpl* const>(this); | |
170 return reinterpret_cast<FXJSE_HRUNTIME>( | |
171 lpArguments->m_pRetValue->GetIsolate()); | |
172 } | |
173 | |
174 int32_t CFXJSE_Arguments::GetLength() const { | |
175 const CFXJSE_ArgumentsImpl* lpArguments = | |
176 reinterpret_cast<const CFXJSE_ArgumentsImpl* const>(this); | |
177 return lpArguments->m_pInfo->Length(); | |
178 } | |
179 | |
180 FXJSE_HVALUE CFXJSE_Arguments::GetValue(int32_t index) const { | |
181 const CFXJSE_ArgumentsImpl* lpArguments = | |
182 reinterpret_cast<const CFXJSE_ArgumentsImpl* const>(this); | |
183 CFXJSE_Value* lpArgValue = CFXJSE_Value::Create(v8::Isolate::GetCurrent()); | |
184 ASSERT(lpArgValue); | |
185 lpArgValue->ForceSetValue((*lpArguments->m_pInfo)[index]); | |
186 return reinterpret_cast<FXJSE_HVALUE>(lpArgValue); | |
187 } | |
188 | |
189 FX_BOOL CFXJSE_Arguments::GetBoolean(int32_t index) const { | |
190 const CFXJSE_ArgumentsImpl* lpArguments = | |
191 reinterpret_cast<const CFXJSE_ArgumentsImpl* const>(this); | |
192 return (*lpArguments->m_pInfo)[index]->BooleanValue(); | |
193 } | |
194 | |
195 int32_t CFXJSE_Arguments::GetInt32(int32_t index) const { | |
196 const CFXJSE_ArgumentsImpl* lpArguments = | |
197 reinterpret_cast<const CFXJSE_ArgumentsImpl* const>(this); | |
198 return static_cast<int32_t>((*lpArguments->m_pInfo)[index]->NumberValue()); | |
199 } | |
200 | |
201 FX_FLOAT CFXJSE_Arguments::GetFloat(int32_t index) const { | |
202 const CFXJSE_ArgumentsImpl* lpArguments = | |
203 reinterpret_cast<const CFXJSE_ArgumentsImpl* const>(this); | |
204 return static_cast<FX_FLOAT>((*lpArguments->m_pInfo)[index]->NumberValue()); | |
205 } | |
206 | |
207 CFX_ByteString CFXJSE_Arguments::GetUTF8String(int32_t index) const { | |
208 const CFXJSE_ArgumentsImpl* lpArguments = | |
209 reinterpret_cast<const CFXJSE_ArgumentsImpl* const>(this); | |
210 v8::Local<v8::String> hString = (*lpArguments->m_pInfo)[index]->ToString(); | |
211 v8::String::Utf8Value szStringVal(hString); | |
212 return CFX_ByteString(*szStringVal); | |
213 } | |
214 | |
215 void* CFXJSE_Arguments::GetObject(int32_t index, FXJSE_HCLASS hClass) const { | |
216 const CFXJSE_ArgumentsImpl* lpArguments = | |
217 reinterpret_cast<const CFXJSE_ArgumentsImpl* const>(this); | |
218 v8::Local<v8::Value> hValue = (*lpArguments->m_pInfo)[index]; | |
219 ASSERT(!hValue.IsEmpty()); | |
220 if (!hValue->IsObject()) { | |
221 return NULL; | |
222 } | |
223 CFXJSE_Class* lpClass = reinterpret_cast<CFXJSE_Class*>(hClass); | |
224 return FXJSE_RetrieveObjectBinding(hValue.As<v8::Object>(), lpClass); | |
225 } | |
226 | |
227 FXJSE_HVALUE CFXJSE_Arguments::GetReturnValue() { | |
228 const CFXJSE_ArgumentsImpl* lpArguments = | |
229 reinterpret_cast<const CFXJSE_ArgumentsImpl* const>(this); | |
230 return reinterpret_cast<FXJSE_HVALUE>(lpArguments->m_pRetValue); | |
231 } | |
232 static void FXJSE_Context_GlobalObjToString( | |
233 const v8::FunctionCallbackInfo<v8::Value>& info) { | |
234 const FXJSE_CLASS* lpClass = | |
235 static_cast<FXJSE_CLASS*>(info.Data().As<v8::External>()->Value()); | |
236 if (!lpClass) { | |
237 return; | |
238 } | |
239 if (info.This() == info.Holder() && lpClass->name) { | |
240 CFX_ByteString szStringVal; | |
241 szStringVal.Format("[object %s]", lpClass->name); | |
242 info.GetReturnValue().Set(v8::String::NewFromUtf8( | |
243 info.GetIsolate(), (const FX_CHAR*)szStringVal, | |
244 v8::String::kNormalString, szStringVal.GetLength())); | |
245 } else { | |
246 v8::Local<v8::String> local_str = | |
247 info.This() | |
248 ->ObjectProtoToString(info.GetIsolate()->GetCurrentContext()) | |
249 .FromMaybe(v8::Local<v8::String>()); | |
250 info.GetReturnValue().Set(local_str); | |
251 } | |
252 } | |
253 | |
254 CFXJSE_Class* CFXJSE_Class::Create(CFXJSE_Context* lpContext, | |
255 const FXJSE_CLASS* lpClassDefinition, | |
256 FX_BOOL bIsJSGlobal) { | |
257 if (!lpContext || !lpClassDefinition) { | |
258 return NULL; | |
259 } | |
260 CFXJSE_Class* pClass = | |
261 GetClassFromContext(lpContext, lpClassDefinition->name); | |
262 if (pClass) { | |
263 return pClass; | |
264 } | |
265 v8::Isolate* pIsolate = lpContext->m_pIsolate; | |
266 pClass = new CFXJSE_Class(lpContext); | |
267 pClass->m_szClassName = lpClassDefinition->name; | |
268 pClass->m_lpClassDefinition = lpClassDefinition; | |
269 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(pIsolate); | |
270 v8::Local<v8::FunctionTemplate> hFunctionTemplate = v8::FunctionTemplate::New( | |
271 pIsolate, bIsJSGlobal ? 0 : FXJSE_V8ConstructorCallback_Wrapper, | |
272 v8::External::New(pIsolate, const_cast<FXJSE_CLASS*>(lpClassDefinition))); | |
273 hFunctionTemplate->SetClassName( | |
274 v8::String::NewFromUtf8(pIsolate, lpClassDefinition->name)); | |
275 hFunctionTemplate->InstanceTemplate()->SetInternalFieldCount(1); | |
276 v8::Local<v8::ObjectTemplate> hObjectTemplate = | |
277 hFunctionTemplate->InstanceTemplate(); | |
278 SetUpNamedPropHandler(pIsolate, hObjectTemplate, lpClassDefinition); | |
279 | |
280 if (lpClassDefinition->propNum) { | |
281 for (int32_t i = 0; i < lpClassDefinition->propNum; i++) { | |
282 hObjectTemplate->SetNativeDataProperty( | |
283 v8::String::NewFromUtf8(pIsolate, | |
284 lpClassDefinition->properties[i].name), | |
285 lpClassDefinition->properties[i].getProc | |
286 ? FXJSE_V8GetterCallback_Wrapper | |
287 : NULL, | |
288 lpClassDefinition->properties[i].setProc | |
289 ? FXJSE_V8SetterCallback_Wrapper | |
290 : NULL, | |
291 v8::External::New(pIsolate, const_cast<FXJSE_PROPERTY*>( | |
292 lpClassDefinition->properties + i)), | |
293 static_cast<v8::PropertyAttribute>(v8::DontDelete)); | |
294 } | |
295 } | |
296 if (lpClassDefinition->methNum) { | |
297 for (int32_t i = 0; i < lpClassDefinition->methNum; i++) { | |
298 hObjectTemplate->Set( | |
299 v8::String::NewFromUtf8(pIsolate, lpClassDefinition->methods[i].name), | |
300 v8::FunctionTemplate::New( | |
301 pIsolate, FXJSE_V8FunctionCallback_Wrapper, | |
302 v8::External::New(pIsolate, const_cast<FXJSE_FUNCTION*>( | |
303 lpClassDefinition->methods + i))), | |
304 static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete)); | |
305 } | |
306 } | |
307 if (lpClassDefinition->constructor) { | |
308 if (bIsJSGlobal) { | |
309 hObjectTemplate->Set( | |
310 v8::String::NewFromUtf8(pIsolate, lpClassDefinition->name), | |
311 v8::FunctionTemplate::New( | |
312 pIsolate, FXJSE_V8ClassGlobalConstructorCallback_Wrapper, | |
313 v8::External::New(pIsolate, | |
314 const_cast<FXJSE_CLASS*>(lpClassDefinition))), | |
315 static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete)); | |
316 } else { | |
317 v8::Local<v8::Context> hLocalContext = | |
318 v8::Local<v8::Context>::New(pIsolate, lpContext->m_hContext); | |
319 FXJSE_GetGlobalObjectFromContext(hLocalContext) | |
320 ->Set(v8::String::NewFromUtf8(pIsolate, lpClassDefinition->name), | |
321 v8::Function::New( | |
322 pIsolate, FXJSE_V8ClassGlobalConstructorCallback_Wrapper, | |
323 v8::External::New(pIsolate, const_cast<FXJSE_CLASS*>( | |
324 lpClassDefinition)))); | |
325 } | |
326 } | |
327 if (bIsJSGlobal) { | |
328 hObjectTemplate->Set( | |
329 v8::String::NewFromUtf8(pIsolate, "toString"), | |
330 v8::FunctionTemplate::New( | |
331 pIsolate, FXJSE_Context_GlobalObjToString, | |
332 v8::External::New(pIsolate, | |
333 const_cast<FXJSE_CLASS*>(lpClassDefinition)))); | |
334 } | |
335 pClass->m_hTemplate.Reset(lpContext->m_pIsolate, hFunctionTemplate); | |
336 lpContext->m_rgClasses.Add(pClass); | |
337 return pClass; | |
338 } | |
339 CFXJSE_Class* CFXJSE_Class::GetClassFromContext(CFXJSE_Context* pContext, | |
340 const CFX_ByteStringC& szName) { | |
341 for (int count = pContext->m_rgClasses.GetSize(), i = 0; i < count; i++) { | |
342 CFXJSE_Class* pClass = pContext->m_rgClasses[i]; | |
343 if (pClass->m_szClassName == szName) { | |
344 return pClass; | |
345 } | |
346 } | |
347 return NULL; | |
348 } | |
OLD | NEW |