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