blob: abb30c5dc525ea5b9a4e914828734f7d3490c13e [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
14FXJSE_HCONTEXT FXJSE_Context_Create(FXJSE_HRUNTIME hRuntime,
15 const FXJSE_CLASS* lpGlobalClass,
16 void* lpGlobalObject) {
17 CFXJSE_Context* pContext = CFXJSE_Context::Create(
18 reinterpret_cast<v8::Isolate*>(hRuntime), lpGlobalClass, lpGlobalObject);
19 return reinterpret_cast<FXJSE_HCONTEXT>(pContext);
20}
21
22void FXJSE_Context_Release(FXJSE_HCONTEXT hContext) {
23 CFXJSE_Context* pContext = reinterpret_cast<CFXJSE_Context*>(hContext);
24 if (pContext) {
25 delete pContext;
26 }
27}
28
29FXJSE_HVALUE FXJSE_Context_GetGlobalObject(FXJSE_HCONTEXT hContext) {
30 CFXJSE_Context* pContext = reinterpret_cast<CFXJSE_Context*>(hContext);
31 if (!pContext) {
32 return NULL;
33 }
34 CFXJSE_Value* lpValue = CFXJSE_Value::Create(pContext->GetRuntime());
35 ASSERT(lpValue);
36 pContext->GetGlobalObject(lpValue);
37 return reinterpret_cast<FXJSE_HVALUE>(lpValue);
38}
39
Dan Sinclair1770c022016-03-14 14:14:16 -040040static const FX_CHAR* szCompatibleModeScripts[] = {
41 "(function(global, list) {\n"
42 " 'use strict';\n"
43 " var objname;\n"
44 " for (objname in list) {\n"
45 " var globalobj = global[objname];\n"
46 " if (globalobj) {\n"
47 " list[objname].forEach(function(name) {\n"
48 " if (!globalobj[name]) {\n"
49 " Object.defineProperty(globalobj, name, {\n"
50 " writable: true,\n"
51 " enumerable: false,\n"
52 " value: (function(obj) {\n"
53 " if (arguments.length === 0) {\n"
54 " throw new TypeError('missing argument 0 when calling "
55 " function ' + objname + '.' + name);\n"
56 " }\n"
57 " return globalobj.prototype[name].apply(obj, "
58 " Array.prototype.slice.call(arguments, 1));\n"
59 " })\n"
60 " });\n"
61 " }\n"
62 " });\n"
63 " }\n"
64 " }\n"
65 "}(this, {String: ['substr', 'toUpperCase']}));"};
66void FXJSE_Context_EnableCompatibleMode(FXJSE_HCONTEXT hContext,
tsepez736f28a2016-03-25 14:19:51 -070067 uint32_t dwCompatibleFlags) {
Dan Sinclair1770c022016-03-14 14:14:16 -040068 for (uint32_t i = 0; i < (uint32_t)FXJSE_COMPATIBLEMODEFLAGCOUNT; i++) {
69 if (dwCompatibleFlags & (1 << i)) {
70 FXJSE_ExecuteScript(hContext, szCompatibleModeScripts[i], NULL, NULL);
71 }
72 }
73}
74
75FX_BOOL FXJSE_ExecuteScript(FXJSE_HCONTEXT hContext,
76 const FX_CHAR* szScript,
77 FXJSE_HVALUE hRetValue,
78 FXJSE_HVALUE hNewThisObject) {
79 CFXJSE_Context* pContext = reinterpret_cast<CFXJSE_Context*>(hContext);
80 ASSERT(pContext);
81 return pContext->ExecuteScript(
82 szScript, reinterpret_cast<CFXJSE_Value*>(hRetValue),
83 reinterpret_cast<CFXJSE_Value*>(hNewThisObject));
84}
85
86v8::Local<v8::Object> FXJSE_CreateReturnValue(v8::Isolate* pIsolate,
87 v8::TryCatch& trycatch) {
88 v8::Local<v8::Object> hReturnValue = v8::Object::New(pIsolate);
89 if (trycatch.HasCaught()) {
90 v8::Local<v8::Value> hException = trycatch.Exception();
91 v8::Local<v8::Message> hMessage = trycatch.Message();
92 if (hException->IsObject()) {
93 v8::Local<v8::Value> hValue;
94 hValue = hException.As<v8::Object>()->Get(
95 v8::String::NewFromUtf8(pIsolate, "name"));
96 if (hValue->IsString() || hValue->IsStringObject()) {
97 hReturnValue->Set(0, hValue);
98 } else {
99 hReturnValue->Set(0, v8::String::NewFromUtf8(pIsolate, "Error"));
100 }
101 hValue = hException.As<v8::Object>()->Get(
102 v8::String::NewFromUtf8(pIsolate, "message"));
103 if (hValue->IsString() || hValue->IsStringObject()) {
104 hReturnValue->Set(1, hValue);
105 } else {
106 hReturnValue->Set(1, hMessage->Get());
107 }
108 } else {
109 hReturnValue->Set(0, v8::String::NewFromUtf8(pIsolate, "Error"));
110 hReturnValue->Set(1, hMessage->Get());
111 }
112 hReturnValue->Set(2, hException);
113 hReturnValue->Set(3, v8::Integer::New(pIsolate, hMessage->GetLineNumber()));
114 hReturnValue->Set(4, hMessage->GetSourceLine());
115 v8::Maybe<int32_t> maybe_int =
116 hMessage->GetStartColumn(pIsolate->GetCurrentContext());
117 hReturnValue->Set(5, v8::Integer::New(pIsolate, maybe_int.FromMaybe(0)));
118 maybe_int = hMessage->GetEndColumn(pIsolate->GetCurrentContext());
119 hReturnValue->Set(6, v8::Integer::New(pIsolate, maybe_int.FromMaybe(0)));
120 }
121 return hReturnValue;
122}
123
Dan Sinclair1770c022016-03-14 14:14:16 -0400124CFXJSE_Context* CFXJSE_Context::Create(v8::Isolate* pIsolate,
125 const FXJSE_CLASS* lpGlobalClass,
126 void* lpGlobalObject) {
127 CFXJSE_ScopeUtil_IsolateHandle scope(pIsolate);
128 CFXJSE_Context* pContext = new CFXJSE_Context(pIsolate);
129 CFXJSE_Class* lpGlobalClassObj = NULL;
130 v8::Local<v8::ObjectTemplate> hObjectTemplate;
131 if (lpGlobalClass) {
132 lpGlobalClassObj = CFXJSE_Class::Create(pContext, lpGlobalClass, TRUE);
133 ASSERT(lpGlobalClassObj);
134 v8::Local<v8::FunctionTemplate> hFunctionTemplate =
135 v8::Local<v8::FunctionTemplate>::New(pIsolate,
136 lpGlobalClassObj->m_hTemplate);
137 hObjectTemplate = hFunctionTemplate->InstanceTemplate();
138 } else {
139 hObjectTemplate = v8::ObjectTemplate::New(pIsolate);
140 hObjectTemplate->SetInternalFieldCount(1);
141 }
142 v8::Local<v8::Context> hNewContext =
143 v8::Context::New(pIsolate, NULL, hObjectTemplate);
144 v8::Local<v8::Context> hRootContext = v8::Local<v8::Context>::New(
145 pIsolate, CFXJSE_RuntimeData::Get(pIsolate)->m_hRootContext);
146 hNewContext->SetSecurityToken(hRootContext->GetSecurityToken());
147 v8::Local<v8::Object> hGlobalObject =
148 FXJSE_GetGlobalObjectFromContext(hNewContext);
149 FXJSE_UpdateObjectBinding(hGlobalObject, lpGlobalObject);
150 pContext->m_hContext.Reset(pIsolate, hNewContext);
151 return pContext;
152}
153
154CFXJSE_Context::~CFXJSE_Context() {
155 for (int32_t i = 0, count = m_rgClasses.GetSize(); i < count; i++) {
156 CFXJSE_Class* pClass = m_rgClasses[i];
157 if (pClass) {
158 delete pClass;
159 }
160 }
161 m_rgClasses.RemoveAll();
162}
163
164void CFXJSE_Context::GetGlobalObject(CFXJSE_Value* pValue) {
165 ASSERT(pValue);
166 CFXJSE_ScopeUtil_IsolateHandleContext scope(this);
167 v8::Local<v8::Context> hContext =
168 v8::Local<v8::Context>::New(m_pIsolate, m_hContext);
169 v8::Local<v8::Object> hGlobalObject = hContext->Global();
170 pValue->ForceSetValue(hGlobalObject);
171}
172
173FX_BOOL CFXJSE_Context::ExecuteScript(const FX_CHAR* szScript,
174 CFXJSE_Value* lpRetValue,
175 CFXJSE_Value* lpNewThisObject) {
176 CFXJSE_ScopeUtil_IsolateHandleContext scope(this);
177 v8::TryCatch trycatch(m_pIsolate);
178 v8::Local<v8::String> hScriptString =
179 v8::String::NewFromUtf8(m_pIsolate, szScript);
180 if (lpNewThisObject == NULL) {
181 v8::Local<v8::Script> hScript = v8::Script::Compile(hScriptString);
182 if (!trycatch.HasCaught()) {
183 v8::Local<v8::Value> hValue = hScript->Run();
184 if (!trycatch.HasCaught()) {
185 if (lpRetValue) {
186 lpRetValue->m_hValue.Reset(m_pIsolate, hValue);
187 }
188 return TRUE;
189 }
190 }
191 if (lpRetValue) {
192 lpRetValue->m_hValue.Reset(m_pIsolate,
193 FXJSE_CreateReturnValue(m_pIsolate, trycatch));
194 }
195 return FALSE;
196 } else {
197 v8::Local<v8::Value> hNewThis =
198 v8::Local<v8::Value>::New(m_pIsolate, lpNewThisObject->m_hValue);
199 ASSERT(!hNewThis.IsEmpty());
200 v8::Local<v8::Script> hWrapper =
201 v8::Script::Compile(v8::String::NewFromUtf8(
202 m_pIsolate, "(function () { return eval(arguments[0]); })"));
203 v8::Local<v8::Value> hWrapperValue = hWrapper->Run();
204 ASSERT(hWrapperValue->IsFunction());
205 v8::Local<v8::Function> hWrapperFn = hWrapperValue.As<v8::Function>();
206 if (!trycatch.HasCaught()) {
207 v8::Local<v8::Value> rgArgs[] = {hScriptString};
208 v8::Local<v8::Value> hValue =
209 hWrapperFn->Call(hNewThis.As<v8::Object>(), 1, rgArgs);
210 if (!trycatch.HasCaught()) {
211 if (lpRetValue) {
212 lpRetValue->m_hValue.Reset(m_pIsolate, hValue);
213 }
214 return TRUE;
215 }
216 }
217 if (lpRetValue) {
218 lpRetValue->m_hValue.Reset(m_pIsolate,
219 FXJSE_CreateReturnValue(m_pIsolate, trycatch));
220 }
221 return FALSE;
222 }
223}