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 } |
| 42 // NOTE: Passing in a Symbol value will throw on ToString(). |
| 43 return %CreateSymbol(IS_UNDEFINED(x) ? x : ToString(x)); |
46 } | 44 } |
47 | 45 |
48 function SymbolGetName() { | 46 |
49 var symbol = IS_SYMBOL_WRAPPER(this) ? %_ValueOf(this) : this; | 47 function SymbolToString() { |
50 if (!IS_SYMBOL(symbol)) { | 48 if (!(IS_SYMBOL(this) || IS_SYMBOL_WRAPPER(this))) { |
51 throw MakeTypeError( | 49 throw MakeTypeError( |
52 'incompatible_method_receiver', ["Symbol.prototype.name", this]); | 50 'incompatible_method_receiver', ["Symbol.prototype.toString", this]); |
53 } | 51 } |
54 return %SymbolName(symbol); | 52 var description = %SymbolDescription(%_ValueOf(this)); |
| 53 return "Symbol(" + (IS_UNDEFINED(description) ? "" : description) + ")"; |
55 } | 54 } |
56 | 55 |
57 function SymbolToString() { | |
58 throw MakeTypeError('symbol_to_string'); | |
59 } | |
60 | 56 |
61 function SymbolValueOf() { | 57 function SymbolValueOf() { |
62 // NOTE: Both Symbol objects and values can enter here as | 58 if (!(IS_SYMBOL(this) || IS_SYMBOL_WRAPPER(this))) { |
63 // 'this'. This is not as dictated by ECMA-262. | |
64 if (!IS_SYMBOL(this) && !IS_SYMBOL_WRAPPER(this)) { | |
65 throw MakeTypeError( | 59 throw MakeTypeError( |
66 'incompatible_method_receiver', ["Symbol.prototype.valueOf", this]); | 60 'incompatible_method_receiver', ["Symbol.prototype.valueOf", this]); |
67 } | 61 } |
68 return %_ValueOf(this); | 62 return %_ValueOf(this); |
69 } | 63 } |
70 | 64 |
71 | 65 |
72 // ES6 19.1.2.8 | 66 // ES6 19.1.2.8 |
73 function ObjectGetOwnPropertySymbols(obj) { | 67 function ObjectGetOwnPropertySymbols(obj) { |
74 if (!IS_SPEC_OBJECT(obj)) { | 68 if (!IS_SPEC_OBJECT(obj)) { |
75 throw MakeTypeError("called_on_non_object", | 69 throw MakeTypeError("called_on_non_object", |
76 ["Object.getOwnPropertySymbols"]); | 70 ["Object.getOwnPropertySymbols"]); |
77 } | 71 } |
78 | 72 |
79 // 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. |
80 | 74 |
81 return ObjectGetOwnPropertyKeys(obj, true); | 75 return ObjectGetOwnPropertyKeys(obj, true); |
82 } | 76 } |
83 | 77 |
84 | 78 |
85 //------------------------------------------------------------------- | 79 //------------------------------------------------------------------- |
86 | 80 |
87 function SetUpSymbol() { | 81 function SetUpSymbol() { |
88 %CheckIsBootstrapping(); | 82 %CheckIsBootstrapping(); |
89 | 83 |
90 %SetCode($Symbol, SymbolConstructor); | 84 %SetCode($Symbol, SymbolConstructor); |
91 %FunctionSetPrototype($Symbol, new $Symbol()); | 85 %FunctionSetPrototype($Symbol, new $Object()); |
| 86 |
92 %SetProperty($Symbol.prototype, "constructor", $Symbol, DONT_ENUM); | 87 %SetProperty($Symbol.prototype, "constructor", $Symbol, DONT_ENUM); |
93 | |
94 InstallGetter($Symbol.prototype, "name", SymbolGetName); | |
95 InstallFunctions($Symbol.prototype, DONT_ENUM, $Array( | 88 InstallFunctions($Symbol.prototype, DONT_ENUM, $Array( |
96 "toString", SymbolToString, | 89 "toString", SymbolToString, |
97 "valueOf", SymbolValueOf | 90 "valueOf", SymbolValueOf |
98 )); | 91 )); |
99 } | 92 } |
100 | 93 |
101 SetUpSymbol(); | 94 SetUpSymbol(); |
102 | 95 |
103 | 96 |
104 function ExtendObject() { | 97 function ExtendObject() { |
105 %CheckIsBootstrapping(); | 98 %CheckIsBootstrapping(); |
106 | 99 |
107 InstallFunctions($Object, DONT_ENUM, $Array( | 100 InstallFunctions($Object, DONT_ENUM, $Array( |
108 "getOwnPropertySymbols", ObjectGetOwnPropertySymbols | 101 "getOwnPropertySymbols", ObjectGetOwnPropertySymbols |
109 )); | 102 )); |
110 } | 103 } |
111 | 104 |
112 ExtendObject(); | 105 ExtendObject(); |
OLD | NEW |