Chromium Code Reviews| 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 18 matching lines...) Expand all Loading... | |
| 29 | 29 |
| 30 // This file relies on the fact that the following declaration has been made | 30 // This file relies on the fact that the following declaration has been made |
| 31 // in runtime.js: | 31 // in runtime.js: |
| 32 // var $Array = global.Array; | 32 // var $Array = global.Array; |
| 33 | 33 |
| 34 var $Symbol = global.Symbol; | 34 var $Symbol = global.Symbol; |
| 35 | 35 |
| 36 // ------------------------------------------------------------------- | 36 // ------------------------------------------------------------------- |
| 37 | 37 |
| 38 function SymbolConstructor(x) { | 38 function SymbolConstructor(x) { |
| 39 var value = | |
| 40 IS_SYMBOL(x) ? x : %CreateSymbol(IS_UNDEFINED(x) ? x : ToString(x)); | |
| 41 if (%_IsConstructCall()) { | 39 if (%_IsConstructCall()) { |
| 42 %_SetValueOf(this, value); | 40 throw MakeTypeError('not_constructor', ["Symbol"]); |
| 43 } else { | |
| 44 return value; | |
| 45 } | 41 } |
| 46 } | 42 // NOTE: Passing in a Symbol value will throw on ToString(). |
| 47 | 43 return %CreateSymbol(IS_UNDEFINED(x) ? x : ToString(x)); |
| 48 function SymbolGetName() { | |
| 49 var symbol = IS_SYMBOL_WRAPPER(this) ? %_ValueOf(this) : this; | |
| 50 if (!IS_SYMBOL(symbol)) { | |
| 51 throw MakeTypeError( | |
| 52 'incompatible_method_receiver', ["Symbol.prototype.name", this]); | |
| 53 } | |
| 54 return %SymbolName(symbol); | |
| 55 } | 44 } |
| 56 | 45 |
| 57 function SymbolToString() { | 46 function SymbolToString() { |
| 58 throw MakeTypeError('symbol_to_string'); | 47 if (IS_SYMBOL(this)) { |
| 48 throw MakeTypeError('symbol_to_string', []); | |
| 49 } else if (!IS_SYMBOL_WRAPPER(this)) { | |
| 50 throw MakeTypeError( | |
| 51 'incompatible_method_receiver', ["Symbol.prototype.toString", this]); | |
| 52 } | |
| 53 var description = %SymbolDescription(%_ValueOf(this)); | |
| 54 return "Symbol(" + (IS_UNDEFINED(description) ? "" : description) + ")"; | |
| 59 } | 55 } |
| 60 | 56 |
| 61 function SymbolValueOf() { | 57 function SymbolValueOf() { |
| 62 // NOTE: Both Symbol objects and values can enter here as | 58 if (IS_SYMBOL(this)) { |
| 63 // 'this'. This is not as dictated by ECMA-262. | 59 throw MakeTypeError('symbol_to_value', []); |
|
rossberg
2014/02/04 15:16:25
We should leave this out for now (and also not do
sof
2014/02/04 15:40:58
Not just non-strict, but the methods would also ha
| |
| 64 if (!IS_SYMBOL(this) && !IS_SYMBOL_WRAPPER(this)) { | 60 } else if (!IS_SYMBOL_WRAPPER(this)) { |
| 65 throw MakeTypeError( | 61 throw MakeTypeError( |
| 66 'incompatible_method_receiver', ["Symbol.prototype.valueOf", this]); | 62 'incompatible_method_receiver', ["Symbol.prototype.valueOf", this]); |
| 67 } | 63 } |
| 68 return %_ValueOf(this); | 64 return %_ValueOf(this); |
| 69 } | 65 } |
| 70 | 66 |
| 71 | 67 |
| 72 // ES6 19.1.2.8 | 68 // ES6 19.1.2.8 |
| 73 function ObjectGetOwnPropertySymbols(obj) { | 69 function ObjectGetOwnPropertySymbols(obj) { |
| 74 if (!IS_SPEC_OBJECT(obj)) { | 70 if (!IS_SPEC_OBJECT(obj)) { |
| 75 throw MakeTypeError("called_on_non_object", | 71 throw MakeTypeError("called_on_non_object", |
| 76 ["Object.getOwnPropertySymbols"]); | 72 ["Object.getOwnPropertySymbols"]); |
| 77 } | 73 } |
| 78 | 74 |
| 79 // TODO(arv): Proxies use a shared trap for String and Symbol keys. | 75 // TODO(arv): Proxies use a shared trap for String and Symbol keys. |
| 80 | 76 |
| 81 return ObjectGetOwnPropertyKeys(obj, true); | 77 return ObjectGetOwnPropertyKeys(obj, true); |
| 82 } | 78 } |
| 83 | 79 |
| 84 | 80 |
| 85 //------------------------------------------------------------------- | 81 //------------------------------------------------------------------- |
| 86 | 82 |
| 87 function SetUpSymbol() { | 83 function SetUpSymbol() { |
| 88 %CheckIsBootstrapping(); | 84 %CheckIsBootstrapping(); |
| 89 | 85 |
| 90 %SetCode($Symbol, SymbolConstructor); | 86 %SetCode($Symbol, SymbolConstructor); |
| 91 %FunctionSetPrototype($Symbol, new $Symbol()); | 87 %FunctionSetPrototype($Symbol, new $Object()); |
| 88 | |
| 92 %SetProperty($Symbol.prototype, "constructor", $Symbol, DONT_ENUM); | 89 %SetProperty($Symbol.prototype, "constructor", $Symbol, DONT_ENUM); |
| 93 | |
| 94 InstallGetter($Symbol.prototype, "name", SymbolGetName); | |
| 95 InstallFunctions($Symbol.prototype, DONT_ENUM, $Array( | 90 InstallFunctions($Symbol.prototype, DONT_ENUM, $Array( |
| 96 "toString", SymbolToString, | 91 "toString", SymbolToString, |
| 97 "valueOf", SymbolValueOf | 92 "valueOf", SymbolValueOf |
| 98 )); | 93 )); |
| 99 } | 94 } |
| 100 | 95 |
| 101 SetUpSymbol(); | 96 SetUpSymbol(); |
| 102 | 97 |
| 103 | 98 |
| 104 function ExtendObject() { | 99 function ExtendObject() { |
| 105 %CheckIsBootstrapping(); | 100 %CheckIsBootstrapping(); |
| 106 | 101 |
| 107 InstallFunctions($Object, DONT_ENUM, $Array( | 102 InstallFunctions($Object, DONT_ENUM, $Array( |
| 108 "getOwnPropertySymbols", ObjectGetOwnPropertySymbols | 103 "getOwnPropertySymbols", ObjectGetOwnPropertySymbols |
| 109 )); | 104 )); |
| 110 } | 105 } |
| 111 | 106 |
| 112 ExtendObject(); | 107 ExtendObject(); |
| OLD | NEW |