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 GlobalObject = global.Object; 15var GlobalSymbol = global.Symbol; 16var hasInstanceSymbol = utils.ImportNow("has_instance_symbol"); 17var isConcatSpreadableSymbol = 18 utils.ImportNow("is_concat_spreadable_symbol"); 19var iteratorSymbol = utils.ImportNow("iterator_symbol"); 20var MakeTypeError; 21var toPrimitiveSymbol = utils.ImportNow("to_primitive_symbol"); 22var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol"); 23var unscopablesSymbol = utils.ImportNow("unscopables_symbol"); 24 25utils.Import(function(from) { 26 MakeTypeError = from.MakeTypeError; 27}); 28 29// ------------------------------------------------------------------- 30 31// 19.4.3.4 Symbol.prototype [ @@toPrimitive ] ( hint ) 32function SymbolToPrimitive(hint) { 33 if (!(IS_SYMBOL(this) || IS_SYMBOL_WRAPPER(this))) { 34 throw MakeTypeError(kIncompatibleMethodReceiver, 35 "Symbol.prototype [ @@toPrimitive ]", this); 36 } 37 return %_ValueOf(this); 38} 39 40 41function SymbolToString() { 42 if (!(IS_SYMBOL(this) || IS_SYMBOL_WRAPPER(this))) { 43 throw MakeTypeError(kIncompatibleMethodReceiver, 44 "Symbol.prototype.toString", this); 45 } 46 return %SymbolDescriptiveString(%_ValueOf(this)); 47} 48 49 50function SymbolValueOf() { 51 if (!(IS_SYMBOL(this) || IS_SYMBOL_WRAPPER(this))) { 52 throw MakeTypeError(kIncompatibleMethodReceiver, 53 "Symbol.prototype.valueOf", this); 54 } 55 return %_ValueOf(this); 56} 57 58 59function SymbolFor(key) { 60 key = TO_STRING(key); 61 var registry = %SymbolRegistry(); 62 if (IS_UNDEFINED(registry.for[key])) { 63 var symbol = %CreateSymbol(key); 64 registry.for[key] = symbol; 65 registry.keyFor[symbol] = key; 66 } 67 return registry.for[key]; 68} 69 70 71function SymbolKeyFor(symbol) { 72 if (!IS_SYMBOL(symbol)) throw MakeTypeError(kSymbolKeyFor, symbol); 73 return %SymbolRegistry().keyFor[symbol]; 74} 75 76 77// ES6 19.1.2.8 78function ObjectGetOwnPropertySymbols(obj) { 79 obj = TO_OBJECT(obj); 80 81 return %GetOwnPropertyKeys(obj, PROPERTY_FILTER_SKIP_STRINGS); 82} 83 84// ------------------------------------------------------------------- 85 86%FunctionSetPrototype(GlobalSymbol, new GlobalObject()); 87 88utils.InstallConstants(GlobalSymbol, [ 89 // TODO(rossberg): expose when implemented. 90 // "hasInstance", hasInstanceSymbol, 91 // "isConcatSpreadable", isConcatSpreadableSymbol, 92 "iterator", iteratorSymbol, 93 // TODO(yangguo): expose when implemented. 94 // "match", matchSymbol, 95 // "replace", replaceSymbol, 96 // "search", searchSymbol, 97 // "split, splitSymbol, 98 "toPrimitive", toPrimitiveSymbol, 99 // TODO(dslomov, caitp): Currently defined in harmony-tostring.js --- 100 // Move here when shipping 101 // "toStringTag", toStringTagSymbol, 102 "unscopables", unscopablesSymbol, 103]); 104 105utils.InstallFunctions(GlobalSymbol, DONT_ENUM, [ 106 "for", SymbolFor, 107 "keyFor", SymbolKeyFor 108]); 109 110%AddNamedProperty( 111 GlobalSymbol.prototype, "constructor", GlobalSymbol, DONT_ENUM); 112%AddNamedProperty( 113 GlobalSymbol.prototype, toStringTagSymbol, "Symbol", DONT_ENUM | READ_ONLY); 114 115utils.InstallFunctions(GlobalSymbol.prototype, DONT_ENUM | READ_ONLY, [ 116 toPrimitiveSymbol, SymbolToPrimitive 117]); 118 119utils.InstallFunctions(GlobalSymbol.prototype, DONT_ENUM, [ 120 "toString", SymbolToString, 121 "valueOf", SymbolValueOf 122]); 123 124utils.InstallFunctions(GlobalObject, DONT_ENUM, [ 125 "getOwnPropertySymbols", ObjectGetOwnPropertySymbols 126]); 127 128// ------------------------------------------------------------------- 129// Exports 130 131utils.Export(function(to) { 132 to.SymbolToString = SymbolToString; 133}) 134 135}) 136