blob: a49ee312f31d1406e43d2e11e9b0d8b34464af2d [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
dsinclair8bd9ce02016-06-09 13:24:34 -07007#include "fxjse/include/cfxjse_value.h"
Dan Sinclair1770c022016-03-14 14:14:16 -04008
9#include <math.h>
10
dsinclair8bd9ce02016-06-09 13:24:34 -070011#include "fxjse/context.h"
12#include "fxjse/include/cfxjse_class.h"
Dan Sinclair1770c022016-03-14 14:14:16 -040013
dsinclair769b1372016-06-08 13:12:41 -070014void FXJSE_ThrowMessage(const CFX_ByteStringC& utf8Message) {
Dan Sinclair1770c022016-03-14 14:14:16 -040015 v8::Isolate* pIsolate = v8::Isolate::GetCurrent();
16 ASSERT(pIsolate);
17
18 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(pIsolate);
19 v8::Local<v8::String> hMessage = v8::String::NewFromUtf8(
dsinclair179bebb2016-04-05 11:02:18 -070020 pIsolate, utf8Message.c_str(), v8::String::kNormalString,
Dan Sinclair1770c022016-03-14 14:14:16 -040021 utf8Message.GetLength());
dsinclair769b1372016-06-08 13:12:41 -070022 v8::Local<v8::Value> hError = v8::Exception::Error(hMessage);
Dan Sinclair1770c022016-03-14 14:14:16 -040023 pIsolate->ThrowException(hError);
24}
25
dsinclair8f3074b2016-06-02 17:45:25 -070026CFXJSE_HostObject* CFXJSE_Value::ToHostObject(CFXJSE_Class* lpClass) const {
Dan Sinclair1770c022016-03-14 14:14:16 -040027 ASSERT(!m_hValue.IsEmpty());
28
29 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
dsinclair12a6b0c2016-05-26 11:14:08 -070030 v8::Local<v8::Value> pValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
31 ASSERT(!pValue.IsEmpty());
Dan Sinclair1770c022016-03-14 14:14:16 -040032
dsinclair12a6b0c2016-05-26 11:14:08 -070033 if (!pValue->IsObject())
Dan Sinclair1770c022016-03-14 14:14:16 -040034 return nullptr;
35
dsinclair12a6b0c2016-05-26 11:14:08 -070036 return FXJSE_RetrieveObjectBinding(pValue.As<v8::Object>(), lpClass);
Dan Sinclair1770c022016-03-14 14:14:16 -040037}
38
dsinclairf27aeec2016-06-07 19:36:18 -070039void CFXJSE_Value::SetObject(CFXJSE_HostObject* lpObject,
40 CFXJSE_Class* pClass) {
41 if (!pClass) {
42 ASSERT(!lpObject);
43 SetJSObject();
44 return;
Dan Sinclair1770c022016-03-14 14:14:16 -040045 }
dsinclairf27aeec2016-06-07 19:36:18 -070046 SetHostObject(lpObject, pClass);
Dan Sinclair1770c022016-03-14 14:14:16 -040047}
48
tsepez29adee72016-05-31 14:22:09 -070049void CFXJSE_Value::SetHostObject(CFXJSE_HostObject* lpObject,
50 CFXJSE_Class* lpClass) {
Dan Sinclair1770c022016-03-14 14:14:16 -040051 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
52 ASSERT(lpClass);
53 v8::Local<v8::FunctionTemplate> hClass =
54 v8::Local<v8::FunctionTemplate>::New(m_pIsolate, lpClass->m_hTemplate);
55 v8::Local<v8::Object> hObject = hClass->InstanceTemplate()->NewInstance();
56 FXJSE_UpdateObjectBinding(hObject, lpObject);
57 m_hValue.Reset(m_pIsolate, hObject);
58}
59
60void CFXJSE_Value::SetArray(uint32_t uValueCount, CFXJSE_Value** rgValues) {
61 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
62 v8::Local<v8::Array> hArrayObject = v8::Array::New(m_pIsolate, uValueCount);
63 if (rgValues) {
64 for (uint32_t i = 0; i < uValueCount; i++) {
65 if (rgValues[i]) {
66 hArrayObject->Set(i, v8::Local<v8::Value>::New(
67 m_pIsolate, rgValues[i]->DirectGetValue()));
68 }
69 }
70 }
71 m_hValue.Reset(m_pIsolate, hArrayObject);
72}
73
dsinclair04fd27b2016-03-30 12:59:04 -070074void CFXJSE_Value::SetDate(double dDouble) {
Dan Sinclair1770c022016-03-14 14:14:16 -040075 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
76 v8::Local<v8::Value> hDate = v8::Date::New(m_pIsolate, dDouble);
77 m_hValue.Reset(m_pIsolate, hDate);
78}
79
80FX_BOOL CFXJSE_Value::SetObjectProperty(const CFX_ByteStringC& szPropName,
81 CFXJSE_Value* lpPropValue) {
dsinclairf27aeec2016-06-07 19:36:18 -070082 ASSERT(lpPropValue);
Dan Sinclair1770c022016-03-14 14:14:16 -040083 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
84 v8::Local<v8::Value> hObject =
85 v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
86 if (!hObject->IsObject())
87 return FALSE;
88
89 v8::Local<v8::Value> hPropValue =
90 v8::Local<v8::Value>::New(m_pIsolate, lpPropValue->DirectGetValue());
91 return (FX_BOOL)hObject.As<v8::Object>()->Set(
dsinclair179bebb2016-04-05 11:02:18 -070092 v8::String::NewFromUtf8(m_pIsolate, szPropName.c_str(),
Dan Sinclair1770c022016-03-14 14:14:16 -040093 v8::String::kNormalString,
94 szPropName.GetLength()),
95 hPropValue);
96}
97
98FX_BOOL CFXJSE_Value::GetObjectProperty(const CFX_ByteStringC& szPropName,
99 CFXJSE_Value* lpPropValue) {
dsinclairf27aeec2016-06-07 19:36:18 -0700100 ASSERT(lpPropValue);
Dan Sinclair1770c022016-03-14 14:14:16 -0400101 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
102 v8::Local<v8::Value> hObject =
103 v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
104 if (!hObject->IsObject())
105 return FALSE;
106
107 v8::Local<v8::Value> hPropValue =
108 hObject.As<v8::Object>()->Get(v8::String::NewFromUtf8(
dsinclair179bebb2016-04-05 11:02:18 -0700109 m_pIsolate, szPropName.c_str(), v8::String::kNormalString,
Dan Sinclair1770c022016-03-14 14:14:16 -0400110 szPropName.GetLength()));
111 lpPropValue->ForceSetValue(hPropValue);
112 return TRUE;
113}
114
115FX_BOOL CFXJSE_Value::SetObjectProperty(uint32_t uPropIdx,
116 CFXJSE_Value* lpPropValue) {
117 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
118 v8::Local<v8::Value> hObject =
119 v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
120 if (!hObject->IsObject())
121 return FALSE;
122
123 v8::Local<v8::Value> hPropValue =
124 v8::Local<v8::Value>::New(m_pIsolate, lpPropValue->DirectGetValue());
125 return (FX_BOOL)hObject.As<v8::Object>()->Set(uPropIdx, hPropValue);
126}
127
dsinclairf27aeec2016-06-07 19:36:18 -0700128FX_BOOL CFXJSE_Value::GetObjectPropertyByIdx(uint32_t uPropIdx,
129 CFXJSE_Value* lpPropValue) {
Dan Sinclair1770c022016-03-14 14:14:16 -0400130 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
131 v8::Local<v8::Value> hObject =
132 v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
133 if (!hObject->IsObject())
134 return FALSE;
135
136 v8::Local<v8::Value> hPropValue = hObject.As<v8::Object>()->Get(uPropIdx);
137 lpPropValue->ForceSetValue(hPropValue);
138 return TRUE;
139}
140
141FX_BOOL CFXJSE_Value::DeleteObjectProperty(const CFX_ByteStringC& szPropName) {
142 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
143 v8::Local<v8::Value> hObject =
144 v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
145 if (!hObject->IsObject())
146 return FALSE;
147
148 hObject.As<v8::Object>()->Delete(v8::String::NewFromUtf8(
dsinclair179bebb2016-04-05 11:02:18 -0700149 m_pIsolate, szPropName.c_str(), v8::String::kNormalString,
Dan Sinclair1770c022016-03-14 14:14:16 -0400150 szPropName.GetLength()));
151 return TRUE;
152}
153
154FX_BOOL CFXJSE_Value::HasObjectOwnProperty(const CFX_ByteStringC& szPropName,
155 FX_BOOL bUseTypeGetter) {
156 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
157 v8::Local<v8::Value> hObject =
158 v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
159 if (!hObject->IsObject())
160 return FALSE;
161
162 v8::Local<v8::String> hKey = v8::String::NewFromUtf8(
dsinclair179bebb2016-04-05 11:02:18 -0700163 m_pIsolate, szPropName.c_str(), v8::String::kNormalString,
Dan Sinclair1770c022016-03-14 14:14:16 -0400164 szPropName.GetLength());
165 return hObject.As<v8::Object>()->HasRealNamedProperty(hKey) ||
166 (bUseTypeGetter &&
167 hObject.As<v8::Object>()
168 ->HasOwnProperty(m_pIsolate->GetCurrentContext(), hKey)
169 .FromMaybe(false));
170}
171
172FX_BOOL CFXJSE_Value::SetObjectOwnProperty(const CFX_ByteStringC& szPropName,
173 CFXJSE_Value* lpPropValue) {
dsinclairf27aeec2016-06-07 19:36:18 -0700174 ASSERT(lpPropValue);
Dan Sinclair1770c022016-03-14 14:14:16 -0400175 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
176 v8::Local<v8::Value> hObject =
177 v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
178 if (!hObject->IsObject())
179 return FALSE;
180
dsinclair12a6b0c2016-05-26 11:14:08 -0700181 v8::Local<v8::Value> pValue =
Dan Sinclair1770c022016-03-14 14:14:16 -0400182 v8::Local<v8::Value>::New(m_pIsolate, lpPropValue->m_hValue);
183 return hObject.As<v8::Object>()
184 ->DefineOwnProperty(
185 m_pIsolate->GetCurrentContext(),
dsinclair179bebb2016-04-05 11:02:18 -0700186 v8::String::NewFromUtf8(m_pIsolate, szPropName.c_str(),
Dan Sinclair1770c022016-03-14 14:14:16 -0400187 v8::String::kNormalString,
188 szPropName.GetLength()),
dsinclair12a6b0c2016-05-26 11:14:08 -0700189 pValue)
Dan Sinclair1770c022016-03-14 14:14:16 -0400190 .FromMaybe(false);
191}
192
193FX_BOOL CFXJSE_Value::SetFunctionBind(CFXJSE_Value* lpOldFunction,
194 CFXJSE_Value* lpNewThis) {
dsinclairf27aeec2016-06-07 19:36:18 -0700195 ASSERT(lpOldFunction && lpNewThis);
196
Dan Sinclair1770c022016-03-14 14:14:16 -0400197 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
198 v8::Local<v8::Value> rgArgs[2];
199 v8::Local<v8::Value> hOldFunction =
200 v8::Local<v8::Value>::New(m_pIsolate, lpOldFunction->DirectGetValue());
201 if (hOldFunction.IsEmpty() || !hOldFunction->IsFunction())
202 return FALSE;
203
204 rgArgs[0] = hOldFunction;
205 v8::Local<v8::Value> hNewThis =
206 v8::Local<v8::Value>::New(m_pIsolate, lpNewThis->DirectGetValue());
207 if (hNewThis.IsEmpty())
208 return FALSE;
209
210 rgArgs[1] = hNewThis;
211 v8::Local<v8::String> hBinderFuncSource =
212 v8::String::NewFromUtf8(m_pIsolate,
213 "(function (oldfunction, newthis) { return "
214 "oldfunction.bind(newthis); })");
215 v8::Local<v8::Function> hBinderFunc =
216 v8::Script::Compile(hBinderFuncSource)->Run().As<v8::Function>();
217 v8::Local<v8::Value> hBoundFunction =
218 hBinderFunc->Call(m_pIsolate->GetCurrentContext()->Global(), 2, rgArgs);
219 if (hBoundFunction.IsEmpty() || !hBoundFunction->IsFunction())
220 return FALSE;
221
222 m_hValue.Reset(m_pIsolate, hBoundFunction);
223 return TRUE;
224}
225
226#define FXJSE_INVALID_PTR ((void*)(intptr_t)-1)
227FX_BOOL CFXJSE_Value::Call(CFXJSE_Value* lpReceiver,
228 CFXJSE_Value* lpRetValue,
229 uint32_t nArgCount,
dsinclair12a6b0c2016-05-26 11:14:08 -0700230 CFXJSE_Value** lpArgs) {
Dan Sinclair1770c022016-03-14 14:14:16 -0400231 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
232 v8::Local<v8::Value> hFunctionValue =
233 v8::Local<v8::Value>::New(m_pIsolate, DirectGetValue());
234 v8::Local<v8::Object> hFunctionObject =
235 !hFunctionValue.IsEmpty() && hFunctionValue->IsObject()
236 ? hFunctionValue.As<v8::Object>()
237 : v8::Local<v8::Object>();
238
239 v8::TryCatch trycatch(m_pIsolate);
240 if (hFunctionObject.IsEmpty() || !hFunctionObject->IsCallable()) {
241 if (lpRetValue)
242 lpRetValue->ForceSetValue(FXJSE_CreateReturnValue(m_pIsolate, trycatch));
243 return FALSE;
244 }
245
246 v8::Local<v8::Value> hReturnValue;
247 v8::Local<v8::Value>* lpLocalArgs = NULL;
248 if (nArgCount) {
249 lpLocalArgs = FX_Alloc(v8::Local<v8::Value>, nArgCount);
250 for (uint32_t i = 0; i < nArgCount; i++) {
251 new (lpLocalArgs + i) v8::Local<v8::Value>;
dsinclair12a6b0c2016-05-26 11:14:08 -0700252 CFXJSE_Value* lpArg = lpArgs[i];
Dan Sinclair1770c022016-03-14 14:14:16 -0400253 if (lpArg) {
254 lpLocalArgs[i] =
255 v8::Local<v8::Value>::New(m_pIsolate, lpArg->DirectGetValue());
256 }
257 if (lpLocalArgs[i].IsEmpty()) {
258 lpLocalArgs[i] = v8::Undefined(m_pIsolate);
259 }
260 }
261 }
262
263 FX_BOOL bRetValue = TRUE;
264 if (lpReceiver == FXJSE_INVALID_PTR) {
265 v8::MaybeLocal<v8::Value> maybe_retvalue =
266 hFunctionObject->CallAsConstructor(m_pIsolate->GetCurrentContext(),
267 nArgCount, lpLocalArgs);
268 hReturnValue = maybe_retvalue.FromMaybe(v8::Local<v8::Value>());
269 } else {
270 v8::Local<v8::Value> hReceiver;
271 if (lpReceiver) {
272 hReceiver =
273 v8::Local<v8::Value>::New(m_pIsolate, lpReceiver->DirectGetValue());
274 }
275 if (hReceiver.IsEmpty() || !hReceiver->IsObject())
276 hReceiver = v8::Object::New(m_pIsolate);
277
278 v8::MaybeLocal<v8::Value> maybe_retvalue = hFunctionObject->CallAsFunction(
279 m_pIsolate->GetCurrentContext(), hReceiver, nArgCount, lpLocalArgs);
280 hReturnValue = maybe_retvalue.FromMaybe(v8::Local<v8::Value>());
281 }
282
283 if (trycatch.HasCaught()) {
284 hReturnValue = FXJSE_CreateReturnValue(m_pIsolate, trycatch);
285 bRetValue = FALSE;
286 }
287
288 if (lpRetValue)
289 lpRetValue->ForceSetValue(hReturnValue);
290
291 if (lpLocalArgs) {
292 for (uint32_t i = 0; i < nArgCount; i++)
293 lpLocalArgs[i].~Local();
294 FX_Free(lpLocalArgs);
295 }
296 return bRetValue;
297}