blob: d3b55ff43ec3603349a1a81a7885d3fc5486eafc [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
dsinclair43554682016-09-29 17:29:48 -07009#include "fxjs/cfxjse_class.h"
10#include "fxjs/cfxjse_value.h"
Dan Sinclair0bb13332017-03-30 16:12:02 -040011#include "third_party/base/ptr_util.h"
Dan Sinclair1770c022016-03-14 14:14:16 -040012
tsepezfb2a8242016-06-01 16:10:41 -070013namespace {
14
Dan Sinclair812e96c2017-03-13 16:43:37 -040015const char szCompatibleModeScript[] =
tsepezfb2a8242016-06-01 16:10:41 -070016 "(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
dsinclair08fea802016-07-12 10:37:52 -070044// 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.
47class 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
tsepez3a005f22016-05-27 17:45:00 -070072v8::Local<v8::Object> FXJSE_GetGlobalObjectFromContext(
73 const v8::Local<v8::Context>& hContext) {
74 return hContext->Global()->GetPrototype().As<v8::Object>();
75}
76
77void FXJSE_UpdateObjectBinding(v8::Local<v8::Object>& hObject,
tsepez29adee72016-05-31 14:22:09 -070078 CFXJSE_HostObject* lpNewBinding) {
tsepez3a005f22016-05-27 17:45:00 -070079 ASSERT(!hObject.IsEmpty());
80 ASSERT(hObject->InternalFieldCount() > 0);
tsepez29adee72016-05-31 14:22:09 -070081 hObject->SetAlignedPointerInInternalField(0,
82 static_cast<void*>(lpNewBinding));
tsepez3a005f22016-05-27 17:45:00 -070083}
84
tsepez29adee72016-05-31 14:22:09 -070085CFXJSE_HostObject* FXJSE_RetrieveObjectBinding(
86 const v8::Local<v8::Object>& hJSObject,
87 CFXJSE_Class* lpClass) {
tsepez3a005f22016-05-27 17:45:00 -070088 ASSERT(!hJSObject.IsEmpty());
dsinclair08fea802016-07-12 10:37:52 -070089 if (!hJSObject->IsObject())
tsepez29adee72016-05-31 14:22:09 -070090 return nullptr;
dsinclair08fea802016-07-12 10:37:52 -070091
tsepez3a005f22016-05-27 17:45:00 -070092 v8::Local<v8::Object> hObject = hJSObject;
93 if (hObject->InternalFieldCount() == 0) {
94 v8::Local<v8::Value> hProtoObject = hObject->GetPrototype();
dsinclair08fea802016-07-12 10:37:52 -070095 if (hProtoObject.IsEmpty() || !hProtoObject->IsObject())
tsepez29adee72016-05-31 14:22:09 -070096 return nullptr;
dsinclair08fea802016-07-12 10:37:52 -070097
tsepez3a005f22016-05-27 17:45:00 -070098 hObject = hProtoObject.As<v8::Object>();
dsinclair08fea802016-07-12 10:37:52 -070099 if (hObject->InternalFieldCount() == 0)
tsepez29adee72016-05-31 14:22:09 -0700100 return nullptr;
tsepez3a005f22016-05-27 17:45:00 -0700101 }
102 if (lpClass) {
103 v8::Local<v8::FunctionTemplate> hClass =
104 v8::Local<v8::FunctionTemplate>::New(
105 lpClass->GetContext()->GetRuntime(), lpClass->GetTemplate());
dsinclair08fea802016-07-12 10:37:52 -0700106 if (!hClass->HasInstance(hObject))
tsepez29adee72016-05-31 14:22:09 -0700107 return nullptr;
tsepez3a005f22016-05-27 17:45:00 -0700108 }
tsepez29adee72016-05-31 14:22:09 -0700109 return static_cast<CFXJSE_HostObject*>(
110 hObject->GetAlignedPointerFromInternalField(0));
tsepez3a005f22016-05-27 17:45:00 -0700111}
112
Dan Sinclair1770c022016-03-14 14:14:16 -0400113v8::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"));
dsinclair08fea802016-07-12 10:37:52 -0700123 if (hValue->IsString() || hValue->IsStringObject())
Dan Sinclair1770c022016-03-14 14:14:16 -0400124 hReturnValue->Set(0, hValue);
dsinclair08fea802016-07-12 10:37:52 -0700125 else
Dan Sinclair1770c022016-03-14 14:14:16 -0400126 hReturnValue->Set(0, v8::String::NewFromUtf8(pIsolate, "Error"));
dsinclair08fea802016-07-12 10:37:52 -0700127
Dan Sinclair1770c022016-03-14 14:14:16 -0400128 hValue = hException.As<v8::Object>()->Get(
129 v8::String::NewFromUtf8(pIsolate, "message"));
dsinclair08fea802016-07-12 10:37:52 -0700130 if (hValue->IsString() || hValue->IsStringObject())
Dan Sinclair1770c022016-03-14 14:14:16 -0400131 hReturnValue->Set(1, hValue);
dsinclair08fea802016-07-12 10:37:52 -0700132 else
Dan Sinclair1770c022016-03-14 14:14:16 -0400133 hReturnValue->Set(1, hMessage->Get());
Dan Sinclair1770c022016-03-14 14:14:16 -0400134 } 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
dsinclair08fea802016-07-12 10:37:52 -0700150// static
tsepeze3b2a4e2016-05-26 12:39:34 -0700151CFXJSE_Context* CFXJSE_Context::Create(
152 v8::Isolate* pIsolate,
153 const FXJSE_CLASS_DESCRIPTOR* lpGlobalClass,
tsepez29adee72016-05-31 14:22:09 -0700154 CFXJSE_HostObject* lpGlobalObject) {
Dan Sinclair1770c022016-03-14 14:14:16 -0400155 CFXJSE_ScopeUtil_IsolateHandle scope(pIsolate);
156 CFXJSE_Context* pContext = new CFXJSE_Context(pIsolate);
dsinclair08fea802016-07-12 10:37:52 -0700157 CFXJSE_Class* lpGlobalClassObj = nullptr;
Dan Sinclair1770c022016-03-14 14:14:16 -0400158 v8::Local<v8::ObjectTemplate> hObjectTemplate;
159 if (lpGlobalClass) {
tsepez304bb912016-11-03 06:10:26 -0700160 lpGlobalClassObj = CFXJSE_Class::Create(pContext, lpGlobalClass, true);
Dan Sinclair1770c022016-03-14 14:14:16 -0400161 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 }
jochen7e6a8482016-07-06 11:02:27 -0700170 hObjectTemplate->Set(
171 v8::Symbol::GetToStringTag(pIsolate),
172 v8::String::NewFromUtf8(pIsolate, "global", v8::NewStringType::kNormal)
173 .ToLocalChecked());
Dan Sinclair1770c022016-03-14 14:14:16 -0400174 v8::Local<v8::Context> hNewContext =
dsinclair08fea802016-07-12 10:37:52 -0700175 v8::Context::New(pIsolate, nullptr, hObjectTemplate);
Dan Sinclair1770c022016-03-14 14:14:16 -0400176 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
tsepez56286b32016-05-17 16:24:34 -0700186CFXJSE_Context::CFXJSE_Context(v8::Isolate* pIsolate) : m_pIsolate(pIsolate) {}
dsinclair769b1372016-06-08 13:12:41 -0700187
tsepez56286b32016-05-17 16:24:34 -0700188CFXJSE_Context::~CFXJSE_Context() {}
Dan Sinclair1770c022016-03-14 14:14:16 -0400189
dsinclair3cace322016-06-09 11:49:22 -0700190std::unique_ptr<CFXJSE_Value> CFXJSE_Context::GetGlobalObject() {
Dan Sinclair0bb13332017-03-30 16:12:02 -0400191 auto pValue = pdfium::MakeUnique<CFXJSE_Value>(m_pIsolate);
dsinclair3cace322016-06-09 11:49:22 -0700192
Dan Sinclair1770c022016-03-14 14:14:16 -0400193 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);
dsinclair3cace322016-06-09 11:49:22 -0700198
199 return pValue;
Dan Sinclair1770c022016-03-14 14:14:16 -0400200}
201
dsinclair769b1372016-06-08 13:12:41 -0700202void CFXJSE_Context::EnableCompatibleMode() {
203 ExecuteScript(szCompatibleModeScript, nullptr, nullptr);
204}
205
Dan Sinclair812e96c2017-03-13 16:43:37 -0400206bool CFXJSE_Context::ExecuteScript(const char* szScript,
tsepez304bb912016-11-03 06:10:26 -0700207 CFXJSE_Value* lpRetValue,
208 CFXJSE_Value* lpNewThisObject) {
Dan Sinclair1770c022016-03-14 14:14:16 -0400209 CFXJSE_ScopeUtil_IsolateHandleContext scope(this);
210 v8::TryCatch trycatch(m_pIsolate);
211 v8::Local<v8::String> hScriptString =
212 v8::String::NewFromUtf8(m_pIsolate, szScript);
dsinclair08fea802016-07-12 10:37:52 -0700213 if (!lpNewThisObject) {
Dan Sinclair1770c022016-03-14 14:14:16 -0400214 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()) {
tsepez304bb912016-11-03 06:10:26 -0700218 if (lpRetValue)
Dan Sinclair1770c022016-03-14 14:14:16 -0400219 lpRetValue->m_hValue.Reset(m_pIsolate, hValue);
tsepez304bb912016-11-03 06:10:26 -0700220 return true;
Dan Sinclair1770c022016-03-14 14:14:16 -0400221 }
222 }
223 if (lpRetValue) {
224 lpRetValue->m_hValue.Reset(m_pIsolate,
225 FXJSE_CreateReturnValue(m_pIsolate, trycatch));
226 }
tsepez304bb912016-11-03 06:10:26 -0700227 return false;
Dan Sinclair1770c022016-03-14 14:14:16 -0400228 }
tsepez304bb912016-11-03 06:10:26 -0700229
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 Sinclair1770c022016-03-14 14:14:16 -0400253}