blob: d82f875e0f037ad5f3876730a9d6093d01ae03f9 [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 Sinclair1770c022016-03-14 14:14:16 -040011
tsepezfb2a8242016-06-01 16:10:41 -070012namespace {
13
14const FX_CHAR szCompatibleModeScript[] =
15 "(function(global, list) {\n"
16 " 'use strict';\n"
17 " var objname;\n"
18 " for (objname in list) {\n"
19 " var globalobj = global[objname];\n"
20 " if (globalobj) {\n"
21 " list[objname].forEach(function(name) {\n"
22 " if (!globalobj[name]) {\n"
23 " Object.defineProperty(globalobj, name, {\n"
24 " writable: true,\n"
25 " enumerable: false,\n"
26 " value: (function(obj) {\n"
27 " if (arguments.length === 0) {\n"
28 " throw new TypeError('missing argument 0 when calling "
29 " function ' + objname + '.' + name);\n"
30 " }\n"
31 " return globalobj.prototype[name].apply(obj, "
32 " Array.prototype.slice.call(arguments, 1));\n"
33 " })\n"
34 " });\n"
35 " }\n"
36 " });\n"
37 " }\n"
38 " }\n"
39 "}(this, {String: ['substr', 'toUpperCase']}));";
40
41} // namespace
42
dsinclair08fea802016-07-12 10:37:52 -070043// Note, not in the anonymous namespace due to the friend call
44// in cfxjse_context.h
45// TODO(dsinclair): Remove the friending, use public methods.
46class CFXJSE_ScopeUtil_IsolateHandleContext {
47 public:
48 explicit CFXJSE_ScopeUtil_IsolateHandleContext(CFXJSE_Context* pContext)
49 : m_context(pContext),
50 m_parent(pContext->m_pIsolate),
51 m_cscope(v8::Local<v8::Context>::New(pContext->m_pIsolate,
52 pContext->m_hContext)) {}
53 v8::Isolate* GetIsolate() { return m_context->m_pIsolate; }
54 v8::Local<v8::Context> GetLocalContext() {
55 return v8::Local<v8::Context>::New(m_context->m_pIsolate,
56 m_context->m_hContext);
57 }
58
59 private:
60 CFXJSE_ScopeUtil_IsolateHandleContext(
61 const CFXJSE_ScopeUtil_IsolateHandleContext&) = delete;
62 void operator=(const CFXJSE_ScopeUtil_IsolateHandleContext&) = delete;
63 void* operator new(size_t size) = delete;
64 void operator delete(void*, size_t) = delete;
65
66 CFXJSE_Context* m_context;
67 CFXJSE_ScopeUtil_IsolateHandle m_parent;
68 v8::Context::Scope m_cscope;
69};
70
tsepez3a005f22016-05-27 17:45:00 -070071v8::Local<v8::Object> FXJSE_GetGlobalObjectFromContext(
72 const v8::Local<v8::Context>& hContext) {
73 return hContext->Global()->GetPrototype().As<v8::Object>();
74}
75
76void FXJSE_UpdateObjectBinding(v8::Local<v8::Object>& hObject,
tsepez29adee72016-05-31 14:22:09 -070077 CFXJSE_HostObject* lpNewBinding) {
tsepez3a005f22016-05-27 17:45:00 -070078 ASSERT(!hObject.IsEmpty());
79 ASSERT(hObject->InternalFieldCount() > 0);
tsepez29adee72016-05-31 14:22:09 -070080 hObject->SetAlignedPointerInInternalField(0,
81 static_cast<void*>(lpNewBinding));
tsepez3a005f22016-05-27 17:45:00 -070082}
83
tsepez29adee72016-05-31 14:22:09 -070084CFXJSE_HostObject* FXJSE_RetrieveObjectBinding(
85 const v8::Local<v8::Object>& hJSObject,
86 CFXJSE_Class* lpClass) {
tsepez3a005f22016-05-27 17:45:00 -070087 ASSERT(!hJSObject.IsEmpty());
dsinclair08fea802016-07-12 10:37:52 -070088 if (!hJSObject->IsObject())
tsepez29adee72016-05-31 14:22:09 -070089 return nullptr;
dsinclair08fea802016-07-12 10:37:52 -070090
tsepez3a005f22016-05-27 17:45:00 -070091 v8::Local<v8::Object> hObject = hJSObject;
92 if (hObject->InternalFieldCount() == 0) {
93 v8::Local<v8::Value> hProtoObject = hObject->GetPrototype();
dsinclair08fea802016-07-12 10:37:52 -070094 if (hProtoObject.IsEmpty() || !hProtoObject->IsObject())
tsepez29adee72016-05-31 14:22:09 -070095 return nullptr;
dsinclair08fea802016-07-12 10:37:52 -070096
tsepez3a005f22016-05-27 17:45:00 -070097 hObject = hProtoObject.As<v8::Object>();
dsinclair08fea802016-07-12 10:37:52 -070098 if (hObject->InternalFieldCount() == 0)
tsepez29adee72016-05-31 14:22:09 -070099 return nullptr;
tsepez3a005f22016-05-27 17:45:00 -0700100 }
101 if (lpClass) {
102 v8::Local<v8::FunctionTemplate> hClass =
103 v8::Local<v8::FunctionTemplate>::New(
104 lpClass->GetContext()->GetRuntime(), lpClass->GetTemplate());
dsinclair08fea802016-07-12 10:37:52 -0700105 if (!hClass->HasInstance(hObject))
tsepez29adee72016-05-31 14:22:09 -0700106 return nullptr;
tsepez3a005f22016-05-27 17:45:00 -0700107 }
tsepez29adee72016-05-31 14:22:09 -0700108 return static_cast<CFXJSE_HostObject*>(
109 hObject->GetAlignedPointerFromInternalField(0));
tsepez3a005f22016-05-27 17:45:00 -0700110}
111
Dan Sinclair1770c022016-03-14 14:14:16 -0400112v8::Local<v8::Object> FXJSE_CreateReturnValue(v8::Isolate* pIsolate,
113 v8::TryCatch& trycatch) {
114 v8::Local<v8::Object> hReturnValue = v8::Object::New(pIsolate);
115 if (trycatch.HasCaught()) {
116 v8::Local<v8::Value> hException = trycatch.Exception();
117 v8::Local<v8::Message> hMessage = trycatch.Message();
118 if (hException->IsObject()) {
119 v8::Local<v8::Value> hValue;
120 hValue = hException.As<v8::Object>()->Get(
121 v8::String::NewFromUtf8(pIsolate, "name"));
dsinclair08fea802016-07-12 10:37:52 -0700122 if (hValue->IsString() || hValue->IsStringObject())
Dan Sinclair1770c022016-03-14 14:14:16 -0400123 hReturnValue->Set(0, hValue);
dsinclair08fea802016-07-12 10:37:52 -0700124 else
Dan Sinclair1770c022016-03-14 14:14:16 -0400125 hReturnValue->Set(0, v8::String::NewFromUtf8(pIsolate, "Error"));
dsinclair08fea802016-07-12 10:37:52 -0700126
Dan Sinclair1770c022016-03-14 14:14:16 -0400127 hValue = hException.As<v8::Object>()->Get(
128 v8::String::NewFromUtf8(pIsolate, "message"));
dsinclair08fea802016-07-12 10:37:52 -0700129 if (hValue->IsString() || hValue->IsStringObject())
Dan Sinclair1770c022016-03-14 14:14:16 -0400130 hReturnValue->Set(1, hValue);
dsinclair08fea802016-07-12 10:37:52 -0700131 else
Dan Sinclair1770c022016-03-14 14:14:16 -0400132 hReturnValue->Set(1, hMessage->Get());
Dan Sinclair1770c022016-03-14 14:14:16 -0400133 } else {
134 hReturnValue->Set(0, v8::String::NewFromUtf8(pIsolate, "Error"));
135 hReturnValue->Set(1, hMessage->Get());
136 }
137 hReturnValue->Set(2, hException);
138 hReturnValue->Set(3, v8::Integer::New(pIsolate, hMessage->GetLineNumber()));
139 hReturnValue->Set(4, hMessage->GetSourceLine());
140 v8::Maybe<int32_t> maybe_int =
141 hMessage->GetStartColumn(pIsolate->GetCurrentContext());
142 hReturnValue->Set(5, v8::Integer::New(pIsolate, maybe_int.FromMaybe(0)));
143 maybe_int = hMessage->GetEndColumn(pIsolate->GetCurrentContext());
144 hReturnValue->Set(6, v8::Integer::New(pIsolate, maybe_int.FromMaybe(0)));
145 }
146 return hReturnValue;
147}
148
dsinclair08fea802016-07-12 10:37:52 -0700149// static
tsepeze3b2a4e2016-05-26 12:39:34 -0700150CFXJSE_Context* CFXJSE_Context::Create(
151 v8::Isolate* pIsolate,
152 const FXJSE_CLASS_DESCRIPTOR* lpGlobalClass,
tsepez29adee72016-05-31 14:22:09 -0700153 CFXJSE_HostObject* lpGlobalObject) {
Dan Sinclair1770c022016-03-14 14:14:16 -0400154 CFXJSE_ScopeUtil_IsolateHandle scope(pIsolate);
155 CFXJSE_Context* pContext = new CFXJSE_Context(pIsolate);
dsinclair08fea802016-07-12 10:37:52 -0700156 CFXJSE_Class* lpGlobalClassObj = nullptr;
Dan Sinclair1770c022016-03-14 14:14:16 -0400157 v8::Local<v8::ObjectTemplate> hObjectTemplate;
158 if (lpGlobalClass) {
tsepez304bb912016-11-03 06:10:26 -0700159 lpGlobalClassObj = CFXJSE_Class::Create(pContext, lpGlobalClass, true);
Dan Sinclair1770c022016-03-14 14:14:16 -0400160 ASSERT(lpGlobalClassObj);
161 v8::Local<v8::FunctionTemplate> hFunctionTemplate =
162 v8::Local<v8::FunctionTemplate>::New(pIsolate,
163 lpGlobalClassObj->m_hTemplate);
164 hObjectTemplate = hFunctionTemplate->InstanceTemplate();
165 } else {
166 hObjectTemplate = v8::ObjectTemplate::New(pIsolate);
167 hObjectTemplate->SetInternalFieldCount(1);
168 }
jochen7e6a8482016-07-06 11:02:27 -0700169 hObjectTemplate->Set(
170 v8::Symbol::GetToStringTag(pIsolate),
171 v8::String::NewFromUtf8(pIsolate, "global", v8::NewStringType::kNormal)
172 .ToLocalChecked());
Dan Sinclair1770c022016-03-14 14:14:16 -0400173 v8::Local<v8::Context> hNewContext =
dsinclair08fea802016-07-12 10:37:52 -0700174 v8::Context::New(pIsolate, nullptr, hObjectTemplate);
Dan Sinclair1770c022016-03-14 14:14:16 -0400175 v8::Local<v8::Context> hRootContext = v8::Local<v8::Context>::New(
176 pIsolate, CFXJSE_RuntimeData::Get(pIsolate)->m_hRootContext);
177 hNewContext->SetSecurityToken(hRootContext->GetSecurityToken());
178 v8::Local<v8::Object> hGlobalObject =
179 FXJSE_GetGlobalObjectFromContext(hNewContext);
180 FXJSE_UpdateObjectBinding(hGlobalObject, lpGlobalObject);
181 pContext->m_hContext.Reset(pIsolate, hNewContext);
182 return pContext;
183}
184
tsepez56286b32016-05-17 16:24:34 -0700185CFXJSE_Context::CFXJSE_Context(v8::Isolate* pIsolate) : m_pIsolate(pIsolate) {}
dsinclair769b1372016-06-08 13:12:41 -0700186
tsepez56286b32016-05-17 16:24:34 -0700187CFXJSE_Context::~CFXJSE_Context() {}
Dan Sinclair1770c022016-03-14 14:14:16 -0400188
dsinclair3cace322016-06-09 11:49:22 -0700189std::unique_ptr<CFXJSE_Value> CFXJSE_Context::GetGlobalObject() {
190 std::unique_ptr<CFXJSE_Value> pValue(new CFXJSE_Value(m_pIsolate));
191
Dan Sinclair1770c022016-03-14 14:14:16 -0400192 CFXJSE_ScopeUtil_IsolateHandleContext scope(this);
193 v8::Local<v8::Context> hContext =
194 v8::Local<v8::Context>::New(m_pIsolate, m_hContext);
195 v8::Local<v8::Object> hGlobalObject = hContext->Global();
196 pValue->ForceSetValue(hGlobalObject);
dsinclair3cace322016-06-09 11:49:22 -0700197
198 return pValue;
Dan Sinclair1770c022016-03-14 14:14:16 -0400199}
200
dsinclair769b1372016-06-08 13:12:41 -0700201void CFXJSE_Context::EnableCompatibleMode() {
202 ExecuteScript(szCompatibleModeScript, nullptr, nullptr);
203}
204
tsepez304bb912016-11-03 06:10:26 -0700205bool CFXJSE_Context::ExecuteScript(const FX_CHAR* szScript,
206 CFXJSE_Value* lpRetValue,
207 CFXJSE_Value* lpNewThisObject) {
Dan Sinclair1770c022016-03-14 14:14:16 -0400208 CFXJSE_ScopeUtil_IsolateHandleContext scope(this);
209 v8::TryCatch trycatch(m_pIsolate);
210 v8::Local<v8::String> hScriptString =
211 v8::String::NewFromUtf8(m_pIsolate, szScript);
dsinclair08fea802016-07-12 10:37:52 -0700212 if (!lpNewThisObject) {
Dan Sinclair1770c022016-03-14 14:14:16 -0400213 v8::Local<v8::Script> hScript = v8::Script::Compile(hScriptString);
214 if (!trycatch.HasCaught()) {
215 v8::Local<v8::Value> hValue = hScript->Run();
216 if (!trycatch.HasCaught()) {
tsepez304bb912016-11-03 06:10:26 -0700217 if (lpRetValue)
Dan Sinclair1770c022016-03-14 14:14:16 -0400218 lpRetValue->m_hValue.Reset(m_pIsolate, hValue);
tsepez304bb912016-11-03 06:10:26 -0700219 return true;
Dan Sinclair1770c022016-03-14 14:14:16 -0400220 }
221 }
222 if (lpRetValue) {
223 lpRetValue->m_hValue.Reset(m_pIsolate,
224 FXJSE_CreateReturnValue(m_pIsolate, trycatch));
225 }
tsepez304bb912016-11-03 06:10:26 -0700226 return false;
Dan Sinclair1770c022016-03-14 14:14:16 -0400227 }
tsepez304bb912016-11-03 06:10:26 -0700228
229 v8::Local<v8::Value> hNewThis =
230 v8::Local<v8::Value>::New(m_pIsolate, lpNewThisObject->m_hValue);
231 ASSERT(!hNewThis.IsEmpty());
232 v8::Local<v8::Script> hWrapper = v8::Script::Compile(v8::String::NewFromUtf8(
233 m_pIsolate, "(function () { return eval(arguments[0]); })"));
234 v8::Local<v8::Value> hWrapperValue = hWrapper->Run();
235 ASSERT(hWrapperValue->IsFunction());
236 v8::Local<v8::Function> hWrapperFn = hWrapperValue.As<v8::Function>();
237 if (!trycatch.HasCaught()) {
238 v8::Local<v8::Value> rgArgs[] = {hScriptString};
239 v8::Local<v8::Value> hValue =
240 hWrapperFn->Call(hNewThis.As<v8::Object>(), 1, rgArgs);
241 if (!trycatch.HasCaught()) {
242 if (lpRetValue)
243 lpRetValue->m_hValue.Reset(m_pIsolate, hValue);
244 return true;
245 }
246 }
247 if (lpRetValue) {
248 lpRetValue->m_hValue.Reset(m_pIsolate,
249 FXJSE_CreateReturnValue(m_pIsolate, trycatch));
250 }
251 return false;
Dan Sinclair1770c022016-03-14 14:14:16 -0400252}