blob: bdc31e45946b8be448f241bb1054a63572a5468d [file] [log] [blame]
Dan Sinclair1770c022016-03-14 14:14:16 -04001// Copyright 2014 PDFium Authors. All rights reserved.
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 "xfa/fxjse/context.h"
8
9#include "xfa/fxjse/class.h"
10#include "xfa/fxjse/scope_inline.h"
11#include "xfa/fxjse/util_inline.h"
12#include "xfa/fxjse/value.h"
13
dsinclair7f2abcc2016-05-26 09:40:27 -070014CFXJSE_Context* FXJSE_Context_Create(v8::Isolate* pIsolate,
15 const FXJSE_CLASS* lpGlobalClass,
16 void* lpGlobalObject) {
17 return CFXJSE_Context::Create(pIsolate, lpGlobalClass, lpGlobalObject);
thestig495bda12016-04-28 17:29:19 -070018}
19
dsinclair7f2abcc2016-05-26 09:40:27 -070020void FXJSE_Context_Release(CFXJSE_Context* pContext) {
21 delete pContext;
Dan Sinclair1770c022016-03-14 14:14:16 -040022}
23
dsinclair12a6b0c2016-05-26 11:14:08 -070024CFXJSE_Value* FXJSE_Context_GetGlobalObject(CFXJSE_Context* pContext) {
thestig495bda12016-04-28 17:29:19 -070025 if (!pContext)
26 return nullptr;
27
Dan Sinclair1770c022016-03-14 14:14:16 -040028 CFXJSE_Value* lpValue = CFXJSE_Value::Create(pContext->GetRuntime());
29 ASSERT(lpValue);
30 pContext->GetGlobalObject(lpValue);
dsinclair12a6b0c2016-05-26 11:14:08 -070031 return lpValue;
Dan Sinclair1770c022016-03-14 14:14:16 -040032}
33
Dan Sinclair1770c022016-03-14 14:14:16 -040034static const FX_CHAR* szCompatibleModeScripts[] = {
35 "(function(global, list) {\n"
36 " 'use strict';\n"
37 " var objname;\n"
38 " for (objname in list) {\n"
39 " var globalobj = global[objname];\n"
40 " if (globalobj) {\n"
41 " list[objname].forEach(function(name) {\n"
42 " if (!globalobj[name]) {\n"
43 " Object.defineProperty(globalobj, name, {\n"
44 " writable: true,\n"
45 " enumerable: false,\n"
46 " value: (function(obj) {\n"
47 " if (arguments.length === 0) {\n"
48 " throw new TypeError('missing argument 0 when calling "
49 " function ' + objname + '.' + name);\n"
50 " }\n"
51 " return globalobj.prototype[name].apply(obj, "
52 " Array.prototype.slice.call(arguments, 1));\n"
53 " })\n"
54 " });\n"
55 " }\n"
56 " });\n"
57 " }\n"
58 " }\n"
59 "}(this, {String: ['substr', 'toUpperCase']}));"};
dsinclair7f2abcc2016-05-26 09:40:27 -070060void FXJSE_Context_EnableCompatibleMode(CFXJSE_Context* pContext,
tsepez736f28a2016-03-25 14:19:51 -070061 uint32_t dwCompatibleFlags) {
Dan Sinclair1770c022016-03-14 14:14:16 -040062 for (uint32_t i = 0; i < (uint32_t)FXJSE_COMPATIBLEMODEFLAGCOUNT; i++) {
63 if (dwCompatibleFlags & (1 << i)) {
dsinclair7f2abcc2016-05-26 09:40:27 -070064 FXJSE_ExecuteScript(pContext, szCompatibleModeScripts[i], NULL, NULL);
Dan Sinclair1770c022016-03-14 14:14:16 -040065 }
66 }
67}
68
dsinclair7f2abcc2016-05-26 09:40:27 -070069FX_BOOL FXJSE_ExecuteScript(CFXJSE_Context* pContext,
Dan Sinclair1770c022016-03-14 14:14:16 -040070 const FX_CHAR* szScript,
dsinclair12a6b0c2016-05-26 11:14:08 -070071 CFXJSE_Value* pRetValue,
72 CFXJSE_Value* pNewThisObject) {
73 return pContext->ExecuteScript(szScript, pRetValue, pNewThisObject);
Dan Sinclair1770c022016-03-14 14:14:16 -040074}
75
76v8::Local<v8::Object> FXJSE_CreateReturnValue(v8::Isolate* pIsolate,
77 v8::TryCatch& trycatch) {
78 v8::Local<v8::Object> hReturnValue = v8::Object::New(pIsolate);
79 if (trycatch.HasCaught()) {
80 v8::Local<v8::Value> hException = trycatch.Exception();
81 v8::Local<v8::Message> hMessage = trycatch.Message();
82 if (hException->IsObject()) {
83 v8::Local<v8::Value> hValue;
84 hValue = hException.As<v8::Object>()->Get(
85 v8::String::NewFromUtf8(pIsolate, "name"));
86 if (hValue->IsString() || hValue->IsStringObject()) {
87 hReturnValue->Set(0, hValue);
88 } else {
89 hReturnValue->Set(0, v8::String::NewFromUtf8(pIsolate, "Error"));
90 }
91 hValue = hException.As<v8::Object>()->Get(
92 v8::String::NewFromUtf8(pIsolate, "message"));
93 if (hValue->IsString() || hValue->IsStringObject()) {
94 hReturnValue->Set(1, hValue);
95 } else {
96 hReturnValue->Set(1, hMessage->Get());
97 }
98 } else {
99 hReturnValue->Set(0, v8::String::NewFromUtf8(pIsolate, "Error"));
100 hReturnValue->Set(1, hMessage->Get());
101 }
102 hReturnValue->Set(2, hException);
103 hReturnValue->Set(3, v8::Integer::New(pIsolate, hMessage->GetLineNumber()));
104 hReturnValue->Set(4, hMessage->GetSourceLine());
105 v8::Maybe<int32_t> maybe_int =
106 hMessage->GetStartColumn(pIsolate->GetCurrentContext());
107 hReturnValue->Set(5, v8::Integer::New(pIsolate, maybe_int.FromMaybe(0)));
108 maybe_int = hMessage->GetEndColumn(pIsolate->GetCurrentContext());
109 hReturnValue->Set(6, v8::Integer::New(pIsolate, maybe_int.FromMaybe(0)));
110 }
111 return hReturnValue;
112}
113
Dan Sinclair1770c022016-03-14 14:14:16 -0400114CFXJSE_Context* CFXJSE_Context::Create(v8::Isolate* pIsolate,
115 const FXJSE_CLASS* lpGlobalClass,
116 void* lpGlobalObject) {
117 CFXJSE_ScopeUtil_IsolateHandle scope(pIsolate);
118 CFXJSE_Context* pContext = new CFXJSE_Context(pIsolate);
119 CFXJSE_Class* lpGlobalClassObj = NULL;
120 v8::Local<v8::ObjectTemplate> hObjectTemplate;
121 if (lpGlobalClass) {
122 lpGlobalClassObj = CFXJSE_Class::Create(pContext, lpGlobalClass, TRUE);
123 ASSERT(lpGlobalClassObj);
124 v8::Local<v8::FunctionTemplate> hFunctionTemplate =
125 v8::Local<v8::FunctionTemplate>::New(pIsolate,
126 lpGlobalClassObj->m_hTemplate);
127 hObjectTemplate = hFunctionTemplate->InstanceTemplate();
128 } else {
129 hObjectTemplate = v8::ObjectTemplate::New(pIsolate);
130 hObjectTemplate->SetInternalFieldCount(1);
131 }
132 v8::Local<v8::Context> hNewContext =
133 v8::Context::New(pIsolate, NULL, hObjectTemplate);
134 v8::Local<v8::Context> hRootContext = v8::Local<v8::Context>::New(
135 pIsolate, CFXJSE_RuntimeData::Get(pIsolate)->m_hRootContext);
136 hNewContext->SetSecurityToken(hRootContext->GetSecurityToken());
137 v8::Local<v8::Object> hGlobalObject =
138 FXJSE_GetGlobalObjectFromContext(hNewContext);
139 FXJSE_UpdateObjectBinding(hGlobalObject, lpGlobalObject);
140 pContext->m_hContext.Reset(pIsolate, hNewContext);
141 return pContext;
142}
143
tsepez56286b32016-05-17 16:24:34 -0700144CFXJSE_Context::CFXJSE_Context(v8::Isolate* pIsolate) : m_pIsolate(pIsolate) {}
145CFXJSE_Context::~CFXJSE_Context() {}
Dan Sinclair1770c022016-03-14 14:14:16 -0400146
147void CFXJSE_Context::GetGlobalObject(CFXJSE_Value* pValue) {
148 ASSERT(pValue);
149 CFXJSE_ScopeUtil_IsolateHandleContext scope(this);
150 v8::Local<v8::Context> hContext =
151 v8::Local<v8::Context>::New(m_pIsolate, m_hContext);
152 v8::Local<v8::Object> hGlobalObject = hContext->Global();
153 pValue->ForceSetValue(hGlobalObject);
154}
155
156FX_BOOL CFXJSE_Context::ExecuteScript(const FX_CHAR* szScript,
157 CFXJSE_Value* lpRetValue,
158 CFXJSE_Value* lpNewThisObject) {
159 CFXJSE_ScopeUtil_IsolateHandleContext scope(this);
160 v8::TryCatch trycatch(m_pIsolate);
161 v8::Local<v8::String> hScriptString =
162 v8::String::NewFromUtf8(m_pIsolate, szScript);
163 if (lpNewThisObject == NULL) {
164 v8::Local<v8::Script> hScript = v8::Script::Compile(hScriptString);
165 if (!trycatch.HasCaught()) {
166 v8::Local<v8::Value> hValue = hScript->Run();
167 if (!trycatch.HasCaught()) {
168 if (lpRetValue) {
169 lpRetValue->m_hValue.Reset(m_pIsolate, hValue);
170 }
171 return TRUE;
172 }
173 }
174 if (lpRetValue) {
175 lpRetValue->m_hValue.Reset(m_pIsolate,
176 FXJSE_CreateReturnValue(m_pIsolate, trycatch));
177 }
178 return FALSE;
179 } else {
180 v8::Local<v8::Value> hNewThis =
181 v8::Local<v8::Value>::New(m_pIsolate, lpNewThisObject->m_hValue);
182 ASSERT(!hNewThis.IsEmpty());
183 v8::Local<v8::Script> hWrapper =
184 v8::Script::Compile(v8::String::NewFromUtf8(
185 m_pIsolate, "(function () { return eval(arguments[0]); })"));
186 v8::Local<v8::Value> hWrapperValue = hWrapper->Run();
187 ASSERT(hWrapperValue->IsFunction());
188 v8::Local<v8::Function> hWrapperFn = hWrapperValue.As<v8::Function>();
189 if (!trycatch.HasCaught()) {
190 v8::Local<v8::Value> rgArgs[] = {hScriptString};
191 v8::Local<v8::Value> hValue =
192 hWrapperFn->Call(hNewThis.As<v8::Object>(), 1, rgArgs);
193 if (!trycatch.HasCaught()) {
194 if (lpRetValue) {
195 lpRetValue->m_hValue.Reset(m_pIsolate, hValue);
196 }
197 return TRUE;
198 }
199 }
200 if (lpRetValue) {
201 lpRetValue->m_hValue.Reset(m_pIsolate,
202 FXJSE_CreateReturnValue(m_pIsolate, trycatch));
203 }
204 return FALSE;
205 }
206}