7#include "fxjs/xfa/cfxjse_context.h"
11#include "fxjs/cfxjs_engine.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"
29const char szCompatibleModeScript[] =
30 "(function(global, list) {\n"
33 " for (objname in list) {\n"
34 " var globalobj = global[objname];\n"
36 " list[objname].forEach(function(name) {\n"
37 " if (!globalobj[name]) {\n"
38 " Object.defineProperty(globalobj, name, {\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"
46 " return globalobj.prototype[name].apply(obj, "
47 " Array.prototype.slice.call(arguments, 1));\n"
54 "}(this, {String: ['substr', 'toUpperCase']}));";
56const char szConsoleScript[] =
57 "console.show = function() {};\n"
59 "console.println = function(...args) {\n"
60 " this.log(...args);\n"
67const wchar_t kFXJSEHostObjectTag[] = L"FXJSE Host Object";
68const wchar_t kFXJSEProxyObjectTag[] = L"FXJSE Proxy Object";
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())
76 v8::Local<v8::Message> hMessage = trycatch->Message();
77 if (hMessage.IsEmpty())
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();
89 v8::Local<v8::String> hErrorStr =
90 fxv8::NewStringHelper(pIsolate,
"Error");
91 hReturnValue->Set(context, 0, hErrorStr).FromJust();
93 v8::Local<v8::String> hMessageStr =
94 fxv8::NewStringHelper(pIsolate,
"message");
96 hException.As<v8::Object>()->Get(context, hMessageStr).ToLocalChecked();
97 if (hValue->IsString() || hValue->IsStringObject())
98 hReturnValue->Set(context, 1, hValue).FromJust();
100 hReturnValue->Set(context, 1, hMessage->Get()).FromJust();
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();
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();
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);
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);
139 DCHECK(!hObject.IsEmpty());
140 DCHECK_EQ(hObject->InternalFieldCount(), 2);
141 hObject->SetAlignedPointerInInternalField(0,
nullptr);
142 hObject->SetAlignedPointerInInternalField(1,
nullptr);
146 if (!fxv8::IsObject(hValue))
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))
156 hObject = hProtoObject.As<v8::Object>();
157 if (hObject->InternalFieldCount() != 2)
160 if (hObject->GetAlignedPointerFromInternalField(0) != kFXJSEHostObjectTag)
163 return static_cast<CFXJSE_HostObject*>(
164 hObject->GetAlignedPointerFromInternalField(1));
169 v8::Isolate* pIsolate,
172 CXFA_ThisProxy* pProxy) {
176 auto pContext = pdfium::WrapUnique(
new CFXJSE_Context(pIsolate, pProxy));
177 v8::Local<v8::ObjectTemplate> hObjectTemplate;
182 pGlobalClassObj->GetTemplate(pIsolate)->InstanceTemplate();
184 hObjectTemplate = v8::ObjectTemplate::New(pIsolate);
185 hObjectTemplate->SetInternalFieldCount(2);
187 hObjectTemplate->Set(v8::Symbol::GetToStringTag(pIsolate),
188 fxv8::NewStringHelper(pIsolate,
"global"));
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);
195 v8::Local<v8::Object> pThis = pThisProxy->GetPrototype().As<v8::Object>();
196 FXJSE_UpdateObjectBinding(pThis, pGlobalObject);
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);
205CFXJSE_Context::CFXJSE_Context(
v8::Isolate* pIsolate, CXFA_ThisProxy* pProxy)
206 : m_pIsolate(pIsolate), m_pProxy(pProxy) {}
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);
221 return v8::Local<v8::Context>::New(GetIsolate(), m_hContext);
225 m_rgClasses.push_back(std::move(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);
234 return pClass != m_rgClasses.end() ? pClass->get() :
nullptr;
238 ExecuteScript(szCompatibleModeScript, v8::Local<v8::Object>());
239 ExecuteScript(szConsoleScript, v8::Local<v8::Object>());
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));
261 return ExecutionResult(
262 false, std::make_unique<CFXJSE_Value>(
263 GetIsolate(), CreateReturnValue(GetIsolate(), &trycatch)));
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));
285 v8::String::Utf8Value error(GetIsolate(), trycatch.Exception());
286 fprintf(stderr,
"JS Error: %s\n", *error);
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);
298 return ExecutionResult(
299 false, std::make_unique<CFXJSE_Value>(
300 GetIsolate(), CreateReturnValue(GetIsolate(), &trycatch)));
307 std::unique_ptr<CFXJSE_Value> val)
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)
void EnableCompatibleMode()
CFXJSE_Class * GetClassByName(ByteStringView szName) const
CFXJSE_ScopeUtil_IsolateHandleContext(CFXJSE_Context *pContext)
ExecutionResult & operator=(ExecutionResult &&that) noexcept
ExecutionResult(ExecutionResult &&that) noexcept
ExecutionResult(bool sts, std::unique_ptr< CFXJSE_Value > val)