• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2013 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5(function(global, utils) {
6
7"use strict";
8
9%CheckIsBootstrapping();
10
11// -------------------------------------------------------------------
12// Imports
13
14var GlobalSymbol = global.Symbol;
15var hasInstanceSymbol = utils.ImportNow("has_instance_symbol");
16var isConcatSpreadableSymbol =
17    utils.ImportNow("is_concat_spreadable_symbol");
18var iteratorSymbol = utils.ImportNow("iterator_symbol");
19var MakeTypeError;
20var matchSymbol = utils.ImportNow("match_symbol");
21var replaceSymbol = utils.ImportNow("replace_symbol");
22var searchSymbol = utils.ImportNow("search_symbol");
23var speciesSymbol = utils.ImportNow("species_symbol");
24var splitSymbol = utils.ImportNow("split_symbol");
25var toPrimitiveSymbol = utils.ImportNow("to_primitive_symbol");
26var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol");
27var unscopablesSymbol = utils.ImportNow("unscopables_symbol");
28
29utils.Import(function(from) {
30  MakeTypeError = from.MakeTypeError;
31});
32
33// -------------------------------------------------------------------
34
35// 19.4.3.4 Symbol.prototype [ @@toPrimitive ] ( hint )
36function SymbolToPrimitive(hint) {
37  if (!(IS_SYMBOL(this) || IS_SYMBOL_WRAPPER(this))) {
38    throw MakeTypeError(kIncompatibleMethodReceiver,
39                        "Symbol.prototype [ @@toPrimitive ]", this);
40  }
41  return %_ValueOf(this);
42}
43
44
45function SymbolToString() {
46  if (!(IS_SYMBOL(this) || IS_SYMBOL_WRAPPER(this))) {
47    throw MakeTypeError(kIncompatibleMethodReceiver,
48                        "Symbol.prototype.toString", this);
49  }
50  return %SymbolDescriptiveString(%_ValueOf(this));
51}
52
53
54function SymbolValueOf() {
55  if (!(IS_SYMBOL(this) || IS_SYMBOL_WRAPPER(this))) {
56    throw MakeTypeError(kIncompatibleMethodReceiver,
57                        "Symbol.prototype.valueOf", this);
58  }
59  return %_ValueOf(this);
60}
61
62
63function SymbolFor(key) {
64  key = TO_STRING(key);
65  var registry = %SymbolRegistry();
66  if (IS_UNDEFINED(registry.for[key])) {
67    var symbol = %CreateSymbol(key);
68    registry.for[key] = symbol;
69    registry.keyFor[symbol] = key;
70  }
71  return registry.for[key];
72}
73
74
75function SymbolKeyFor(symbol) {
76  if (!IS_SYMBOL(symbol)) throw MakeTypeError(kSymbolKeyFor, symbol);
77  return %SymbolRegistry().keyFor[symbol];
78}
79
80// -------------------------------------------------------------------
81
82utils.InstallConstants(GlobalSymbol, [
83  "hasInstance", hasInstanceSymbol,
84  "isConcatSpreadable", isConcatSpreadableSymbol,
85  "iterator", iteratorSymbol,
86  "match", matchSymbol,
87  "replace", replaceSymbol,
88  "search", searchSymbol,
89  "species", speciesSymbol,
90  "split", splitSymbol,
91  "toPrimitive", toPrimitiveSymbol,
92  "toStringTag", toStringTagSymbol,
93  "unscopables", unscopablesSymbol,
94]);
95
96utils.InstallFunctions(GlobalSymbol, DONT_ENUM, [
97  "for", SymbolFor,
98  "keyFor", SymbolKeyFor
99]);
100
101%AddNamedProperty(
102    GlobalSymbol.prototype, toStringTagSymbol, "Symbol", DONT_ENUM | READ_ONLY);
103
104utils.InstallFunctions(GlobalSymbol.prototype, DONT_ENUM | READ_ONLY, [
105  toPrimitiveSymbol, SymbolToPrimitive
106]);
107
108utils.InstallFunctions(GlobalSymbol.prototype, DONT_ENUM, [
109  "toString", SymbolToString,
110  "valueOf", SymbolValueOf
111]);
112
113// -------------------------------------------------------------------
114// Exports
115
116utils.Export(function(to) {
117  to.SymbolToString = SymbolToString;
118})
119
120})
121