blob: 0af2d361dda4b9fd167664b2ab0f7fc0982fd87e [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(
dan sinclair80435cb2017-10-24 21:40:24 -040077 v8::Local<v8::Context> hContext) {
tsepez3a005f22016-05-27 17:45:00 -070078 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
dan sinclair80435cb2017-10-24 21:40:24 -040089CFXJSE_HostObject* FXJSE_RetrieveObjectBinding(v8::Local<v8::Object> hJSObject,
90 CFXJSE_Class* lpClass) {
tsepez3a005f22016-05-27 17:45:00 -070091 ASSERT(!hJSObject.IsEmpty());
dsinclair08fea802016-07-12 10:37:52 -070092 if (!hJSObject->IsObject())
tsepez29adee72016-05-31 14:22:09 -070093 return nullptr;
dsinclair08fea802016-07-12 10:37:52 -070094
tsepez3a005f22016-05-27 17:45:00 -070095 v8::Local<v8::Object> hObject = hJSObject;
Tom Sepez336544a2017-04-24 16:38:51 -070096 if (hObject->InternalFieldCount() != 2) {
tsepez3a005f22016-05-27 17:45:00 -070097 v8::Local<v8::Value> hProtoObject = hObject->GetPrototype();
dsinclair08fea802016-07-12 10:37:52 -070098 if (hProtoObject.IsEmpty() || !hProtoObject->IsObject())
tsepez29adee72016-05-31 14:22:09 -070099 return nullptr;
dsinclair08fea802016-07-12 10:37:52 -0700100
tsepez3a005f22016-05-27 17:45:00 -0700101 hObject = hProtoObject.As<v8::Object>();
Tom Sepez336544a2017-04-24 16:38:51 -0700102 if (hObject->InternalFieldCount() != 2)
tsepez29adee72016-05-31 14:22:09 -0700103 return nullptr;
tsepez3a005f22016-05-27 17:45:00 -0700104 }
Tom Sepez336544a2017-04-24 16:38:51 -0700105 if (hObject->GetAlignedPointerFromInternalField(0) != g_FXJSETagString)
106 return nullptr;
tsepez3a005f22016-05-27 17:45:00 -0700107 if (lpClass) {
108 v8::Local<v8::FunctionTemplate> hClass =
109 v8::Local<v8::FunctionTemplate>::New(
Tom Sepez80547a12017-04-25 12:43:13 -0700110 lpClass->GetContext()->GetIsolate(), lpClass->GetTemplate());
dsinclair08fea802016-07-12 10:37:52 -0700111 if (!hClass->HasInstance(hObject))
tsepez29adee72016-05-31 14:22:09 -0700112 return nullptr;
tsepez3a005f22016-05-27 17:45:00 -0700113 }
tsepez29adee72016-05-31 14:22:09 -0700114 return static_cast<CFXJSE_HostObject*>(
Tom Sepez336544a2017-04-24 16:38:51 -0700115 hObject->GetAlignedPointerFromInternalField(1));
tsepez3a005f22016-05-27 17:45:00 -0700116}
117
Dan Sinclair1770c022016-03-14 14:14:16 -0400118v8::Local<v8::Object> FXJSE_CreateReturnValue(v8::Isolate* pIsolate,
119 v8::TryCatch& trycatch) {
120 v8::Local<v8::Object> hReturnValue = v8::Object::New(pIsolate);
121 if (trycatch.HasCaught()) {
122 v8::Local<v8::Value> hException = trycatch.Exception();
123 v8::Local<v8::Message> hMessage = trycatch.Message();
124 if (hException->IsObject()) {
125 v8::Local<v8::Value> hValue;
126 hValue = hException.As<v8::Object>()->Get(
127 v8::String::NewFromUtf8(pIsolate, "name"));
dsinclair08fea802016-07-12 10:37:52 -0700128 if (hValue->IsString() || hValue->IsStringObject())
Dan Sinclair1770c022016-03-14 14:14:16 -0400129 hReturnValue->Set(0, hValue);
dsinclair08fea802016-07-12 10:37:52 -0700130 else
Dan Sinclair1770c022016-03-14 14:14:16 -0400131 hReturnValue->Set(0, v8::String::NewFromUtf8(pIsolate, "Error"));
dsinclair08fea802016-07-12 10:37:52 -0700132
Dan Sinclair1770c022016-03-14 14:14:16 -0400133 hValue = hException.As<v8::Object>()->Get(
134 v8::String::NewFromUtf8(pIsolate, "message"));
dsinclair08fea802016-07-12 10:37:52 -0700135 if (hValue->IsString() || hValue->IsStringObject())
Dan Sinclair1770c022016-03-14 14:14:16 -0400136 hReturnValue->Set(1, hValue);
dsinclair08fea802016-07-12 10:37:52 -0700137 else
Dan Sinclair1770c022016-03-14 14:14:16 -0400138 hReturnValue->Set(1, hMessage->Get());
Dan Sinclair1770c022016-03-14 14:14:16 -0400139 } else {
140 hReturnValue->Set(0, v8::String::NewFromUtf8(pIsolate, "Error"));
141 hReturnValue->Set(1, hMessage->Get());
142 }
143 hReturnValue->Set(2, hException);
144 hReturnValue->Set(3, v8::Integer::New(pIsolate, hMessage->GetLineNumber()));
145 hReturnValue->Set(4, hMessage->GetSourceLine());
146 v8::Maybe<int32_t> maybe_int =
147 hMessage->GetStartColumn(pIsolate->GetCurrentContext());
148 hReturnValue->Set(5, v8::Integer::New(pIsolate, maybe_int.FromMaybe(0)));
149 maybe_int = hMessage->GetEndColumn(pIsolate->GetCurrentContext());
150 hReturnValue->Set(6, v8::Integer::New(pIsolate, maybe_int.FromMaybe(0)));
151 }
152 return hReturnValue;
153}
154
dsinclair08fea802016-07-12 10:37:52 -0700155// static
Tom Sepez80547a12017-04-25 12:43:13 -0700156std::unique_ptr<CFXJSE_Context> CFXJSE_Context::Create(
tsepeze3b2a4e2016-05-26 12:39:34 -0700157 v8::Isolate* pIsolate,
Tom Sepez80547a12017-04-25 12:43:13 -0700158 const FXJSE_CLASS_DESCRIPTOR* pGlobalClass,
159 CFXJSE_HostObject* pGlobalObject) {
Dan Sinclair1770c022016-03-14 14:14:16 -0400160 CFXJSE_ScopeUtil_IsolateHandle scope(pIsolate);
Tom Sepez80547a12017-04-25 12:43:13 -0700161 auto pContext = pdfium::MakeUnique<CFXJSE_Context>(pIsolate);
162 CFXJSE_Class* pGlobalClassObj = nullptr;
Dan Sinclair1770c022016-03-14 14:14:16 -0400163 v8::Local<v8::ObjectTemplate> hObjectTemplate;
Tom Sepez80547a12017-04-25 12:43:13 -0700164 if (pGlobalClass) {
165 pGlobalClassObj = CFXJSE_Class::Create(pContext.get(), pGlobalClass, true);
166 ASSERT(pGlobalClassObj);
Dan Sinclair1770c022016-03-14 14:14:16 -0400167 v8::Local<v8::FunctionTemplate> hFunctionTemplate =
168 v8::Local<v8::FunctionTemplate>::New(pIsolate,
Tom Sepez80547a12017-04-25 12:43:13 -0700169 pGlobalClassObj->m_hTemplate);
Dan Sinclair1770c022016-03-14 14:14:16 -0400170 hObjectTemplate = hFunctionTemplate->InstanceTemplate();
171 } else {
172 hObjectTemplate = v8::ObjectTemplate::New(pIsolate);
Tom Sepez336544a2017-04-24 16:38:51 -0700173 hObjectTemplate->SetInternalFieldCount(2);
Dan Sinclair1770c022016-03-14 14:14:16 -0400174 }
jochen7e6a8482016-07-06 11:02:27 -0700175 hObjectTemplate->Set(
176 v8::Symbol::GetToStringTag(pIsolate),
177 v8::String::NewFromUtf8(pIsolate, "global", v8::NewStringType::kNormal)
178 .ToLocalChecked());
Dan Sinclair1770c022016-03-14 14:14:16 -0400179 v8::Local<v8::Context> hNewContext =
dsinclair08fea802016-07-12 10:37:52 -0700180 v8::Context::New(pIsolate, nullptr, hObjectTemplate);
Dan Sinclair1770c022016-03-14 14:14:16 -0400181 v8::Local<v8::Context> hRootContext = v8::Local<v8::Context>::New(
182 pIsolate, CFXJSE_RuntimeData::Get(pIsolate)->m_hRootContext);
183 hNewContext->SetSecurityToken(hRootContext->GetSecurityToken());
184 v8::Local<v8::Object> hGlobalObject =
185 FXJSE_GetGlobalObjectFromContext(hNewContext);
Tom Sepez80547a12017-04-25 12:43:13 -0700186 FXJSE_UpdateObjectBinding(hGlobalObject, pGlobalObject);
Dan Sinclair1770c022016-03-14 14:14:16 -0400187 pContext->m_hContext.Reset(pIsolate, hNewContext);
188 return pContext;
189}
190
tsepez56286b32016-05-17 16:24:34 -0700191CFXJSE_Context::CFXJSE_Context(v8::Isolate* pIsolate) : m_pIsolate(pIsolate) {}
dsinclair769b1372016-06-08 13:12:41 -0700192
tsepez56286b32016-05-17 16:24:34 -0700193CFXJSE_Context::~CFXJSE_Context() {}
Dan Sinclair1770c022016-03-14 14:14:16 -0400194
dsinclair3cace322016-06-09 11:49:22 -0700195std::unique_ptr<CFXJSE_Value> CFXJSE_Context::GetGlobalObject() {
Dan Sinclair0bb13332017-03-30 16:12:02 -0400196 auto pValue = pdfium::MakeUnique<CFXJSE_Value>(m_pIsolate);
Dan Sinclair1770c022016-03-14 14:14:16 -0400197 CFXJSE_ScopeUtil_IsolateHandleContext scope(this);
198 v8::Local<v8::Context> hContext =
199 v8::Local<v8::Context>::New(m_pIsolate, m_hContext);
Dan Sinclair145cb372017-07-26 15:40:17 -0400200 v8::Local<v8::Object> hGlobalObject =
201 FXJSE_GetGlobalObjectFromContext(hContext);
Dan Sinclair1770c022016-03-14 14:14:16 -0400202 pValue->ForceSetValue(hGlobalObject);
dsinclair3cace322016-06-09 11:49:22 -0700203 return pValue;
Dan Sinclair1770c022016-03-14 14:14:16 -0400204}
205
Tom Sepez80547a12017-04-25 12:43:13 -0700206v8::Local<v8::Context> CFXJSE_Context::GetContext() {
207 return v8::Local<v8::Context>::New(m_pIsolate, m_hContext);
208}
209
210void CFXJSE_Context::AddClass(std::unique_ptr<CFXJSE_Class> pClass) {
211 m_rgClasses.push_back(std::move(pClass));
212}
213
214CFXJSE_Class* CFXJSE_Context::GetClassByName(
Ryan Harrison275e2602017-09-18 14:23:18 -0400215 const ByteStringView& szName) const {
Tom Sepez80547a12017-04-25 12:43:13 -0700216 auto pClass =
217 std::find_if(m_rgClasses.begin(), m_rgClasses.end(),
218 [szName](const std::unique_ptr<CFXJSE_Class>& item) {
219 return szName == item->m_szClassName;
220 });
221 return pClass != m_rgClasses.end() ? pClass->get() : nullptr;
222}
223
dsinclair769b1372016-06-08 13:12:41 -0700224void CFXJSE_Context::EnableCompatibleMode() {
225 ExecuteScript(szCompatibleModeScript, nullptr, nullptr);
226}
227
Dan Sinclair812e96c2017-03-13 16:43:37 -0400228bool CFXJSE_Context::ExecuteScript(const char* szScript,
tsepez304bb912016-11-03 06:10:26 -0700229 CFXJSE_Value* lpRetValue,
230 CFXJSE_Value* lpNewThisObject) {
Dan Sinclair1770c022016-03-14 14:14:16 -0400231 CFXJSE_ScopeUtil_IsolateHandleContext scope(this);
232 v8::TryCatch trycatch(m_pIsolate);
233 v8::Local<v8::String> hScriptString =
234 v8::String::NewFromUtf8(m_pIsolate, szScript);
dsinclair08fea802016-07-12 10:37:52 -0700235 if (!lpNewThisObject) {
Dan Sinclair1770c022016-03-14 14:14:16 -0400236 v8::Local<v8::Script> hScript = v8::Script::Compile(hScriptString);
237 if (!trycatch.HasCaught()) {
238 v8::Local<v8::Value> hValue = hScript->Run();
239 if (!trycatch.HasCaught()) {
tsepez304bb912016-11-03 06:10:26 -0700240 if (lpRetValue)
Dan Sinclair1770c022016-03-14 14:14:16 -0400241 lpRetValue->m_hValue.Reset(m_pIsolate, hValue);
tsepez304bb912016-11-03 06:10:26 -0700242 return true;
Dan Sinclair1770c022016-03-14 14:14:16 -0400243 }
244 }
245 if (lpRetValue) {
246 lpRetValue->m_hValue.Reset(m_pIsolate,
247 FXJSE_CreateReturnValue(m_pIsolate, trycatch));
248 }
tsepez304bb912016-11-03 06:10:26 -0700249 return false;
Dan Sinclair1770c022016-03-14 14:14:16 -0400250 }
tsepez304bb912016-11-03 06:10:26 -0700251
252 v8::Local<v8::Value> hNewThis =
253 v8::Local<v8::Value>::New(m_pIsolate, lpNewThisObject->m_hValue);
254 ASSERT(!hNewThis.IsEmpty());
255 v8::Local<v8::Script> hWrapper = v8::Script::Compile(v8::String::NewFromUtf8(
256 m_pIsolate, "(function () { return eval(arguments[0]); })"));
257 v8::Local<v8::Value> hWrapperValue = hWrapper->Run();
258 ASSERT(hWrapperValue->IsFunction());
259 v8::Local<v8::Function> hWrapperFn = hWrapperValue.As<v8::Function>();
260 if (!trycatch.HasCaught()) {
261 v8::Local<v8::Value> rgArgs[] = {hScriptString};
262 v8::Local<v8::Value> hValue =
263 hWrapperFn->Call(hNewThis.As<v8::Object>(), 1, rgArgs);
264 if (!trycatch.HasCaught()) {
265 if (lpRetValue)
266 lpRetValue->m_hValue.Reset(m_pIsolate, hValue);
267 return true;
268 }
269 }
270 if (lpRetValue) {
271 lpRetValue->m_hValue.Reset(m_pIsolate,
272 FXJSE_CreateReturnValue(m_pIsolate, trycatch));
273 }
274 return false;
Dan Sinclair1770c022016-03-14 14:14:16 -0400275}