Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1324)

Side by Side Diff: fxjs/cfxjse_class.cpp

Issue 2136213002: Rename fxjse/ to fxjs/ update files to match class names. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 PDFium Authors. All rights reserved.
Tom Sepez 2016/07/11 21:03:27 Is there a way to avoid losing the history on this
dsinclair 2016/07/12 13:38:30 Looks like similiarity=40 worked.
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 "fxjs/include/cfxjse_class.h"
8
9 #include "fxjs/include/cfxjse_context.h"
10 #include "fxjs/include/cfxjse_value.h"
11
12 namespace {
13
14 void V8FunctionCallback_Wrapper(
15 const v8::FunctionCallbackInfo<v8::Value>& info) {
16 const FXJSE_FUNCTION_DESCRIPTOR* lpFunctionInfo =
17 static_cast<FXJSE_FUNCTION_DESCRIPTOR*>(
18 info.Data().As<v8::External>()->Value());
19 if (!lpFunctionInfo)
20 return;
21
22 CFX_ByteStringC szFunctionName(lpFunctionInfo->name);
23 std::unique_ptr<CFXJSE_Value> lpThisValue(
24 new CFXJSE_Value(info.GetIsolate()));
25 lpThisValue->ForceSetValue(info.This());
26 std::unique_ptr<CFXJSE_Value> lpRetValue(new CFXJSE_Value(info.GetIsolate()));
27 CFXJSE_Arguments impl(&info, lpRetValue.get());
28 lpFunctionInfo->callbackProc(lpThisValue.get(), szFunctionName, impl);
29 if (!lpRetValue->DirectGetValue().IsEmpty())
30 info.GetReturnValue().Set(lpRetValue->DirectGetValue());
31 }
32
33 void V8ClassGlobalConstructorCallback_Wrapper(
34 const v8::FunctionCallbackInfo<v8::Value>& info) {
35 const FXJSE_CLASS_DESCRIPTOR* lpClassDefinition =
36 static_cast<FXJSE_CLASS_DESCRIPTOR*>(
37 info.Data().As<v8::External>()->Value());
38 if (!lpClassDefinition)
39 return;
40
41 CFX_ByteStringC szFunctionName(lpClassDefinition->name);
42 std::unique_ptr<CFXJSE_Value> lpThisValue(
43 new CFXJSE_Value(info.GetIsolate()));
44 lpThisValue->ForceSetValue(info.This());
45 std::unique_ptr<CFXJSE_Value> lpRetValue(new CFXJSE_Value(info.GetIsolate()));
46 CFXJSE_Arguments impl(&info, lpRetValue.get());
47 lpClassDefinition->constructor(lpThisValue.get(), szFunctionName, impl);
48 if (!lpRetValue->DirectGetValue().IsEmpty())
49 info.GetReturnValue().Set(lpRetValue->DirectGetValue());
50 }
51
52 void V8GetterCallback_Wrapper(v8::Local<v8::String> property,
53 const v8::PropertyCallbackInfo<v8::Value>& info) {
54 const FXJSE_PROPERTY_DESCRIPTOR* lpPropertyInfo =
55 static_cast<FXJSE_PROPERTY_DESCRIPTOR*>(
56 info.Data().As<v8::External>()->Value());
57 if (!lpPropertyInfo)
58 return;
59
60 CFX_ByteStringC szPropertyName(lpPropertyInfo->name);
61 std::unique_ptr<CFXJSE_Value> lpThisValue(
62 new CFXJSE_Value(info.GetIsolate()));
63 std::unique_ptr<CFXJSE_Value> lpPropValue(
64 new CFXJSE_Value(info.GetIsolate()));
65 lpThisValue->ForceSetValue(info.This());
66 lpPropertyInfo->getProc(lpThisValue.get(), szPropertyName, lpPropValue.get());
67 info.GetReturnValue().Set(lpPropValue->DirectGetValue());
68 }
69
70 void V8SetterCallback_Wrapper(v8::Local<v8::String> property,
71 v8::Local<v8::Value> value,
72 const v8::PropertyCallbackInfo<void>& info) {
73 const FXJSE_PROPERTY_DESCRIPTOR* lpPropertyInfo =
74 static_cast<FXJSE_PROPERTY_DESCRIPTOR*>(
75 info.Data().As<v8::External>()->Value());
76 if (!lpPropertyInfo)
77 return;
78
79 CFX_ByteStringC szPropertyName(lpPropertyInfo->name);
80 std::unique_ptr<CFXJSE_Value> lpThisValue(
81 new CFXJSE_Value(info.GetIsolate()));
82 std::unique_ptr<CFXJSE_Value> lpPropValue(
83 new CFXJSE_Value(info.GetIsolate()));
84 lpThisValue->ForceSetValue(info.This());
85 lpPropValue->ForceSetValue(value);
86 lpPropertyInfo->setProc(lpThisValue.get(), szPropertyName, lpPropValue.get());
87 }
88
89 void V8ConstructorCallback_Wrapper(
90 const v8::FunctionCallbackInfo<v8::Value>& info) {
91 if (!info.IsConstructCall())
92 return;
93
94 const FXJSE_CLASS_DESCRIPTOR* lpClassDefinition =
95 static_cast<FXJSE_CLASS_DESCRIPTOR*>(
96 info.Data().As<v8::External>()->Value());
97 if (!lpClassDefinition)
98 return;
99
100 ASSERT(info.This()->InternalFieldCount());
101 info.This()->SetAlignedPointerInInternalField(0, nullptr);
102 }
103
104 void Context_GlobalObjToString(
105 const v8::FunctionCallbackInfo<v8::Value>& info) {
106 const FXJSE_CLASS_DESCRIPTOR* lpClass = static_cast<FXJSE_CLASS_DESCRIPTOR*>(
107 info.Data().As<v8::External>()->Value());
108 if (!lpClass)
109 return;
110
111 if (info.This() == info.Holder() && lpClass->name) {
112 CFX_ByteString szStringVal;
113 szStringVal.Format("[object %s]", lpClass->name);
114 info.GetReturnValue().Set(v8::String::NewFromUtf8(
115 info.GetIsolate(), szStringVal.c_str(), v8::String::kNormalString,
116 szStringVal.GetLength()));
117 return;
118 }
119 v8::Local<v8::String> local_str =
120 info.This()
121 ->ObjectProtoToString(info.GetIsolate()->GetCurrentContext())
122 .FromMaybe(v8::Local<v8::String>());
123 info.GetReturnValue().Set(local_str);
124 }
125
126 void DynPropGetterAdapter_MethodCallback(
127 const v8::FunctionCallbackInfo<v8::Value>& info) {
128 v8::Local<v8::Object> hCallBackInfo = info.Data().As<v8::Object>();
129 FXJSE_CLASS_DESCRIPTOR* lpClass = static_cast<FXJSE_CLASS_DESCRIPTOR*>(
130 hCallBackInfo->GetAlignedPointerFromInternalField(0));
131 v8::Local<v8::String> hPropName =
132 hCallBackInfo->GetInternalField(1).As<v8::String>();
133 ASSERT(lpClass && !hPropName.IsEmpty());
134 v8::String::Utf8Value szPropName(hPropName);
135 CFX_ByteStringC szFxPropName = *szPropName;
136 std::unique_ptr<CFXJSE_Value> lpThisValue(
137 new CFXJSE_Value(info.GetIsolate()));
138 lpThisValue->ForceSetValue(info.This());
139 std::unique_ptr<CFXJSE_Value> lpRetValue(new CFXJSE_Value(info.GetIsolate()));
140 CFXJSE_Arguments impl(&info, lpRetValue.get());
141 lpClass->dynMethodCall(lpThisValue.get(), szFxPropName, impl);
142 if (!lpRetValue->DirectGetValue().IsEmpty())
143 info.GetReturnValue().Set(lpRetValue->DirectGetValue());
144 }
145
146 void DynPropGetterAdapter(const FXJSE_CLASS_DESCRIPTOR* lpClass,
147 CFXJSE_Value* pObject,
148 const CFX_ByteStringC& szPropName,
149 CFXJSE_Value* pValue) {
150 ASSERT(lpClass);
151 int32_t nPropType =
152 lpClass->dynPropTypeGetter == nullptr
153 ? FXJSE_ClassPropType_Property
154 : lpClass->dynPropTypeGetter(pObject, szPropName, FALSE);
155 if (nPropType == FXJSE_ClassPropType_Property) {
156 if (lpClass->dynPropGetter)
157 lpClass->dynPropGetter(pObject, szPropName, pValue);
158 } else if (nPropType == FXJSE_ClassPropType_Method) {
159 if (lpClass->dynMethodCall && pValue) {
160 v8::Isolate* pIsolate = pValue->GetIsolate();
161 v8::HandleScope hscope(pIsolate);
162 v8::Local<v8::ObjectTemplate> hCallBackInfoTemplate =
163 v8::ObjectTemplate::New(pIsolate);
164 hCallBackInfoTemplate->SetInternalFieldCount(2);
165 v8::Local<v8::Object> hCallBackInfo =
166 hCallBackInfoTemplate->NewInstance();
167 hCallBackInfo->SetAlignedPointerInInternalField(
168 0, const_cast<FXJSE_CLASS_DESCRIPTOR*>(lpClass));
169 hCallBackInfo->SetInternalField(
170 1, v8::String::NewFromUtf8(
171 pIsolate, reinterpret_cast<const char*>(szPropName.raw_str()),
172 v8::String::kNormalString, szPropName.GetLength()));
173 pValue->ForceSetValue(
174 v8::Function::New(pValue->GetIsolate()->GetCurrentContext(),
175 DynPropGetterAdapter_MethodCallback, hCallBackInfo,
176 0, v8::ConstructorBehavior::kThrow)
177 .ToLocalChecked());
178 }
179 }
180 }
181
182 void DynPropSetterAdapter(const FXJSE_CLASS_DESCRIPTOR* lpClass,
183 CFXJSE_Value* pObject,
184 const CFX_ByteStringC& szPropName,
185 CFXJSE_Value* pValue) {
186 ASSERT(lpClass);
187 int32_t nPropType =
188 lpClass->dynPropTypeGetter == nullptr
189 ? FXJSE_ClassPropType_Property
190 : lpClass->dynPropTypeGetter(pObject, szPropName, FALSE);
191 if (nPropType != FXJSE_ClassPropType_Method) {
192 if (lpClass->dynPropSetter)
193 lpClass->dynPropSetter(pObject, szPropName, pValue);
194 }
195 }
196
197 FX_BOOL DynPropQueryAdapter(const FXJSE_CLASS_DESCRIPTOR* lpClass,
198 CFXJSE_Value* pObject,
199 const CFX_ByteStringC& szPropName) {
200 ASSERT(lpClass);
201 int32_t nPropType =
202 lpClass->dynPropTypeGetter == nullptr
203 ? FXJSE_ClassPropType_Property
204 : lpClass->dynPropTypeGetter(pObject, szPropName, TRUE);
205 return nPropType != FXJSE_ClassPropType_None;
206 }
207
208 FX_BOOL DynPropDeleterAdapter(const FXJSE_CLASS_DESCRIPTOR* lpClass,
209 CFXJSE_Value* pObject,
210 const CFX_ByteStringC& szPropName) {
211 ASSERT(lpClass);
212 int32_t nPropType =
213 lpClass->dynPropTypeGetter == nullptr
214 ? FXJSE_ClassPropType_Property
215 : lpClass->dynPropTypeGetter(pObject, szPropName, FALSE);
216 if (nPropType != FXJSE_ClassPropType_Method) {
217 if (lpClass->dynPropDeleter)
218 return lpClass->dynPropDeleter(pObject, szPropName);
219 return nPropType == FXJSE_ClassPropType_Property ? FALSE : TRUE;
220 }
221 return FALSE;
222 }
223
224 void NamedPropertyQueryCallback(
225 v8::Local<v8::Name> property,
226 const v8::PropertyCallbackInfo<v8::Integer>& info) {
227 v8::Local<v8::Object> thisObject = info.This();
228 const FXJSE_CLASS_DESCRIPTOR* lpClass = static_cast<FXJSE_CLASS_DESCRIPTOR*>(
229 info.Data().As<v8::External>()->Value());
230 v8::Isolate* pIsolate = info.GetIsolate();
231 v8::HandleScope scope(pIsolate);
232 v8::String::Utf8Value szPropName(property);
233 CFX_ByteStringC szFxPropName(*szPropName, szPropName.length());
234 std::unique_ptr<CFXJSE_Value> lpThisValue(
235 new CFXJSE_Value(info.GetIsolate()));
236 lpThisValue->ForceSetValue(thisObject);
237 if (DynPropQueryAdapter(lpClass, lpThisValue.get(), szFxPropName)) {
238 info.GetReturnValue().Set(v8::DontDelete);
239 return;
240 }
241 const int32_t iV8Absent = 64;
242 info.GetReturnValue().Set(iV8Absent);
243 }
244
245 void NamedPropertyDeleterCallback(
246 v8::Local<v8::Name> property,
247 const v8::PropertyCallbackInfo<v8::Boolean>& info) {
248 v8::Local<v8::Object> thisObject = info.This();
249 const FXJSE_CLASS_DESCRIPTOR* lpClass = static_cast<FXJSE_CLASS_DESCRIPTOR*>(
250 info.Data().As<v8::External>()->Value());
251 v8::Isolate* pIsolate = info.GetIsolate();
252 v8::HandleScope scope(pIsolate);
253 v8::String::Utf8Value szPropName(property);
254 CFX_ByteStringC szFxPropName(*szPropName, szPropName.length());
255 std::unique_ptr<CFXJSE_Value> lpThisValue(
256 new CFXJSE_Value(info.GetIsolate()));
257 lpThisValue->ForceSetValue(thisObject);
258 info.GetReturnValue().Set(
259 !!DynPropDeleterAdapter(lpClass, lpThisValue.get(), szFxPropName));
260 }
261
262 void NamedPropertyGetterCallback(
263 v8::Local<v8::Name> property,
264 const v8::PropertyCallbackInfo<v8::Value>& info) {
265 v8::Local<v8::Object> thisObject = info.This();
266 const FXJSE_CLASS_DESCRIPTOR* lpClass = static_cast<FXJSE_CLASS_DESCRIPTOR*>(
267 info.Data().As<v8::External>()->Value());
268 v8::String::Utf8Value szPropName(property);
269 CFX_ByteStringC szFxPropName(*szPropName, szPropName.length());
270 std::unique_ptr<CFXJSE_Value> lpThisValue(
271 new CFXJSE_Value(info.GetIsolate()));
272 lpThisValue->ForceSetValue(thisObject);
273 std::unique_ptr<CFXJSE_Value> lpNewValue(new CFXJSE_Value(info.GetIsolate()));
274 DynPropGetterAdapter(lpClass, lpThisValue.get(), szFxPropName,
275 lpNewValue.get());
276 info.GetReturnValue().Set(lpNewValue->DirectGetValue());
277 }
278
279 void NamedPropertySetterCallback(
280 v8::Local<v8::Name> property,
281 v8::Local<v8::Value> value,
282 const v8::PropertyCallbackInfo<v8::Value>& info) {
283 v8::Local<v8::Object> thisObject = info.This();
284 const FXJSE_CLASS_DESCRIPTOR* lpClass = static_cast<FXJSE_CLASS_DESCRIPTOR*>(
285 info.Data().As<v8::External>()->Value());
286 v8::String::Utf8Value szPropName(property);
287 CFX_ByteStringC szFxPropName(*szPropName, szPropName.length());
288 std::unique_ptr<CFXJSE_Value> lpThisValue(
289 new CFXJSE_Value(info.GetIsolate()));
290 lpThisValue->ForceSetValue(thisObject);
291
292 CFXJSE_Value* lpNewValue = new CFXJSE_Value(info.GetIsolate());
293 lpNewValue->ForceSetValue(value);
294 DynPropSetterAdapter(lpClass, lpThisValue.get(), szFxPropName, lpNewValue);
295 info.GetReturnValue().Set(value);
296 }
297
298 void NamedPropertyEnumeratorCallback(
299 const v8::PropertyCallbackInfo<v8::Array>& info) {
300 const FXJSE_CLASS_DESCRIPTOR* lpClass = static_cast<FXJSE_CLASS_DESCRIPTOR*>(
301 info.Data().As<v8::External>()->Value());
302 v8::Isolate* pIsolate = info.GetIsolate();
303 v8::Local<v8::Array> newArray = v8::Array::New(pIsolate, lpClass->propNum);
304 for (int i = 0; i < lpClass->propNum; i++) {
305 newArray->Set(
306 i, v8::String::NewFromUtf8(pIsolate, lpClass->properties[i].name));
307 }
308 info.GetReturnValue().Set(newArray);
309 }
310
311 } // namespace
312
313 // static
314 CFXJSE_Class* CFXJSE_Class::Create(
315 CFXJSE_Context* lpContext,
316 const FXJSE_CLASS_DESCRIPTOR* lpClassDefinition,
317 FX_BOOL bIsJSGlobal) {
318 if (!lpContext || !lpClassDefinition)
319 return nullptr;
320
321 CFXJSE_Class* pClass =
322 GetClassFromContext(lpContext, lpClassDefinition->name);
323 if (pClass)
324 return pClass;
325
326 v8::Isolate* pIsolate = lpContext->m_pIsolate;
327 pClass = new CFXJSE_Class(lpContext);
328 pClass->m_szClassName = lpClassDefinition->name;
329 pClass->m_lpClassDefinition = lpClassDefinition;
330 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(pIsolate);
331 v8::Local<v8::FunctionTemplate> hFunctionTemplate = v8::FunctionTemplate::New(
332 pIsolate, bIsJSGlobal ? 0 : V8ConstructorCallback_Wrapper,
333 v8::External::New(
334 pIsolate, const_cast<FXJSE_CLASS_DESCRIPTOR*>(lpClassDefinition)));
335 hFunctionTemplate->SetClassName(
336 v8::String::NewFromUtf8(pIsolate, lpClassDefinition->name));
337 hFunctionTemplate->InstanceTemplate()->SetInternalFieldCount(1);
338 v8::Local<v8::ObjectTemplate> hObjectTemplate =
339 hFunctionTemplate->InstanceTemplate();
340 SetUpNamedPropHandler(pIsolate, hObjectTemplate, lpClassDefinition);
341
342 if (lpClassDefinition->propNum) {
343 for (int32_t i = 0; i < lpClassDefinition->propNum; i++) {
344 hObjectTemplate->SetNativeDataProperty(
345 v8::String::NewFromUtf8(pIsolate,
346 lpClassDefinition->properties[i].name),
347 lpClassDefinition->properties[i].getProc ? V8GetterCallback_Wrapper
348 : nullptr,
349 lpClassDefinition->properties[i].setProc ? V8SetterCallback_Wrapper
350 : nullptr,
351 v8::External::New(pIsolate, const_cast<FXJSE_PROPERTY_DESCRIPTOR*>(
352 lpClassDefinition->properties + i)),
353 static_cast<v8::PropertyAttribute>(v8::DontDelete));
354 }
355 }
356 if (lpClassDefinition->methNum) {
357 for (int32_t i = 0; i < lpClassDefinition->methNum; i++) {
358 v8::Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New(
359 pIsolate, V8FunctionCallback_Wrapper,
360 v8::External::New(pIsolate, const_cast<FXJSE_FUNCTION_DESCRIPTOR*>(
361 lpClassDefinition->methods + i)));
362 fun->RemovePrototype();
363 hObjectTemplate->Set(
364 v8::String::NewFromUtf8(pIsolate, lpClassDefinition->methods[i].name),
365 fun,
366 static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete));
367 }
368 }
369 if (lpClassDefinition->constructor) {
370 if (bIsJSGlobal) {
371 hObjectTemplate->Set(
372 v8::String::NewFromUtf8(pIsolate, lpClassDefinition->name),
373 v8::FunctionTemplate::New(
374 pIsolate, V8ClassGlobalConstructorCallback_Wrapper,
375 v8::External::New(pIsolate, const_cast<FXJSE_CLASS_DESCRIPTOR*>(
376 lpClassDefinition))),
377 static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete));
378 } else {
379 v8::Local<v8::Context> hLocalContext =
380 v8::Local<v8::Context>::New(pIsolate, lpContext->m_hContext);
381 FXJSE_GetGlobalObjectFromContext(hLocalContext)
382 ->Set(v8::String::NewFromUtf8(pIsolate, lpClassDefinition->name),
383 v8::Function::New(
384 pIsolate, V8ClassGlobalConstructorCallback_Wrapper,
385 v8::External::New(pIsolate,
386 const_cast<FXJSE_CLASS_DESCRIPTOR*>(
387 lpClassDefinition))));
388 }
389 }
390 if (bIsJSGlobal) {
391 v8::Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New(
392 pIsolate, Context_GlobalObjToString,
393 v8::External::New(
394 pIsolate, const_cast<FXJSE_CLASS_DESCRIPTOR*>(lpClassDefinition)));
395 fun->RemovePrototype();
396 hObjectTemplate->Set(v8::String::NewFromUtf8(pIsolate, "toString"), fun);
397 }
398 pClass->m_hTemplate.Reset(lpContext->m_pIsolate, hFunctionTemplate);
399 lpContext->m_rgClasses.push_back(std::unique_ptr<CFXJSE_Class>(pClass));
400 return pClass;
401 }
402
403 // static
404 CFXJSE_Class* CFXJSE_Class::GetClassFromContext(CFXJSE_Context* pContext,
405 const CFX_ByteStringC& szName) {
406 for (const auto& pClass : pContext->m_rgClasses) {
407 if (pClass->m_szClassName == szName)
408 return pClass.get();
409 }
410 return nullptr;
411 }
412
413 // static
414 void CFXJSE_Class::SetUpNamedPropHandler(
415 v8::Isolate* pIsolate,
416 v8::Local<v8::ObjectTemplate>& hObjectTemplate,
417 const FXJSE_CLASS_DESCRIPTOR* lpClassDefinition) {
418 v8::NamedPropertyHandlerConfiguration configuration(
419 lpClassDefinition->dynPropGetter ? NamedPropertyGetterCallback : 0,
420 lpClassDefinition->dynPropSetter ? NamedPropertySetterCallback : 0,
421 lpClassDefinition->dynPropTypeGetter ? NamedPropertyQueryCallback : 0,
422 lpClassDefinition->dynPropDeleter ? NamedPropertyDeleterCallback : 0,
423 NamedPropertyEnumeratorCallback,
424 v8::External::New(pIsolate,
425 const_cast<FXJSE_CLASS_DESCRIPTOR*>(lpClassDefinition)),
426 v8::PropertyHandlerFlags::kNonMasking);
427 hObjectTemplate->SetHandler(configuration);
428 }
429
430 CFXJSE_Class::CFXJSE_Class(CFXJSE_Context* lpContext)
431 : m_lpClassDefinition(nullptr), m_pContext(lpContext) {}
432
433 CFXJSE_Class::~CFXJSE_Class() {}
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698