dsinclair | 08fea80 | 2016-07-12 10:37:52 -0700 | [diff] [blame] | 1 | // Copyright 2016 PDFium Authors. All rights reserved. |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 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 | |
dsinclair | 4355468 | 2016-09-29 17:29:48 -0700 | [diff] [blame] | 7 | #include "fxjs/cfxjse_context.h" |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 8 | |
dsinclair | 4355468 | 2016-09-29 17:29:48 -0700 | [diff] [blame] | 9 | #include "fxjs/cfxjse_class.h" |
| 10 | #include "fxjs/cfxjse_value.h" |
Dan Sinclair | 0bb1333 | 2017-03-30 16:12:02 -0400 | [diff] [blame^] | 11 | #include "third_party/base/ptr_util.h" |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 12 | |
tsepez | fb2a824 | 2016-06-01 16:10:41 -0700 | [diff] [blame] | 13 | namespace { |
| 14 | |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 15 | const char szCompatibleModeScript[] = |
tsepez | fb2a824 | 2016-06-01 16:10:41 -0700 | [diff] [blame] | 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 | |
dsinclair | 08fea80 | 2016-07-12 10:37:52 -0700 | [diff] [blame] | 44 | // Note, not in the anonymous namespace due to the friend call |
| 45 | // in cfxjse_context.h |
| 46 | // TODO(dsinclair): Remove the friending, use public methods. |
| 47 | class CFXJSE_ScopeUtil_IsolateHandleContext { |
| 48 | public: |
| 49 | explicit CFXJSE_ScopeUtil_IsolateHandleContext(CFXJSE_Context* pContext) |
| 50 | : m_context(pContext), |
| 51 | m_parent(pContext->m_pIsolate), |
| 52 | m_cscope(v8::Local<v8::Context>::New(pContext->m_pIsolate, |
| 53 | pContext->m_hContext)) {} |
| 54 | v8::Isolate* GetIsolate() { return m_context->m_pIsolate; } |
| 55 | v8::Local<v8::Context> GetLocalContext() { |
| 56 | return v8::Local<v8::Context>::New(m_context->m_pIsolate, |
| 57 | m_context->m_hContext); |
| 58 | } |
| 59 | |
| 60 | private: |
| 61 | CFXJSE_ScopeUtil_IsolateHandleContext( |
| 62 | const CFXJSE_ScopeUtil_IsolateHandleContext&) = delete; |
| 63 | void operator=(const CFXJSE_ScopeUtil_IsolateHandleContext&) = delete; |
| 64 | void* operator new(size_t size) = delete; |
| 65 | void operator delete(void*, size_t) = delete; |
| 66 | |
| 67 | CFXJSE_Context* m_context; |
| 68 | CFXJSE_ScopeUtil_IsolateHandle m_parent; |
| 69 | v8::Context::Scope m_cscope; |
| 70 | }; |
| 71 | |
tsepez | 3a005f2 | 2016-05-27 17:45:00 -0700 | [diff] [blame] | 72 | v8::Local<v8::Object> FXJSE_GetGlobalObjectFromContext( |
| 73 | const v8::Local<v8::Context>& hContext) { |
| 74 | return hContext->Global()->GetPrototype().As<v8::Object>(); |
| 75 | } |
| 76 | |
| 77 | void FXJSE_UpdateObjectBinding(v8::Local<v8::Object>& hObject, |
tsepez | 29adee7 | 2016-05-31 14:22:09 -0700 | [diff] [blame] | 78 | CFXJSE_HostObject* lpNewBinding) { |
tsepez | 3a005f2 | 2016-05-27 17:45:00 -0700 | [diff] [blame] | 79 | ASSERT(!hObject.IsEmpty()); |
| 80 | ASSERT(hObject->InternalFieldCount() > 0); |
tsepez | 29adee7 | 2016-05-31 14:22:09 -0700 | [diff] [blame] | 81 | hObject->SetAlignedPointerInInternalField(0, |
| 82 | static_cast<void*>(lpNewBinding)); |
tsepez | 3a005f2 | 2016-05-27 17:45:00 -0700 | [diff] [blame] | 83 | } |
| 84 | |
tsepez | 29adee7 | 2016-05-31 14:22:09 -0700 | [diff] [blame] | 85 | CFXJSE_HostObject* FXJSE_RetrieveObjectBinding( |
| 86 | const v8::Local<v8::Object>& hJSObject, |
| 87 | CFXJSE_Class* lpClass) { |
tsepez | 3a005f2 | 2016-05-27 17:45:00 -0700 | [diff] [blame] | 88 | ASSERT(!hJSObject.IsEmpty()); |
dsinclair | 08fea80 | 2016-07-12 10:37:52 -0700 | [diff] [blame] | 89 | if (!hJSObject->IsObject()) |
tsepez | 29adee7 | 2016-05-31 14:22:09 -0700 | [diff] [blame] | 90 | return nullptr; |
dsinclair | 08fea80 | 2016-07-12 10:37:52 -0700 | [diff] [blame] | 91 | |
tsepez | 3a005f2 | 2016-05-27 17:45:00 -0700 | [diff] [blame] | 92 | v8::Local<v8::Object> hObject = hJSObject; |
| 93 | if (hObject->InternalFieldCount() == 0) { |
| 94 | v8::Local<v8::Value> hProtoObject = hObject->GetPrototype(); |
dsinclair | 08fea80 | 2016-07-12 10:37:52 -0700 | [diff] [blame] | 95 | if (hProtoObject.IsEmpty() || !hProtoObject->IsObject()) |
tsepez | 29adee7 | 2016-05-31 14:22:09 -0700 | [diff] [blame] | 96 | return nullptr; |
dsinclair | 08fea80 | 2016-07-12 10:37:52 -0700 | [diff] [blame] | 97 | |
tsepez | 3a005f2 | 2016-05-27 17:45:00 -0700 | [diff] [blame] | 98 | hObject = hProtoObject.As<v8::Object>(); |
dsinclair | 08fea80 | 2016-07-12 10:37:52 -0700 | [diff] [blame] | 99 | if (hObject->InternalFieldCount() == 0) |
tsepez | 29adee7 | 2016-05-31 14:22:09 -0700 | [diff] [blame] | 100 | return nullptr; |
tsepez | 3a005f2 | 2016-05-27 17:45:00 -0700 | [diff] [blame] | 101 | } |
| 102 | if (lpClass) { |
| 103 | v8::Local<v8::FunctionTemplate> hClass = |
| 104 | v8::Local<v8::FunctionTemplate>::New( |
| 105 | lpClass->GetContext()->GetRuntime(), lpClass->GetTemplate()); |
dsinclair | 08fea80 | 2016-07-12 10:37:52 -0700 | [diff] [blame] | 106 | if (!hClass->HasInstance(hObject)) |
tsepez | 29adee7 | 2016-05-31 14:22:09 -0700 | [diff] [blame] | 107 | return nullptr; |
tsepez | 3a005f2 | 2016-05-27 17:45:00 -0700 | [diff] [blame] | 108 | } |
tsepez | 29adee7 | 2016-05-31 14:22:09 -0700 | [diff] [blame] | 109 | return static_cast<CFXJSE_HostObject*>( |
| 110 | hObject->GetAlignedPointerFromInternalField(0)); |
tsepez | 3a005f2 | 2016-05-27 17:45:00 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 113 | v8::Local<v8::Object> FXJSE_CreateReturnValue(v8::Isolate* pIsolate, |
| 114 | v8::TryCatch& trycatch) { |
| 115 | v8::Local<v8::Object> hReturnValue = v8::Object::New(pIsolate); |
| 116 | if (trycatch.HasCaught()) { |
| 117 | v8::Local<v8::Value> hException = trycatch.Exception(); |
| 118 | v8::Local<v8::Message> hMessage = trycatch.Message(); |
| 119 | if (hException->IsObject()) { |
| 120 | v8::Local<v8::Value> hValue; |
| 121 | hValue = hException.As<v8::Object>()->Get( |
| 122 | v8::String::NewFromUtf8(pIsolate, "name")); |
dsinclair | 08fea80 | 2016-07-12 10:37:52 -0700 | [diff] [blame] | 123 | if (hValue->IsString() || hValue->IsStringObject()) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 124 | hReturnValue->Set(0, hValue); |
dsinclair | 08fea80 | 2016-07-12 10:37:52 -0700 | [diff] [blame] | 125 | else |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 126 | hReturnValue->Set(0, v8::String::NewFromUtf8(pIsolate, "Error")); |
dsinclair | 08fea80 | 2016-07-12 10:37:52 -0700 | [diff] [blame] | 127 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 128 | hValue = hException.As<v8::Object>()->Get( |
| 129 | v8::String::NewFromUtf8(pIsolate, "message")); |
dsinclair | 08fea80 | 2016-07-12 10:37:52 -0700 | [diff] [blame] | 130 | if (hValue->IsString() || hValue->IsStringObject()) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 131 | hReturnValue->Set(1, hValue); |
dsinclair | 08fea80 | 2016-07-12 10:37:52 -0700 | [diff] [blame] | 132 | else |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 133 | hReturnValue->Set(1, hMessage->Get()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 134 | } else { |
| 135 | hReturnValue->Set(0, v8::String::NewFromUtf8(pIsolate, "Error")); |
| 136 | hReturnValue->Set(1, hMessage->Get()); |
| 137 | } |
| 138 | hReturnValue->Set(2, hException); |
| 139 | hReturnValue->Set(3, v8::Integer::New(pIsolate, hMessage->GetLineNumber())); |
| 140 | hReturnValue->Set(4, hMessage->GetSourceLine()); |
| 141 | v8::Maybe<int32_t> maybe_int = |
| 142 | hMessage->GetStartColumn(pIsolate->GetCurrentContext()); |
| 143 | hReturnValue->Set(5, v8::Integer::New(pIsolate, maybe_int.FromMaybe(0))); |
| 144 | maybe_int = hMessage->GetEndColumn(pIsolate->GetCurrentContext()); |
| 145 | hReturnValue->Set(6, v8::Integer::New(pIsolate, maybe_int.FromMaybe(0))); |
| 146 | } |
| 147 | return hReturnValue; |
| 148 | } |
| 149 | |
dsinclair | 08fea80 | 2016-07-12 10:37:52 -0700 | [diff] [blame] | 150 | // static |
tsepez | e3b2a4e | 2016-05-26 12:39:34 -0700 | [diff] [blame] | 151 | CFXJSE_Context* CFXJSE_Context::Create( |
| 152 | v8::Isolate* pIsolate, |
| 153 | const FXJSE_CLASS_DESCRIPTOR* lpGlobalClass, |
tsepez | 29adee7 | 2016-05-31 14:22:09 -0700 | [diff] [blame] | 154 | CFXJSE_HostObject* lpGlobalObject) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 155 | CFXJSE_ScopeUtil_IsolateHandle scope(pIsolate); |
| 156 | CFXJSE_Context* pContext = new CFXJSE_Context(pIsolate); |
dsinclair | 08fea80 | 2016-07-12 10:37:52 -0700 | [diff] [blame] | 157 | CFXJSE_Class* lpGlobalClassObj = nullptr; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 158 | v8::Local<v8::ObjectTemplate> hObjectTemplate; |
| 159 | if (lpGlobalClass) { |
tsepez | 304bb91 | 2016-11-03 06:10:26 -0700 | [diff] [blame] | 160 | lpGlobalClassObj = CFXJSE_Class::Create(pContext, lpGlobalClass, true); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 161 | ASSERT(lpGlobalClassObj); |
| 162 | v8::Local<v8::FunctionTemplate> hFunctionTemplate = |
| 163 | v8::Local<v8::FunctionTemplate>::New(pIsolate, |
| 164 | lpGlobalClassObj->m_hTemplate); |
| 165 | hObjectTemplate = hFunctionTemplate->InstanceTemplate(); |
| 166 | } else { |
| 167 | hObjectTemplate = v8::ObjectTemplate::New(pIsolate); |
| 168 | hObjectTemplate->SetInternalFieldCount(1); |
| 169 | } |
jochen | 7e6a848 | 2016-07-06 11:02:27 -0700 | [diff] [blame] | 170 | hObjectTemplate->Set( |
| 171 | v8::Symbol::GetToStringTag(pIsolate), |
| 172 | v8::String::NewFromUtf8(pIsolate, "global", v8::NewStringType::kNormal) |
| 173 | .ToLocalChecked()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 174 | v8::Local<v8::Context> hNewContext = |
dsinclair | 08fea80 | 2016-07-12 10:37:52 -0700 | [diff] [blame] | 175 | v8::Context::New(pIsolate, nullptr, hObjectTemplate); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 176 | v8::Local<v8::Context> hRootContext = v8::Local<v8::Context>::New( |
| 177 | pIsolate, CFXJSE_RuntimeData::Get(pIsolate)->m_hRootContext); |
| 178 | hNewContext->SetSecurityToken(hRootContext->GetSecurityToken()); |
| 179 | v8::Local<v8::Object> hGlobalObject = |
| 180 | FXJSE_GetGlobalObjectFromContext(hNewContext); |
| 181 | FXJSE_UpdateObjectBinding(hGlobalObject, lpGlobalObject); |
| 182 | pContext->m_hContext.Reset(pIsolate, hNewContext); |
| 183 | return pContext; |
| 184 | } |
| 185 | |
tsepez | 56286b3 | 2016-05-17 16:24:34 -0700 | [diff] [blame] | 186 | CFXJSE_Context::CFXJSE_Context(v8::Isolate* pIsolate) : m_pIsolate(pIsolate) {} |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 187 | |
tsepez | 56286b3 | 2016-05-17 16:24:34 -0700 | [diff] [blame] | 188 | CFXJSE_Context::~CFXJSE_Context() {} |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 189 | |
dsinclair | 3cace32 | 2016-06-09 11:49:22 -0700 | [diff] [blame] | 190 | std::unique_ptr<CFXJSE_Value> CFXJSE_Context::GetGlobalObject() { |
Dan Sinclair | 0bb1333 | 2017-03-30 16:12:02 -0400 | [diff] [blame^] | 191 | auto pValue = pdfium::MakeUnique<CFXJSE_Value>(m_pIsolate); |
dsinclair | 3cace32 | 2016-06-09 11:49:22 -0700 | [diff] [blame] | 192 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 193 | CFXJSE_ScopeUtil_IsolateHandleContext scope(this); |
| 194 | v8::Local<v8::Context> hContext = |
| 195 | v8::Local<v8::Context>::New(m_pIsolate, m_hContext); |
| 196 | v8::Local<v8::Object> hGlobalObject = hContext->Global(); |
| 197 | pValue->ForceSetValue(hGlobalObject); |
dsinclair | 3cace32 | 2016-06-09 11:49:22 -0700 | [diff] [blame] | 198 | |
| 199 | return pValue; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 200 | } |
| 201 | |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 202 | void CFXJSE_Context::EnableCompatibleMode() { |
| 203 | ExecuteScript(szCompatibleModeScript, nullptr, nullptr); |
| 204 | } |
| 205 | |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 206 | bool CFXJSE_Context::ExecuteScript(const char* szScript, |
tsepez | 304bb91 | 2016-11-03 06:10:26 -0700 | [diff] [blame] | 207 | CFXJSE_Value* lpRetValue, |
| 208 | CFXJSE_Value* lpNewThisObject) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 209 | CFXJSE_ScopeUtil_IsolateHandleContext scope(this); |
| 210 | v8::TryCatch trycatch(m_pIsolate); |
| 211 | v8::Local<v8::String> hScriptString = |
| 212 | v8::String::NewFromUtf8(m_pIsolate, szScript); |
dsinclair | 08fea80 | 2016-07-12 10:37:52 -0700 | [diff] [blame] | 213 | if (!lpNewThisObject) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 214 | v8::Local<v8::Script> hScript = v8::Script::Compile(hScriptString); |
| 215 | if (!trycatch.HasCaught()) { |
| 216 | v8::Local<v8::Value> hValue = hScript->Run(); |
| 217 | if (!trycatch.HasCaught()) { |
tsepez | 304bb91 | 2016-11-03 06:10:26 -0700 | [diff] [blame] | 218 | if (lpRetValue) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 219 | lpRetValue->m_hValue.Reset(m_pIsolate, hValue); |
tsepez | 304bb91 | 2016-11-03 06:10:26 -0700 | [diff] [blame] | 220 | return true; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | if (lpRetValue) { |
| 224 | lpRetValue->m_hValue.Reset(m_pIsolate, |
| 225 | FXJSE_CreateReturnValue(m_pIsolate, trycatch)); |
| 226 | } |
tsepez | 304bb91 | 2016-11-03 06:10:26 -0700 | [diff] [blame] | 227 | return false; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 228 | } |
tsepez | 304bb91 | 2016-11-03 06:10:26 -0700 | [diff] [blame] | 229 | |
| 230 | v8::Local<v8::Value> hNewThis = |
| 231 | v8::Local<v8::Value>::New(m_pIsolate, lpNewThisObject->m_hValue); |
| 232 | ASSERT(!hNewThis.IsEmpty()); |
| 233 | v8::Local<v8::Script> hWrapper = v8::Script::Compile(v8::String::NewFromUtf8( |
| 234 | m_pIsolate, "(function () { return eval(arguments[0]); })")); |
| 235 | v8::Local<v8::Value> hWrapperValue = hWrapper->Run(); |
| 236 | ASSERT(hWrapperValue->IsFunction()); |
| 237 | v8::Local<v8::Function> hWrapperFn = hWrapperValue.As<v8::Function>(); |
| 238 | if (!trycatch.HasCaught()) { |
| 239 | v8::Local<v8::Value> rgArgs[] = {hScriptString}; |
| 240 | v8::Local<v8::Value> hValue = |
| 241 | hWrapperFn->Call(hNewThis.As<v8::Object>(), 1, rgArgs); |
| 242 | if (!trycatch.HasCaught()) { |
| 243 | if (lpRetValue) |
| 244 | lpRetValue->m_hValue.Reset(m_pIsolate, hValue); |
| 245 | return true; |
| 246 | } |
| 247 | } |
| 248 | if (lpRetValue) { |
| 249 | lpRetValue->m_hValue.Reset(m_pIsolate, |
| 250 | FXJSE_CreateReturnValue(m_pIsolate, trycatch)); |
| 251 | } |
| 252 | return false; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 253 | } |