blob: 8696cee42f2054e005e43a31bb1d53a5380266d2 [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
71 CFXJSE_Context* m_context;
72 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);
201 v8::Local<v8::Object> hGlobalObject = hContext->Global();
202 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(
215 const CFX_ByteStringC& szName) const {
216 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}