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/fxjse/class.h" | |
8 | |
9 #include "xfa/fxjse/cfxjse_arguments.h" | |
10 #include "xfa/fxjse/context.h" | |
11 #include "xfa/fxjse/scope_inline.h" | |
12 #include "xfa/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 static void FXJSE_V8FunctionCallback_Wrapper( | |
27 const v8::FunctionCallbackInfo<v8::Value>& info) { | |
28 const FXJSE_FUNCTION_DESCRIPTOR* lpFunctionInfo = | |
29 static_cast<FXJSE_FUNCTION_DESCRIPTOR*>( | |
30 info.Data().As<v8::External>()->Value()); | |
31 if (!lpFunctionInfo) { | |
32 return; | |
33 } | |
34 CFX_ByteStringC szFunctionName(lpFunctionInfo->name); | |
35 std::unique_ptr<CFXJSE_Value> lpThisValue( | |
36 new CFXJSE_Value(info.GetIsolate())); | |
37 lpThisValue->ForceSetValue(info.This()); | |
38 std::unique_ptr<CFXJSE_Value> lpRetValue(new CFXJSE_Value(info.GetIsolate())); | |
39 CFXJSE_Arguments impl(&info, lpRetValue.get()); | |
40 lpFunctionInfo->callbackProc(lpThisValue.get(), szFunctionName, impl); | |
41 if (!lpRetValue->DirectGetValue().IsEmpty()) { | |
42 info.GetReturnValue().Set(lpRetValue->DirectGetValue()); | |
43 } | |
44 } | |
45 | |
46 static void FXJSE_V8ClassGlobalConstructorCallback_Wrapper( | |
47 const v8::FunctionCallbackInfo<v8::Value>& info) { | |
48 const FXJSE_CLASS_DESCRIPTOR* lpClassDefinition = | |
49 static_cast<FXJSE_CLASS_DESCRIPTOR*>( | |
50 info.Data().As<v8::External>()->Value()); | |
51 if (!lpClassDefinition) { | |
52 return; | |
53 } | |
54 CFX_ByteStringC szFunctionName(lpClassDefinition->name); | |
55 std::unique_ptr<CFXJSE_Value> lpThisValue( | |
56 new CFXJSE_Value(info.GetIsolate())); | |
57 lpThisValue->ForceSetValue(info.This()); | |
58 std::unique_ptr<CFXJSE_Value> lpRetValue(new CFXJSE_Value(info.GetIsolate())); | |
59 CFXJSE_Arguments impl(&info, lpRetValue.get()); | |
60 lpClassDefinition->constructor(lpThisValue.get(), szFunctionName, impl); | |
61 if (!lpRetValue->DirectGetValue().IsEmpty()) { | |
62 info.GetReturnValue().Set(lpRetValue->DirectGetValue()); | |
63 } | |
64 } | |
65 | |
66 static void FXJSE_V8GetterCallback_Wrapper( | |
67 v8::Local<v8::String> property, | |
68 const v8::PropertyCallbackInfo<v8::Value>& info) { | |
69 const FXJSE_PROPERTY_DESCRIPTOR* lpPropertyInfo = | |
70 static_cast<FXJSE_PROPERTY_DESCRIPTOR*>( | |
71 info.Data().As<v8::External>()->Value()); | |
72 if (!lpPropertyInfo) { | |
73 return; | |
74 } | |
75 CFX_ByteStringC szPropertyName(lpPropertyInfo->name); | |
76 std::unique_ptr<CFXJSE_Value> lpThisValue( | |
77 new CFXJSE_Value(info.GetIsolate())); | |
78 std::unique_ptr<CFXJSE_Value> lpPropValue( | |
79 new CFXJSE_Value(info.GetIsolate())); | |
80 lpThisValue->ForceSetValue(info.This()); | |
81 lpPropertyInfo->getProc(lpThisValue.get(), szPropertyName, lpPropValue.get()); | |
82 info.GetReturnValue().Set(lpPropValue->DirectGetValue()); | |
83 } | |
84 | |
85 static void FXJSE_V8SetterCallback_Wrapper( | |
86 v8::Local<v8::String> property, | |
87 v8::Local<v8::Value> value, | |
88 const v8::PropertyCallbackInfo<void>& info) { | |
89 const FXJSE_PROPERTY_DESCRIPTOR* lpPropertyInfo = | |
90 static_cast<FXJSE_PROPERTY_DESCRIPTOR*>( | |
91 info.Data().As<v8::External>()->Value()); | |
92 if (!lpPropertyInfo) { | |
93 return; | |
94 } | |
95 CFX_ByteStringC szPropertyName(lpPropertyInfo->name); | |
96 std::unique_ptr<CFXJSE_Value> lpThisValue( | |
97 new CFXJSE_Value(info.GetIsolate())); | |
98 std::unique_ptr<CFXJSE_Value> lpPropValue( | |
99 new CFXJSE_Value(info.GetIsolate())); | |
100 lpThisValue->ForceSetValue(info.This()); | |
101 lpPropValue->ForceSetValue(value); | |
102 lpPropertyInfo->setProc(lpThisValue.get(), szPropertyName, lpPropValue.get()); | |
103 } | |
104 | |
105 static void FXJSE_V8ConstructorCallback_Wrapper( | |
106 const v8::FunctionCallbackInfo<v8::Value>& info) { | |
107 const FXJSE_CLASS_DESCRIPTOR* lpClassDefinition = | |
108 static_cast<FXJSE_CLASS_DESCRIPTOR*>( | |
109 info.Data().As<v8::External>()->Value()); | |
110 if (!lpClassDefinition) { | |
111 return; | |
112 } | |
113 ASSERT(info.This()->InternalFieldCount()); | |
114 info.This()->SetAlignedPointerInInternalField(0, NULL); | |
115 } | |
116 | |
117 v8::Isolate* CFXJSE_Arguments::GetRuntime() const { | |
118 return m_pRetValue->GetIsolate(); | |
119 } | |
120 | |
121 int32_t CFXJSE_Arguments::GetLength() const { | |
122 return m_pInfo->Length(); | |
123 } | |
124 | |
125 std::unique_ptr<CFXJSE_Value> CFXJSE_Arguments::GetValue(int32_t index) const { | |
126 std::unique_ptr<CFXJSE_Value> lpArgValue( | |
127 new CFXJSE_Value(v8::Isolate::GetCurrent())); | |
128 lpArgValue->ForceSetValue((*m_pInfo)[index]); | |
129 return lpArgValue; | |
130 } | |
131 | |
132 FX_BOOL CFXJSE_Arguments::GetBoolean(int32_t index) const { | |
133 return (*m_pInfo)[index]->BooleanValue(); | |
134 } | |
135 | |
136 int32_t CFXJSE_Arguments::GetInt32(int32_t index) const { | |
137 return static_cast<int32_t>((*m_pInfo)[index]->NumberValue()); | |
138 } | |
139 | |
140 FX_FLOAT CFXJSE_Arguments::GetFloat(int32_t index) const { | |
141 return static_cast<FX_FLOAT>((*m_pInfo)[index]->NumberValue()); | |
142 } | |
143 | |
144 CFX_ByteString CFXJSE_Arguments::GetUTF8String(int32_t index) const { | |
145 v8::Local<v8::String> hString = (*m_pInfo)[index]->ToString(); | |
146 v8::String::Utf8Value szStringVal(hString); | |
147 return CFX_ByteString(*szStringVal); | |
148 } | |
149 | |
150 CFXJSE_HostObject* CFXJSE_Arguments::GetObject(int32_t index, | |
151 CFXJSE_Class* pClass) const { | |
152 v8::Local<v8::Value> hValue = (*m_pInfo)[index]; | |
153 ASSERT(!hValue.IsEmpty()); | |
154 if (!hValue->IsObject()) | |
155 return nullptr; | |
156 return FXJSE_RetrieveObjectBinding(hValue.As<v8::Object>(), pClass); | |
157 } | |
158 | |
159 CFXJSE_Value* CFXJSE_Arguments::GetReturnValue() { | |
160 return m_pRetValue; | |
161 } | |
162 | |
163 static void FXJSE_Context_GlobalObjToString( | |
164 const v8::FunctionCallbackInfo<v8::Value>& info) { | |
165 const FXJSE_CLASS_DESCRIPTOR* lpClass = static_cast<FXJSE_CLASS_DESCRIPTOR*>( | |
166 info.Data().As<v8::External>()->Value()); | |
167 if (!lpClass) { | |
168 return; | |
169 } | |
170 if (info.This() == info.Holder() && lpClass->name) { | |
171 CFX_ByteString szStringVal; | |
172 szStringVal.Format("[object %s]", lpClass->name); | |
173 info.GetReturnValue().Set(v8::String::NewFromUtf8( | |
174 info.GetIsolate(), szStringVal.c_str(), v8::String::kNormalString, | |
175 szStringVal.GetLength())); | |
176 } else { | |
177 v8::Local<v8::String> local_str = | |
178 info.This() | |
179 ->ObjectProtoToString(info.GetIsolate()->GetCurrentContext()) | |
180 .FromMaybe(v8::Local<v8::String>()); | |
181 info.GetReturnValue().Set(local_str); | |
182 } | |
183 } | |
184 | |
185 CFXJSE_Class* CFXJSE_Class::Create( | |
186 CFXJSE_Context* lpContext, | |
187 const FXJSE_CLASS_DESCRIPTOR* lpClassDefinition, | |
188 FX_BOOL bIsJSGlobal) { | |
189 if (!lpContext || !lpClassDefinition) { | |
190 return NULL; | |
191 } | |
192 CFXJSE_Class* pClass = | |
193 GetClassFromContext(lpContext, lpClassDefinition->name); | |
194 if (pClass) { | |
195 return pClass; | |
196 } | |
197 v8::Isolate* pIsolate = lpContext->m_pIsolate; | |
198 pClass = new CFXJSE_Class(lpContext); | |
199 pClass->m_szClassName = lpClassDefinition->name; | |
200 pClass->m_lpClassDefinition = lpClassDefinition; | |
201 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(pIsolate); | |
202 v8::Local<v8::FunctionTemplate> hFunctionTemplate = v8::FunctionTemplate::New( | |
203 pIsolate, bIsJSGlobal ? 0 : FXJSE_V8ConstructorCallback_Wrapper, | |
204 v8::External::New( | |
205 pIsolate, const_cast<FXJSE_CLASS_DESCRIPTOR*>(lpClassDefinition))); | |
206 hFunctionTemplate->SetClassName( | |
207 v8::String::NewFromUtf8(pIsolate, lpClassDefinition->name)); | |
208 hFunctionTemplate->InstanceTemplate()->SetInternalFieldCount(1); | |
209 v8::Local<v8::ObjectTemplate> hObjectTemplate = | |
210 hFunctionTemplate->InstanceTemplate(); | |
211 SetUpNamedPropHandler(pIsolate, hObjectTemplate, lpClassDefinition); | |
212 | |
213 if (lpClassDefinition->propNum) { | |
214 for (int32_t i = 0; i < lpClassDefinition->propNum; i++) { | |
215 hObjectTemplate->SetNativeDataProperty( | |
216 v8::String::NewFromUtf8(pIsolate, | |
217 lpClassDefinition->properties[i].name), | |
218 lpClassDefinition->properties[i].getProc | |
219 ? FXJSE_V8GetterCallback_Wrapper | |
220 : NULL, | |
221 lpClassDefinition->properties[i].setProc | |
222 ? FXJSE_V8SetterCallback_Wrapper | |
223 : NULL, | |
224 v8::External::New(pIsolate, const_cast<FXJSE_PROPERTY_DESCRIPTOR*>( | |
225 lpClassDefinition->properties + i)), | |
226 static_cast<v8::PropertyAttribute>(v8::DontDelete)); | |
227 } | |
228 } | |
229 if (lpClassDefinition->methNum) { | |
230 for (int32_t i = 0; i < lpClassDefinition->methNum; i++) { | |
231 hObjectTemplate->Set( | |
232 v8::String::NewFromUtf8(pIsolate, lpClassDefinition->methods[i].name), | |
233 v8::FunctionTemplate::New( | |
234 pIsolate, FXJSE_V8FunctionCallback_Wrapper, | |
235 v8::External::New(pIsolate, | |
236 const_cast<FXJSE_FUNCTION_DESCRIPTOR*>( | |
237 lpClassDefinition->methods + i))), | |
238 static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete)); | |
239 } | |
240 } | |
241 if (lpClassDefinition->constructor) { | |
242 if (bIsJSGlobal) { | |
243 hObjectTemplate->Set( | |
244 v8::String::NewFromUtf8(pIsolate, lpClassDefinition->name), | |
245 v8::FunctionTemplate::New( | |
246 pIsolate, FXJSE_V8ClassGlobalConstructorCallback_Wrapper, | |
247 v8::External::New(pIsolate, const_cast<FXJSE_CLASS_DESCRIPTOR*>( | |
248 lpClassDefinition))), | |
249 static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete)); | |
250 } else { | |
251 v8::Local<v8::Context> hLocalContext = | |
252 v8::Local<v8::Context>::New(pIsolate, lpContext->m_hContext); | |
253 FXJSE_GetGlobalObjectFromContext(hLocalContext) | |
254 ->Set(v8::String::NewFromUtf8(pIsolate, lpClassDefinition->name), | |
255 v8::Function::New( | |
256 pIsolate, FXJSE_V8ClassGlobalConstructorCallback_Wrapper, | |
257 v8::External::New(pIsolate, | |
258 const_cast<FXJSE_CLASS_DESCRIPTOR*>( | |
259 lpClassDefinition)))); | |
260 } | |
261 } | |
262 if (bIsJSGlobal) { | |
263 hObjectTemplate->Set( | |
264 v8::String::NewFromUtf8(pIsolate, "toString"), | |
265 v8::FunctionTemplate::New( | |
266 pIsolate, FXJSE_Context_GlobalObjToString, | |
267 v8::External::New(pIsolate, const_cast<FXJSE_CLASS_DESCRIPTOR*>( | |
268 lpClassDefinition)))); | |
269 } | |
270 pClass->m_hTemplate.Reset(lpContext->m_pIsolate, hFunctionTemplate); | |
271 lpContext->m_rgClasses.push_back(std::unique_ptr<CFXJSE_Class>(pClass)); | |
272 return pClass; | |
273 } | |
274 | |
275 CFXJSE_Class* CFXJSE_Class::GetClassFromContext(CFXJSE_Context* pContext, | |
276 const CFX_ByteStringC& szName) { | |
277 for (const auto& pClass : pContext->m_rgClasses) { | |
278 if (pClass->m_szClassName == szName) | |
279 return pClass.get(); | |
280 } | |
281 return nullptr; | |
282 } | |
OLD | NEW |