2013-08-22 09:26:56 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
* vim: set ts=8 sts=4 et sw=4 tw=99:
|
|
|
|
* 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
|
|
|
|
|
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.
|
|
|
|
|
2013-08-22 09:26:56 +04:00
|
|
|
#include "jstypes.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"
|
2013-08-28 06:59:14 +04:00
|
|
|
#include "js/TypeDecls.h"
|
2013-08-22 09:26:56 +04:00
|
|
|
#include "js/Utility.h"
|
|
|
|
|
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
|
2018-05-30 14:51:58 +03:00
|
|
|
#define JSID_TYPE_EMPTY 0x6
|
2018-05-11 13:01:32 +03:00
|
|
|
#define JSID_TYPE_MASK 0x7
|
|
|
|
|
2013-08-22 09:26:56 +04:00
|
|
|
struct jsid
|
|
|
|
{
|
|
|
|
size_t asBits;
|
2018-05-11 13:01:32 +03:00
|
|
|
|
|
|
|
constexpr jsid() : asBits(JSID_TYPE_VOID) {}
|
|
|
|
|
2018-05-26 15:14:10 +03:00
|
|
|
static constexpr MOZ_ALWAYS_INLINE jsid fromRawBits(size_t bits) {
|
2018-05-11 13:01:32 +03:00
|
|
|
jsid id;
|
|
|
|
id.asBits = bits;
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2016-03-19 02:43:53 +03:00
|
|
|
bool operator==(const jsid& rhs) const { return asBits == rhs.asBits; }
|
|
|
|
bool operator!=(const jsid& rhs) const { return asBits != rhs.asBits; }
|
2015-09-29 23:39:33 +03:00
|
|
|
} JS_HAZ_GC_POINTER;
|
2013-12-11 03:45:41 +04:00
|
|
|
#define JSID_BITS(id) (id.asBits)
|
2013-08-22 09:26:56 +04:00
|
|
|
|
|
|
|
// Avoid using canonical 'id' for jsid parameters since this is a magic word in
|
|
|
|
// Objective-C++ which, apparently, wants to be able to #include jsapi.h.
|
|
|
|
#define id iden
|
|
|
|
|
2014-01-25 08:14:56 +04:00
|
|
|
static MOZ_ALWAYS_INLINE bool
|
2013-08-22 09:26:56 +04:00
|
|
|
JSID_IS_STRING(jsid id)
|
|
|
|
{
|
2018-05-30 14:51:58 +03:00
|
|
|
return (JSID_BITS(id) & JSID_TYPE_MASK) == JSID_TYPE_STRING;
|
2013-08-22 09:26:56 +04:00
|
|
|
}
|
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
static MOZ_ALWAYS_INLINE JSString*
|
2013-08-22 09:26:56 +04:00
|
|
|
JSID_TO_STRING(jsid id)
|
|
|
|
{
|
2018-05-30 14:51:58 +03:00
|
|
|
// Use XOR instead of `& ~JSID_TYPE_MASK` because small immediates can be
|
|
|
|
// encoded more efficiently on some platorms.
|
2014-02-18 10:24:15 +04:00
|
|
|
MOZ_ASSERT(JSID_IS_STRING(id));
|
2018-05-30 14:51:58 +03:00
|
|
|
return (JSString*)(JSID_BITS(id) ^ JSID_TYPE_STRING);
|
2013-08-22 09:26:56 +04:00
|
|
|
}
|
|
|
|
|
2015-10-17 20:27:16 +03:00
|
|
|
/**
|
2014-07-30 19:37:03 +04:00
|
|
|
* Only JSStrings that have been interned via the JSAPI can be turned into
|
|
|
|
* jsids by API clients.
|
|
|
|
*
|
|
|
|
* N.B. if a jsid is backed by a string which has not been interned, that
|
|
|
|
* string must be appropriately rooted to avoid being collected by the GC.
|
|
|
|
*/
|
|
|
|
JS_PUBLIC_API(jsid)
|
2015-03-29 01:22:11 +03:00
|
|
|
INTERNED_STRING_TO_JSID(JSContext* cx, JSString* str);
|
2014-07-30 19:37:03 +04:00
|
|
|
|
2014-01-25 08:14:56 +04:00
|
|
|
static MOZ_ALWAYS_INLINE bool
|
2013-08-22 09:26:56 +04:00
|
|
|
JSID_IS_INT(jsid id)
|
|
|
|
{
|
2018-05-30 14:51:58 +03:00
|
|
|
return !!(JSID_BITS(id) & JSID_TYPE_INT_BIT);
|
2013-08-22 09:26:56 +04:00
|
|
|
}
|
|
|
|
|
2014-01-25 08:14:56 +04:00
|
|
|
static MOZ_ALWAYS_INLINE int32_t
|
2013-08-22 09:26:56 +04:00
|
|
|
JSID_TO_INT(jsid id)
|
|
|
|
{
|
2014-02-18 10:24:15 +04:00
|
|
|
MOZ_ASSERT(JSID_IS_INT(id));
|
2016-12-01 11:23:21 +03:00
|
|
|
uint32_t bits = static_cast<uint32_t>(JSID_BITS(id)) >> 1;
|
|
|
|
return static_cast<int32_t>(bits);
|
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
|
2013-08-22 09:26:56 +04:00
|
|
|
INT_FITS_IN_JSID(int32_t i)
|
|
|
|
{
|
|
|
|
return i >= 0;
|
|
|
|
}
|
|
|
|
|
2014-01-25 08:14:56 +04:00
|
|
|
static MOZ_ALWAYS_INLINE jsid
|
2013-08-22 09:26:56 +04:00
|
|
|
INT_TO_JSID(int32_t i)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-01-25 08:14:56 +04:00
|
|
|
static MOZ_ALWAYS_INLINE bool
|
2014-06-23 19:56:52 +04:00
|
|
|
JSID_IS_SYMBOL(jsid id)
|
2013-08-22 09:26:56 +04:00
|
|
|
{
|
2018-05-30 14:51:58 +03:00
|
|
|
return (JSID_BITS(id) & JSID_TYPE_MASK) == JSID_TYPE_SYMBOL;
|
2013-08-22 09:26:56 +04:00
|
|
|
}
|
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
static MOZ_ALWAYS_INLINE JS::Symbol*
|
2014-06-23 19:56:52 +04:00
|
|
|
JSID_TO_SYMBOL(jsid id)
|
2013-08-22 09:26:56 +04:00
|
|
|
{
|
2014-06-23 19:56:52 +04:00
|
|
|
MOZ_ASSERT(JSID_IS_SYMBOL(id));
|
2018-05-30 14:51:58 +03:00
|
|
|
return (JS::Symbol*)(JSID_BITS(id) ^ JSID_TYPE_SYMBOL);
|
2013-08-22 09:26:56 +04:00
|
|
|
}
|
|
|
|
|
2014-01-25 08:14:56 +04:00
|
|
|
static MOZ_ALWAYS_INLINE jsid
|
2015-03-29 01:22:11 +03:00
|
|
|
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
|
2013-08-22 09:26:56 +04:00
|
|
|
JSID_IS_GCTHING(jsid id)
|
|
|
|
{
|
2014-06-23 19:56:52 +04:00
|
|
|
return JSID_IS_STRING(id) || JSID_IS_SYMBOL(id);
|
2013-08-22 09:26:56 +04:00
|
|
|
}
|
|
|
|
|
2014-12-05 20:38:34 +03:00
|
|
|
static MOZ_ALWAYS_INLINE JS::GCCellPtr
|
2013-08-22 09:26:56 +04:00
|
|
|
JSID_TO_GCTHING(jsid id)
|
|
|
|
{
|
2015-03-29 01:22:11 +03:00
|
|
|
void* thing = (void*)(JSID_BITS(id) & ~(size_t)JSID_TYPE_MASK);
|
2018-09-06 13:11:07 +03:00
|
|
|
if (JSID_IS_STRING(id)) {
|
2015-05-22 20:40:24 +03:00
|
|
|
return JS::GCCellPtr(thing, JS::TraceKind::String);
|
2018-09-06 13:11:07 +03:00
|
|
|
}
|
2014-12-05 20:38:34 +03:00
|
|
|
MOZ_ASSERT(JSID_IS_SYMBOL(id));
|
2015-05-22 20:40:24 +03:00
|
|
|
return JS::GCCellPtr(thing, JS::TraceKind::Symbol);
|
2013-08-22 09:26:56 +04:00
|
|
|
}
|
|
|
|
|
2014-01-25 08:14:56 +04:00
|
|
|
static MOZ_ALWAYS_INLINE bool
|
2013-08-22 09:26:56 +04:00
|
|
|
JSID_IS_VOID(const jsid id)
|
|
|
|
{
|
2018-05-30 14:51:58 +03:00
|
|
|
MOZ_ASSERT_IF((JSID_BITS(id) & JSID_TYPE_MASK) == JSID_TYPE_VOID,
|
|
|
|
JSID_BITS(id) == JSID_TYPE_VOID);
|
|
|
|
return JSID_BITS(id) == JSID_TYPE_VOID;
|
2013-08-22 09:26:56 +04:00
|
|
|
}
|
|
|
|
|
2014-01-25 08:14:56 +04:00
|
|
|
static MOZ_ALWAYS_INLINE bool
|
2013-08-22 09:26:56 +04:00
|
|
|
JSID_IS_EMPTY(const jsid id)
|
|
|
|
{
|
2018-05-30 14:51:58 +03:00
|
|
|
MOZ_ASSERT_IF((JSID_BITS(id) & JSID_TYPE_MASK) == JSID_TYPE_EMPTY,
|
|
|
|
JSID_BITS(id) == JSID_TYPE_EMPTY);
|
|
|
|
return JSID_BITS(id) == JSID_TYPE_EMPTY;
|
2013-08-22 09:26:56 +04:00
|
|
|
}
|
|
|
|
|
2018-05-11 13:01:32 +03:00
|
|
|
constexpr const jsid JSID_VOID;
|
2018-05-30 14:51:58 +03:00
|
|
|
constexpr const jsid JSID_EMPTY = jsid::fromRawBits(JSID_TYPE_EMPTY);
|
2013-12-17 06:27:43 +04:00
|
|
|
|
2014-01-15 14:31:00 +04:00
|
|
|
extern JS_PUBLIC_DATA(const JS::HandleId) JSID_VOIDHANDLE;
|
|
|
|
extern JS_PUBLIC_DATA(const JS::HandleId) JSID_EMPTYHANDLE;
|
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) {
|
|
|
|
js::UnsafeTraceManuallyBarrieredEdge(trc, idp, name);
|
|
|
|
}
|
2017-09-19 14:31:31 +03:00
|
|
|
static bool isValid(jsid id) {
|
|
|
|
return !JSID_IS_GCTHING(id) || js::gc::IsCellPointerValid(JSID_TO_GCTHING(id).asCell());
|
|
|
|
}
|
2016-04-26 19:18:48 +03:00
|
|
|
};
|
|
|
|
|
2018-02-27 16:01:49 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
MOZ_ALWAYS_INLINE bool
|
|
|
|
IdIsNotGray(jsid id)
|
|
|
|
{
|
2018-09-06 13:11:07 +03:00
|
|
|
if (!JSID_IS_GCTHING(id)) {
|
2018-02-27 16:01:49 +03:00
|
|
|
return true;
|
2018-09-06 13:11:07 +03:00
|
|
|
}
|
2018-02-27 16:01:49 +03:00
|
|
|
|
|
|
|
return CellIsNotGray(JSID_TO_GCTHING(id).asCell());
|
|
|
|
}
|
|
|
|
#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) {
|
2018-09-06 13:11:07 +03:00
|
|
|
if (JSID_IS_STRING(id)) {
|
2017-07-11 18:51:37 +03:00
|
|
|
return reinterpret_cast<gc::Cell*>(JSID_TO_STRING(id));
|
2018-09-06 13:11:07 +03:00
|
|
|
}
|
|
|
|
if (JSID_IS_SYMBOL(id)) {
|
2017-07-11 18:51:37 +03:00
|
|
|
return reinterpret_cast<gc::Cell*>(JSID_TO_SYMBOL(id));
|
2018-09-06 13:11:07 +03:00
|
|
|
}
|
2017-07-11 18:51:37 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
2015-06-18 20:23:49 +03:00
|
|
|
static void postBarrier(jsid* idp, jsid prev, jsid next) {}
|
2016-02-07 20:08:55 +03:00
|
|
|
static void exposeToJS(jsid id) {
|
2018-09-06 13:11:07 +03:00
|
|
|
if (JSID_IS_GCTHING(id)) {
|
2016-02-07 20:08:55 +03:00
|
|
|
js::gc::ExposeGCThingToActiveJS(JSID_TO_GCTHING(id));
|
2018-09-06 13:11:07 +03:00
|
|
|
}
|
2016-02-07 20:08:55 +03:00
|
|
|
}
|
2013-09-12 03:51:17 +04:00
|
|
|
};
|
|
|
|
|
2015-04-23 20:42:31 +03:00
|
|
|
// If the jsid is a GC pointer type, convert to that type and call |f| with
|
|
|
|
// the pointer. If the jsid is not a GC type, calls F::defaultValue.
|
|
|
|
template <typename F, typename... Args>
|
|
|
|
auto
|
2016-09-11 12:15:22 +03:00
|
|
|
DispatchTyped(F f, const jsid& id, Args&&... args)
|
2018-06-01 19:30:30 +03:00
|
|
|
-> decltype(f(static_cast<JSString*>(nullptr), std::forward<Args>(args)...))
|
2015-04-23 20:42:31 +03:00
|
|
|
{
|
2018-09-06 13:11:07 +03:00
|
|
|
if (JSID_IS_STRING(id)) {
|
2018-06-01 19:30:30 +03:00
|
|
|
return f(JSID_TO_STRING(id), std::forward<Args>(args)...);
|
2018-09-06 13:11:07 +03:00
|
|
|
}
|
|
|
|
if (JSID_IS_SYMBOL(id)) {
|
2018-06-01 19:30:30 +03:00
|
|
|
return f(JSID_TO_SYMBOL(id), std::forward<Args>(args)...);
|
2018-09-06 13:11:07 +03:00
|
|
|
}
|
2015-04-23 20:42:31 +03:00
|
|
|
MOZ_ASSERT(!JSID_IS_GCTHING(id));
|
|
|
|
return F::defaultValue(id);
|
|
|
|
}
|
|
|
|
|
2014-06-23 19:56:52 +04:00
|
|
|
#undef id
|
|
|
|
|
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 */
|