Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cfxjse_context.cpp
Go to the documentation of this file.
1// Copyright 2016 The PDFium Authors
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
7#include "fxjs/xfa/cfxjse_context.h"
8
9#include <utility>
10
11#include "fxjs/cfxjs_engine.h"
12#include "fxjs/fxv8.h"
13#include "fxjs/xfa/cfxjse_class.h"
14#include "fxjs/xfa/cfxjse_isolatetracker.h"
15#include "fxjs/xfa/cfxjse_runtimedata.h"
16#include "fxjs/xfa/cfxjse_value.h"
17#include "fxjs/xfa/cjx_object.h"
18#include "third_party/base/check.h"
19#include "third_party/base/check_op.h"
20#include "third_party/base/memory/ptr_util.h"
21#include "v8/include/v8-exception.h"
22#include "v8/include/v8-function.h"
23#include "v8/include/v8-message.h"
24#include "v8/include/v8-script.h"
25#include "xfa/fxfa/parser/cxfa_thisproxy.h"
26
27namespace {
28
29const char szCompatibleModeScript[] =
30 "(function(global, list) {\n"
31 " 'use strict';\n"
32 " var objname;\n"
33 " for (objname in list) {\n"
34 " var globalobj = global[objname];\n"
35 " if (globalobj) {\n"
36 " list[objname].forEach(function(name) {\n"
37 " if (!globalobj[name]) {\n"
38 " Object.defineProperty(globalobj, name, {\n"
39 " writable: true,\n"
40 " enumerable: false,\n"
41 " value: (function(obj) {\n"
42 " if (arguments.length === 0) {\n"
43 " throw new TypeError('missing argument 0 when calling "
44 " function ' + objname + '.' + name);\n"
45 " }\n"
46 " return globalobj.prototype[name].apply(obj, "
47 " Array.prototype.slice.call(arguments, 1));\n"
48 " })\n"
49 " });\n"
50 " }\n"
51 " });\n"
52 " }\n"
53 " }\n"
54 "}(this, {String: ['substr', 'toUpperCase']}));";
55
56const char szConsoleScript[] =
57 "console.show = function() {};\n"
58 "\n"
59 "console.println = function(...args) {\n"
60 " this.log(...args);\n"
61 "};";
62
63// Only address matters, values are for humans debuging here. Keep these
64// wchar_t to prevent the compiler from doing something clever, like
65// aligning them on a byte boundary to save space, which would make them
66// incompatible for use as V8 aligned pointers.
67const wchar_t kFXJSEHostObjectTag[] = L"FXJSE Host Object";
68const wchar_t kFXJSEProxyObjectTag[] = L"FXJSE Proxy Object";
69
70v8::Local<v8::Object> CreateReturnValue(v8::Isolate* pIsolate,
71 v8::TryCatch* trycatch) {
72 v8::Local<v8::Object> hReturnValue = v8::Object::New(pIsolate);
73 if (!trycatch->HasCaught())
74 return hReturnValue;
75
76 v8::Local<v8::Message> hMessage = trycatch->Message();
77 if (hMessage.IsEmpty())
78 return hReturnValue;
79
80 v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
81 v8::Local<v8::Value> hException = trycatch->Exception();
82 if (hException->IsObject()) {
83 v8::Local<v8::String> hNameStr = fxv8::NewStringHelper(pIsolate, "name");
84 v8::Local<v8::Value> hValue =
85 hException.As<v8::Object>()->Get(context, hNameStr).ToLocalChecked();
86 if (hValue->IsString() || hValue->IsStringObject()) {
87 hReturnValue->Set(context, 0, hValue).FromJust();
88 } else {
89 v8::Local<v8::String> hErrorStr =
90 fxv8::NewStringHelper(pIsolate, "Error");
91 hReturnValue->Set(context, 0, hErrorStr).FromJust();
92 }
93 v8::Local<v8::String> hMessageStr =
94 fxv8::NewStringHelper(pIsolate, "message");
95 hValue =
96 hException.As<v8::Object>()->Get(context, hMessageStr).ToLocalChecked();
97 if (hValue->IsString() || hValue->IsStringObject())
98 hReturnValue->Set(context, 1, hValue).FromJust();
99 else
100 hReturnValue->Set(context, 1, hMessage->Get()).FromJust();
101 } else {
102 v8::Local<v8::String> hErrorStr = fxv8::NewStringHelper(pIsolate, "Error");
103 hReturnValue->Set(context, 0, hErrorStr).FromJust();
104 hReturnValue->Set(context, 1, hMessage->Get()).FromJust();
105 }
106 hReturnValue->Set(context, 2, hException).FromJust();
107 int line = hMessage->GetLineNumber(context).FromMaybe(0);
108 hReturnValue->Set(context, 3, v8::Integer::New(pIsolate, line)).FromJust();
109 v8::Local<v8::String> source =
110 hMessage->GetSourceLine(context).FromMaybe(v8::Local<v8::String>());
111 hReturnValue->Set(context, 4, source).FromJust();
112 int column = hMessage->GetStartColumn(context).FromMaybe(0);
113 hReturnValue->Set(context, 5, v8::Integer::New(pIsolate, column)).FromJust();
114 column = hMessage->GetEndColumn(context).FromMaybe(0);
115 hReturnValue->Set(context, 6, v8::Integer::New(pIsolate, column)).FromJust();
116 return hReturnValue;
117}
118
119void FXJSE_UpdateProxyBinding(v8::Local<v8::Object> hObject) {
120 DCHECK(!hObject.IsEmpty());
121 DCHECK_EQ(hObject->InternalFieldCount(), 2);
122 hObject->SetAlignedPointerInInternalField(
123 0, const_cast<wchar_t*>(kFXJSEProxyObjectTag));
124 hObject->SetAlignedPointerInInternalField(1, nullptr);
125}
126
127} // namespace
128
129void FXJSE_UpdateObjectBinding(v8::Local<v8::Object> hObject,
130 CFXJSE_HostObject* pNewBinding) {
131 DCHECK(!hObject.IsEmpty());
132 DCHECK_EQ(hObject->InternalFieldCount(), 2);
133 hObject->SetAlignedPointerInInternalField(
134 0, const_cast<wchar_t*>(kFXJSEHostObjectTag));
135 hObject->SetAlignedPointerInInternalField(1, pNewBinding);
136}
137
138void FXJSE_ClearObjectBinding(v8::Local<v8::Object> hObject) {
139 DCHECK(!hObject.IsEmpty());
140 DCHECK_EQ(hObject->InternalFieldCount(), 2);
141 hObject->SetAlignedPointerInInternalField(0, nullptr);
142 hObject->SetAlignedPointerInInternalField(1, nullptr);
143}
144
145CFXJSE_HostObject* FXJSE_RetrieveObjectBinding(v8::Local<v8::Value> hValue) {
146 if (!fxv8::IsObject(hValue))
147 return nullptr;
148
149 v8::Local<v8::Object> hObject = hValue.As<v8::Object>();
150 if (hObject->InternalFieldCount() != 2 ||
151 hObject->GetAlignedPointerFromInternalField(0) == kFXJSEProxyObjectTag) {
152 v8::Local<v8::Value> hProtoObject = hObject->GetPrototype();
153 if (!fxv8::IsObject(hProtoObject))
154 return nullptr;
155
156 hObject = hProtoObject.As<v8::Object>();
157 if (hObject->InternalFieldCount() != 2)
158 return nullptr;
159 }
160 if (hObject->GetAlignedPointerFromInternalField(0) != kFXJSEHostObjectTag)
161 return nullptr;
162
163 return static_cast<CFXJSE_HostObject*>(
164 hObject->GetAlignedPointerFromInternalField(1));
165}
166
167// static
168std::unique_ptr<CFXJSE_Context> CFXJSE_Context::Create(
169 v8::Isolate* pIsolate,
170 const FXJSE_CLASS_DESCRIPTOR* pGlobalClass,
171 CFXJSE_HostObject* pGlobalObject,
172 CXFA_ThisProxy* pProxy) {
173 CFXJSE_ScopeUtil_IsolateHandle scope(pIsolate);
174
175 // Private constructor.
176 auto pContext = pdfium::WrapUnique(new CFXJSE_Context(pIsolate, pProxy));
177 v8::Local<v8::ObjectTemplate> hObjectTemplate;
178 if (pGlobalClass) {
179 CFXJSE_Class* pGlobalClassObj =
180 CFXJSE_Class::Create(pContext.get(), pGlobalClass, true);
181 hObjectTemplate =
182 pGlobalClassObj->GetTemplate(pIsolate)->InstanceTemplate();
183 } else {
184 hObjectTemplate = v8::ObjectTemplate::New(pIsolate);
185 hObjectTemplate->SetInternalFieldCount(2);
186 }
187 hObjectTemplate->Set(v8::Symbol::GetToStringTag(pIsolate),
188 fxv8::NewStringHelper(pIsolate, "global"));
189
190 v8::Local<v8::Context> hNewContext =
191 v8::Context::New(pIsolate, nullptr, hObjectTemplate);
192 v8::Local<v8::Object> pThisProxy = hNewContext->Global();
193 FXJSE_UpdateProxyBinding(pThisProxy);
194
195 v8::Local<v8::Object> pThis = pThisProxy->GetPrototype().As<v8::Object>();
196 FXJSE_UpdateObjectBinding(pThis, pGlobalObject);
197
198 v8::Local<v8::Context> hRootContext =
199 CFXJSE_RuntimeData::Get(pIsolate)->GetRootContext(pIsolate);
200 hNewContext->SetSecurityToken(hRootContext->GetSecurityToken());
201 pContext->m_hContext.Reset(pIsolate, hNewContext);
202 return pContext;
203}
204
205CFXJSE_Context::CFXJSE_Context(v8::Isolate* pIsolate, CXFA_ThisProxy* pProxy)
206 : m_pIsolate(pIsolate), m_pProxy(pProxy) {}
207
208CFXJSE_Context::~CFXJSE_Context() = default;
209
211 v8::Isolate::Scope isolate_scope(GetIsolate());
212 v8::EscapableHandleScope handle_scope(GetIsolate());
213 v8::Local<v8::Context> hContext =
214 v8::Local<v8::Context>::New(GetIsolate(), m_hContext);
215 v8::Local<v8::Object> result =
216 hContext->Global()->GetPrototype().As<v8::Object>();
217 return handle_scope.Escape(result);
218}
219
221 return v8::Local<v8::Context>::New(GetIsolate(), m_hContext);
222}
223
224void CFXJSE_Context::AddClass(std::unique_ptr<CFXJSE_Class> pClass) {
225 m_rgClasses.push_back(std::move(pClass));
226}
227
228CFXJSE_Class* CFXJSE_Context::GetClassByName(ByteStringView szName) const {
229 auto pClass =
230 std::find_if(m_rgClasses.begin(), m_rgClasses.end(),
231 [szName](const std::unique_ptr<CFXJSE_Class>& item) {
232 return item->IsName(szName);
233 });
234 return pClass != m_rgClasses.end() ? pClass->get() : nullptr;
235}
236
238 ExecuteScript(szCompatibleModeScript, v8::Local<v8::Object>());
239 ExecuteScript(szConsoleScript, v8::Local<v8::Object>());
240}
241
243 ByteStringView bsScript,
244 v8::Local<v8::Object> hNewThis) {
246 v8::Local<v8::Context> hContext = GetIsolate()->GetCurrentContext();
247 v8::TryCatch trycatch(GetIsolate());
248 v8::Local<v8::String> hScriptString =
249 fxv8::NewStringHelper(GetIsolate(), bsScript);
250 if (hNewThis.IsEmpty()) {
251 v8::Local<v8::Script> hScript;
252 if (v8::Script::Compile(hContext, hScriptString).ToLocal(&hScript)) {
253 CHECK(!trycatch.HasCaught());
254 v8::Local<v8::Value> hValue;
255 if (hScript->Run(hContext).ToLocal(&hValue)) {
256 CHECK(!trycatch.HasCaught());
257 return ExecutionResult(
258 true, std::make_unique<CFXJSE_Value>(GetIsolate(), hValue));
259 }
260 }
261 return ExecutionResult(
262 false, std::make_unique<CFXJSE_Value>(
263 GetIsolate(), CreateReturnValue(GetIsolate(), &trycatch)));
264 }
265
266 v8::Local<v8::String> hEval = fxv8::NewStringHelper(
267 GetIsolate(), "(function () { return eval(arguments[0]); })");
268 v8::Local<v8::Script> hWrapper =
269 v8::Script::Compile(hContext, hEval).ToLocalChecked();
270 v8::Local<v8::Value> hWrapperValue;
271 if (hWrapper->Run(hContext).ToLocal(&hWrapperValue)) {
272 CHECK(!trycatch.HasCaught());
273 CHECK(hWrapperValue->IsFunction());
274 v8::Local<v8::Function> hWrapperFn = hWrapperValue.As<v8::Function>();
275 v8::Local<v8::Value> rgArgs[] = {hScriptString};
276 v8::Local<v8::Value> hValue;
277 if (hWrapperFn->Call(hContext, hNewThis, 1, rgArgs).ToLocal(&hValue)) {
278 DCHECK(!trycatch.HasCaught());
279 return ExecutionResult(
280 true, std::make_unique<CFXJSE_Value>(GetIsolate(), hValue));
281 }
282 }
283
284#ifndef NDEBUG
285 v8::String::Utf8Value error(GetIsolate(), trycatch.Exception());
286 fprintf(stderr, "JS Error: %s\n", *error);
287
288 v8::Local<v8::Message> message = trycatch.Message();
289 if (!message.IsEmpty()) {
290 v8::Local<v8::Context> context(GetIsolate()->GetCurrentContext());
291 int linenum = message->GetLineNumber(context).FromJust();
292 v8::String::Utf8Value sourceline(
293 GetIsolate(), message->GetSourceLine(context).ToLocalChecked());
294 fprintf(stderr, "Line %d: %s\n", linenum, *sourceline);
295 }
296#endif // NDEBUG
297
298 return ExecutionResult(
299 false, std::make_unique<CFXJSE_Value>(
300 GetIsolate(), CreateReturnValue(GetIsolate(), &trycatch)));
301}
302
304
306 bool sts,
307 std::unique_ptr<CFXJSE_Value> val)
308 : status(sts), value(std::move(val)) {}
309
311 ExecutionResult&& that) noexcept = default;
312
314 ExecutionResult&& that) noexcept = default;
315
void FXJSE_ClearObjectBinding(v8::Local< v8::Object > hJSObject)
CFXJSE_HostObject * FXJSE_RetrieveObjectBinding(v8::Local< v8::Value > hValue)
void FXJSE_UpdateObjectBinding(v8::Local< v8::Object > hObject, CFXJSE_HostObject *pNewBinding)
static CFXJSE_Class * Create(CFXJSE_Context *pContext, const FXJSE_CLASS_DESCRIPTOR *pClassDescriptor, bool bIsJSGlobal)
v8::Local< v8::Object > GetGlobalObject()
v8::Local< v8::Context > GetContext()
ExecutionResult ExecuteScript(ByteStringView bsScript, v8::Local< v8::Object > pNewThisObject)
void AddClass(std::unique_ptr< CFXJSE_Class > pClass)
CFXJSE_Class * GetClassByName(ByteStringView szName) const
CFXJSE_ScopeUtil_IsolateHandleContext(CFXJSE_Context *pContext)
#define CHECK(cvref)
ExecutionResult & operator=(ExecutionResult &&that) noexcept
ExecutionResult(ExecutionResult &&that) noexcept
ExecutionResult(bool sts, std::unique_ptr< CFXJSE_Value > val)