gecko-dev/js/public/Symbol.h
Jeff Walden 3093d025b8 Bug 1510012 - Move Symbol-related APIs out of jsapi.h into js/public/Symbol.h. r=sfink, r=mccr8 for the bindings change
--HG--
extra : rebase_source : 3a8d2d694052435fbc47b9131a521e31e9b0ea6f
2018-11-24 12:21:40 -08:00

115 lines
3.3 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* 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/. */
/* Symbols. */
#ifndef js_Symbol_h
#define js_Symbol_h
#include <stddef.h> // size_t
#include <stdint.h> // uintptr_t, uint32_t
#include "jstypes.h" // JS_PUBLIC_API
#include "js/RootingAPI.h" // JS::Handle
struct JSContext;
class JSString;
namespace JS {
class Symbol;
/**
* Create a new Symbol with the given description. This function never returns
* a Symbol that is in the Runtime-wide symbol registry.
*
* If description is null, the new Symbol's [[Description]] attribute is
* undefined.
*/
extern JS_PUBLIC_API Symbol*
NewSymbol(JSContext* cx, Handle<JSString*> description);
/**
* Symbol.for as specified in ES6.
*
* Get a Symbol with the description 'key' from the Runtime-wide symbol registry.
* If there is not already a Symbol with that description in the registry, a new
* Symbol is created and registered. 'key' must not be null.
*/
extern JS_PUBLIC_API Symbol*
GetSymbolFor(JSContext* cx, Handle<JSString*> key);
/**
* Get the [[Description]] attribute of the given symbol.
*
* This function is infallible. If it returns null, that means the symbol's
* [[Description]] is undefined.
*/
extern JS_PUBLIC_API JSString*
GetSymbolDescription(Handle<Symbol*> symbol);
/* Well-known symbols. */
#define JS_FOR_EACH_WELL_KNOWN_SYMBOL(MACRO) \
MACRO(isConcatSpreadable) \
MACRO(iterator) \
MACRO(match) \
MACRO(replace) \
MACRO(search) \
MACRO(species) \
MACRO(hasInstance) \
MACRO(split) \
MACRO(toPrimitive) \
MACRO(toStringTag) \
MACRO(unscopables) \
MACRO(asyncIterator)
enum class SymbolCode : uint32_t
{
// There is one SymbolCode for each well-known symbol.
#define JS_DEFINE_SYMBOL_ENUM(name) name,
JS_FOR_EACH_WELL_KNOWN_SYMBOL(JS_DEFINE_SYMBOL_ENUM) // SymbolCode::iterator, etc.
#undef JS_DEFINE_SYMBOL_ENUM
Limit,
WellKnownAPILimit = 0x80000000, // matches JS::shadow::Symbol::WellKnownAPILimit for inline use
InSymbolRegistry = 0xfffffffe, // created by Symbol.for() or JS::GetSymbolFor()
UniqueSymbol = 0xffffffff // created by Symbol() or JS::NewSymbol()
};
/* For use in loops that iterate over the well-known symbols. */
const size_t WellKnownSymbolLimit = size_t(SymbolCode::Limit);
/**
* Return the SymbolCode telling what sort of symbol `symbol` is.
*
* A symbol's SymbolCode never changes once it is created.
*/
extern JS_PUBLIC_API SymbolCode
GetSymbolCode(Handle<Symbol*> symbol);
/**
* Get one of the well-known symbols defined by ES6. A single set of well-known
* symbols is shared by all compartments in a JSRuntime.
*
* `which` must be in the range [0, WellKnownSymbolLimit).
*/
extern JS_PUBLIC_API Symbol*
GetWellKnownSymbol(JSContext* cx, SymbolCode which);
/**
* Return true if the given JSPropertySpec::name or JSFunctionSpec::name value
* is actually a symbol code and not a string. See JS_SYM_FN.
*/
inline bool
PropertySpecNameIsSymbol(const char* name)
{
uintptr_t u = reinterpret_cast<uintptr_t>(name);
return u != 0 && u - 1 < WellKnownSymbolLimit;
}
} // namespace JS
#endif /* js_Symbol_h */