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