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" |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 11 | #include "xfa/fxjse/value.h" |
| 12 | |
tsepez | fb2a824 | 2016-06-01 16:10:41 -0700 | [diff] [blame] | 13 | namespace { |
| 14 | |
| 15 | const FX_CHAR szCompatibleModeScript[] = |
| 16 | "(function(global, list) {\n" |
| 17 | " 'use strict';\n" |
| 18 | " var objname;\n" |
| 19 | " for (objname in list) {\n" |
| 20 | " var globalobj = global[objname];\n" |
| 21 | " if (globalobj) {\n" |
| 22 | " list[objname].forEach(function(name) {\n" |
| 23 | " if (!globalobj[name]) {\n" |
| 24 | " Object.defineProperty(globalobj, name, {\n" |
| 25 | " writable: true,\n" |
| 26 | " enumerable: false,\n" |
| 27 | " value: (function(obj) {\n" |
| 28 | " if (arguments.length === 0) {\n" |
| 29 | " throw new TypeError('missing argument 0 when calling " |
| 30 | " function ' + objname + '.' + name);\n" |
| 31 | " }\n" |
| 32 | " return globalobj.prototype[name].apply(obj, " |
| 33 | " Array.prototype.slice.call(arguments, 1));\n" |
| 34 | " })\n" |
| 35 | " });\n" |
| 36 | " }\n" |
| 37 | " });\n" |
| 38 | " }\n" |
| 39 | " }\n" |
| 40 | "}(this, {String: ['substr', 'toUpperCase']}));"; |
| 41 | |
| 42 | } // namespace |
| 43 | |
tsepez | 3a005f2 | 2016-05-27 17:45:00 -0700 | [diff] [blame] | 44 | v8::Local<v8::Object> FXJSE_GetGlobalObjectFromContext( |
| 45 | const v8::Local<v8::Context>& hContext) { |
| 46 | return hContext->Global()->GetPrototype().As<v8::Object>(); |
| 47 | } |
| 48 | |
| 49 | void FXJSE_UpdateObjectBinding(v8::Local<v8::Object>& hObject, |
tsepez | 29adee7 | 2016-05-31 14:22:09 -0700 | [diff] [blame] | 50 | CFXJSE_HostObject* lpNewBinding) { |
tsepez | 3a005f2 | 2016-05-27 17:45:00 -0700 | [diff] [blame] | 51 | ASSERT(!hObject.IsEmpty()); |
| 52 | ASSERT(hObject->InternalFieldCount() > 0); |
tsepez | 29adee7 | 2016-05-31 14:22:09 -0700 | [diff] [blame] | 53 | hObject->SetAlignedPointerInInternalField(0, |
| 54 | static_cast<void*>(lpNewBinding)); |
tsepez | 3a005f2 | 2016-05-27 17:45:00 -0700 | [diff] [blame] | 55 | } |
| 56 | |
tsepez | 29adee7 | 2016-05-31 14:22:09 -0700 | [diff] [blame] | 57 | CFXJSE_HostObject* FXJSE_RetrieveObjectBinding( |
| 58 | const v8::Local<v8::Object>& hJSObject, |
| 59 | CFXJSE_Class* lpClass) { |
tsepez | 3a005f2 | 2016-05-27 17:45:00 -0700 | [diff] [blame] | 60 | ASSERT(!hJSObject.IsEmpty()); |
| 61 | if (!hJSObject->IsObject()) { |
tsepez | 29adee7 | 2016-05-31 14:22:09 -0700 | [diff] [blame] | 62 | return nullptr; |
tsepez | 3a005f2 | 2016-05-27 17:45:00 -0700 | [diff] [blame] | 63 | } |
| 64 | v8::Local<v8::Object> hObject = hJSObject; |
| 65 | if (hObject->InternalFieldCount() == 0) { |
| 66 | v8::Local<v8::Value> hProtoObject = hObject->GetPrototype(); |
| 67 | if (hProtoObject.IsEmpty() || !hProtoObject->IsObject()) { |
tsepez | 29adee7 | 2016-05-31 14:22:09 -0700 | [diff] [blame] | 68 | return nullptr; |
tsepez | 3a005f2 | 2016-05-27 17:45:00 -0700 | [diff] [blame] | 69 | } |
| 70 | hObject = hProtoObject.As<v8::Object>(); |
| 71 | if (hObject->InternalFieldCount() == 0) { |
tsepez | 29adee7 | 2016-05-31 14:22:09 -0700 | [diff] [blame] | 72 | return nullptr; |
tsepez | 3a005f2 | 2016-05-27 17:45:00 -0700 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | if (lpClass) { |
| 76 | v8::Local<v8::FunctionTemplate> hClass = |
| 77 | v8::Local<v8::FunctionTemplate>::New( |
| 78 | lpClass->GetContext()->GetRuntime(), lpClass->GetTemplate()); |
| 79 | if (!hClass->HasInstance(hObject)) { |
tsepez | 29adee7 | 2016-05-31 14:22:09 -0700 | [diff] [blame] | 80 | return nullptr; |
tsepez | 3a005f2 | 2016-05-27 17:45:00 -0700 | [diff] [blame] | 81 | } |
| 82 | } |
tsepez | 29adee7 | 2016-05-31 14:22:09 -0700 | [diff] [blame] | 83 | return static_cast<CFXJSE_HostObject*>( |
| 84 | hObject->GetAlignedPointerFromInternalField(0)); |
tsepez | 3a005f2 | 2016-05-27 17:45:00 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 87 | v8::Local<v8::Object> FXJSE_CreateReturnValue(v8::Isolate* pIsolate, |
| 88 | v8::TryCatch& trycatch) { |
| 89 | v8::Local<v8::Object> hReturnValue = v8::Object::New(pIsolate); |
| 90 | if (trycatch.HasCaught()) { |
| 91 | v8::Local<v8::Value> hException = trycatch.Exception(); |
| 92 | v8::Local<v8::Message> hMessage = trycatch.Message(); |
| 93 | if (hException->IsObject()) { |
| 94 | v8::Local<v8::Value> hValue; |
| 95 | hValue = hException.As<v8::Object>()->Get( |
| 96 | v8::String::NewFromUtf8(pIsolate, "name")); |
| 97 | if (hValue->IsString() || hValue->IsStringObject()) { |
| 98 | hReturnValue->Set(0, hValue); |
| 99 | } else { |
| 100 | hReturnValue->Set(0, v8::String::NewFromUtf8(pIsolate, "Error")); |
| 101 | } |
| 102 | hValue = hException.As<v8::Object>()->Get( |
| 103 | v8::String::NewFromUtf8(pIsolate, "message")); |
| 104 | if (hValue->IsString() || hValue->IsStringObject()) { |
| 105 | hReturnValue->Set(1, hValue); |
| 106 | } else { |
| 107 | hReturnValue->Set(1, hMessage->Get()); |
| 108 | } |
| 109 | } else { |
| 110 | hReturnValue->Set(0, v8::String::NewFromUtf8(pIsolate, "Error")); |
| 111 | hReturnValue->Set(1, hMessage->Get()); |
| 112 | } |
| 113 | hReturnValue->Set(2, hException); |
| 114 | hReturnValue->Set(3, v8::Integer::New(pIsolate, hMessage->GetLineNumber())); |
| 115 | hReturnValue->Set(4, hMessage->GetSourceLine()); |
| 116 | v8::Maybe<int32_t> maybe_int = |
| 117 | hMessage->GetStartColumn(pIsolate->GetCurrentContext()); |
| 118 | hReturnValue->Set(5, v8::Integer::New(pIsolate, maybe_int.FromMaybe(0))); |
| 119 | maybe_int = hMessage->GetEndColumn(pIsolate->GetCurrentContext()); |
| 120 | hReturnValue->Set(6, v8::Integer::New(pIsolate, maybe_int.FromMaybe(0))); |
| 121 | } |
| 122 | return hReturnValue; |
| 123 | } |
| 124 | |
tsepez | e3b2a4e | 2016-05-26 12:39:34 -0700 | [diff] [blame] | 125 | CFXJSE_Context* CFXJSE_Context::Create( |
| 126 | v8::Isolate* pIsolate, |
| 127 | const FXJSE_CLASS_DESCRIPTOR* lpGlobalClass, |
tsepez | 29adee7 | 2016-05-31 14:22:09 -0700 | [diff] [blame] | 128 | CFXJSE_HostObject* lpGlobalObject) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 129 | CFXJSE_ScopeUtil_IsolateHandle scope(pIsolate); |
| 130 | CFXJSE_Context* pContext = new CFXJSE_Context(pIsolate); |
| 131 | CFXJSE_Class* lpGlobalClassObj = NULL; |
| 132 | v8::Local<v8::ObjectTemplate> hObjectTemplate; |
| 133 | if (lpGlobalClass) { |
| 134 | lpGlobalClassObj = CFXJSE_Class::Create(pContext, lpGlobalClass, TRUE); |
| 135 | ASSERT(lpGlobalClassObj); |
| 136 | v8::Local<v8::FunctionTemplate> hFunctionTemplate = |
| 137 | v8::Local<v8::FunctionTemplate>::New(pIsolate, |
| 138 | lpGlobalClassObj->m_hTemplate); |
| 139 | hObjectTemplate = hFunctionTemplate->InstanceTemplate(); |
| 140 | } else { |
| 141 | hObjectTemplate = v8::ObjectTemplate::New(pIsolate); |
| 142 | hObjectTemplate->SetInternalFieldCount(1); |
| 143 | } |
| 144 | v8::Local<v8::Context> hNewContext = |
| 145 | v8::Context::New(pIsolate, NULL, hObjectTemplate); |
| 146 | v8::Local<v8::Context> hRootContext = v8::Local<v8::Context>::New( |
| 147 | pIsolate, CFXJSE_RuntimeData::Get(pIsolate)->m_hRootContext); |
| 148 | hNewContext->SetSecurityToken(hRootContext->GetSecurityToken()); |
| 149 | v8::Local<v8::Object> hGlobalObject = |
| 150 | FXJSE_GetGlobalObjectFromContext(hNewContext); |
| 151 | FXJSE_UpdateObjectBinding(hGlobalObject, lpGlobalObject); |
| 152 | pContext->m_hContext.Reset(pIsolate, hNewContext); |
| 153 | return pContext; |
| 154 | } |
| 155 | |
tsepez | 56286b3 | 2016-05-17 16:24:34 -0700 | [diff] [blame] | 156 | CFXJSE_Context::CFXJSE_Context(v8::Isolate* pIsolate) : m_pIsolate(pIsolate) {} |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame^] | 157 | |
tsepez | 56286b3 | 2016-05-17 16:24:34 -0700 | [diff] [blame] | 158 | CFXJSE_Context::~CFXJSE_Context() {} |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 159 | |
| 160 | void CFXJSE_Context::GetGlobalObject(CFXJSE_Value* pValue) { |
| 161 | ASSERT(pValue); |
| 162 | CFXJSE_ScopeUtil_IsolateHandleContext scope(this); |
| 163 | v8::Local<v8::Context> hContext = |
| 164 | v8::Local<v8::Context>::New(m_pIsolate, m_hContext); |
| 165 | v8::Local<v8::Object> hGlobalObject = hContext->Global(); |
| 166 | pValue->ForceSetValue(hGlobalObject); |
| 167 | } |
| 168 | |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame^] | 169 | void CFXJSE_Context::EnableCompatibleMode() { |
| 170 | ExecuteScript(szCompatibleModeScript, nullptr, nullptr); |
| 171 | } |
| 172 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 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 | } |