1 |
|
2 |
|
3 | "use strict";
|
4 |
|
5 | var d = require("d")
|
6 | , validateSymbol = require("./validate-symbol")
|
7 | , NativeSymbol = require("ext/global-this").Symbol
|
8 | , generateName = require("./lib/private/generate-name")
|
9 | , setupStandardSymbols = require("./lib/private/setup/standard-symbols")
|
10 | , setupSymbolRegistry = require("./lib/private/setup/symbol-registry");
|
11 |
|
12 | var create = Object.create
|
13 | , defineProperties = Object.defineProperties
|
14 | , defineProperty = Object.defineProperty;
|
15 |
|
16 | var SymbolPolyfill, HiddenSymbol, isNativeSafe;
|
17 |
|
18 | if (typeof NativeSymbol === "function") {
|
19 | try {
|
20 | String(NativeSymbol());
|
21 | isNativeSafe = true;
|
22 | } catch (ignore) {}
|
23 | } else {
|
24 | NativeSymbol = null;
|
25 | }
|
26 |
|
27 |
|
28 |
|
29 | HiddenSymbol = function Symbol(description) {
|
30 | if (this instanceof HiddenSymbol) throw new TypeError("Symbol is not a constructor");
|
31 | return SymbolPolyfill(description);
|
32 | };
|
33 |
|
34 |
|
35 |
|
36 | module.exports = SymbolPolyfill = function Symbol(description) {
|
37 | var symbol;
|
38 | if (this instanceof Symbol) throw new TypeError("Symbol is not a constructor");
|
39 | if (isNativeSafe) return NativeSymbol(description);
|
40 | symbol = create(HiddenSymbol.prototype);
|
41 | description = description === undefined ? "" : String(description);
|
42 | return defineProperties(symbol, {
|
43 | __description__: d("", description),
|
44 | __name__: d("", generateName(description))
|
45 | });
|
46 | };
|
47 |
|
48 | setupStandardSymbols(SymbolPolyfill);
|
49 | setupSymbolRegistry(SymbolPolyfill);
|
50 |
|
51 |
|
52 | defineProperties(HiddenSymbol.prototype, {
|
53 | constructor: d(SymbolPolyfill),
|
54 | toString: d("", function () { return this.__name__; })
|
55 | });
|
56 |
|
57 |
|
58 |
|
59 | defineProperties(SymbolPolyfill.prototype, {
|
60 | toString: d(function () { return "Symbol (" + validateSymbol(this).__description__ + ")"; }),
|
61 | valueOf: d(function () { return validateSymbol(this); })
|
62 | });
|
63 | defineProperty(
|
64 | SymbolPolyfill.prototype, SymbolPolyfill.toPrimitive,
|
65 | d("", function () {
|
66 | var symbol = validateSymbol(this);
|
67 | if (typeof symbol === "symbol") return symbol;
|
68 | return symbol.toString();
|
69 | })
|
70 | );
|
71 | defineProperty(SymbolPolyfill.prototype, SymbolPolyfill.toStringTag, d("c", "Symbol"));
|
72 |
|
73 |
|
74 | defineProperty(
|
75 | HiddenSymbol.prototype, SymbolPolyfill.toStringTag,
|
76 | d("c", SymbolPolyfill.prototype[SymbolPolyfill.toStringTag])
|
77 | );
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 | defineProperty(
|
84 | HiddenSymbol.prototype, SymbolPolyfill.toPrimitive,
|
85 | d("c", SymbolPolyfill.prototype[SymbolPolyfill.toPrimitive])
|
86 | );
|