Bug 1568151 - Replace `target.getInspector()` by `target.getFront("inspector")`....
[gecko.git] / js / public / Symbol.h
blob39d74f18c7919b2c529f4223bb9d37797d7fbf30
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 /* Symbols. */
8 #ifndef js_Symbol_h
9 #define js_Symbol_h
11 #include <stddef.h> // size_t
12 #include <stdint.h> // uintptr_t, uint32_t
14 #include "jstypes.h" // JS_PUBLIC_API
16 #include "js/RootingAPI.h" // JS::Handle
18 struct JSContext;
19 class JSString;
21 namespace JS {
23 class Symbol;
25 /**
26 * Create a new Symbol with the given description. This function never returns
27 * a Symbol that is in the Runtime-wide symbol registry.
29 * If description is null, the new Symbol's [[Description]] attribute is
30 * undefined.
32 extern JS_PUBLIC_API Symbol* NewSymbol(JSContext* cx,
33 Handle<JSString*> description);
35 /**
36 * Symbol.for as specified in ES6.
38 * Get a Symbol with the description 'key' from the Runtime-wide symbol
39 * registry. If there is not already a Symbol with that description in the
40 * registry, a new Symbol is created and registered. 'key' must not be null.
42 extern JS_PUBLIC_API Symbol* GetSymbolFor(JSContext* cx, Handle<JSString*> key);
44 /**
45 * Get the [[Description]] attribute of the given symbol.
47 * This function is infallible. If it returns null, that means the symbol's
48 * [[Description]] is undefined.
50 extern JS_PUBLIC_API JSString* GetSymbolDescription(Handle<Symbol*> symbol);
52 /* Well-known symbols. */
53 #define JS_FOR_EACH_WELL_KNOWN_SYMBOL(MACRO) \
54 MACRO(isConcatSpreadable) \
55 MACRO(iterator) \
56 MACRO(match) \
57 MACRO(replace) \
58 MACRO(search) \
59 MACRO(species) \
60 MACRO(hasInstance) \
61 MACRO(split) \
62 MACRO(toPrimitive) \
63 MACRO(toStringTag) \
64 MACRO(unscopables) \
65 MACRO(asyncIterator) \
66 MACRO(matchAll)
68 enum class SymbolCode : uint32_t {
69 // There is one SymbolCode for each well-known symbol.
70 #define JS_DEFINE_SYMBOL_ENUM(name) name,
71 JS_FOR_EACH_WELL_KNOWN_SYMBOL(
72 JS_DEFINE_SYMBOL_ENUM) // SymbolCode::iterator, etc.
73 #undef JS_DEFINE_SYMBOL_ENUM
74 Limit,
75 WellKnownAPILimit =
76 0x80000000, // matches JS::shadow::Symbol::WellKnownAPILimit for inline
77 // use
78 InSymbolRegistry =
79 0xfffffffe, // created by Symbol.for() or JS::GetSymbolFor()
80 UniqueSymbol = 0xffffffff // created by Symbol() or JS::NewSymbol()
83 /* For use in loops that iterate over the well-known symbols. */
84 const size_t WellKnownSymbolLimit = size_t(SymbolCode::Limit);
86 /**
87 * Return the SymbolCode telling what sort of symbol `symbol` is.
89 * A symbol's SymbolCode never changes once it is created.
91 extern JS_PUBLIC_API SymbolCode GetSymbolCode(Handle<Symbol*> symbol);
93 /**
94 * Get one of the well-known symbols defined by ES6. A single set of well-known
95 * symbols is shared by all compartments in a JSRuntime.
97 * `which` must be in the range [0, WellKnownSymbolLimit).
99 extern JS_PUBLIC_API Symbol* GetWellKnownSymbol(JSContext* cx,
100 SymbolCode which);
103 * Return true if the given JSPropertySpec::name or JSFunctionSpec::name value
104 * is actually a symbol code and not a string. See JS_SYM_FN.
106 inline bool PropertySpecNameIsSymbol(uintptr_t name) {
107 return name != 0 && name - 1 < WellKnownSymbolLimit;
110 } // namespace JS
112 #endif /* js_Symbol_h */