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 #include "xfa/src/fxjse/value.h" | |
9 | |
10 static void FXJSE_DynPropGetterAdapter_MethodCallback( | |
11 const v8::FunctionCallbackInfo<v8::Value>& info) { | |
12 v8::Local<v8::Object> hCallBackInfo = info.Data().As<v8::Object>(); | |
13 FXJSE_CLASS* lpClass = static_cast<FXJSE_CLASS*>( | |
14 hCallBackInfo->GetAlignedPointerFromInternalField(0)); | |
15 v8::Local<v8::String> hPropName = | |
16 hCallBackInfo->GetInternalField(1).As<v8::String>(); | |
17 ASSERT(lpClass && !hPropName.IsEmpty()); | |
18 v8::String::Utf8Value szPropName(hPropName); | |
19 CFX_ByteStringC szFxPropName = *szPropName; | |
20 CFXJSE_Value* lpThisValue = CFXJSE_Value::Create(info.GetIsolate()); | |
21 lpThisValue->ForceSetValue(info.This()); | |
22 CFXJSE_Value* lpRetValue = CFXJSE_Value::Create(info.GetIsolate()); | |
23 CFXJSE_ArgumentsImpl impl = {&info, lpRetValue}; | |
24 lpClass->dynMethodCall(reinterpret_cast<FXJSE_HOBJECT>(lpThisValue), | |
25 szFxPropName, | |
26 reinterpret_cast<CFXJSE_Arguments&>(impl)); | |
27 if (!lpRetValue->DirectGetValue().IsEmpty()) { | |
28 info.GetReturnValue().Set(lpRetValue->DirectGetValue()); | |
29 } | |
30 delete lpRetValue; | |
31 lpRetValue = nullptr; | |
32 delete lpThisValue; | |
33 lpThisValue = nullptr; | |
34 } | |
35 | |
36 static void FXJSE_DynPropGetterAdapter(const FXJSE_CLASS* lpClass, | |
37 FXJSE_HOBJECT hObject, | |
38 const CFX_ByteStringC& szPropName, | |
39 FXJSE_HVALUE hValue) { | |
40 ASSERT(lpClass); | |
41 int32_t nPropType = | |
42 lpClass->dynPropTypeGetter == nullptr | |
43 ? FXJSE_ClassPropType_Property | |
44 : lpClass->dynPropTypeGetter(hObject, szPropName, FALSE); | |
45 if (nPropType == FXJSE_ClassPropType_Property) { | |
46 if (lpClass->dynPropGetter) { | |
47 lpClass->dynPropGetter(hObject, szPropName, hValue); | |
48 } | |
49 } else if (nPropType == FXJSE_ClassPropType_Method) { | |
50 if (lpClass->dynMethodCall && hValue) { | |
51 CFXJSE_Value* lpValue = reinterpret_cast<CFXJSE_Value*>(hValue); | |
52 v8::Isolate* pIsolate = lpValue->GetIsolate(); | |
53 v8::HandleScope hscope(pIsolate); | |
54 v8::Local<v8::ObjectTemplate> hCallBackInfoTemplate = | |
55 v8::ObjectTemplate::New(pIsolate); | |
56 hCallBackInfoTemplate->SetInternalFieldCount(2); | |
57 v8::Local<v8::Object> hCallBackInfo = | |
58 hCallBackInfoTemplate->NewInstance(); | |
59 hCallBackInfo->SetAlignedPointerInInternalField( | |
60 0, const_cast<FXJSE_CLASS*>(lpClass)); | |
61 hCallBackInfo->SetInternalField( | |
62 1, v8::String::NewFromUtf8( | |
63 pIsolate, reinterpret_cast<const char*>(szPropName.GetPtr()), | |
64 v8::String::kNormalString, szPropName.GetLength())); | |
65 lpValue->ForceSetValue(v8::Function::New( | |
66 lpValue->GetIsolate(), FXJSE_DynPropGetterAdapter_MethodCallback, | |
67 hCallBackInfo)); | |
68 } | |
69 } | |
70 } | |
71 | |
72 static void FXJSE_DynPropSetterAdapter(const FXJSE_CLASS* lpClass, | |
73 FXJSE_HOBJECT hObject, | |
74 const CFX_ByteStringC& szPropName, | |
75 FXJSE_HVALUE hValue) { | |
76 ASSERT(lpClass); | |
77 int32_t nPropType = | |
78 lpClass->dynPropTypeGetter == nullptr | |
79 ? FXJSE_ClassPropType_Property | |
80 : lpClass->dynPropTypeGetter(hObject, szPropName, FALSE); | |
81 if (nPropType != FXJSE_ClassPropType_Method) { | |
82 if (lpClass->dynPropSetter) { | |
83 lpClass->dynPropSetter(hObject, szPropName, hValue); | |
84 } | |
85 } | |
86 } | |
87 | |
88 static FX_BOOL FXJSE_DynPropQueryAdapter(const FXJSE_CLASS* lpClass, | |
89 FXJSE_HOBJECT hObject, | |
90 const CFX_ByteStringC& szPropName) { | |
91 ASSERT(lpClass); | |
92 int32_t nPropType = | |
93 lpClass->dynPropTypeGetter == nullptr | |
94 ? FXJSE_ClassPropType_Property | |
95 : lpClass->dynPropTypeGetter(hObject, szPropName, TRUE); | |
96 return nPropType != FXJSE_ClassPropType_None; | |
97 } | |
98 | |
99 static FX_BOOL FXJSE_DynPropDeleterAdapter(const FXJSE_CLASS* lpClass, | |
100 FXJSE_HOBJECT hObject, | |
101 const CFX_ByteStringC& szPropName) { | |
102 ASSERT(lpClass); | |
103 int32_t nPropType = | |
104 lpClass->dynPropTypeGetter == nullptr | |
105 ? FXJSE_ClassPropType_Property | |
106 : lpClass->dynPropTypeGetter(hObject, szPropName, FALSE); | |
107 if (nPropType != FXJSE_ClassPropType_Method) { | |
108 if (lpClass->dynPropDeleter) { | |
109 return lpClass->dynPropDeleter(hObject, szPropName); | |
110 } else { | |
111 return nPropType == FXJSE_ClassPropType_Property ? FALSE : TRUE; | |
112 } | |
113 } | |
114 return FALSE; | |
115 } | |
116 | |
117 static void FXJSE_V8_GenericNamedPropertyQueryCallback( | |
118 v8::Local<v8::Name> property, | |
119 const v8::PropertyCallbackInfo<v8::Integer>& info) { | |
120 v8::Local<v8::Object> thisObject = info.This(); | |
121 const FXJSE_CLASS* lpClass = | |
122 static_cast<FXJSE_CLASS*>(info.Data().As<v8::External>()->Value()); | |
123 v8::Isolate* pIsolate = info.GetIsolate(); | |
124 v8::HandleScope scope(pIsolate); | |
125 v8::String::Utf8Value szPropName(property); | |
126 CFX_ByteStringC szFxPropName(*szPropName, szPropName.length()); | |
127 CFXJSE_Value* lpThisValue = CFXJSE_Value::Create(info.GetIsolate()); | |
128 lpThisValue->ForceSetValue(thisObject); | |
129 if (FXJSE_DynPropQueryAdapter(lpClass, | |
130 reinterpret_cast<FXJSE_HOBJECT>(lpThisValue), | |
131 szFxPropName)) { | |
132 info.GetReturnValue().Set(v8::DontDelete); | |
133 } else { | |
134 const int32_t iV8Absent = 64; | |
135 info.GetReturnValue().Set(iV8Absent); | |
136 } | |
137 delete lpThisValue; | |
138 lpThisValue = nullptr; | |
139 } | |
140 | |
141 static void FXJSE_V8_GenericNamedPropertyDeleterCallback( | |
142 v8::Local<v8::Name> property, | |
143 const v8::PropertyCallbackInfo<v8::Boolean>& info) { | |
144 v8::Local<v8::Object> thisObject = info.This(); | |
145 const FXJSE_CLASS* lpClass = | |
146 static_cast<FXJSE_CLASS*>(info.Data().As<v8::External>()->Value()); | |
147 v8::Isolate* pIsolate = info.GetIsolate(); | |
148 v8::HandleScope scope(pIsolate); | |
149 v8::String::Utf8Value szPropName(property); | |
150 CFX_ByteStringC szFxPropName(*szPropName, szPropName.length()); | |
151 CFXJSE_Value* lpThisValue = CFXJSE_Value::Create(info.GetIsolate()); | |
152 lpThisValue->ForceSetValue(thisObject); | |
153 info.GetReturnValue().Set( | |
154 FXJSE_DynPropDeleterAdapter( | |
155 lpClass, reinterpret_cast<FXJSE_HOBJECT>(lpThisValue), szFxPropName) | |
156 ? true | |
157 : false); | |
158 delete lpThisValue; | |
159 lpThisValue = nullptr; | |
160 } | |
161 | |
162 static void FXJSE_V8_GenericNamedPropertyGetterCallback( | |
163 v8::Local<v8::Name> property, | |
164 const v8::PropertyCallbackInfo<v8::Value>& info) { | |
165 v8::Local<v8::Object> thisObject = info.This(); | |
166 const FXJSE_CLASS* lpClass = | |
167 static_cast<FXJSE_CLASS*>(info.Data().As<v8::External>()->Value()); | |
168 v8::String::Utf8Value szPropName(property); | |
169 CFX_ByteStringC szFxPropName(*szPropName, szPropName.length()); | |
170 CFXJSE_Value* lpThisValue = CFXJSE_Value::Create(info.GetIsolate()); | |
171 lpThisValue->ForceSetValue(thisObject); | |
172 CFXJSE_Value* lpNewValue = CFXJSE_Value::Create(info.GetIsolate()); | |
173 FXJSE_DynPropGetterAdapter( | |
174 lpClass, reinterpret_cast<FXJSE_HOBJECT>(lpThisValue), szFxPropName, | |
175 reinterpret_cast<FXJSE_HVALUE>(lpNewValue)); | |
176 info.GetReturnValue().Set(lpNewValue->DirectGetValue()); | |
177 delete lpThisValue; | |
178 lpThisValue = nullptr; | |
179 } | |
180 | |
181 static void FXJSE_V8_GenericNamedPropertySetterCallback( | |
182 v8::Local<v8::Name> property, | |
183 v8::Local<v8::Value> value, | |
184 const v8::PropertyCallbackInfo<v8::Value>& info) { | |
185 v8::Local<v8::Object> thisObject = info.This(); | |
186 const FXJSE_CLASS* lpClass = | |
187 static_cast<FXJSE_CLASS*>(info.Data().As<v8::External>()->Value()); | |
188 v8::String::Utf8Value szPropName(property); | |
189 CFX_ByteStringC szFxPropName(*szPropName, szPropName.length()); | |
190 CFXJSE_Value* lpThisValue = CFXJSE_Value::Create(info.GetIsolate()); | |
191 lpThisValue->ForceSetValue(thisObject); | |
192 CFXJSE_Value* lpNewValue = CFXJSE_Value::Create(info.GetIsolate()); | |
193 lpNewValue->ForceSetValue(value); | |
194 FXJSE_DynPropSetterAdapter( | |
195 lpClass, reinterpret_cast<FXJSE_HOBJECT>(lpThisValue), szFxPropName, | |
196 reinterpret_cast<FXJSE_HVALUE>(lpNewValue)); | |
197 info.GetReturnValue().Set(value); | |
198 delete lpThisValue; | |
199 lpThisValue = nullptr; | |
200 } | |
201 | |
202 static void FXJSE_V8_GenericNamedPropertyEnumeratorCallback( | |
203 const v8::PropertyCallbackInfo<v8::Array>& info) { | |
204 const FXJSE_CLASS* lpClass = | |
205 static_cast<FXJSE_CLASS*>(info.Data().As<v8::External>()->Value()); | |
206 v8::Isolate* pIsolate = info.GetIsolate(); | |
207 v8::Local<v8::Array> newArray = v8::Array::New(pIsolate, lpClass->propNum); | |
208 for (int i = 0; i < lpClass->propNum; i++) { | |
209 newArray->Set( | |
210 i, v8::String::NewFromUtf8(pIsolate, lpClass->properties[i].name)); | |
211 } | |
212 info.GetReturnValue().Set(newArray); | |
213 } | |
214 | |
215 void CFXJSE_Class::SetUpNamedPropHandler( | |
216 v8::Isolate* pIsolate, | |
217 v8::Local<v8::ObjectTemplate>& hObjectTemplate, | |
218 const FXJSE_CLASS* lpClassDefinition) { | |
219 v8::NamedPropertyHandlerConfiguration configuration( | |
220 lpClassDefinition->dynPropGetter | |
221 ? FXJSE_V8_GenericNamedPropertyGetterCallback | |
222 : 0, | |
223 lpClassDefinition->dynPropSetter | |
224 ? FXJSE_V8_GenericNamedPropertySetterCallback | |
225 : 0, | |
226 lpClassDefinition->dynPropTypeGetter | |
227 ? FXJSE_V8_GenericNamedPropertyQueryCallback | |
228 : 0, | |
229 lpClassDefinition->dynPropDeleter | |
230 ? FXJSE_V8_GenericNamedPropertyDeleterCallback | |
231 : 0, | |
232 FXJSE_V8_GenericNamedPropertyEnumeratorCallback, | |
233 v8::External::New(pIsolate, const_cast<FXJSE_CLASS*>(lpClassDefinition)), | |
234 v8::PropertyHandlerFlags::kNonMasking); | |
235 hObjectTemplate->SetHandler(configuration); | |
236 } | |
OLD | NEW |