| OLD | NEW |
| 1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 | 56 |
| 57 function SymbolValueOf() { | 57 function SymbolValueOf() { |
| 58 if (!(IS_SYMBOL(this) || IS_SYMBOL_WRAPPER(this))) { | 58 if (!(IS_SYMBOL(this) || IS_SYMBOL_WRAPPER(this))) { |
| 59 throw MakeTypeError( | 59 throw MakeTypeError( |
| 60 'incompatible_method_receiver', ["Symbol.prototype.valueOf", this]); | 60 'incompatible_method_receiver', ["Symbol.prototype.valueOf", this]); |
| 61 } | 61 } |
| 62 return %_ValueOf(this); | 62 return %_ValueOf(this); |
| 63 } | 63 } |
| 64 | 64 |
| 65 | 65 |
| 66 function GetSymbolRegistry() { | |
| 67 var registry = %SymbolRegistry(); | |
| 68 if (!('internal' in registry)) { | |
| 69 registry.internal = {__proto__: null}; | |
| 70 registry.for = {__proto__: null}; | |
| 71 registry.keyFor = {__proto__: null}; | |
| 72 } | |
| 73 return registry; | |
| 74 } | |
| 75 | |
| 76 | |
| 77 function InternalSymbol(key) { | |
| 78 var registry = GetSymbolRegistry(); | |
| 79 if (!(key in registry.internal)) { | |
| 80 registry.internal[key] = %CreateSymbol(key); | |
| 81 } | |
| 82 return registry.internal[key]; | |
| 83 } | |
| 84 | |
| 85 | |
| 86 function SymbolFor(key) { | |
| 87 key = TO_STRING_INLINE(key); | |
| 88 var registry = GetSymbolRegistry(); | |
| 89 if (!(key in registry.for)) { | |
| 90 var symbol = %CreateSymbol(key); | |
| 91 registry.for[key] = symbol; | |
| 92 registry.keyFor[symbol] = key; | |
| 93 } | |
| 94 return registry.for[key]; | |
| 95 } | |
| 96 | |
| 97 | |
| 98 function SymbolKeyFor(symbol) { | |
| 99 if (!IS_SYMBOL(symbol)) { | |
| 100 throw MakeTypeError("not_a_symbol", [symbol]); | |
| 101 } | |
| 102 return GetSymbolRegistry().keyFor[symbol]; | |
| 103 } | |
| 104 | |
| 105 | |
| 106 // ES6 19.1.2.8 | 66 // ES6 19.1.2.8 |
| 107 function ObjectGetOwnPropertySymbols(obj) { | 67 function ObjectGetOwnPropertySymbols(obj) { |
| 108 if (!IS_SPEC_OBJECT(obj)) { | 68 if (!IS_SPEC_OBJECT(obj)) { |
| 109 throw MakeTypeError("called_on_non_object", | 69 throw MakeTypeError("called_on_non_object", |
| 110 ["Object.getOwnPropertySymbols"]); | 70 ["Object.getOwnPropertySymbols"]); |
| 111 } | 71 } |
| 112 | 72 |
| 113 // TODO(arv): Proxies use a shared trap for String and Symbol keys. | 73 // TODO(arv): Proxies use a shared trap for String and Symbol keys. |
| 114 | 74 |
| 115 return ObjectGetOwnPropertyKeys(obj, true); | 75 return ObjectGetOwnPropertyKeys(obj, true); |
| 116 } | 76 } |
| 117 | 77 |
| 118 | 78 |
| 119 //------------------------------------------------------------------- | 79 //------------------------------------------------------------------- |
| 120 | 80 |
| 121 var symbolCreate = InternalSymbol("@@create"); | |
| 122 var symbolHasInstance = InternalSymbol("@@hasInstance"); | |
| 123 var symbolIsConcatSpreadable = InternalSymbol("@@isConcatSpreadable"); | |
| 124 var symbolIsRegExp = InternalSymbol("@@isRegExp"); | |
| 125 var symbolIterator = InternalSymbol("@@iterator"); | |
| 126 var symbolToStringTag = InternalSymbol("@@toStringTag"); | |
| 127 var symbolUnscopables = InternalSymbol("@@unscopables"); | |
| 128 | |
| 129 | |
| 130 //------------------------------------------------------------------- | |
| 131 | |
| 132 function SetUpSymbol() { | 81 function SetUpSymbol() { |
| 133 %CheckIsBootstrapping(); | 82 %CheckIsBootstrapping(); |
| 134 | 83 |
| 135 %SetCode($Symbol, SymbolConstructor); | 84 %SetCode($Symbol, SymbolConstructor); |
| 136 %FunctionSetPrototype($Symbol, new $Object()); | 85 %FunctionSetPrototype($Symbol, new $Object()); |
| 137 | 86 |
| 138 %SetProperty($Symbol, "create", symbolCreate, DONT_ENUM); | |
| 139 %SetProperty($Symbol, "hasInstance", symbolHasInstance, DONT_ENUM); | |
| 140 %SetProperty($Symbol, "isConcatSpreadable", | |
| 141 symbolIsConcatSpreadable, DONT_ENUM); | |
| 142 %SetProperty($Symbol, "isRegExp", symbolIsRegExp, DONT_ENUM); | |
| 143 %SetProperty($Symbol, "iterator", symbolIterator, DONT_ENUM); | |
| 144 %SetProperty($Symbol, "toStringTag", symbolToStringTag, DONT_ENUM); | |
| 145 %SetProperty($Symbol, "unscopables", symbolUnscopables, DONT_ENUM); | |
| 146 InstallFunctions($Symbol, DONT_ENUM, $Array( | |
| 147 "for", SymbolFor, | |
| 148 "keyFor", SymbolKeyFor | |
| 149 )); | |
| 150 | |
| 151 %SetProperty($Symbol.prototype, "constructor", $Symbol, DONT_ENUM); | 87 %SetProperty($Symbol.prototype, "constructor", $Symbol, DONT_ENUM); |
| 152 InstallFunctions($Symbol.prototype, DONT_ENUM, $Array( | 88 InstallFunctions($Symbol.prototype, DONT_ENUM, $Array( |
| 153 "toString", SymbolToString, | 89 "toString", SymbolToString, |
| 154 "valueOf", SymbolValueOf | 90 "valueOf", SymbolValueOf |
| 155 )); | 91 )); |
| 156 } | 92 } |
| 157 | 93 |
| 158 SetUpSymbol(); | 94 SetUpSymbol(); |
| 159 | 95 |
| 160 | 96 |
| 161 function ExtendObject() { | 97 function ExtendObject() { |
| 162 %CheckIsBootstrapping(); | 98 %CheckIsBootstrapping(); |
| 163 | 99 |
| 164 InstallFunctions($Object, DONT_ENUM, $Array( | 100 InstallFunctions($Object, DONT_ENUM, $Array( |
| 165 "getOwnPropertySymbols", ObjectGetOwnPropertySymbols | 101 "getOwnPropertySymbols", ObjectGetOwnPropertySymbols |
| 166 )); | 102 )); |
| 167 } | 103 } |
| 168 | 104 |
| 169 ExtendObject(); | 105 ExtendObject(); |
| OLD | NEW |