| 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/context.h" | |
| 8 | |
| 9 #include "xfa/src/fxjse/class.h" | |
| 10 #include "xfa/src/fxjse/scope_inline.h" | |
| 11 #include "xfa/src/fxjse/util_inline.h" | |
| 12 #include "xfa/src/fxjse/value.h" | |
| 13 | |
| 14 FXJSE_HCONTEXT FXJSE_Context_Create(FXJSE_HRUNTIME hRuntime, | |
| 15 const FXJSE_CLASS* lpGlobalClass, | |
| 16 void* lpGlobalObject) { | |
| 17 CFXJSE_Context* pContext = CFXJSE_Context::Create( | |
| 18 reinterpret_cast<v8::Isolate*>(hRuntime), lpGlobalClass, lpGlobalObject); | |
| 19 return reinterpret_cast<FXJSE_HCONTEXT>(pContext); | |
| 20 } | |
| 21 | |
| 22 void FXJSE_Context_Release(FXJSE_HCONTEXT hContext) { | |
| 23 CFXJSE_Context* pContext = reinterpret_cast<CFXJSE_Context*>(hContext); | |
| 24 if (pContext) { | |
| 25 delete pContext; | |
| 26 } | |
| 27 } | |
| 28 | |
| 29 FXJSE_HVALUE FXJSE_Context_GetGlobalObject(FXJSE_HCONTEXT hContext) { | |
| 30 CFXJSE_Context* pContext = reinterpret_cast<CFXJSE_Context*>(hContext); | |
| 31 if (!pContext) { | |
| 32 return NULL; | |
| 33 } | |
| 34 CFXJSE_Value* lpValue = CFXJSE_Value::Create(pContext->GetRuntime()); | |
| 35 ASSERT(lpValue); | |
| 36 pContext->GetGlobalObject(lpValue); | |
| 37 return reinterpret_cast<FXJSE_HVALUE>(lpValue); | |
| 38 } | |
| 39 | |
| 40 FXJSE_HRUNTIME FXJSE_Context_GetRuntime(FXJSE_HCONTEXT hContext) { | |
| 41 CFXJSE_Context* pContext = reinterpret_cast<CFXJSE_Context*>(hContext); | |
| 42 return pContext ? reinterpret_cast<FXJSE_HRUNTIME>(pContext->GetRuntime()) | |
| 43 : NULL; | |
| 44 } | |
| 45 | |
| 46 static const FX_CHAR* szCompatibleModeScripts[] = { | |
| 47 "(function(global, list) {\n" | |
| 48 " 'use strict';\n" | |
| 49 " var objname;\n" | |
| 50 " for (objname in list) {\n" | |
| 51 " var globalobj = global[objname];\n" | |
| 52 " if (globalobj) {\n" | |
| 53 " list[objname].forEach(function(name) {\n" | |
| 54 " if (!globalobj[name]) {\n" | |
| 55 " Object.defineProperty(globalobj, name, {\n" | |
| 56 " writable: true,\n" | |
| 57 " enumerable: false,\n" | |
| 58 " value: (function(obj) {\n" | |
| 59 " if (arguments.length === 0) {\n" | |
| 60 " throw new TypeError('missing argument 0 when calling " | |
| 61 " function ' + objname + '.' + name);\n" | |
| 62 " }\n" | |
| 63 " return globalobj.prototype[name].apply(obj, " | |
| 64 " Array.prototype.slice.call(arguments, 1));\n" | |
| 65 " })\n" | |
| 66 " });\n" | |
| 67 " }\n" | |
| 68 " });\n" | |
| 69 " }\n" | |
| 70 " }\n" | |
| 71 "}(this, {String: ['substr', 'toUpperCase']}));"}; | |
| 72 void FXJSE_Context_EnableCompatibleMode(FXJSE_HCONTEXT hContext, | |
| 73 FX_DWORD dwCompatibleFlags) { | |
| 74 for (uint32_t i = 0; i < (uint32_t)FXJSE_COMPATIBLEMODEFLAGCOUNT; i++) { | |
| 75 if (dwCompatibleFlags & (1 << i)) { | |
| 76 FXJSE_ExecuteScript(hContext, szCompatibleModeScripts[i], NULL, NULL); | |
| 77 } | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 FX_BOOL FXJSE_ExecuteScript(FXJSE_HCONTEXT hContext, | |
| 82 const FX_CHAR* szScript, | |
| 83 FXJSE_HVALUE hRetValue, | |
| 84 FXJSE_HVALUE hNewThisObject) { | |
| 85 CFXJSE_Context* pContext = reinterpret_cast<CFXJSE_Context*>(hContext); | |
| 86 ASSERT(pContext); | |
| 87 return pContext->ExecuteScript( | |
| 88 szScript, reinterpret_cast<CFXJSE_Value*>(hRetValue), | |
| 89 reinterpret_cast<CFXJSE_Value*>(hNewThisObject)); | |
| 90 } | |
| 91 | |
| 92 v8::Local<v8::Object> FXJSE_CreateReturnValue(v8::Isolate* pIsolate, | |
| 93 v8::TryCatch& trycatch) { | |
| 94 v8::Local<v8::Object> hReturnValue = v8::Object::New(pIsolate); | |
| 95 if (trycatch.HasCaught()) { | |
| 96 v8::Local<v8::Value> hException = trycatch.Exception(); | |
| 97 v8::Local<v8::Message> hMessage = trycatch.Message(); | |
| 98 if (hException->IsObject()) { | |
| 99 v8::Local<v8::Value> hValue; | |
| 100 hValue = hException.As<v8::Object>()->Get( | |
| 101 v8::String::NewFromUtf8(pIsolate, "name")); | |
| 102 if (hValue->IsString() || hValue->IsStringObject()) { | |
| 103 hReturnValue->Set(0, hValue); | |
| 104 } else { | |
| 105 hReturnValue->Set(0, v8::String::NewFromUtf8(pIsolate, "Error")); | |
| 106 } | |
| 107 hValue = hException.As<v8::Object>()->Get( | |
| 108 v8::String::NewFromUtf8(pIsolate, "message")); | |
| 109 if (hValue->IsString() || hValue->IsStringObject()) { | |
| 110 hReturnValue->Set(1, hValue); | |
| 111 } else { | |
| 112 hReturnValue->Set(1, hMessage->Get()); | |
| 113 } | |
| 114 } else { | |
| 115 hReturnValue->Set(0, v8::String::NewFromUtf8(pIsolate, "Error")); | |
| 116 hReturnValue->Set(1, hMessage->Get()); | |
| 117 } | |
| 118 hReturnValue->Set(2, hException); | |
| 119 hReturnValue->Set(3, v8::Integer::New(pIsolate, hMessage->GetLineNumber())); | |
| 120 hReturnValue->Set(4, hMessage->GetSourceLine()); | |
| 121 v8::Maybe<int32_t> maybe_int = | |
| 122 hMessage->GetStartColumn(pIsolate->GetCurrentContext()); | |
| 123 hReturnValue->Set(5, v8::Integer::New(pIsolate, maybe_int.FromMaybe(0))); | |
| 124 maybe_int = hMessage->GetEndColumn(pIsolate->GetCurrentContext()); | |
| 125 hReturnValue->Set(6, v8::Integer::New(pIsolate, maybe_int.FromMaybe(0))); | |
| 126 } | |
| 127 return hReturnValue; | |
| 128 } | |
| 129 | |
| 130 FX_BOOL FXJSE_ReturnValue_GetMessage(FXJSE_HVALUE hRetValue, | |
| 131 CFX_ByteString& utf8Name, | |
| 132 CFX_ByteString& utf8Message) { | |
| 133 CFXJSE_Value* lpValue = reinterpret_cast<CFXJSE_Value*>(hRetValue); | |
| 134 if (!lpValue) { | |
| 135 return FALSE; | |
| 136 } | |
| 137 v8::Isolate* pIsolate = lpValue->GetIsolate(); | |
| 138 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(pIsolate); | |
| 139 v8::Local<v8::Value> hValue = | |
| 140 v8::Local<v8::Value>::New(pIsolate, lpValue->DirectGetValue()); | |
| 141 if (!hValue->IsObject()) { | |
| 142 return FALSE; | |
| 143 } | |
| 144 v8::String::Utf8Value hStringVal0( | |
| 145 hValue.As<v8::Object>()->Get(0)->ToString()); | |
| 146 utf8Name = *hStringVal0; | |
| 147 v8::String::Utf8Value hStringVal1( | |
| 148 hValue.As<v8::Object>()->Get(1)->ToString()); | |
| 149 utf8Message = *hStringVal1; | |
| 150 return TRUE; | |
| 151 } | |
| 152 | |
| 153 FX_BOOL FXJSE_ReturnValue_GetLineInfo(FXJSE_HVALUE hRetValue, | |
| 154 int32_t& nLine, | |
| 155 int32_t& nCol) { | |
| 156 CFXJSE_Value* lpValue = reinterpret_cast<CFXJSE_Value*>(hRetValue); | |
| 157 if (!lpValue) { | |
| 158 return FALSE; | |
| 159 } | |
| 160 v8::Isolate* pIsolate = lpValue->GetIsolate(); | |
| 161 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(pIsolate); | |
| 162 v8::Local<v8::Value> hValue = | |
| 163 v8::Local<v8::Value>::New(pIsolate, lpValue->DirectGetValue()); | |
| 164 if (!hValue->IsObject()) { | |
| 165 return FALSE; | |
| 166 } | |
| 167 v8::MaybeLocal<v8::Int32> maybe_int = | |
| 168 hValue.As<v8::Object>()->Get(3)->ToInt32(pIsolate->GetCurrentContext()); | |
| 169 nLine = maybe_int.FromMaybe(v8::Local<v8::Int32>())->Value(); | |
| 170 maybe_int = | |
| 171 hValue.As<v8::Object>()->Get(5)->ToInt32(pIsolate->GetCurrentContext()); | |
| 172 nCol = maybe_int.FromMaybe(v8::Local<v8::Int32>())->Value(); | |
| 173 return TRUE; | |
| 174 } | |
| 175 | |
| 176 CFXJSE_Context* CFXJSE_Context::Create(v8::Isolate* pIsolate, | |
| 177 const FXJSE_CLASS* lpGlobalClass, | |
| 178 void* lpGlobalObject) { | |
| 179 CFXJSE_ScopeUtil_IsolateHandle scope(pIsolate); | |
| 180 CFXJSE_Context* pContext = new CFXJSE_Context(pIsolate); | |
| 181 CFXJSE_Class* lpGlobalClassObj = NULL; | |
| 182 v8::Local<v8::ObjectTemplate> hObjectTemplate; | |
| 183 if (lpGlobalClass) { | |
| 184 lpGlobalClassObj = CFXJSE_Class::Create(pContext, lpGlobalClass, TRUE); | |
| 185 ASSERT(lpGlobalClassObj); | |
| 186 v8::Local<v8::FunctionTemplate> hFunctionTemplate = | |
| 187 v8::Local<v8::FunctionTemplate>::New(pIsolate, | |
| 188 lpGlobalClassObj->m_hTemplate); | |
| 189 hObjectTemplate = hFunctionTemplate->InstanceTemplate(); | |
| 190 } else { | |
| 191 hObjectTemplate = v8::ObjectTemplate::New(pIsolate); | |
| 192 hObjectTemplate->SetInternalFieldCount(1); | |
| 193 } | |
| 194 v8::Local<v8::Context> hNewContext = | |
| 195 v8::Context::New(pIsolate, NULL, hObjectTemplate); | |
| 196 v8::Local<v8::Context> hRootContext = v8::Local<v8::Context>::New( | |
| 197 pIsolate, CFXJSE_RuntimeData::Get(pIsolate)->m_hRootContext); | |
| 198 hNewContext->SetSecurityToken(hRootContext->GetSecurityToken()); | |
| 199 v8::Local<v8::Object> hGlobalObject = | |
| 200 FXJSE_GetGlobalObjectFromContext(hNewContext); | |
| 201 FXJSE_UpdateObjectBinding(hGlobalObject, lpGlobalObject); | |
| 202 pContext->m_hContext.Reset(pIsolate, hNewContext); | |
| 203 return pContext; | |
| 204 } | |
| 205 | |
| 206 CFXJSE_Context::~CFXJSE_Context() { | |
| 207 for (int32_t i = 0, count = m_rgClasses.GetSize(); i < count; i++) { | |
| 208 CFXJSE_Class* pClass = m_rgClasses[i]; | |
| 209 if (pClass) { | |
| 210 delete pClass; | |
| 211 } | |
| 212 } | |
| 213 m_rgClasses.RemoveAll(); | |
| 214 } | |
| 215 | |
| 216 void CFXJSE_Context::GetGlobalObject(CFXJSE_Value* pValue) { | |
| 217 ASSERT(pValue); | |
| 218 CFXJSE_ScopeUtil_IsolateHandleContext scope(this); | |
| 219 v8::Local<v8::Context> hContext = | |
| 220 v8::Local<v8::Context>::New(m_pIsolate, m_hContext); | |
| 221 v8::Local<v8::Object> hGlobalObject = hContext->Global(); | |
| 222 pValue->ForceSetValue(hGlobalObject); | |
| 223 } | |
| 224 | |
| 225 FX_BOOL CFXJSE_Context::ExecuteScript(const FX_CHAR* szScript, | |
| 226 CFXJSE_Value* lpRetValue, | |
| 227 CFXJSE_Value* lpNewThisObject) { | |
| 228 CFXJSE_ScopeUtil_IsolateHandleContext scope(this); | |
| 229 v8::TryCatch trycatch(m_pIsolate); | |
| 230 v8::Local<v8::String> hScriptString = | |
| 231 v8::String::NewFromUtf8(m_pIsolate, szScript); | |
| 232 if (lpNewThisObject == NULL) { | |
| 233 v8::Local<v8::Script> hScript = v8::Script::Compile(hScriptString); | |
| 234 if (!trycatch.HasCaught()) { | |
| 235 v8::Local<v8::Value> hValue = hScript->Run(); | |
| 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 } else { | |
| 249 v8::Local<v8::Value> hNewThis = | |
| 250 v8::Local<v8::Value>::New(m_pIsolate, lpNewThisObject->m_hValue); | |
| 251 ASSERT(!hNewThis.IsEmpty()); | |
| 252 v8::Local<v8::Script> hWrapper = | |
| 253 v8::Script::Compile(v8::String::NewFromUtf8( | |
| 254 m_pIsolate, "(function () { return eval(arguments[0]); })")); | |
| 255 v8::Local<v8::Value> hWrapperValue = hWrapper->Run(); | |
| 256 ASSERT(hWrapperValue->IsFunction()); | |
| 257 v8::Local<v8::Function> hWrapperFn = hWrapperValue.As<v8::Function>(); | |
| 258 if (!trycatch.HasCaught()) { | |
| 259 v8::Local<v8::Value> rgArgs[] = {hScriptString}; | |
| 260 v8::Local<v8::Value> hValue = | |
| 261 hWrapperFn->Call(hNewThis.As<v8::Object>(), 1, rgArgs); | |
| 262 if (!trycatch.HasCaught()) { | |
| 263 if (lpRetValue) { | |
| 264 lpRetValue->m_hValue.Reset(m_pIsolate, hValue); | |
| 265 } | |
| 266 return TRUE; | |
| 267 } | |
| 268 } | |
| 269 if (lpRetValue) { | |
| 270 lpRetValue->m_hValue.Reset(m_pIsolate, | |
| 271 FXJSE_CreateReturnValue(m_pIsolate, trycatch)); | |
| 272 } | |
| 273 return FALSE; | |
| 274 } | |
| 275 } | |
| OLD | NEW |