2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2018-11-30 18:39:55 +03:00
|
|
|
* vim: set ts=8 sts=2 et sw=2 tw=80:
|
2013-08-22 09:26:56 +04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef js_Id_h
|
|
|
|
#define js_Id_h
|
|
|
|
|
2019-11-15 19:49:08 +03:00
|
|
|
// [SMDOC] Property Key / JSID
|
|
|
|
//
|
2013-08-28 06:59:14 +04:00
|
|
|
// A jsid is an identifier for a property or method of an object which is
|
2015-03-13 02:09:21 +03:00
|
|
|
// either a 31-bit unsigned integer, interned string or symbol.
|
2013-08-28 06:59:14 +04:00
|
|
|
//
|
2013-12-17 06:27:43 +04:00
|
|
|
// Also, there is an additional jsid value, JSID_VOID, which does not occur in
|
2013-08-28 06:59:14 +04:00
|
|
|
// JS scripts but may be used to indicate the absence of a valid jsid. A void
|
|
|
|
// jsid is not a valid id and only arises as an exceptional API return value,
|
2013-12-17 06:27:43 +04:00
|
|
|
// such as in JS_NextProperty. Embeddings must not pass JSID_VOID into JSAPI
|
|
|
|
// entry points expecting a jsid and do not need to handle JSID_VOID in hooks
|
2013-08-28 06:59:14 +04:00
|
|
|
// receiving a jsid except when explicitly noted in the API contract.
|
|
|
|
//
|
2015-03-13 02:09:21 +03:00
|
|
|
// A jsid is not implicitly convertible to or from a Value; JS_ValueToId or
|
2013-08-28 06:59:14 +04:00
|
|
|
// JS_IdToValue must be used instead.
|
|
|
|
|
2019-02-09 11:26:27 +03:00
|
|
|
#include "mozilla/Maybe.h"
|
|
|
|
|
2013-08-22 09:26:56 +04:00
|
|
|
#include "jstypes.h"
|
|
|
|
|
2021-03-12 13:42:21 +03:00
|
|
|
#include "js/GCAnnotations.h"
|
2014-02-21 01:38:57 +04:00
|
|
|
#include "js/HeapAPI.h"
|
2013-09-12 03:51:17 +04:00
|
|
|
#include "js/RootingAPI.h"
|
2021-03-12 13:42:21 +03:00
|
|
|
#include "js/TraceKind.h"
|
|
|
|
#include "js/TracingAPI.h"
|
2013-08-28 06:59:14 +04:00
|
|
|
#include "js/TypeDecls.h"
|
2013-08-22 09:26:56 +04:00
|
|
|
|
2018-05-30 14:51:58 +03:00
|
|
|
// All jsids with the low bit set are integer ids. This means the other type
|
|
|
|
// tags must all be even.
|
|
|
|
#define JSID_TYPE_INT_BIT 0x1
|
|
|
|
|
|
|
|
// Use 0 for JSID_TYPE_STRING to avoid a bitwise op for atom <-> id conversions.
|
2018-05-11 13:01:32 +03:00
|
|
|
#define JSID_TYPE_STRING 0x0
|
|
|
|
#define JSID_TYPE_VOID 0x2
|
|
|
|
#define JSID_TYPE_SYMBOL 0x4
|
2021-06-18 15:02:16 +03:00
|
|
|
// (0x6 is unused)
|
2018-05-11 13:01:32 +03:00
|
|
|
#define JSID_TYPE_MASK 0x7
|
|
|
|
|
2018-11-20 15:01:42 +03:00
|
|
|
namespace JS {
|
2018-05-11 13:01:32 +03:00
|
|
|
|
2020-04-26 20:01:57 +03:00
|
|
|
enum class SymbolCode : uint32_t;
|
|
|
|
|
2018-11-20 15:01:42 +03:00
|
|
|
struct PropertyKey {
|
|
|
|
size_t asBits;
|
2018-05-11 13:01:32 +03:00
|
|
|
|
2018-11-20 15:01:42 +03:00
|
|
|
constexpr PropertyKey() : asBits(JSID_TYPE_VOID) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-11-20 15:01:42 +03:00
|
|
|
static constexpr MOZ_ALWAYS_INLINE PropertyKey fromRawBits(size_t bits) {
|
|
|
|
PropertyKey id;
|
2018-05-11 13:01:32 +03:00
|
|
|
id.asBits = bits;
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2018-11-20 15:01:42 +03:00
|
|
|
bool operator==(const PropertyKey& rhs) const { return asBits == rhs.asBits; }
|
|
|
|
bool operator!=(const PropertyKey& rhs) const { return asBits != rhs.asBits; }
|
2018-12-22 17:17:44 +03:00
|
|
|
|
2021-06-17 19:51:42 +03:00
|
|
|
MOZ_ALWAYS_INLINE bool isVoid() const {
|
|
|
|
MOZ_ASSERT_IF((asBits & JSID_TYPE_MASK) == JSID_TYPE_VOID,
|
|
|
|
asBits == JSID_TYPE_VOID);
|
|
|
|
return asBits == JSID_TYPE_VOID;
|
|
|
|
}
|
|
|
|
|
2018-12-22 17:17:44 +03:00
|
|
|
MOZ_ALWAYS_INLINE bool isInt() const {
|
|
|
|
return !!(asBits & JSID_TYPE_INT_BIT);
|
|
|
|
}
|
2018-12-28 18:48:06 +03:00
|
|
|
|
2020-04-26 20:01:39 +03:00
|
|
|
MOZ_ALWAYS_INLINE bool isString() const {
|
|
|
|
return (asBits & JSID_TYPE_MASK) == JSID_TYPE_STRING;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ALWAYS_INLINE bool isSymbol() const {
|
|
|
|
return (asBits & JSID_TYPE_MASK) == JSID_TYPE_SYMBOL;
|
|
|
|
}
|
|
|
|
|
2020-04-26 20:03:01 +03:00
|
|
|
MOZ_ALWAYS_INLINE bool isGCThing() const { return isString() || isSymbol(); }
|
|
|
|
|
2020-04-26 20:01:39 +03:00
|
|
|
MOZ_ALWAYS_INLINE int32_t toInt() const {
|
|
|
|
MOZ_ASSERT(isInt());
|
|
|
|
uint32_t bits = static_cast<uint32_t>(asBits) >> 1;
|
|
|
|
return static_cast<int32_t>(bits);
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ALWAYS_INLINE JSString* toString() const {
|
|
|
|
MOZ_ASSERT(isString());
|
|
|
|
// Use XOR instead of `& ~JSID_TYPE_MASK` because small immediates can be
|
|
|
|
// encoded more efficiently on some platorms.
|
|
|
|
return reinterpret_cast<JSString*>(asBits ^ JSID_TYPE_STRING);
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ALWAYS_INLINE JS::Symbol* toSymbol() const {
|
|
|
|
MOZ_ASSERT(isSymbol());
|
|
|
|
return reinterpret_cast<JS::Symbol*>(asBits ^ JSID_TYPE_SYMBOL);
|
|
|
|
}
|
|
|
|
|
2020-09-26 13:40:48 +03:00
|
|
|
js::gc::Cell* toGCThing() const {
|
|
|
|
MOZ_ASSERT(isGCThing());
|
|
|
|
return reinterpret_cast<js::gc::Cell*>(asBits & ~(size_t)JSID_TYPE_MASK);
|
|
|
|
}
|
|
|
|
|
2020-04-26 20:02:30 +03:00
|
|
|
GCCellPtr toGCCellPtr() const {
|
2020-09-26 13:40:48 +03:00
|
|
|
js::gc::Cell* thing = toGCThing();
|
2020-04-26 20:02:30 +03:00
|
|
|
if (isString()) {
|
|
|
|
return JS::GCCellPtr(thing, JS::TraceKind::String);
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(isSymbol());
|
|
|
|
return JS::GCCellPtr(thing, JS::TraceKind::Symbol);
|
|
|
|
}
|
|
|
|
|
2020-08-27 22:51:32 +03:00
|
|
|
bool isPrivateName() const;
|
|
|
|
|
2020-04-26 20:01:57 +03:00
|
|
|
bool isWellKnownSymbol(JS::SymbolCode code) const;
|
|
|
|
|
2020-04-30 21:18:12 +03:00
|
|
|
// This API can be used by embedders to convert pinned (aka interned) strings,
|
2021-08-06 11:44:14 +03:00
|
|
|
// as created by JS_AtomizeAndPinString, into PropertyKeys. This means the
|
|
|
|
// string does not have to be explicitly rooted.
|
2020-04-30 21:18:12 +03:00
|
|
|
//
|
|
|
|
// Only use this API when absolutely necessary, otherwise use JS_StringToId.
|
|
|
|
static PropertyKey fromPinnedString(JSString* str);
|
|
|
|
|
2020-04-30 21:18:35 +03:00
|
|
|
// Must not be used on atoms that are representable as integer PropertyKey.
|
|
|
|
// Prefer NameToId or AtomToId over this function:
|
|
|
|
//
|
|
|
|
// A PropertyName is an atom that does not contain an integer in the range
|
|
|
|
// [0, UINT32_MAX]. However, PropertyKey can only hold an integer in the range
|
|
|
|
// [0, JSID_INT_MAX] (where JSID_INT_MAX == 2^31-1). Thus, for the range of
|
|
|
|
// integers (JSID_INT_MAX, UINT32_MAX], to represent as a 'id', it must be
|
|
|
|
// the case id.isString() and id.toString()->isIndex(). In most
|
|
|
|
// cases when creating a PropertyKey, code does not have to care about
|
|
|
|
// this corner case because:
|
|
|
|
//
|
|
|
|
// - When given an arbitrary JSAtom*, AtomToId must be used, which checks for
|
|
|
|
// integer atoms representable as integer PropertyKey, and does this
|
|
|
|
// conversion.
|
|
|
|
//
|
|
|
|
// - When given a PropertyName*, NameToId can be used which does not need
|
|
|
|
// to do any dynamic checks.
|
|
|
|
//
|
|
|
|
// Thus, it is only the rare third case which needs this function, which
|
|
|
|
// handles any JSAtom* that is known not to be representable with an int
|
|
|
|
// PropertyKey.
|
|
|
|
static PropertyKey fromNonIntAtom(JSAtom* atom) {
|
|
|
|
MOZ_ASSERT((size_t(atom) & JSID_TYPE_MASK) == 0);
|
|
|
|
MOZ_ASSERT(PropertyKey::isNonIntAtom(atom));
|
|
|
|
return PropertyKey::fromRawBits(size_t(atom) | JSID_TYPE_STRING);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The JSAtom/JSString type exposed to embedders is opaque.
|
|
|
|
static PropertyKey fromNonIntAtom(JSString* str) {
|
|
|
|
MOZ_ASSERT((size_t(str) & JSID_TYPE_MASK) == 0);
|
|
|
|
MOZ_ASSERT(PropertyKey::isNonIntAtom(str));
|
|
|
|
return PropertyKey::fromRawBits(size_t(str) | JSID_TYPE_STRING);
|
|
|
|
}
|
|
|
|
|
2021-03-03 13:12:55 +03:00
|
|
|
// Internal API!
|
|
|
|
// All string PropertyKeys are actually atomized.
|
|
|
|
MOZ_ALWAYS_INLINE bool isAtom() const { return isString(); }
|
|
|
|
|
|
|
|
MOZ_ALWAYS_INLINE bool isAtom(JSAtom* atom) const {
|
|
|
|
MOZ_ASSERT(PropertyKey::isNonIntAtom(atom));
|
|
|
|
return isAtom() && toAtom() == atom;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ALWAYS_INLINE JSAtom* toAtom() const { return (JSAtom*)toString(); }
|
|
|
|
|
2020-04-30 21:18:35 +03:00
|
|
|
private:
|
|
|
|
static bool isNonIntAtom(JSAtom* atom);
|
|
|
|
static bool isNonIntAtom(JSString* atom);
|
2015-09-29 23:39:33 +03:00
|
|
|
} JS_HAZ_GC_POINTER;
|
2018-11-20 15:01:42 +03:00
|
|
|
|
|
|
|
} // namespace JS
|
|
|
|
|
|
|
|
using jsid = JS::PropertyKey;
|
|
|
|
|
2013-12-11 03:45:41 +04:00
|
|
|
#define JSID_BITS(id) (id.asBits)
|
2013-08-22 09:26:56 +04:00
|
|
|
|
2020-04-26 20:01:39 +03:00
|
|
|
static MOZ_ALWAYS_INLINE bool JSID_IS_STRING(jsid id) { return id.isString(); }
|
2013-08-22 09:26:56 +04:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
static MOZ_ALWAYS_INLINE JSString* JSID_TO_STRING(jsid id) {
|
2020-04-26 20:01:39 +03:00
|
|
|
return id.toString();
|
2013-08-22 09:26:56 +04:00
|
|
|
}
|
|
|
|
|
2020-04-26 20:01:39 +03:00
|
|
|
static MOZ_ALWAYS_INLINE bool JSID_IS_INT(jsid id) { return id.isInt(); }
|
2013-08-22 09:26:56 +04:00
|
|
|
|
2020-04-26 20:01:39 +03:00
|
|
|
static MOZ_ALWAYS_INLINE int32_t JSID_TO_INT(jsid id) { return id.toInt(); }
|
2013-08-22 09:26:56 +04:00
|
|
|
|
|
|
|
#define JSID_INT_MIN 0
|
|
|
|
#define JSID_INT_MAX INT32_MAX
|
|
|
|
|
2014-01-25 08:14:56 +04:00
|
|
|
static MOZ_ALWAYS_INLINE bool INT_FITS_IN_JSID(int32_t i) { return i >= 0; }
|
2013-08-22 09:26:56 +04:00
|
|
|
|
2014-01-25 08:14:56 +04:00
|
|
|
static MOZ_ALWAYS_INLINE jsid INT_TO_JSID(int32_t i) {
|
2013-08-22 09:26:56 +04:00
|
|
|
jsid id;
|
2014-02-18 10:24:15 +04:00
|
|
|
MOZ_ASSERT(INT_FITS_IN_JSID(i));
|
2018-05-30 14:51:58 +03:00
|
|
|
uint32_t bits = (static_cast<uint32_t>(i) << 1) | JSID_TYPE_INT_BIT;
|
2016-12-01 11:23:21 +03:00
|
|
|
JSID_BITS(id) = static_cast<size_t>(bits);
|
2013-08-22 09:26:56 +04:00
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
static MOZ_ALWAYS_INLINE jsid SYMBOL_TO_JSID(JS::Symbol* sym) {
|
2013-08-22 09:26:56 +04:00
|
|
|
jsid id;
|
2014-06-23 19:56:52 +04:00
|
|
|
MOZ_ASSERT(sym != nullptr);
|
|
|
|
MOZ_ASSERT((size_t(sym) & JSID_TYPE_MASK) == 0);
|
2015-03-29 01:22:11 +03:00
|
|
|
MOZ_ASSERT(!js::gc::IsInsideNursery(reinterpret_cast<js::gc::Cell*>(sym)));
|
2014-06-23 19:56:52 +04:00
|
|
|
JSID_BITS(id) = (size_t(sym) | JSID_TYPE_SYMBOL);
|
2013-08-22 09:26:56 +04:00
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2014-01-25 08:14:56 +04:00
|
|
|
static MOZ_ALWAYS_INLINE bool JSID_IS_VOID(const jsid id) {
|
2021-06-17 19:51:42 +03:00
|
|
|
return id.isVoid();
|
2013-08-22 09:26:56 +04:00
|
|
|
}
|
|
|
|
|
2018-05-11 13:01:32 +03:00
|
|
|
constexpr const jsid JSID_VOID;
|
2013-12-17 06:27:43 +04:00
|
|
|
|
2018-11-19 20:02:47 +03:00
|
|
|
extern JS_PUBLIC_DATA const JS::HandleId JSID_VOIDHANDLE;
|
2013-09-06 03:08:57 +04:00
|
|
|
|
2016-04-26 19:18:48 +03:00
|
|
|
namespace JS {
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct GCPolicy<jsid> {
|
|
|
|
static void trace(JSTracer* trc, jsid* idp, const char* name) {
|
2019-10-09 13:30:02 +03:00
|
|
|
// It's not safe to trace unbarriered pointers except as part of root
|
|
|
|
// marking.
|
|
|
|
UnsafeTraceRoot(trc, idp, name);
|
2016-04-26 19:18:48 +03:00
|
|
|
}
|
2017-09-19 14:31:31 +03:00
|
|
|
static bool isValid(jsid id) {
|
2020-04-26 20:03:01 +03:00
|
|
|
return !id.isGCThing() ||
|
2020-04-26 20:02:30 +03:00
|
|
|
js::gc::IsCellPointerValid(id.toGCCellPtr().asCell());
|
2017-09-19 14:31:31 +03:00
|
|
|
}
|
2020-09-02 10:35:15 +03:00
|
|
|
|
|
|
|
static bool isTenured(jsid id) {
|
|
|
|
MOZ_ASSERT_IF(id.isGCThing(),
|
|
|
|
!js::gc::IsInsideNursery(id.toGCCellPtr().asCell()));
|
|
|
|
return true;
|
|
|
|
}
|
2016-04-26 19:18:48 +03:00
|
|
|
};
|
|
|
|
|
2018-02-27 16:01:49 +03:00
|
|
|
#ifdef DEBUG
|
2018-12-07 00:28:10 +03:00
|
|
|
MOZ_ALWAYS_INLINE void AssertIdIsNotGray(jsid id) {
|
2020-04-26 20:03:01 +03:00
|
|
|
if (id.isGCThing()) {
|
2020-04-26 20:02:30 +03:00
|
|
|
AssertCellIsNotGray(id.toGCCellPtr().asCell());
|
2018-09-06 13:11:07 +03:00
|
|
|
}
|
2018-02-27 16:01:49 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-04-26 19:18:48 +03:00
|
|
|
} // namespace JS
|
|
|
|
|
2013-09-12 03:51:17 +04:00
|
|
|
namespace js {
|
|
|
|
|
2015-12-28 21:11:40 +03:00
|
|
|
template <>
|
|
|
|
struct BarrierMethods<jsid> {
|
2017-07-11 18:51:37 +03:00
|
|
|
static gc::Cell* asGCThingOrNull(jsid id) {
|
2021-06-21 23:56:10 +03:00
|
|
|
if (id.isGCThing()) {
|
|
|
|
return id.toGCThing();
|
2017-07-11 18:51:37 +03:00
|
|
|
}
|
|
|
|
return nullptr;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2021-09-23 16:09:28 +03:00
|
|
|
static void postWriteBarrier(jsid* idp, jsid prev, jsid next) {
|
|
|
|
MOZ_ASSERT_IF(JSID_IS_STRING(next),
|
|
|
|
!gc::IsInsideNursery(JSID_TO_STRING(next)));
|
2019-03-27 19:26:09 +03:00
|
|
|
}
|
2016-02-07 20:08:55 +03:00
|
|
|
static void exposeToJS(jsid id) {
|
2020-04-26 20:03:01 +03:00
|
|
|
if (id.isGCThing()) {
|
2020-04-26 20:02:30 +03:00
|
|
|
js::gc::ExposeGCThingToActiveJS(id.toGCCellPtr());
|
2016-02-07 20:08:55 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2013-09-12 03:51:17 +04:00
|
|
|
};
|
|
|
|
|
2019-02-09 11:26:27 +03:00
|
|
|
// If the jsid is a GC pointer type, convert to that type and call |f| with the
|
|
|
|
// pointer and return the result wrapped in a Maybe, otherwise return None().
|
|
|
|
template <typename F>
|
|
|
|
auto MapGCThingTyped(const jsid& id, F&& f) {
|
2021-06-21 23:56:10 +03:00
|
|
|
if (id.isString()) {
|
|
|
|
return mozilla::Some(f(id.toString()));
|
2018-09-06 13:11:07 +03:00
|
|
|
}
|
2021-06-21 23:56:10 +03:00
|
|
|
if (id.isSymbol()) {
|
|
|
|
return mozilla::Some(f(id.toSymbol()));
|
2018-09-06 13:11:07 +03:00
|
|
|
}
|
2020-04-26 20:03:01 +03:00
|
|
|
MOZ_ASSERT(!id.isGCThing());
|
2019-02-09 11:26:27 +03:00
|
|
|
using ReturnType = decltype(f(static_cast<JSString*>(nullptr)));
|
|
|
|
return mozilla::Maybe<ReturnType>();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the jsid is a GC pointer type, convert to that type and call |f| with the
|
|
|
|
// pointer. Return whether this happened.
|
|
|
|
template <typename F>
|
|
|
|
bool ApplyGCThingTyped(const jsid& id, F&& f) {
|
|
|
|
return MapGCThingTyped(id,
|
|
|
|
[&f](auto t) {
|
|
|
|
f(t);
|
|
|
|
return true;
|
|
|
|
})
|
|
|
|
.isSome();
|
2015-04-23 20:42:31 +03:00
|
|
|
}
|
|
|
|
|
2020-04-26 20:01:39 +03:00
|
|
|
template <typename Wrapper>
|
|
|
|
class WrappedPtrOperations<JS::PropertyKey, Wrapper> {
|
|
|
|
const JS::PropertyKey& id() const {
|
|
|
|
return static_cast<const Wrapper*>(this)->get();
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2021-06-17 19:51:42 +03:00
|
|
|
bool isVoid() const { return id().isVoid(); }
|
2020-04-26 20:01:39 +03:00
|
|
|
bool isInt() const { return id().isInt(); }
|
|
|
|
bool isString() const { return id().isString(); }
|
|
|
|
bool isSymbol() const { return id().isSymbol(); }
|
2020-04-26 20:03:01 +03:00
|
|
|
bool isGCThing() const { return id().isGCThing(); }
|
2020-04-26 20:01:39 +03:00
|
|
|
|
|
|
|
int32_t toInt() const { return id().toInt(); }
|
|
|
|
JSString* toString() const { return id().toString(); }
|
|
|
|
JS::Symbol* toSymbol() const { return id().toSymbol(); }
|
2020-04-26 20:01:57 +03:00
|
|
|
|
2020-08-27 22:51:32 +03:00
|
|
|
bool isPrivateName() const { return id().isPrivateName(); }
|
|
|
|
|
2020-04-26 20:01:57 +03:00
|
|
|
bool isWellKnownSymbol(JS::SymbolCode code) const {
|
|
|
|
return id().isWellKnownSymbol(code);
|
|
|
|
}
|
2021-03-03 13:12:55 +03:00
|
|
|
|
|
|
|
// Internal API
|
|
|
|
bool isAtom() const { return id().isAtom(); }
|
|
|
|
bool isAtom(JSAtom* atom) const { return id().isAtom(atom); }
|
|
|
|
JSAtom* toAtom() const { return id().toAtom(); }
|
2020-04-26 20:01:39 +03:00
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace js
|
2013-09-12 03:51:17 +04:00
|
|
|
|
2013-08-22 09:26:56 +04:00
|
|
|
#endif /* js_Id_h */
|