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