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