2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2018-11-24 23:21:40 +03: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/. */
|
|
|
|
|
|
|
|
/* Symbols. */
|
|
|
|
|
|
|
|
#ifndef js_Symbol_h
|
|
|
|
#define js_Symbol_h
|
|
|
|
|
2020-08-20 22:22:23 +03:00
|
|
|
#include "js/shadow/Symbol.h" // JS::shadow::Symbol::WellKnownAPILimit
|
|
|
|
|
2018-11-24 23:21:40 +03:00
|
|
|
#include <stddef.h> // size_t
|
|
|
|
#include <stdint.h> // uintptr_t, uint32_t
|
|
|
|
|
|
|
|
#include "jstypes.h" // JS_PUBLIC_API
|
|
|
|
|
2020-11-23 19:12:12 +03:00
|
|
|
#include "js/TypeDecls.h"
|
2018-11-24 23:21:40 +03:00
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2019-10-28 01:34:11 +03:00
|
|
|
class JS_PUBLIC_API Symbol;
|
2018-11-24 23:21:40 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) \
|
2018-12-13 19:04:00 +03:00
|
|
|
MACRO(asyncIterator) \
|
|
|
|
MACRO(matchAll)
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-11-24 23:21:40 +03:00
|
|
|
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,
|
2020-08-20 22:22:23 +03:00
|
|
|
WellKnownAPILimit = JS::shadow::Symbol::WellKnownAPILimit,
|
2020-06-02 23:57:34 +03:00
|
|
|
PrivateNameSymbol = 0xfffffffd, // created by the #PrivateName syntax.
|
2018-11-24 23:21:40 +03:00
|
|
|
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.
|
|
|
|
*/
|
2019-05-13 13:26:48 +03:00
|
|
|
inline bool PropertySpecNameIsSymbol(uintptr_t name) {
|
|
|
|
return name != 0 && name - 1 < WellKnownSymbolLimit;
|
2018-11-24 23:21:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace JS
|
|
|
|
|
|
|
|
#endif /* js_Symbol_h */
|