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 | |
| 7 | #include "xfa/fxjse/value.h" |
| 8 | |
| 9 | #include <math.h> |
| 10 | |
| 11 | #include "xfa/fxjse/class.h" |
tsepez | 3a005f2 | 2016-05-27 17:45:00 -0700 | [diff] [blame] | 12 | #include "xfa/fxjse/context.h" |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 13 | |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 14 | FX_BOOL FXJSE_Value_IsUndefined(CFXJSE_Value* pValue) { |
| 15 | return pValue && pValue->IsUndefined(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 16 | } |
| 17 | |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 18 | FX_BOOL FXJSE_Value_IsNull(CFXJSE_Value* pValue) { |
| 19 | return pValue && pValue->IsNull(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 20 | } |
| 21 | |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 22 | FX_BOOL FXJSE_Value_IsBoolean(CFXJSE_Value* pValue) { |
| 23 | return pValue && pValue->IsBoolean(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 24 | } |
| 25 | |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 26 | FX_BOOL FXJSE_Value_IsUTF8String(CFXJSE_Value* pValue) { |
| 27 | return pValue && pValue->IsString(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 28 | } |
| 29 | |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 30 | FX_BOOL FXJSE_Value_IsNumber(CFXJSE_Value* pValue) { |
| 31 | return pValue && pValue->IsNumber(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 32 | } |
| 33 | |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 34 | FX_BOOL FXJSE_Value_IsObject(CFXJSE_Value* pValue) { |
| 35 | return pValue && pValue->IsObject(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 36 | } |
| 37 | |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 38 | FX_BOOL FXJSE_Value_IsArray(CFXJSE_Value* pValue) { |
| 39 | return pValue && pValue->IsArray(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 40 | } |
| 41 | |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 42 | FX_BOOL FXJSE_Value_IsFunction(CFXJSE_Value* pValue) { |
| 43 | return pValue && pValue->IsFunction(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 44 | } |
| 45 | |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 46 | FX_BOOL FXJSE_Value_ToBoolean(CFXJSE_Value* pValue) { |
| 47 | return pValue->ToBoolean(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 48 | } |
| 49 | |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 50 | FX_FLOAT FXJSE_Value_ToFloat(CFXJSE_Value* pValue) { |
| 51 | return pValue->ToFloat(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 52 | } |
| 53 | |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 54 | double FXJSE_Value_ToDouble(CFXJSE_Value* pValue) { |
| 55 | return pValue->ToDouble(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 56 | } |
| 57 | |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 58 | void FXJSE_Value_ToUTF8String(CFXJSE_Value* pValue, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 59 | CFX_ByteString& szStrOutput) { |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 60 | pValue->ToString(szStrOutput); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 61 | } |
| 62 | |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 63 | int32_t FXJSE_Value_ToInteger(CFXJSE_Value* pValue) { |
| 64 | return pValue->ToInteger(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 65 | } |
| 66 | |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 67 | void FXJSE_Value_SetUndefined(CFXJSE_Value* pValue) { |
| 68 | pValue->SetUndefined(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 69 | } |
| 70 | |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 71 | void FXJSE_Value_SetNull(CFXJSE_Value* pValue) { |
| 72 | pValue->SetNull(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 73 | } |
| 74 | |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 75 | void FXJSE_Value_SetBoolean(CFXJSE_Value* pValue, FX_BOOL bBoolean) { |
| 76 | pValue->SetBoolean(bBoolean); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 77 | } |
| 78 | |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 79 | void FXJSE_Value_SetUTF8String(CFXJSE_Value* pValue, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 80 | const CFX_ByteStringC& szString) { |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 81 | pValue->SetString(szString); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 82 | } |
| 83 | |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 84 | void FXJSE_Value_SetInteger(CFXJSE_Value* pValue, int32_t nInteger) { |
| 85 | pValue->SetInteger(nInteger); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 86 | } |
| 87 | |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 88 | void FXJSE_Value_SetFloat(CFXJSE_Value* pValue, FX_FLOAT fFloat) { |
| 89 | pValue->SetFloat(fFloat); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 90 | } |
| 91 | |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 92 | void FXJSE_Value_SetDouble(CFXJSE_Value* pValue, double dDouble) { |
| 93 | pValue->SetDouble(dDouble); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 94 | } |
| 95 | |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 96 | void FXJSE_Value_SetObject(CFXJSE_Value* pValue, |
tsepez | 29adee7 | 2016-05-31 14:22:09 -0700 | [diff] [blame] | 97 | CFXJSE_HostObject* lpObject, |
dsinclair | e9885e7 | 2016-05-26 10:14:00 -0700 | [diff] [blame] | 98 | CFXJSE_Class* pClass) { |
dsinclair | e9885e7 | 2016-05-26 10:14:00 -0700 | [diff] [blame] | 99 | if (!pClass) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 100 | ASSERT(!lpObject); |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 101 | pValue->SetJSObject(); |
tsepez | 29adee7 | 2016-05-31 14:22:09 -0700 | [diff] [blame] | 102 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 103 | } |
tsepez | 29adee7 | 2016-05-31 14:22:09 -0700 | [diff] [blame] | 104 | pValue->SetHostObject(lpObject, pClass); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 105 | } |
| 106 | |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 107 | void FXJSE_Value_SetArray(CFXJSE_Value* pValue, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 108 | uint32_t uValueCount, |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 109 | CFXJSE_Value** rgValues) { |
| 110 | pValue->SetArray(uValueCount, rgValues); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 111 | } |
| 112 | |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 113 | void FXJSE_Value_Set(CFXJSE_Value* pValue, CFXJSE_Value* pOriginalValue) { |
| 114 | ASSERT(pOriginalValue); |
| 115 | pValue->Assign(pOriginalValue); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 116 | } |
| 117 | |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 118 | FX_BOOL FXJSE_Value_GetObjectProp(CFXJSE_Value* pValue, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 119 | const CFX_ByteStringC& szPropName, |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 120 | CFXJSE_Value* pPropValue) { |
| 121 | ASSERT(pPropValue); |
| 122 | return pValue->GetObjectProperty(szPropName, pPropValue); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 123 | } |
| 124 | |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 125 | FX_BOOL FXJSE_Value_SetObjectProp(CFXJSE_Value* pValue, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 126 | const CFX_ByteStringC& szPropName, |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 127 | CFXJSE_Value* pPropValue) { |
| 128 | ASSERT(pPropValue); |
| 129 | return pValue->SetObjectProperty(szPropName, pPropValue); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 130 | } |
| 131 | |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 132 | FX_BOOL FXJSE_Value_GetObjectPropByIdx(CFXJSE_Value* pValue, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 133 | uint32_t uPropIdx, |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 134 | CFXJSE_Value* pPropValue) { |
| 135 | ASSERT(pPropValue); |
| 136 | return pValue->GetObjectProperty(uPropIdx, pPropValue); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 137 | } |
| 138 | |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 139 | FX_BOOL FXJSE_Value_DeleteObjectProp(CFXJSE_Value* pValue, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 140 | const CFX_ByteStringC& szPropName) { |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 141 | return pValue->DeleteObjectProperty(szPropName); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 142 | } |
| 143 | |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 144 | FX_BOOL FXJSE_Value_ObjectHasOwnProp(CFXJSE_Value* pValue, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 145 | const CFX_ByteStringC& szPropName, |
| 146 | FX_BOOL bUseTypeGetter) { |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 147 | return pValue->HasObjectOwnProperty(szPropName, bUseTypeGetter); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 148 | } |
| 149 | |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 150 | FX_BOOL FXJSE_Value_SetObjectOwnProp(CFXJSE_Value* pValue, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 151 | const CFX_ByteStringC& szPropName, |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 152 | CFXJSE_Value* pPropValue) { |
| 153 | ASSERT(pPropValue); |
| 154 | return pValue->SetObjectOwnProperty(szPropName, pPropValue); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 155 | } |
| 156 | |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 157 | FX_BOOL FXJSE_Value_SetFunctionBind(CFXJSE_Value* pValue, |
| 158 | CFXJSE_Value* pOldFunction, |
| 159 | CFXJSE_Value* pNewThis) { |
| 160 | ASSERT(pOldFunction && pNewThis); |
| 161 | return pValue->SetFunctionBind(pOldFunction, pNewThis); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 162 | } |
| 163 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 164 | void FXJSE_ThrowMessage(const CFX_ByteStringC& utf8Name, |
| 165 | const CFX_ByteStringC& utf8Message) { |
| 166 | v8::Isolate* pIsolate = v8::Isolate::GetCurrent(); |
| 167 | ASSERT(pIsolate); |
| 168 | |
| 169 | CFXJSE_ScopeUtil_IsolateHandleRootContext scope(pIsolate); |
| 170 | v8::Local<v8::String> hMessage = v8::String::NewFromUtf8( |
dsinclair | 179bebb | 2016-04-05 11:02:18 -0700 | [diff] [blame] | 171 | pIsolate, utf8Message.c_str(), v8::String::kNormalString, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 172 | utf8Message.GetLength()); |
| 173 | v8::Local<v8::Value> hError; |
| 174 | |
| 175 | if (utf8Name == "RangeError") { |
| 176 | hError = v8::Exception::RangeError(hMessage); |
| 177 | } else if (utf8Name == "ReferenceError") { |
| 178 | hError = v8::Exception::ReferenceError(hMessage); |
| 179 | } else if (utf8Name == "SyntaxError") { |
| 180 | hError = v8::Exception::SyntaxError(hMessage); |
| 181 | } else if (utf8Name == "TypeError") { |
| 182 | hError = v8::Exception::TypeError(hMessage); |
| 183 | } else { |
| 184 | hError = v8::Exception::Error(hMessage); |
| 185 | if (utf8Name != "Error" && !utf8Name.IsEmpty()) { |
| 186 | hError.As<v8::Object>()->Set( |
| 187 | v8::String::NewFromUtf8(pIsolate, "name"), |
dsinclair | 179bebb | 2016-04-05 11:02:18 -0700 | [diff] [blame] | 188 | v8::String::NewFromUtf8(pIsolate, utf8Name.c_str(), |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 189 | v8::String::kNormalString, |
| 190 | utf8Name.GetLength())); |
| 191 | } |
| 192 | } |
| 193 | pIsolate->ThrowException(hError); |
| 194 | } |
| 195 | |
dsinclair | 8f3074b | 2016-06-02 17:45:25 -0700 | [diff] [blame^] | 196 | CFXJSE_HostObject* CFXJSE_Value::ToHostObject(CFXJSE_Class* lpClass) const { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 197 | ASSERT(!m_hValue.IsEmpty()); |
| 198 | |
| 199 | CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 200 | v8::Local<v8::Value> pValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue); |
| 201 | ASSERT(!pValue.IsEmpty()); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 202 | |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 203 | if (!pValue->IsObject()) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 204 | return nullptr; |
| 205 | |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 206 | return FXJSE_RetrieveObjectBinding(pValue.As<v8::Object>(), lpClass); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | V8_INLINE static double FXJSE_ftod(FX_FLOAT fNumber) { |
| 210 | if (sizeof(FX_FLOAT) != 4) { |
| 211 | ASSERT(FALSE); |
| 212 | return fNumber; |
| 213 | } |
| 214 | |
| 215 | uint32_t nFloatBits = (uint32_t&)fNumber; |
| 216 | uint8_t nExponent = (uint8_t)(nFloatBits >> 16 >> 7); |
| 217 | if (nExponent == 0 || nExponent == 255) |
| 218 | return fNumber; |
| 219 | |
| 220 | int8_t nErrExp = nExponent - 127 - 23; |
| 221 | if (nErrExp >= 0) |
| 222 | return fNumber; |
| 223 | |
| 224 | double dwError = pow(2.0, nErrExp), dwErrorHalf = dwError / 2; |
| 225 | double dNumber = fNumber, dNumberAbs = fabs(fNumber); |
| 226 | double dNumberAbsMin = dNumberAbs - dwErrorHalf, |
| 227 | dNumberAbsMax = dNumberAbs + dwErrorHalf; |
| 228 | int32_t iErrPos = 0; |
| 229 | if (floor(dNumberAbsMin) == floor(dNumberAbsMax)) { |
| 230 | dNumberAbsMin = fmod(dNumberAbsMin, 1.0); |
| 231 | dNumberAbsMax = fmod(dNumberAbsMax, 1.0); |
| 232 | int32_t iErrPosMin = 1, iErrPosMax = 38; |
| 233 | do { |
| 234 | int32_t iMid = (iErrPosMin + iErrPosMax) / 2; |
| 235 | double dPow = pow(10.0, iMid); |
| 236 | if (floor(dNumberAbsMin * dPow) == floor(dNumberAbsMax * dPow)) { |
| 237 | iErrPosMin = iMid + 1; |
| 238 | } else { |
| 239 | iErrPosMax = iMid; |
| 240 | } |
| 241 | } while (iErrPosMin < iErrPosMax); |
| 242 | iErrPos = iErrPosMax; |
| 243 | } |
| 244 | double dPow = pow(10.0, iErrPos); |
| 245 | return fNumber < 0 ? ceil(dNumber * dPow - 0.5) / dPow |
| 246 | : floor(dNumber * dPow + 0.5) / dPow; |
| 247 | } |
| 248 | |
| 249 | void CFXJSE_Value::SetFloat(FX_FLOAT fFloat) { |
| 250 | CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 251 | v8::Local<v8::Value> pValue = v8::Number::New(m_pIsolate, FXJSE_ftod(fFloat)); |
| 252 | m_hValue.Reset(m_pIsolate, pValue); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 253 | } |
| 254 | |
tsepez | 29adee7 | 2016-05-31 14:22:09 -0700 | [diff] [blame] | 255 | void CFXJSE_Value::SetHostObject(CFXJSE_HostObject* lpObject, |
| 256 | CFXJSE_Class* lpClass) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 257 | CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); |
| 258 | ASSERT(lpClass); |
| 259 | v8::Local<v8::FunctionTemplate> hClass = |
| 260 | v8::Local<v8::FunctionTemplate>::New(m_pIsolate, lpClass->m_hTemplate); |
| 261 | v8::Local<v8::Object> hObject = hClass->InstanceTemplate()->NewInstance(); |
| 262 | FXJSE_UpdateObjectBinding(hObject, lpObject); |
| 263 | m_hValue.Reset(m_pIsolate, hObject); |
| 264 | } |
| 265 | |
| 266 | void CFXJSE_Value::SetArray(uint32_t uValueCount, CFXJSE_Value** rgValues) { |
| 267 | CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); |
| 268 | v8::Local<v8::Array> hArrayObject = v8::Array::New(m_pIsolate, uValueCount); |
| 269 | if (rgValues) { |
| 270 | for (uint32_t i = 0; i < uValueCount; i++) { |
| 271 | if (rgValues[i]) { |
| 272 | hArrayObject->Set(i, v8::Local<v8::Value>::New( |
| 273 | m_pIsolate, rgValues[i]->DirectGetValue())); |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | m_hValue.Reset(m_pIsolate, hArrayObject); |
| 278 | } |
| 279 | |
dsinclair | 04fd27b | 2016-03-30 12:59:04 -0700 | [diff] [blame] | 280 | void CFXJSE_Value::SetDate(double dDouble) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 281 | CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); |
| 282 | v8::Local<v8::Value> hDate = v8::Date::New(m_pIsolate, dDouble); |
| 283 | m_hValue.Reset(m_pIsolate, hDate); |
| 284 | } |
| 285 | |
| 286 | FX_BOOL CFXJSE_Value::SetObjectProperty(const CFX_ByteStringC& szPropName, |
| 287 | CFXJSE_Value* lpPropValue) { |
| 288 | CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); |
| 289 | v8::Local<v8::Value> hObject = |
| 290 | v8::Local<v8::Value>::New(m_pIsolate, m_hValue); |
| 291 | if (!hObject->IsObject()) |
| 292 | return FALSE; |
| 293 | |
| 294 | v8::Local<v8::Value> hPropValue = |
| 295 | v8::Local<v8::Value>::New(m_pIsolate, lpPropValue->DirectGetValue()); |
| 296 | return (FX_BOOL)hObject.As<v8::Object>()->Set( |
dsinclair | 179bebb | 2016-04-05 11:02:18 -0700 | [diff] [blame] | 297 | v8::String::NewFromUtf8(m_pIsolate, szPropName.c_str(), |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 298 | v8::String::kNormalString, |
| 299 | szPropName.GetLength()), |
| 300 | hPropValue); |
| 301 | } |
| 302 | |
| 303 | FX_BOOL CFXJSE_Value::GetObjectProperty(const CFX_ByteStringC& szPropName, |
| 304 | CFXJSE_Value* lpPropValue) { |
| 305 | CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); |
| 306 | v8::Local<v8::Value> hObject = |
| 307 | v8::Local<v8::Value>::New(m_pIsolate, m_hValue); |
| 308 | if (!hObject->IsObject()) |
| 309 | return FALSE; |
| 310 | |
| 311 | v8::Local<v8::Value> hPropValue = |
| 312 | hObject.As<v8::Object>()->Get(v8::String::NewFromUtf8( |
dsinclair | 179bebb | 2016-04-05 11:02:18 -0700 | [diff] [blame] | 313 | m_pIsolate, szPropName.c_str(), v8::String::kNormalString, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 314 | szPropName.GetLength())); |
| 315 | lpPropValue->ForceSetValue(hPropValue); |
| 316 | return TRUE; |
| 317 | } |
| 318 | |
| 319 | FX_BOOL CFXJSE_Value::SetObjectProperty(uint32_t uPropIdx, |
| 320 | CFXJSE_Value* lpPropValue) { |
| 321 | CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); |
| 322 | v8::Local<v8::Value> hObject = |
| 323 | v8::Local<v8::Value>::New(m_pIsolate, m_hValue); |
| 324 | if (!hObject->IsObject()) |
| 325 | return FALSE; |
| 326 | |
| 327 | v8::Local<v8::Value> hPropValue = |
| 328 | v8::Local<v8::Value>::New(m_pIsolate, lpPropValue->DirectGetValue()); |
| 329 | return (FX_BOOL)hObject.As<v8::Object>()->Set(uPropIdx, hPropValue); |
| 330 | } |
| 331 | |
| 332 | FX_BOOL CFXJSE_Value::GetObjectProperty(uint32_t uPropIdx, |
| 333 | CFXJSE_Value* lpPropValue) { |
| 334 | CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); |
| 335 | v8::Local<v8::Value> hObject = |
| 336 | v8::Local<v8::Value>::New(m_pIsolate, m_hValue); |
| 337 | if (!hObject->IsObject()) |
| 338 | return FALSE; |
| 339 | |
| 340 | v8::Local<v8::Value> hPropValue = hObject.As<v8::Object>()->Get(uPropIdx); |
| 341 | lpPropValue->ForceSetValue(hPropValue); |
| 342 | return TRUE; |
| 343 | } |
| 344 | |
| 345 | FX_BOOL CFXJSE_Value::DeleteObjectProperty(const CFX_ByteStringC& szPropName) { |
| 346 | CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); |
| 347 | v8::Local<v8::Value> hObject = |
| 348 | v8::Local<v8::Value>::New(m_pIsolate, m_hValue); |
| 349 | if (!hObject->IsObject()) |
| 350 | return FALSE; |
| 351 | |
| 352 | hObject.As<v8::Object>()->Delete(v8::String::NewFromUtf8( |
dsinclair | 179bebb | 2016-04-05 11:02:18 -0700 | [diff] [blame] | 353 | m_pIsolate, szPropName.c_str(), v8::String::kNormalString, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 354 | szPropName.GetLength())); |
| 355 | return TRUE; |
| 356 | } |
| 357 | |
| 358 | FX_BOOL CFXJSE_Value::HasObjectOwnProperty(const CFX_ByteStringC& szPropName, |
| 359 | FX_BOOL bUseTypeGetter) { |
| 360 | CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); |
| 361 | v8::Local<v8::Value> hObject = |
| 362 | v8::Local<v8::Value>::New(m_pIsolate, m_hValue); |
| 363 | if (!hObject->IsObject()) |
| 364 | return FALSE; |
| 365 | |
| 366 | v8::Local<v8::String> hKey = v8::String::NewFromUtf8( |
dsinclair | 179bebb | 2016-04-05 11:02:18 -0700 | [diff] [blame] | 367 | m_pIsolate, szPropName.c_str(), v8::String::kNormalString, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 368 | szPropName.GetLength()); |
| 369 | return hObject.As<v8::Object>()->HasRealNamedProperty(hKey) || |
| 370 | (bUseTypeGetter && |
| 371 | hObject.As<v8::Object>() |
| 372 | ->HasOwnProperty(m_pIsolate->GetCurrentContext(), hKey) |
| 373 | .FromMaybe(false)); |
| 374 | } |
| 375 | |
| 376 | FX_BOOL CFXJSE_Value::SetObjectOwnProperty(const CFX_ByteStringC& szPropName, |
| 377 | CFXJSE_Value* lpPropValue) { |
| 378 | CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); |
| 379 | v8::Local<v8::Value> hObject = |
| 380 | v8::Local<v8::Value>::New(m_pIsolate, m_hValue); |
| 381 | if (!hObject->IsObject()) |
| 382 | return FALSE; |
| 383 | |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 384 | v8::Local<v8::Value> pValue = |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 385 | v8::Local<v8::Value>::New(m_pIsolate, lpPropValue->m_hValue); |
| 386 | return hObject.As<v8::Object>() |
| 387 | ->DefineOwnProperty( |
| 388 | m_pIsolate->GetCurrentContext(), |
dsinclair | 179bebb | 2016-04-05 11:02:18 -0700 | [diff] [blame] | 389 | v8::String::NewFromUtf8(m_pIsolate, szPropName.c_str(), |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 390 | v8::String::kNormalString, |
| 391 | szPropName.GetLength()), |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 392 | pValue) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 393 | .FromMaybe(false); |
| 394 | } |
| 395 | |
| 396 | FX_BOOL CFXJSE_Value::SetFunctionBind(CFXJSE_Value* lpOldFunction, |
| 397 | CFXJSE_Value* lpNewThis) { |
| 398 | CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); |
| 399 | v8::Local<v8::Value> rgArgs[2]; |
| 400 | v8::Local<v8::Value> hOldFunction = |
| 401 | v8::Local<v8::Value>::New(m_pIsolate, lpOldFunction->DirectGetValue()); |
| 402 | if (hOldFunction.IsEmpty() || !hOldFunction->IsFunction()) |
| 403 | return FALSE; |
| 404 | |
| 405 | rgArgs[0] = hOldFunction; |
| 406 | v8::Local<v8::Value> hNewThis = |
| 407 | v8::Local<v8::Value>::New(m_pIsolate, lpNewThis->DirectGetValue()); |
| 408 | if (hNewThis.IsEmpty()) |
| 409 | return FALSE; |
| 410 | |
| 411 | rgArgs[1] = hNewThis; |
| 412 | v8::Local<v8::String> hBinderFuncSource = |
| 413 | v8::String::NewFromUtf8(m_pIsolate, |
| 414 | "(function (oldfunction, newthis) { return " |
| 415 | "oldfunction.bind(newthis); })"); |
| 416 | v8::Local<v8::Function> hBinderFunc = |
| 417 | v8::Script::Compile(hBinderFuncSource)->Run().As<v8::Function>(); |
| 418 | v8::Local<v8::Value> hBoundFunction = |
| 419 | hBinderFunc->Call(m_pIsolate->GetCurrentContext()->Global(), 2, rgArgs); |
| 420 | if (hBoundFunction.IsEmpty() || !hBoundFunction->IsFunction()) |
| 421 | return FALSE; |
| 422 | |
| 423 | m_hValue.Reset(m_pIsolate, hBoundFunction); |
| 424 | return TRUE; |
| 425 | } |
| 426 | |
| 427 | #define FXJSE_INVALID_PTR ((void*)(intptr_t)-1) |
| 428 | FX_BOOL CFXJSE_Value::Call(CFXJSE_Value* lpReceiver, |
| 429 | CFXJSE_Value* lpRetValue, |
| 430 | uint32_t nArgCount, |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 431 | CFXJSE_Value** lpArgs) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 432 | CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); |
| 433 | v8::Local<v8::Value> hFunctionValue = |
| 434 | v8::Local<v8::Value>::New(m_pIsolate, DirectGetValue()); |
| 435 | v8::Local<v8::Object> hFunctionObject = |
| 436 | !hFunctionValue.IsEmpty() && hFunctionValue->IsObject() |
| 437 | ? hFunctionValue.As<v8::Object>() |
| 438 | : v8::Local<v8::Object>(); |
| 439 | |
| 440 | v8::TryCatch trycatch(m_pIsolate); |
| 441 | if (hFunctionObject.IsEmpty() || !hFunctionObject->IsCallable()) { |
| 442 | if (lpRetValue) |
| 443 | lpRetValue->ForceSetValue(FXJSE_CreateReturnValue(m_pIsolate, trycatch)); |
| 444 | return FALSE; |
| 445 | } |
| 446 | |
| 447 | v8::Local<v8::Value> hReturnValue; |
| 448 | v8::Local<v8::Value>* lpLocalArgs = NULL; |
| 449 | if (nArgCount) { |
| 450 | lpLocalArgs = FX_Alloc(v8::Local<v8::Value>, nArgCount); |
| 451 | for (uint32_t i = 0; i < nArgCount; i++) { |
| 452 | new (lpLocalArgs + i) v8::Local<v8::Value>; |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 453 | CFXJSE_Value* lpArg = lpArgs[i]; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 454 | if (lpArg) { |
| 455 | lpLocalArgs[i] = |
| 456 | v8::Local<v8::Value>::New(m_pIsolate, lpArg->DirectGetValue()); |
| 457 | } |
| 458 | if (lpLocalArgs[i].IsEmpty()) { |
| 459 | lpLocalArgs[i] = v8::Undefined(m_pIsolate); |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | FX_BOOL bRetValue = TRUE; |
| 465 | if (lpReceiver == FXJSE_INVALID_PTR) { |
| 466 | v8::MaybeLocal<v8::Value> maybe_retvalue = |
| 467 | hFunctionObject->CallAsConstructor(m_pIsolate->GetCurrentContext(), |
| 468 | nArgCount, lpLocalArgs); |
| 469 | hReturnValue = maybe_retvalue.FromMaybe(v8::Local<v8::Value>()); |
| 470 | } else { |
| 471 | v8::Local<v8::Value> hReceiver; |
| 472 | if (lpReceiver) { |
| 473 | hReceiver = |
| 474 | v8::Local<v8::Value>::New(m_pIsolate, lpReceiver->DirectGetValue()); |
| 475 | } |
| 476 | if (hReceiver.IsEmpty() || !hReceiver->IsObject()) |
| 477 | hReceiver = v8::Object::New(m_pIsolate); |
| 478 | |
| 479 | v8::MaybeLocal<v8::Value> maybe_retvalue = hFunctionObject->CallAsFunction( |
| 480 | m_pIsolate->GetCurrentContext(), hReceiver, nArgCount, lpLocalArgs); |
| 481 | hReturnValue = maybe_retvalue.FromMaybe(v8::Local<v8::Value>()); |
| 482 | } |
| 483 | |
| 484 | if (trycatch.HasCaught()) { |
| 485 | hReturnValue = FXJSE_CreateReturnValue(m_pIsolate, trycatch); |
| 486 | bRetValue = FALSE; |
| 487 | } |
| 488 | |
| 489 | if (lpRetValue) |
| 490 | lpRetValue->ForceSetValue(hReturnValue); |
| 491 | |
| 492 | if (lpLocalArgs) { |
| 493 | for (uint32_t i = 0; i < nArgCount; i++) |
| 494 | lpLocalArgs[i].~Local(); |
| 495 | FX_Free(lpLocalArgs); |
| 496 | } |
| 497 | return bRetValue; |
| 498 | } |