blob: ad6dbac0ade5bf49ebd8d014c6104b14b6186c47 [file] [log] [blame]
dsinclair08fea802016-07-12 10:37:52 -07001// Copyright 2016 PDFium Authors. All rights reserved.
Dan Sinclair1770c022016-03-14 14:14:16 -04002// 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
dsinclair43554682016-09-29 17:29:48 -07007#include "fxjs/cfxjse_context.h"
Dan Sinclair1770c022016-03-14 14:14:16 -04008
Tom Sepez80547a12017-04-25 12:43:13 -07009#include <utility>
10
dsinclair43554682016-09-29 17:29:48 -070011#include "fxjs/cfxjse_class.h"
12#include "fxjs/cfxjse_value.h"
Dan Sinclair0bb13332017-03-30 16:12:02 -040013#include "third_party/base/ptr_util.h"
Dan Sinclair1770c022016-03-14 14:14:16 -040014
tsepezfb2a8242016-06-01 16:10:41 -070015namespace {
16
Dan Sinclair812e96c2017-03-13 16:43:37 -040017const char szCompatibleModeScript[] =
tsepezfb2a8242016-06-01 16:10:41 -070018 "(function(global, list) {\n"
19 " 'use strict';\n"
20 " var objname;\n"
21 " for (objname in list) {\n"
22 " var globalobj = global[objname];\n"
23 " if (globalobj) {\n"
24 " list[objname].forEach(function(name) {\n"
25 " if (!globalobj[name]) {\n"
26 " Object.defineProperty(globalobj, name, {\n"
27 " writable: true,\n"
28 " enumerable: false,\n"
29 " value: (function(obj) {\n"
30 " if (arguments.length === 0) {\n"
31 " throw new TypeError('missing argument 0 when calling "
32 " function ' + objname + '.' + name);\n"
33 " }\n"
34 " return globalobj.prototype[name].apply(obj, "
35 " Array.prototype.slice.call(arguments, 1));\n"
36 " })\n"
37 " });\n"
38 " }\n"
39 " });\n"
40 " }\n"
41 " }\n"
42 "}(this, {String: ['substr', 'toUpperCase']}));";
43
Tom Sepez336544a2017-04-24 16:38:51 -070044wchar_t g_FXJSETagString[] = L"FXJSE_HostObject";
45
tsepezfb2a8242016-06-01 16:10:41 -070046} // namespace
47
dsinclair08fea802016-07-12 10:37:52 -070048// Note, not in the anonymous namespace due to the friend call
49// in cfxjse_context.h
50// TODO(dsinclair): Remove the friending, use public methods.
51class CFXJSE_ScopeUtil_IsolateHandleContext {
52 public:
53 explicit CFXJSE_ScopeUtil_IsolateHandleContext(CFXJSE_Context* pContext)
54 : m_context(pContext),
55 m_parent(pContext->m_pIsolate),
56 m_cscope(v8::Local<v8::Context>::New(pContext->m_pIsolate,
57 pContext->m_hContext)) {}
58 v8::Isolate* GetIsolate() { return m_context->m_pIsolate; }
59 v8::Local<v8::Context> GetLocalContext() {
60 return v8::Local<v8::Context>::New(m_context->m_pIsolate,
61 m_context->m_hContext);
62 }
63
64 private:
65 CFXJSE_ScopeUtil_IsolateHandleContext(
66 const CFXJSE_ScopeUtil_IsolateHandleContext&) = delete;
67 void operator=(const CFXJSE_ScopeUtil_IsolateHandleContext&) = delete;
68 void* operator new(size_t size) = delete;
69 void operator delete(void*, size_t) = delete;
70
Dan Sinclairaee0db02017-09-21 16:53:58 -040071 UnownedPtr<CFXJSE_Context> m_context;
dsinclair08fea802016-07-12 10:37:52 -070072 CFXJSE_ScopeUtil_IsolateHandle m_parent;
73 v8::Context::Scope m_cscope;
74};
75
tsepez3a005f22016-05-27 17:45:00 -070076v8::Local<v8::Object> FXJSE_GetGlobalObjectFromContext(
77 const v8::Local<v8::Context>& hContext) {
78 return hContext->Global()->GetPrototype().As<v8::Object>();
79}
80
81void FXJSE_UpdateObjectBinding(v8::Local<v8::Object>& hObject,
tsepez29adee72016-05-31 14:22:09 -070082 CFXJSE_HostObject* lpNewBinding) {
tsepez3a005f22016-05-27 17:45:00 -070083 ASSERT(!hObject.IsEmpty());
Tom Sepez336544a2017-04-24 16:38:51 -070084 ASSERT(hObject->InternalFieldCount() == 2);
85 hObject->SetAlignedPointerInInternalField(0, g_FXJSETagString);
86 hObject->SetAlignedPointerInInternalField(1, lpNewBinding);
tsepez3a005f22016-05-27 17:45:00 -070087}
88
tsepez29adee72016-05-31 14:22:09 -070089CFXJSE_HostObject* FXJSE_RetrieveObjectBinding(
90 const v8::Local<v8::Object>& hJSObject,
91 CFXJSE_Class* lpClass) {
tsepez3a005f22016-05-27 17:45:00 -070092 ASSERT(!hJSObject.IsEmpty());
dsinclair08fea802016-07-12 10:37:52 -070093 if (!hJSObject->IsObject())
tsepez29adee72016-05-31 14:22:09 -070094 return nullptr;
dsinclair08fea802016-07-12 10:37:52 -070095
tsepez3a005f22016-05-27 17:45:00 -070096 v8::Local<v8::Object> hObject = hJSObject;
Tom Sepez336544a2017-04-24 16:38:51 -070097 if (hObject->InternalFieldCount() != 2) {
tsepez3a005f22016-05-27 17:45:00 -070098 v8::Local<v8::Value> hProtoObject = hObject->GetPrototype();
dsinclair08fea802016-07-12 10:37:52 -070099 if (hProtoObject.IsEmpty() || !hProtoObject->IsObject())
tsepez29adee72016-05-31 14:22:09 -0700100 return nullptr;
dsinclair08fea802016-07-12 10:37:52 -0700101
tsepez3a005f22016-05-27 17:45:00 -0700102 hObject = hProtoObject.As<v8::Object>();
Tom Sepez336544a2017-04-24 16:38:51 -0700103 if (hObject->InternalFieldCount() != 2)
tsepez29adee72016-05-31 14:22:09 -0700104 return nullptr;
tsepez3a005f22016-05-27 17:45:00 -0700105 }
Tom Sepez336544a2017-04-24 16:38:51 -0700106 if (hObject->GetAlignedPointerFromInternalField(0) != g_FXJSETagString)
107 return nullptr;
tsepez3a005f22016-05-27 17:45:00 -0700108 if (lpClass) {
109 v8::Local<v8::FunctionTemplate> hClass =
110 v8::Local<v8::FunctionTemplate>::New(
Tom Sepez80547a12017-04-25 12:43:13 -0700111 lpClass->GetContext()->GetIsolate(), lpClass->GetTemplate());
dsinclair08fea802016-07-12 10:37:52 -0700112 if (!hClass->HasInstance(hObject))
tsepez29adee72016-05-31 14:22:09 -0700113 return nullptr;
tsepez3a005f22016-05-27 17:45:00 -0700114 }
tsepez29adee72016-05-31 14:22:09 -0700115 return static_cast<CFXJSE_HostObject*>(
Tom Sepez336544a2017-04-24 16:38:51 -0700116 hObject->GetAlignedPointerFromInternalField(1));
tsepez3a005f22016-05-27 17:45:00 -0700117}
118
Dan Sinclair1770c022016-03-14 14:14:16 -0400119v8::Local<v8::Object> FXJSE_CreateReturnValue(v8::Isolate* pIsolate,
120 v8::TryCatch& trycatch) {
121 v8::Local<v8::Object> hReturnValue = v8::Object::New(pIsolate);
122 if (trycatch.HasCaught()) {
123 v8::Local<v8::Value> hException = trycatch.Exception();
124 v8::Local<v8::Message> hMessage = trycatch.Message();
125 if (hException->IsObject()) {
126 v8::Local<v8::Value> hValue;
127 hValue = hException.As<v8::Object>()->Get(
128 v8::String::NewFromUtf8(pIsolate, "name"));
dsinclair08fea802016-07-12 10:37:52 -0700129 if (hValue->IsString() || hValue->IsStringObject())
Dan Sinclair1770c022016-03-14 14:14:16 -0400130 hReturnValue->Set(0, hValue);
dsinclair08fea802016-07-12 10:37:52 -0700131 else
Dan Sinclair1770c022016-03-14 14:14:16 -0400132 hReturnValue->Set(0, v8::String::NewFromUtf8(pIsolate, "Error"));
dsinclair08fea802016-07-12 10:37:52 -0700133
Dan Sinclair1770c022016-03-14 14:14:16 -0400134 hValue = hException.As<v8::Object>()->Get(
135 v8::String::NewFromUtf8(pIsolate, "message"));
dsinclair08fea802016-07-12 10:37:52 -0700136 if (hValue->IsString() || hValue->IsStringObject())
Dan Sinclair1770c022016-03-14 14:14:16 -0400137 hReturnValue->Set(1, hValue);
dsinclair08fea802016-07-12 10:37:52 -0700138 else
Dan Sinclair1770c022016-03-14 14:14:16 -0400139 hReturnValue->Set(1, hMessage->Get());
Dan Sinclair1770c022016-03-14 14:14:16 -0400140 } else {
141 hReturnValue->Set(0, v8::String::NewFromUtf8(pIsolate, "Error"));
142 hReturnValue->Set(1, hMessage->Get());
143 }
144 hReturnValue->Set(2, hException);
145 hReturnValue->Set(3, v8::Integer::New(pIsolate, hMessage->GetLineNumber()));
146 hReturnValue->Set(4, hMessage->GetSourceLine());
147 v8::Maybe<int32_t> maybe_int =
148 hMessage->GetStartColumn(pIsolate->GetCurrentContext());
149 hReturnValue->Set(5, v8::Integer::New(pIsolate, maybe_int.FromMaybe(0)));
150 maybe_int = hMessage->GetEndColumn(pIsolate->GetCurrentContext());
151 hReturnValue->Set(6, v8::Integer::New(pIsolate, maybe_int.FromMaybe(0)));
152 }
153 return hReturnValue;
154}
155
dsinclair08fea802016-07-12 10:37:52 -0700156// static
Tom Sepez80547a12017-04-25 12:43:13 -0700157std::unique_ptr<CFXJSE_Context> CFXJSE_Context::Create(
tsepeze3b2a4e2016-05-26 12:39:34 -0700158 v8::Isolate* pIsolate,
Tom Sepez80547a12017-04-25 12:43:13 -0700159 const FXJSE_CLASS_DESCRIPTOR* pGlobalClass,
160 CFXJSE_HostObject* pGlobalObject) {
Dan Sinclair1770c022016-03-14 14:14:16 -0400161 CFXJSE_ScopeUtil_IsolateHandle scope(pIsolate);
Tom Sepez80547a12017-04-25 12:43:13 -0700162 auto pContext = pdfium::MakeUnique<CFXJSE_Context>(pIsolate);
163 CFXJSE_Class* pGlobalClassObj = nullptr;
Dan Sinclair1770c022016-03-14 14:14:16 -0400164 v8::Local<v8::ObjectTemplate> hObjectTemplate;
Tom Sepez80547a12017-04-25 12:43:13 -0700165 if (pGlobalClass) {
166 pGlobalClassObj = CFXJSE_Class::Create(pContext.get(), pGlobalClass, true);
167 ASSERT(pGlobalClassObj);
Dan Sinclair1770c022016-03-14 14:14:16 -0400168 v8::Local<v8::FunctionTemplate> hFunctionTemplate =
169 v8::Local<v8::FunctionTemplate>::New(pIsolate,
Tom Sepez80547a12017-04-25 12:43:13 -0700170 pGlobalClassObj->m_hTemplate);
Dan Sinclair1770c022016-03-14 14:14:16 -0400171 hObjectTemplate = hFunctionTemplate->InstanceTemplate();
172 } else {
173 hObjectTemplate = v8::ObjectTemplate::New(pIsolate);
Tom Sepez336544a2017-04-24 16:38:51 -0700174 hObjectTemplate->SetInternalFieldCount(2);
Dan Sinclair1770c022016-03-14 14:14:16 -0400175 }
jochen7e6a8482016-07-06 11:02:27 -0700176 hObjectTemplate->Set(
177 v8::Symbol::GetToStringTag(pIsolate),
178 v8::String::NewFromUtf8(pIsolate, "global", v8::NewStringType::kNormal)
179 .ToLocalChecked());
Dan Sinclair1770c022016-03-14 14:14:16 -0400180 v8::Local<v8::Context> hNewContext =
dsinclair08fea802016-07-12 10:37:52 -0700181 v8::Context::New(pIsolate, nullptr, hObjectTemplate);
Dan Sinclair1770c022016-03-14 14:14:16 -0400182 v8::Local<v8::Context> hRootContext = v8::Local<v8::Context>::New(
183 pIsolate, CFXJSE_RuntimeData::Get(pIsolate)->m_hRootContext);
184 hNewContext->SetSecurityToken(hRootContext->GetSecurityToken());
185 v8::Local<v8::Object> hGlobalObject =
186 FXJSE_GetGlobalObjectFromContext(hNewContext);
Tom Sepez80547a12017-04-25 12:43:13 -0700187 FXJSE_UpdateObjectBinding(hGlobalObject, pGlobalObject);
Dan Sinclair1770c022016-03-14 14:14:16 -0400188 pContext->m_hContext.Reset(pIsolate, hNewContext);
189 return pContext;
190}
191
tsepez56286b32016-05-17 16:24:34 -0700192CFXJSE_Context::CFXJSE_Context(v8::Isolate* pIsolate) : m_pIsolate(pIsolate) {}
dsinclair769b1372016-06-08 13:12:41 -0700193
tsepez56286b32016-05-17 16:24:34 -0700194CFXJSE_Context::~CFXJSE_Context() {}
Dan Sinclair1770c022016-03-14 14:14:16 -0400195
dsinclair3cace322016-06-09 11:49:22 -0700196std::unique_ptr<CFXJSE_Value> CFXJSE_Context::GetGlobalObject() {
Dan Sinclair0bb13332017-03-30 16:12:02 -0400197 auto pValue = pdfium::MakeUnique<CFXJSE_Value>(m_pIsolate);
Dan Sinclair1770c022016-03-14 14:14:16 -0400198 CFXJSE_ScopeUtil_IsolateHandleContext scope(this);
199 v8::Local<v8::Context> hContext =
200 v8::Local<v8::Context>::New(m_pIsolate, m_hContext);
Dan Sinclair145cb372017-07-26 15:40:17 -0400201 v8::Local<v8::Object> hGlobalObject =
202 FXJSE_GetGlobalObjectFromContext(hContext);
Dan Sinclair1770c022016-03-14 14:14:16 -0400203 pValue->ForceSetValue(hGlobalObject);
dsinclair3cace322016-06-09 11:49:22 -0700204 return pValue;
Dan Sinclair1770c022016-03-14 14:14:16 -0400205}
206
Tom Sepez80547a12017-04-25 12:43:13 -0700207v8::Local<v8::Context> CFXJSE_Context::GetContext() {
208 return v8::Local<v8::Context>::New(m_pIsolate, m_hContext);
209}
210
211void CFXJSE_Context::AddClass(std::unique_ptr<CFXJSE_Class> pClass) {
212 m_rgClasses.push_back(std::move(pClass));
213}
214
215CFXJSE_Class* CFXJSE_Context::GetClassByName(
Ryan Harrison275e2602017-09-18 14:23:18 -0400216 const ByteStringView& szName) const {
Tom Sepez80547a12017-04-25 12:43:13 -0700217 auto pClass =
218 std::find_if(m_rgClasses.begin(), m_rgClasses.end(),
219 [szName](const std::unique_ptr<CFXJSE_Class>& item) {
220 return szName == item->m_szClassName;
221 });
222 return pClass != m_rgClasses.end() ? pClass->get() : nullptr;
223}
224
dsinclair769b1372016-06-08 13:12:41 -0700225void CFXJSE_Context::EnableCompatibleMode() {
226 ExecuteScript(szCompatibleModeScript, nullptr, nullptr);
227}
228
Dan Sinclair812e96c2017-03-13 16:43:37 -0400229bool CFXJSE_Context::ExecuteScript(const char* szScript,
tsepez304bb912016-11-03 06:10:26 -0700230 CFXJSE_Value* lpRetValue,
231 CFXJSE_Value* lpNewThisObject) {
Dan Sinclair1770c022016-03-14 14:14:16 -0400232 CFXJSE_ScopeUtil_IsolateHandleContext scope(this);
233 v8::TryCatch trycatch(m_pIsolate);
234 v8::Local<v8::String> hScriptString =
235 v8::String::NewFromUtf8(m_pIsolate, szScript);
dsinclair08fea802016-07-12 10:37:52 -0700236 if (!lpNewThisObject) {
Dan Sinclair1770c022016-03-14 14:14:16 -0400237 v8::Local<v8::Script> hScript = v8::Script::Compile(hScriptString);
238 if (!trycatch.HasCaught()) {
239 v8::Local<v8::Value> hValue = hScript->Run();
240 if (!trycatch.HasCaught()) {
tsepez304bb912016-11-03 06:10:26 -0700241 if (lpRetValue)
Dan Sinclair1770c022016-03-14 14:14:16 -0400242 lpRetValue->m_hValue.Reset(m_pIsolate, hValue);
tsepez304bb912016-11-03 06:10:26 -0700243 return true;
Dan Sinclair1770c022016-03-14 14:14:16 -0400244 }
245 }
246 if (lpRetValue) {
247 lpRetValue->m_hValue.Reset(m_pIsolate,
248 FXJSE_CreateReturnValue(m_pIsolate, trycatch));
249 }
tsepez304bb912016-11-03 06:10:26 -0700250 return false;
Dan Sinclair1770c022016-03-14 14:14:16 -0400251 }
tsepez304bb912016-11-03 06:10:26 -0700252
253 v8::Local<v8::Value> hNewThis =
254 v8::Local<v8::Value>::New(m_pIsolate, lpNewThisObject->m_hValue);
255 ASSERT(!hNewThis.IsEmpty());
256 v8::Local<v8::Script> hWrapper = v8::Script::Compile(v8::String::NewFromUtf8(
257 m_pIsolate, "(function () { return eval(arguments[0]); })"));
258 v8::Local<v8::Value> hWrapperValue = hWrapper->Run();
259 ASSERT(hWrapperValue->IsFunction());
260 v8::Local<v8::Function> hWrapperFn = hWrapperValue.As<v8::Function>();
261 if (!trycatch.HasCaught()) {
262 v8::Local<v8::Value> rgArgs[] = {hScriptString};
263 v8::Local<v8::Value> hValue =
264 hWrapperFn->Call(hNewThis.As<v8::Object>(), 1, rgArgs);
265 if (!trycatch.HasCaught()) {
266 if (lpRetValue)
267 lpRetValue->m_hValue.Reset(m_pIsolate, hValue);
268 return true;
269 }
270 }
271 if (lpRetValue) {
272 lpRetValue->m_hValue.Reset(m_pIsolate,
273 FXJSE_CreateReturnValue(m_pIsolate, trycatch));
274 }
275 return false;
Dan Sinclair1770c022016-03-14 14:14:16 -0400276}