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 "fxjse/include/cfxjse_class.h" | |
8 | |
9 #include "fxjse/context.h" | |
10 #include "fxjse/include/cfxjse_arguments.h" | |
11 #include "fxjse/include/cfxjse_value.h" | |
12 #include "fxjse/scope_inline.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 if (!info.IsConstructCall()) { | |
108 return; | |
109 } | |
110 const FXJSE_CLASS_DESCRIPTOR* lpClassDefinition = | |
111 static_cast<FXJSE_CLASS_DESCRIPTOR*>( | |
112 info.Data().As<v8::External>()->Value()); | |
113 if (!lpClassDefinition) { | |
114 return; | |
115 } | |
116 ASSERT(info.This()->InternalFieldCount()); | |
117 info.This()->SetAlignedPointerInInternalField(0, NULL); | |
118 } | |
119 | |
120 v8::Isolate* CFXJSE_Arguments::GetRuntime() const { | |
121 return m_pRetValue->GetIsolate(); | |
122 } | |
123 | |
124 int32_t CFXJSE_Arguments::GetLength() const { | |
125 return m_pInfo->Length(); | |
126 } | |
127 | |
128 std::unique_ptr<CFXJSE_Value> CFXJSE_Arguments::GetValue(int32_t index) const { | |
129 std::unique_ptr<CFXJSE_Value> lpArgValue( | |
130 new CFXJSE_Value(v8::Isolate::GetCurrent())); | |
131 lpArgValue->ForceSetValue((*m_pInfo)[index]); | |
132 return lpArgValue; | |
133 } | |
134 | |
135 FX_BOOL CFXJSE_Arguments::GetBoolean(int32_t index) const { | |
136 return (*m_pInfo)[index]->BooleanValue(); | |
137 } | |
138 | |
139 int32_t CFXJSE_Arguments::GetInt32(int32_t index) const { | |
140 return static_cast<int32_t>((*m_pInfo)[index]->NumberValue()); | |
141 } | |
142 | |
143 FX_FLOAT CFXJSE_Arguments::GetFloat(int32_t index) const { | |
144 return static_cast<FX_FLOAT>((*m_pInfo)[index]->NumberValue()); | |
145 } | |
146 | |
147 CFX_ByteString CFXJSE_Arguments::GetUTF8String(int32_t index) const { | |
148 v8::Local<v8::String> hString = (*m_pInfo)[index]->ToString(); | |
149 v8::String::Utf8Value szStringVal(hString); | |
150 return CFX_ByteString(*szStringVal); | |
151 } | |
152 | |
153 CFXJSE_HostObject* CFXJSE_Arguments::GetObject(int32_t index, | |
154 CFXJSE_Class* pClass) const { | |
155 v8::Local<v8::Value> hValue = (*m_pInfo)[index]; | |
156 ASSERT(!hValue.IsEmpty()); | |
157 if (!hValue->IsObject()) | |
158 return nullptr; | |
159 return FXJSE_RetrieveObjectBinding(hValue.As<v8::Object>(), pClass); | |
160 } | |
161 | |
162 CFXJSE_Value* CFXJSE_Arguments::GetReturnValue() { | |
163 return m_pRetValue; | |
164 } | |
165 | |
166 static void FXJSE_Context_GlobalObjToString( | |
167 const v8::FunctionCallbackInfo<v8::Value>& info) { | |
168 const FXJSE_CLASS_DESCRIPTOR* lpClass = static_cast<FXJSE_CLASS_DESCRIPTOR*>( | |
169 info.Data().As<v8::External>()->Value()); | |
170 if (!lpClass) { | |
171 return; | |
172 } | |
173 if (info.This() == info.Holder() && lpClass->name) { | |
174 CFX_ByteString szStringVal; | |
175 szStringVal.Format("[object %s]", lpClass->name); | |
176 info.GetReturnValue().Set(v8::String::NewFromUtf8( | |
177 info.GetIsolate(), szStringVal.c_str(), v8::String::kNormalString, | |
178 szStringVal.GetLength())); | |
179 } else { | |
180 v8::Local<v8::String> local_str = | |
181 info.This() | |
182 ->ObjectProtoToString(info.GetIsolate()->GetCurrentContext()) | |
183 .FromMaybe(v8::Local<v8::String>()); | |
184 info.GetReturnValue().Set(local_str); | |
185 } | |
186 } | |
187 | |
188 CFXJSE_Class* CFXJSE_Class::Create( | |
189 CFXJSE_Context* lpContext, | |
190 const FXJSE_CLASS_DESCRIPTOR* lpClassDefinition, | |
191 FX_BOOL bIsJSGlobal) { | |
192 if (!lpContext || !lpClassDefinition) { | |
193 return NULL; | |
194 } | |
195 CFXJSE_Class* pClass = | |
196 GetClassFromContext(lpContext, lpClassDefinition->name); | |
197 if (pClass) { | |
198 return pClass; | |
199 } | |
200 v8::Isolate* pIsolate = lpContext->m_pIsolate; | |
201 pClass = new CFXJSE_Class(lpContext); | |
202 pClass->m_szClassName = lpClassDefinition->name; | |
203 pClass->m_lpClassDefinition = lpClassDefinition; | |
204 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(pIsolate); | |
205 v8::Local<v8::FunctionTemplate> hFunctionTemplate = v8::FunctionTemplate::New( | |
206 pIsolate, bIsJSGlobal ? 0 : FXJSE_V8ConstructorCallback_Wrapper, | |
207 v8::External::New( | |
208 pIsolate, const_cast<FXJSE_CLASS_DESCRIPTOR*>(lpClassDefinition))); | |
209 hFunctionTemplate->SetClassName( | |
210 v8::String::NewFromUtf8(pIsolate, lpClassDefinition->name)); | |
211 hFunctionTemplate->InstanceTemplate()->SetInternalFieldCount(1); | |
212 v8::Local<v8::ObjectTemplate> hObjectTemplate = | |
213 hFunctionTemplate->InstanceTemplate(); | |
214 SetUpNamedPropHandler(pIsolate, hObjectTemplate, lpClassDefinition); | |
215 | |
216 if (lpClassDefinition->propNum) { | |
217 for (int32_t i = 0; i < lpClassDefinition->propNum; i++) { | |
218 hObjectTemplate->SetNativeDataProperty( | |
219 v8::String::NewFromUtf8(pIsolate, | |
220 lpClassDefinition->properties[i].name), | |
221 lpClassDefinition->properties[i].getProc | |
222 ? FXJSE_V8GetterCallback_Wrapper | |
223 : NULL, | |
224 lpClassDefinition->properties[i].setProc | |
225 ? FXJSE_V8SetterCallback_Wrapper | |
226 : NULL, | |
227 v8::External::New(pIsolate, const_cast<FXJSE_PROPERTY_DESCRIPTOR*>( | |
228 lpClassDefinition->properties + i)), | |
229 static_cast<v8::PropertyAttribute>(v8::DontDelete)); | |
230 } | |
231 } | |
232 if (lpClassDefinition->methNum) { | |
233 for (int32_t i = 0; i < lpClassDefinition->methNum; i++) { | |
234 v8::Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New( | |
235 pIsolate, FXJSE_V8FunctionCallback_Wrapper, | |
236 v8::External::New(pIsolate, const_cast<FXJSE_FUNCTION_DESCRIPTOR*>( | |
237 lpClassDefinition->methods + i))); | |
238 fun->RemovePrototype(); | |
239 hObjectTemplate->Set( | |
240 v8::String::NewFromUtf8(pIsolate, lpClassDefinition->methods[i].name), | |
241 fun, | |
242 static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete)); | |
243 } | |
244 } | |
245 if (lpClassDefinition->constructor) { | |
246 if (bIsJSGlobal) { | |
247 hObjectTemplate->Set( | |
248 v8::String::NewFromUtf8(pIsolate, lpClassDefinition->name), | |
249 v8::FunctionTemplate::New( | |
250 pIsolate, FXJSE_V8ClassGlobalConstructorCallback_Wrapper, | |
251 v8::External::New(pIsolate, const_cast<FXJSE_CLASS_DESCRIPTOR*>( | |
252 lpClassDefinition))), | |
253 static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete)); | |
254 } else { | |
255 v8::Local<v8::Context> hLocalContext = | |
256 v8::Local<v8::Context>::New(pIsolate, lpContext->m_hContext); | |
257 FXJSE_GetGlobalObjectFromContext(hLocalContext) | |
258 ->Set(v8::String::NewFromUtf8(pIsolate, lpClassDefinition->name), | |
259 v8::Function::New( | |
260 pIsolate, FXJSE_V8ClassGlobalConstructorCallback_Wrapper, | |
261 v8::External::New(pIsolate, | |
262 const_cast<FXJSE_CLASS_DESCRIPTOR*>( | |
263 lpClassDefinition)))); | |
264 } | |
265 } | |
266 if (bIsJSGlobal) { | |
267 v8::Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New( | |
268 pIsolate, FXJSE_Context_GlobalObjToString, | |
269 v8::External::New( | |
270 pIsolate, const_cast<FXJSE_CLASS_DESCRIPTOR*>(lpClassDefinition))); | |
271 fun->RemovePrototype(); | |
272 hObjectTemplate->Set(v8::String::NewFromUtf8(pIsolate, "toString"), fun); | |
273 } | |
274 pClass->m_hTemplate.Reset(lpContext->m_pIsolate, hFunctionTemplate); | |
275 lpContext->m_rgClasses.push_back(std::unique_ptr<CFXJSE_Class>(pClass)); | |
276 return pClass; | |
277 } | |
278 | |
279 CFXJSE_Class::CFXJSE_Class(CFXJSE_Context* lpContext) | |
280 : m_lpClassDefinition(nullptr), m_pContext(lpContext) {} | |
281 | |
282 CFXJSE_Class::~CFXJSE_Class() {} | |
283 | |
284 CFXJSE_Class* CFXJSE_Class::GetClassFromContext(CFXJSE_Context* pContext, | |
285 const CFX_ByteStringC& szName) { | |
286 for (const auto& pClass : pContext->m_rgClasses) { | |
287 if (pClass->m_szClassName == szName) | |
288 return pClass.get(); | |
289 } | |
290 return nullptr; | |
291 } | |
OLD | NEW |