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"
|
|
|
|
|
|
|
|
struct jsid
|
|
|
|
{
|
|
|
|
size_t asBits;
|
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
|
|
|
|
2013-12-17 06:27:43 +04:00
|
|
|
#define JSID_TYPE_STRING 0x0
|
|
|
|
#define JSID_TYPE_INT 0x1
|
|
|
|
#define JSID_TYPE_VOID 0x2
|
2014-06-23 19:56:52 +04:00
|
|
|
#define JSID_TYPE_SYMBOL 0x4
|
2013-12-17 06:27:43 +04:00
|
|
|
#define JSID_TYPE_MASK 0x7
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
return (JSID_BITS(id) & JSID_TYPE_MASK) == 0;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2014-02-18 10:24:15 +04:00
|
|
|
MOZ_ASSERT(JSID_IS_STRING(id));
|
2015-03-29 01:22:11 +03:00
|
|
|
return (JSString*)JSID_BITS(id);
|
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)
|
|
|
|
{
|
|
|
|
return !!(JSID_BITS(id) & JSID_TYPE_INT);
|
|
|
|
}
|
|
|
|
|
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));
|
2016-12-01 11:23:21 +03:00
|
|
|
uint32_t bits = (static_cast<uint32_t>(i) << 1) | JSID_TYPE_INT;
|
|
|
|
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
|
|
|
{
|
2014-06-23 19:56:52 +04:00
|
|
|
return (JSID_BITS(id) & JSID_TYPE_MASK) == JSID_TYPE_SYMBOL &&
|
|
|
|
JSID_BITS(id) != 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));
|
2015-03-29 01:22:11 +03:00
|
|
|
return (JS::Symbol*)(JSID_BITS(id) & ~(size_t)JSID_TYPE_MASK);
|
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);
|
2014-12-05 20:38:34 +03:00
|
|
|
if (JSID_IS_STRING(id))
|
2015-05-22 20:40:24 +03:00
|
|
|
return JS::GCCellPtr(thing, JS::TraceKind::String);
|
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)
|
|
|
|
{
|
2014-02-18 10:24:15 +04:00
|
|
|
MOZ_ASSERT_IF(((size_t)JSID_BITS(id) & JSID_TYPE_MASK) == JSID_TYPE_VOID,
|
2013-08-22 09:26:56 +04:00
|
|
|
JSID_BITS(id) == JSID_TYPE_VOID);
|
2014-08-18 23:20:40 +04:00
|
|
|
return (size_t)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)
|
|
|
|
{
|
2014-08-18 23:20:40 +04:00
|
|
|
return (size_t)JSID_BITS(id) == JSID_TYPE_SYMBOL;
|
2013-08-22 09:26:56 +04:00
|
|
|
}
|
|
|
|
|
2013-12-17 06:27:43 +04:00
|
|
|
extern JS_PUBLIC_DATA(const jsid) JSID_VOID;
|
|
|
|
extern JS_PUBLIC_DATA(const jsid) JSID_EMPTY;
|
|
|
|
|
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 jsid initial() { return JSID_VOID; }
|
|
|
|
static void trace(JSTracer* trc, jsid* idp, const char* name) {
|
|
|
|
js::UnsafeTraceManuallyBarrieredEdge(trc, idp, name);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace JS
|
|
|
|
|
2013-09-12 03:51:17 +04:00
|
|
|
namespace js {
|
|
|
|
|
2015-12-28 21:11:40 +03:00
|
|
|
template <>
|
|
|
|
struct BarrierMethods<jsid>
|
|
|
|
{
|
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) {
|
|
|
|
if (JSID_IS_GCTHING(id))
|
|
|
|
js::gc::ExposeGCThingToActiveJS(JSID_TO_GCTHING(id));
|
|
|
|
}
|
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)
|
2015-04-23 20:42:31 +03:00
|
|
|
-> decltype(f(static_cast<JSString*>(nullptr), mozilla::Forward<Args>(args)...))
|
|
|
|
{
|
|
|
|
if (JSID_IS_STRING(id))
|
|
|
|
return f(JSID_TO_STRING(id), mozilla::Forward<Args>(args)...);
|
|
|
|
if (JSID_IS_SYMBOL(id))
|
|
|
|
return f(JSID_TO_SYMBOL(id), mozilla::Forward<Args>(args)...);
|
|
|
|
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 */
|