Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1 | // 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 | |
dsinclair | 8bd9ce0 | 2016-06-09 13:24:34 -0700 | [diff] [blame] | 7 | #include "fxjse/include/cfxjse_value.h" |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 8 | |
| 9 | #include <math.h> |
| 10 | |
dsinclair | 8bd9ce0 | 2016-06-09 13:24:34 -0700 | [diff] [blame] | 11 | #include "fxjse/context.h" |
| 12 | #include "fxjse/include/cfxjse_class.h" |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 13 | |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 14 | void FXJSE_ThrowMessage(const CFX_ByteStringC& utf8Message) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 15 | 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( |
dsinclair | 179bebb | 2016-04-05 11:02:18 -0700 | [diff] [blame] | 20 | pIsolate, utf8Message.c_str(), v8::String::kNormalString, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 21 | utf8Message.GetLength()); |
dsinclair | 769b137 | 2016-06-08 13:12:41 -0700 | [diff] [blame] | 22 | v8::Local<v8::Value> hError = v8::Exception::Error(hMessage); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 23 | pIsolate->ThrowException(hError); |
| 24 | } |
| 25 | |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame] | 26 | CFXJSE_HostObject* CFXJSE_Value::ToHostObject(CFXJSE_Class* lpClass) const { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 27 | ASSERT(!m_hValue.IsEmpty()); |
| 28 | |
| 29 | CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 30 | v8::Local<v8::Value> pValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue); |
| 31 | ASSERT(!pValue.IsEmpty()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 32 | |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 33 | if (!pValue->IsObject()) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 34 | return nullptr; |
| 35 | |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 36 | return FXJSE_RetrieveObjectBinding(pValue.As<v8::Object>(), lpClass); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 37 | } |
| 38 | |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 39 | void CFXJSE_Value::SetObject(CFXJSE_HostObject* lpObject, |
| 40 | CFXJSE_Class* pClass) { |
| 41 | if (!pClass) { |
| 42 | ASSERT(!lpObject); |
| 43 | SetJSObject(); |
| 44 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 45 | } |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 46 | SetHostObject(lpObject, pClass); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 47 | } |
| 48 | |
tsepez | 29adee7 | 2016-05-31 14:22:09 -0700 | [diff] [blame] | 49 | void CFXJSE_Value::SetHostObject(CFXJSE_HostObject* lpObject, |
| 50 | CFXJSE_Class* lpClass) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 51 | 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 | |
| 60 | void 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 | |
dsinclair | 04fd27b | 2016-03-30 12:59:04 -0700 | [diff] [blame] | 74 | void CFXJSE_Value::SetDate(double dDouble) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 75 | 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 | |
| 80 | FX_BOOL CFXJSE_Value::SetObjectProperty(const CFX_ByteStringC& szPropName, |
| 81 | CFXJSE_Value* lpPropValue) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 82 | ASSERT(lpPropValue); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 83 | 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( |
dsinclair | 179bebb | 2016-04-05 11:02:18 -0700 | [diff] [blame] | 92 | v8::String::NewFromUtf8(m_pIsolate, szPropName.c_str(), |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 93 | v8::String::kNormalString, |
| 94 | szPropName.GetLength()), |
| 95 | hPropValue); |
| 96 | } |
| 97 | |
| 98 | FX_BOOL CFXJSE_Value::GetObjectProperty(const CFX_ByteStringC& szPropName, |
| 99 | CFXJSE_Value* lpPropValue) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 100 | ASSERT(lpPropValue); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 101 | 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( |
dsinclair | 179bebb | 2016-04-05 11:02:18 -0700 | [diff] [blame] | 109 | m_pIsolate, szPropName.c_str(), v8::String::kNormalString, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 110 | szPropName.GetLength())); |
| 111 | lpPropValue->ForceSetValue(hPropValue); |
| 112 | return TRUE; |
| 113 | } |
| 114 | |
| 115 | FX_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 | |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 128 | FX_BOOL CFXJSE_Value::GetObjectPropertyByIdx(uint32_t uPropIdx, |
| 129 | CFXJSE_Value* lpPropValue) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 130 | 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 | |
| 141 | FX_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( |
dsinclair | 179bebb | 2016-04-05 11:02:18 -0700 | [diff] [blame] | 149 | m_pIsolate, szPropName.c_str(), v8::String::kNormalString, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 150 | szPropName.GetLength())); |
| 151 | return TRUE; |
| 152 | } |
| 153 | |
| 154 | FX_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( |
dsinclair | 179bebb | 2016-04-05 11:02:18 -0700 | [diff] [blame] | 163 | m_pIsolate, szPropName.c_str(), v8::String::kNormalString, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 164 | 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 | |
| 172 | FX_BOOL CFXJSE_Value::SetObjectOwnProperty(const CFX_ByteStringC& szPropName, |
| 173 | CFXJSE_Value* lpPropValue) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 174 | ASSERT(lpPropValue); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 175 | 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 | |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 181 | v8::Local<v8::Value> pValue = |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 182 | v8::Local<v8::Value>::New(m_pIsolate, lpPropValue->m_hValue); |
| 183 | return hObject.As<v8::Object>() |
| 184 | ->DefineOwnProperty( |
| 185 | m_pIsolate->GetCurrentContext(), |
dsinclair | 179bebb | 2016-04-05 11:02:18 -0700 | [diff] [blame] | 186 | v8::String::NewFromUtf8(m_pIsolate, szPropName.c_str(), |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 187 | v8::String::kNormalString, |
| 188 | szPropName.GetLength()), |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 189 | pValue) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 190 | .FromMaybe(false); |
| 191 | } |
| 192 | |
| 193 | FX_BOOL CFXJSE_Value::SetFunctionBind(CFXJSE_Value* lpOldFunction, |
| 194 | CFXJSE_Value* lpNewThis) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 195 | ASSERT(lpOldFunction && lpNewThis); |
| 196 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 197 | 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) |
| 227 | FX_BOOL CFXJSE_Value::Call(CFXJSE_Value* lpReceiver, |
| 228 | CFXJSE_Value* lpRetValue, |
| 229 | uint32_t nArgCount, |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 230 | CFXJSE_Value** lpArgs) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 231 | 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>; |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 252 | CFXJSE_Value* lpArg = lpArgs[i]; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 253 | 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 | } |