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/. */
7 #ifndef mozilla_dom_ProxyHandlerUtils_h
8 #define mozilla_dom_ProxyHandlerUtils_h
10 #include "mozilla/Likely.h"
11 #include "mozilla/Maybe.h"
12 #include "mozilla/TextUtils.h"
15 #include "js/Object.h" // JS::GetClass
16 #include "js/PropertyDescriptor.h"
17 #include "js/String.h" // JS::AtomToLinearString, JS::GetLinearString{CharAt,Length}
18 #include "js/TypeDecls.h"
20 #include "jsfriendapi.h" // js::StringIsArrayIndex
22 namespace mozilla::dom
{
24 extern jsid s_length_id
;
26 // A return value of UINT32_MAX indicates "not an array index". Note, in
27 // particular, that UINT32_MAX itself is not a valid array index in general.
28 inline uint32_t GetArrayIndexFromId(JS::Handle
<jsid
> id
) {
29 // Much like js::IdIsIndex, except with a fast path for "length" and another
30 // fast path for starting with a lowercase ascii char. Is that second one
31 // really needed? I guess it is because StringIsArrayIndex is out of line...
32 // as of now, use id.get() instead of id otherwise operands mismatch error
34 if (MOZ_LIKELY(id
.isInt())) {
37 if (MOZ_LIKELY(id
.get() == s_length_id
)) {
40 if (MOZ_UNLIKELY(!id
.isAtom())) {
44 JSLinearString
* str
= JS::AtomToLinearString(id
.toAtom());
45 if (MOZ_UNLIKELY(JS::GetLinearStringLength(str
) == 0)) {
49 char16_t firstChar
= JS::GetLinearStringCharAt(str
, 0);
50 if (MOZ_LIKELY(IsAsciiLowercaseAlpha(firstChar
))) {
55 return js::StringIsArrayIndex(str
, &i
) ? i
: UINT32_MAX
;
58 inline bool IsArrayIndex(uint32_t index
) { return index
< UINT32_MAX
; }
60 } // namespace mozilla::dom
62 #endif /* mozilla_dom_ProxyHandlerUtils_h */