OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #include "xfa/fxjse/context.h" | 7 #include "xfa/fxjse/context.h" |
8 | 8 |
9 #include "xfa/fxjse/class.h" | 9 #include "xfa/fxjse/class.h" |
10 #include "xfa/fxjse/scope_inline.h" | 10 #include "xfa/fxjse/scope_inline.h" |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 } | 152 } |
153 return hReturnValue; | 153 return hReturnValue; |
154 } | 154 } |
155 | 155 |
156 CFXJSE_Context* CFXJSE_Context::Create( | 156 CFXJSE_Context* CFXJSE_Context::Create( |
157 v8::Isolate* pIsolate, | 157 v8::Isolate* pIsolate, |
158 const FXJSE_CLASS_DESCRIPTOR* lpGlobalClass, | 158 const FXJSE_CLASS_DESCRIPTOR* lpGlobalClass, |
159 CFXJSE_HostObject* lpGlobalObject) { | 159 CFXJSE_HostObject* lpGlobalObject) { |
160 CFXJSE_ScopeUtil_IsolateHandle scope(pIsolate); | 160 CFXJSE_ScopeUtil_IsolateHandle scope(pIsolate); |
161 CFXJSE_Context* pContext = new CFXJSE_Context(pIsolate); | 161 CFXJSE_Context* pContext = new CFXJSE_Context(pIsolate); |
162 CFXJSE_Class* lpGlobalClassObj = NULL; | 162 CFXJSE_Class* lpGlobalClassObj = nullptr; |
163 v8::Local<v8::ObjectTemplate> hObjectTemplate; | 163 v8::Local<v8::ObjectTemplate> hObjectTemplate; |
164 if (lpGlobalClass) { | 164 if (lpGlobalClass) { |
165 lpGlobalClassObj = CFXJSE_Class::Create(pContext, lpGlobalClass, TRUE); | 165 lpGlobalClassObj = CFXJSE_Class::Create(pContext, lpGlobalClass, TRUE); |
166 ASSERT(lpGlobalClassObj); | 166 ASSERT(lpGlobalClassObj); |
167 v8::Local<v8::FunctionTemplate> hFunctionTemplate = | 167 v8::Local<v8::FunctionTemplate> hFunctionTemplate = |
168 v8::Local<v8::FunctionTemplate>::New(pIsolate, | 168 v8::Local<v8::FunctionTemplate>::New(pIsolate, |
169 lpGlobalClassObj->m_hTemplate); | 169 lpGlobalClassObj->m_hTemplate); |
170 hObjectTemplate = hFunctionTemplate->InstanceTemplate(); | 170 hObjectTemplate = hFunctionTemplate->InstanceTemplate(); |
171 } else { | 171 } else { |
172 hObjectTemplate = v8::ObjectTemplate::New(pIsolate); | 172 hObjectTemplate = v8::ObjectTemplate::New(pIsolate); |
173 hObjectTemplate->SetInternalFieldCount(1); | 173 hObjectTemplate->SetInternalFieldCount(1); |
174 } | 174 } |
175 v8::Local<v8::Context> hNewContext = | 175 v8::Local<v8::Context> hNewContext = |
176 v8::Context::New(pIsolate, NULL, hObjectTemplate); | 176 v8::Context::New(pIsolate, nullptr, hObjectTemplate); |
177 v8::Local<v8::Context> hRootContext = v8::Local<v8::Context>::New( | 177 v8::Local<v8::Context> hRootContext = v8::Local<v8::Context>::New( |
178 pIsolate, CFXJSE_RuntimeData::Get(pIsolate)->m_hRootContext); | 178 pIsolate, CFXJSE_RuntimeData::Get(pIsolate)->m_hRootContext); |
179 hNewContext->SetSecurityToken(hRootContext->GetSecurityToken()); | 179 hNewContext->SetSecurityToken(hRootContext->GetSecurityToken()); |
180 v8::Local<v8::Object> hGlobalObject = | 180 v8::Local<v8::Object> hGlobalObject = |
181 FXJSE_GetGlobalObjectFromContext(hNewContext); | 181 FXJSE_GetGlobalObjectFromContext(hNewContext); |
182 FXJSE_UpdateObjectBinding(hGlobalObject, lpGlobalObject); | 182 FXJSE_UpdateObjectBinding(hGlobalObject, lpGlobalObject); |
183 pContext->m_hContext.Reset(pIsolate, hNewContext); | 183 pContext->m_hContext.Reset(pIsolate, hNewContext); |
184 return pContext; | 184 return pContext; |
185 } | 185 } |
186 | 186 |
187 CFXJSE_Context::CFXJSE_Context(v8::Isolate* pIsolate) : m_pIsolate(pIsolate) {} | 187 CFXJSE_Context::CFXJSE_Context(v8::Isolate* pIsolate) : m_pIsolate(pIsolate) {} |
188 CFXJSE_Context::~CFXJSE_Context() {} | 188 CFXJSE_Context::~CFXJSE_Context() {} |
189 | 189 |
190 void CFXJSE_Context::GetGlobalObject(CFXJSE_Value* pValue) { | 190 void CFXJSE_Context::GetGlobalObject(CFXJSE_Value* pValue) { |
191 ASSERT(pValue); | 191 ASSERT(pValue); |
192 CFXJSE_ScopeUtil_IsolateHandleContext scope(this); | 192 CFXJSE_ScopeUtil_IsolateHandleContext scope(this); |
193 v8::Local<v8::Context> hContext = | 193 v8::Local<v8::Context> hContext = |
194 v8::Local<v8::Context>::New(m_pIsolate, m_hContext); | 194 v8::Local<v8::Context>::New(m_pIsolate, m_hContext); |
195 v8::Local<v8::Object> hGlobalObject = hContext->Global(); | 195 v8::Local<v8::Object> hGlobalObject = hContext->Global(); |
196 pValue->ForceSetValue(hGlobalObject); | 196 pValue->ForceSetValue(hGlobalObject); |
197 } | 197 } |
198 | 198 |
199 FX_BOOL CFXJSE_Context::ExecuteScript(const FX_CHAR* szScript, | 199 FX_BOOL CFXJSE_Context::ExecuteScript(const FX_CHAR* szScript, |
200 CFXJSE_Value* lpRetValue, | 200 CFXJSE_Value* lpRetValue, |
201 CFXJSE_Value* lpNewThisObject) { | 201 CFXJSE_Value* lpNewThisObject) { |
202 CFXJSE_ScopeUtil_IsolateHandleContext scope(this); | 202 CFXJSE_ScopeUtil_IsolateHandleContext scope(this); |
203 v8::TryCatch trycatch(m_pIsolate); | 203 v8::TryCatch trycatch(m_pIsolate); |
204 v8::Local<v8::String> hScriptString = | 204 v8::Local<v8::String> hScriptString = |
205 v8::String::NewFromUtf8(m_pIsolate, szScript); | 205 v8::String::NewFromUtf8(m_pIsolate, szScript); |
206 if (lpNewThisObject == NULL) { | 206 if (!lpNewThisObject) { |
207 v8::Local<v8::Script> hScript = v8::Script::Compile(hScriptString); | 207 v8::Local<v8::Script> hScript = v8::Script::Compile(hScriptString); |
208 if (!trycatch.HasCaught()) { | 208 if (!trycatch.HasCaught()) { |
209 v8::Local<v8::Value> hValue = hScript->Run(); | 209 v8::Local<v8::Value> hValue = hScript->Run(); |
210 if (!trycatch.HasCaught()) { | 210 if (!trycatch.HasCaught()) { |
211 if (lpRetValue) { | 211 if (lpRetValue) { |
212 lpRetValue->m_hValue.Reset(m_pIsolate, hValue); | 212 lpRetValue->m_hValue.Reset(m_pIsolate, hValue); |
213 } | 213 } |
214 return TRUE; | 214 return TRUE; |
215 } | 215 } |
216 } | 216 } |
217 if (lpRetValue) { | 217 if (lpRetValue) { |
218 lpRetValue->m_hValue.Reset(m_pIsolate, | 218 lpRetValue->m_hValue.Reset(m_pIsolate, |
219 FXJSE_CreateReturnValue(m_pIsolate, trycatch)); | 219 FXJSE_CreateReturnValue(m_pIsolate, trycatch)); |
220 } | 220 } |
221 return FALSE; | 221 return FALSE; |
222 } else { | |
223 v8::Local<v8::Value> hNewThis = | |
224 v8::Local<v8::Value>::New(m_pIsolate, lpNewThisObject->m_hValue); | |
225 ASSERT(!hNewThis.IsEmpty()); | |
226 v8::Local<v8::Script> hWrapper = | |
227 v8::Script::Compile(v8::String::NewFromUtf8( | |
228 m_pIsolate, "(function () { return eval(arguments[0]); })")); | |
229 v8::Local<v8::Value> hWrapperValue = hWrapper->Run(); | |
230 ASSERT(hWrapperValue->IsFunction()); | |
231 v8::Local<v8::Function> hWrapperFn = hWrapperValue.As<v8::Function>(); | |
232 if (!trycatch.HasCaught()) { | |
233 v8::Local<v8::Value> rgArgs[] = {hScriptString}; | |
234 v8::Local<v8::Value> hValue = | |
235 hWrapperFn->Call(hNewThis.As<v8::Object>(), 1, rgArgs); | |
236 if (!trycatch.HasCaught()) { | |
237 if (lpRetValue) { | |
238 lpRetValue->m_hValue.Reset(m_pIsolate, hValue); | |
239 } | |
240 return TRUE; | |
241 } | |
242 } | |
243 if (lpRetValue) { | |
244 lpRetValue->m_hValue.Reset(m_pIsolate, | |
245 FXJSE_CreateReturnValue(m_pIsolate, trycatch)); | |
246 } | |
247 return FALSE; | |
248 } | 222 } |
| 223 |
| 224 v8::Local<v8::Value> hNewThis = |
| 225 v8::Local<v8::Value>::New(m_pIsolate, lpNewThisObject->m_hValue); |
| 226 ASSERT(!hNewThis.IsEmpty()); |
| 227 v8::Local<v8::Script> hWrapper = v8::Script::Compile(v8::String::NewFromUtf8( |
| 228 m_pIsolate, "(function () { return eval(arguments[0]); })")); |
| 229 v8::Local<v8::Value> hWrapperValue = hWrapper->Run(); |
| 230 ASSERT(hWrapperValue->IsFunction()); |
| 231 v8::Local<v8::Function> hWrapperFn = hWrapperValue.As<v8::Function>(); |
| 232 if (!trycatch.HasCaught()) { |
| 233 v8::Local<v8::Value> rgArgs[] = {hScriptString}; |
| 234 v8::Local<v8::Value> hValue = |
| 235 hWrapperFn->Call(hNewThis.As<v8::Object>(), 1, rgArgs); |
| 236 if (!trycatch.HasCaught()) { |
| 237 if (lpRetValue) { |
| 238 lpRetValue->m_hValue.Reset(m_pIsolate, hValue); |
| 239 } |
| 240 return TRUE; |
| 241 } |
| 242 } |
| 243 if (lpRetValue) { |
| 244 lpRetValue->m_hValue.Reset(m_pIsolate, |
| 245 FXJSE_CreateReturnValue(m_pIsolate, trycatch)); |
| 246 } |
| 247 return FALSE; |
249 } | 248 } |
OLD | NEW |