Chromium Code Reviews| 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" |
| 11 #include "xfa/fxjse/util_inline.h" | 11 #include "xfa/fxjse/util_inline.h" |
| 12 #include "xfa/fxjse/value.h" | 12 #include "xfa/fxjse/value.h" |
| 13 | 13 |
| 14 namespace { | |
| 15 | |
| 16 CFXJSE_Context* CFXContextFromFXContext(FXJSE_HCONTEXT hContext) { | |
|
Tom Sepez
2016/04/28 23:07:55
nit: can we call this CFXContextFromHContext(), to
Lei Zhang
2016/04/28 23:43:53
Done.
| |
| 17 return reinterpret_cast<CFXJSE_Context*>(hContext); | |
| 18 } | |
| 19 | |
| 20 } // namespace | |
| 21 | |
| 14 FXJSE_HCONTEXT FXJSE_Context_Create(FXJSE_HRUNTIME hRuntime, | 22 FXJSE_HCONTEXT FXJSE_Context_Create(FXJSE_HRUNTIME hRuntime, |
| 15 const FXJSE_CLASS* lpGlobalClass, | 23 const FXJSE_CLASS* lpGlobalClass, |
| 16 void* lpGlobalObject) { | 24 void* lpGlobalObject) { |
| 17 CFXJSE_Context* pContext = CFXJSE_Context::Create( | 25 CFXJSE_Context* pContext = CFXJSE_Context::Create( |
| 18 reinterpret_cast<v8::Isolate*>(hRuntime), lpGlobalClass, lpGlobalObject); | 26 reinterpret_cast<v8::Isolate*>(hRuntime), lpGlobalClass, lpGlobalObject); |
| 19 return reinterpret_cast<FXJSE_HCONTEXT>(pContext); | 27 return reinterpret_cast<FXJSE_HCONTEXT>(pContext); |
| 20 } | 28 } |
| 21 | 29 |
| 22 void FXJSE_Context_Release(FXJSE_HCONTEXT hContext) { | 30 void FXJSE_Context_Release(FXJSE_HCONTEXT hContext) { |
| 23 CFXJSE_Context* pContext = reinterpret_cast<CFXJSE_Context*>(hContext); | 31 delete CFXContextFromFXContext(hContext); |
| 24 if (pContext) { | |
| 25 delete pContext; | |
| 26 } | |
| 27 } | 32 } |
| 28 | 33 |
| 29 FXJSE_HVALUE FXJSE_Context_GetGlobalObject(FXJSE_HCONTEXT hContext) { | 34 FXJSE_HVALUE FXJSE_Context_GetGlobalObject(FXJSE_HCONTEXT hContext) { |
| 30 CFXJSE_Context* pContext = reinterpret_cast<CFXJSE_Context*>(hContext); | 35 CFXJSE_Context* pContext = CFXContextFromFXContext(hContext); |
| 31 if (!pContext) { | 36 if (!pContext) |
| 32 return NULL; | 37 return nullptr; |
| 33 } | 38 |
| 34 CFXJSE_Value* lpValue = CFXJSE_Value::Create(pContext->GetRuntime()); | 39 CFXJSE_Value* lpValue = CFXJSE_Value::Create(pContext->GetRuntime()); |
| 35 ASSERT(lpValue); | 40 ASSERT(lpValue); |
| 36 pContext->GetGlobalObject(lpValue); | 41 pContext->GetGlobalObject(lpValue); |
| 37 return reinterpret_cast<FXJSE_HVALUE>(lpValue); | 42 return reinterpret_cast<FXJSE_HVALUE>(lpValue); |
| 38 } | 43 } |
| 39 | 44 |
| 40 static const FX_CHAR* szCompatibleModeScripts[] = { | 45 static const FX_CHAR* szCompatibleModeScripts[] = { |
| 41 "(function(global, list) {\n" | 46 "(function(global, list) {\n" |
| 42 " 'use strict';\n" | 47 " 'use strict';\n" |
| 43 " var objname;\n" | 48 " var objname;\n" |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 69 if (dwCompatibleFlags & (1 << i)) { | 74 if (dwCompatibleFlags & (1 << i)) { |
| 70 FXJSE_ExecuteScript(hContext, szCompatibleModeScripts[i], NULL, NULL); | 75 FXJSE_ExecuteScript(hContext, szCompatibleModeScripts[i], NULL, NULL); |
| 71 } | 76 } |
| 72 } | 77 } |
| 73 } | 78 } |
| 74 | 79 |
| 75 FX_BOOL FXJSE_ExecuteScript(FXJSE_HCONTEXT hContext, | 80 FX_BOOL FXJSE_ExecuteScript(FXJSE_HCONTEXT hContext, |
| 76 const FX_CHAR* szScript, | 81 const FX_CHAR* szScript, |
| 77 FXJSE_HVALUE hRetValue, | 82 FXJSE_HVALUE hRetValue, |
| 78 FXJSE_HVALUE hNewThisObject) { | 83 FXJSE_HVALUE hNewThisObject) { |
| 79 CFXJSE_Context* pContext = reinterpret_cast<CFXJSE_Context*>(hContext); | 84 CFXJSE_Context* pContext = CFXContextFromFXContext(hContext); |
| 80 ASSERT(pContext); | |
| 81 return pContext->ExecuteScript( | 85 return pContext->ExecuteScript( |
| 82 szScript, reinterpret_cast<CFXJSE_Value*>(hRetValue), | 86 szScript, reinterpret_cast<CFXJSE_Value*>(hRetValue), |
| 83 reinterpret_cast<CFXJSE_Value*>(hNewThisObject)); | 87 reinterpret_cast<CFXJSE_Value*>(hNewThisObject)); |
| 84 } | 88 } |
| 85 | 89 |
| 86 v8::Local<v8::Object> FXJSE_CreateReturnValue(v8::Isolate* pIsolate, | 90 v8::Local<v8::Object> FXJSE_CreateReturnValue(v8::Isolate* pIsolate, |
| 87 v8::TryCatch& trycatch) { | 91 v8::TryCatch& trycatch) { |
| 88 v8::Local<v8::Object> hReturnValue = v8::Object::New(pIsolate); | 92 v8::Local<v8::Object> hReturnValue = v8::Object::New(pIsolate); |
| 89 if (trycatch.HasCaught()) { | 93 if (trycatch.HasCaught()) { |
| 90 v8::Local<v8::Value> hException = trycatch.Exception(); | 94 v8::Local<v8::Value> hException = trycatch.Exception(); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 145 pIsolate, CFXJSE_RuntimeData::Get(pIsolate)->m_hRootContext); | 149 pIsolate, CFXJSE_RuntimeData::Get(pIsolate)->m_hRootContext); |
| 146 hNewContext->SetSecurityToken(hRootContext->GetSecurityToken()); | 150 hNewContext->SetSecurityToken(hRootContext->GetSecurityToken()); |
| 147 v8::Local<v8::Object> hGlobalObject = | 151 v8::Local<v8::Object> hGlobalObject = |
| 148 FXJSE_GetGlobalObjectFromContext(hNewContext); | 152 FXJSE_GetGlobalObjectFromContext(hNewContext); |
| 149 FXJSE_UpdateObjectBinding(hGlobalObject, lpGlobalObject); | 153 FXJSE_UpdateObjectBinding(hGlobalObject, lpGlobalObject); |
| 150 pContext->m_hContext.Reset(pIsolate, hNewContext); | 154 pContext->m_hContext.Reset(pIsolate, hNewContext); |
| 151 return pContext; | 155 return pContext; |
| 152 } | 156 } |
| 153 | 157 |
| 154 CFXJSE_Context::~CFXJSE_Context() { | 158 CFXJSE_Context::~CFXJSE_Context() { |
| 155 for (int32_t i = 0, count = m_rgClasses.GetSize(); i < count; i++) { | 159 for (int32_t i = 0, count = m_rgClasses.GetSize(); i < count; i++) |
| 156 CFXJSE_Class* pClass = m_rgClasses[i]; | 160 delete m_rgClasses[i]; |
| 157 if (pClass) { | |
| 158 delete pClass; | |
| 159 } | |
| 160 } | |
| 161 m_rgClasses.RemoveAll(); | 161 m_rgClasses.RemoveAll(); |
| 162 } | 162 } |
| 163 | 163 |
| 164 void CFXJSE_Context::GetGlobalObject(CFXJSE_Value* pValue) { | 164 void CFXJSE_Context::GetGlobalObject(CFXJSE_Value* pValue) { |
| 165 ASSERT(pValue); | 165 ASSERT(pValue); |
| 166 CFXJSE_ScopeUtil_IsolateHandleContext scope(this); | 166 CFXJSE_ScopeUtil_IsolateHandleContext scope(this); |
| 167 v8::Local<v8::Context> hContext = | 167 v8::Local<v8::Context> hContext = |
| 168 v8::Local<v8::Context>::New(m_pIsolate, m_hContext); | 168 v8::Local<v8::Context>::New(m_pIsolate, m_hContext); |
| 169 v8::Local<v8::Object> hGlobalObject = hContext->Global(); | 169 v8::Local<v8::Object> hGlobalObject = hContext->Global(); |
| 170 pValue->ForceSetValue(hGlobalObject); | 170 pValue->ForceSetValue(hGlobalObject); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 } | 222 } |
| 223 } | 223 } |
| OLD | NEW |