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', []); |
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 | 68 |
73 function SetUpSymbol() { | 69 function SetUpSymbol() { |
74 %CheckIsBootstrapping(); | 70 %CheckIsBootstrapping(); |
75 | 71 |
76 %SetCode($Symbol, SymbolConstructor); | 72 %SetCode($Symbol, SymbolConstructor); |
77 %FunctionSetPrototype($Symbol, new $Symbol()); | 73 %FunctionSetPrototype($Symbol, new $Object()); |
| 74 |
78 %SetProperty($Symbol.prototype, "constructor", $Symbol, DONT_ENUM); | 75 %SetProperty($Symbol.prototype, "constructor", $Symbol, DONT_ENUM); |
79 | |
80 InstallGetter($Symbol.prototype, "name", SymbolGetName); | |
81 InstallFunctions($Symbol.prototype, DONT_ENUM, $Array( | 76 InstallFunctions($Symbol.prototype, DONT_ENUM, $Array( |
82 "toString", SymbolToString, | 77 "toString", SymbolToString, |
83 "valueOf", SymbolValueOf | 78 "valueOf", SymbolValueOf |
84 )); | 79 )); |
85 } | 80 } |
86 | 81 |
87 SetUpSymbol(); | 82 SetUpSymbol(); |
OLD | NEW |