ProxyHandlerUtils.h (2021B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_dom_ProxyHandlerUtils_h 8 #define mozilla_dom_ProxyHandlerUtils_h 9 10 #include "js/Id.h" 11 #include "js/Object.h" // JS::GetClass 12 #include "js/PropertyDescriptor.h" 13 #include "js/String.h" // JS::AtomToLinearString, JS::GetLinearString{CharAt,Length} 14 #include "js/TypeDecls.h" 15 #include "jsfriendapi.h" // js::StringIsArrayIndex 16 #include "mozilla/Likely.h" 17 #include "mozilla/TextUtils.h" 18 19 namespace mozilla::dom { 20 21 extern jsid s_length_id; 22 23 // A return value of UINT32_MAX indicates "not an array index". Note, in 24 // particular, that UINT32_MAX itself is not a valid array index in general. 25 inline uint32_t GetArrayIndexFromId(JS::Handle<jsid> id) { 26 // Much like js::IdIsIndex, except with a fast path for "length" and another 27 // fast path for starting with a lowercase ascii char. Is that second one 28 // really needed? I guess it is because StringIsArrayIndex is out of line... 29 // as of now, use id.get() instead of id otherwise operands mismatch error 30 // occurs. 31 if (MOZ_LIKELY(id.isInt())) { 32 return id.toInt(); 33 } 34 if (MOZ_LIKELY(id.get() == s_length_id)) { 35 return UINT32_MAX; 36 } 37 if (MOZ_UNLIKELY(!id.isAtom())) { 38 return UINT32_MAX; 39 } 40 41 JSLinearString* str = JS::AtomToLinearString(id.toAtom()); 42 if (MOZ_UNLIKELY(JS::GetLinearStringLength(str) == 0)) { 43 return UINT32_MAX; 44 } 45 46 char16_t firstChar = JS::GetLinearStringCharAt(str, 0); 47 if (MOZ_LIKELY(IsAsciiLowercaseAlpha(firstChar))) { 48 return UINT32_MAX; 49 } 50 51 uint32_t i; 52 return js::StringIsArrayIndex(str, &i) ? i : UINT32_MAX; 53 } 54 55 inline bool IsArrayIndex(uint32_t index) { return index < UINT32_MAX; } 56 57 } // namespace mozilla::dom 58 59 #endif /* mozilla_dom_ProxyHandlerUtils_h */