| OLD | NEW |
| (Empty) |
| 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 | |
| 7 #include "xfa/fxjse/value.h" | |
| 8 | |
| 9 #include <math.h> | |
| 10 | |
| 11 #include "xfa/fxjse/class.h" | |
| 12 #include "xfa/fxjse/context.h" | |
| 13 | |
| 14 void FXJSE_ThrowMessage(const CFX_ByteStringC& utf8Message) { | |
| 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( | |
| 20 pIsolate, utf8Message.c_str(), v8::String::kNormalString, | |
| 21 utf8Message.GetLength()); | |
| 22 v8::Local<v8::Value> hError = v8::Exception::Error(hMessage); | |
| 23 pIsolate->ThrowException(hError); | |
| 24 } | |
| 25 | |
| 26 CFXJSE_HostObject* CFXJSE_Value::ToHostObject(CFXJSE_Class* lpClass) const { | |
| 27 ASSERT(!m_hValue.IsEmpty()); | |
| 28 | |
| 29 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); | |
| 30 v8::Local<v8::Value> pValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue); | |
| 31 ASSERT(!pValue.IsEmpty()); | |
| 32 | |
| 33 if (!pValue->IsObject()) | |
| 34 return nullptr; | |
| 35 | |
| 36 return FXJSE_RetrieveObjectBinding(pValue.As<v8::Object>(), lpClass); | |
| 37 } | |
| 38 | |
| 39 void CFXJSE_Value::SetObject(CFXJSE_HostObject* lpObject, | |
| 40 CFXJSE_Class* pClass) { | |
| 41 if (!pClass) { | |
| 42 ASSERT(!lpObject); | |
| 43 SetJSObject(); | |
| 44 return; | |
| 45 } | |
| 46 SetHostObject(lpObject, pClass); | |
| 47 } | |
| 48 | |
| 49 void CFXJSE_Value::SetHostObject(CFXJSE_HostObject* lpObject, | |
| 50 CFXJSE_Class* lpClass) { | |
| 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 | |
| 74 void CFXJSE_Value::SetDate(double dDouble) { | |
| 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) { | |
| 82 ASSERT(lpPropValue); | |
| 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( | |
| 92 v8::String::NewFromUtf8(m_pIsolate, szPropName.c_str(), | |
| 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) { | |
| 100 ASSERT(lpPropValue); | |
| 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( | |
| 109 m_pIsolate, szPropName.c_str(), v8::String::kNormalString, | |
| 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 | |
| 128 FX_BOOL CFXJSE_Value::GetObjectPropertyByIdx(uint32_t uPropIdx, | |
| 129 CFXJSE_Value* lpPropValue) { | |
| 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( | |
| 149 m_pIsolate, szPropName.c_str(), v8::String::kNormalString, | |
| 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( | |
| 163 m_pIsolate, szPropName.c_str(), v8::String::kNormalString, | |
| 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) { | |
| 174 ASSERT(lpPropValue); | |
| 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 | |
| 181 v8::Local<v8::Value> pValue = | |
| 182 v8::Local<v8::Value>::New(m_pIsolate, lpPropValue->m_hValue); | |
| 183 return hObject.As<v8::Object>() | |
| 184 ->DefineOwnProperty( | |
| 185 m_pIsolate->GetCurrentContext(), | |
| 186 v8::String::NewFromUtf8(m_pIsolate, szPropName.c_str(), | |
| 187 v8::String::kNormalString, | |
| 188 szPropName.GetLength()), | |
| 189 pValue) | |
| 190 .FromMaybe(false); | |
| 191 } | |
| 192 | |
| 193 FX_BOOL CFXJSE_Value::SetFunctionBind(CFXJSE_Value* lpOldFunction, | |
| 194 CFXJSE_Value* lpNewThis) { | |
| 195 ASSERT(lpOldFunction && lpNewThis); | |
| 196 | |
| 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, | |
| 230 CFXJSE_Value** lpArgs) { | |
| 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>; | |
| 252 CFXJSE_Value* lpArg = lpArgs[i]; | |
| 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 } | |
| OLD | NEW |