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