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 { | 41 } else { |
| 42 // NOTE: Passing in a Symbol value will throw on ToString(). |
| 43 var value = %CreateSymbol(IS_UNDEFINED(x) ? x : ToString(x)); |
44 return value; | 44 return value; |
45 } | 45 } |
46 } | 46 } |
47 | 47 |
48 function SymbolGetName() { | 48 // The wrapper object for Symbols. |
49 var symbol = IS_SYMBOL_WRAPPER(this) ? %_ValueOf(this) : this; | 49 function $SymbolWrapper(x) { |
50 if (!IS_SYMBOL(symbol)) { | 50 if (!%_IsConstructCall()) { |
51 throw MakeTypeError( | 51 throw MakeTypeError('constructor_not_function', ["Symbol"]); |
52 'incompatible_method_receiver', ["Symbol.prototype.name", this]); | 52 } else if (!IS_SYMBOL(x)) { |
| 53 // A bug elsewhere; shouldn't be possible. |
| 54 throw MakeTypeError('symbol_value_expected', []); |
| 55 } else { |
| 56 %SymbolSetSymbolData(this, x); |
53 } | 57 } |
54 return %SymbolName(symbol); | |
55 } | 58 } |
56 | 59 |
57 function SymbolToString() { | 60 function SymbolToString() { |
58 throw MakeTypeError('symbol_to_string'); | 61 if (this === $SymbolWrapper.prototype) { |
| 62 return "Symbol"; |
| 63 } else if (!IS_SYMBOL_WRAPPER(this)) { |
| 64 throw MakeTypeError( |
| 65 'incompatible_method_receiver', ["Symbol.prototype.toString", this]); |
| 66 } else { |
| 67 var symbol = %SymbolGetSymbolData(this); |
| 68 if (IS_UNDEFINED(symbol)) { |
| 69 throw MakeTypeError('symbol_data_undefined', []); |
| 70 } else { |
| 71 var description = %SymbolDescription(symbol); |
| 72 return "Symbol(" + (IS_UNDEFINED(description) ? "" : description) + ")"; |
| 73 } |
| 74 } |
59 } | 75 } |
60 | 76 |
61 function SymbolValueOf() { | 77 function SymbolValueOf() { |
62 // NOTE: Both Symbol objects and values can enter here as | 78 if (!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( | 79 throw MakeTypeError( |
66 'incompatible_method_receiver', ["Symbol.prototype.valueOf", this]); | 80 'incompatible_method_receiver', ["Symbol.prototype.valueOf", this]); |
| 81 } else { |
| 82 var symbol = %SymbolGetSymbolData(this); |
| 83 if (IS_UNDEFINED(symbol)) { |
| 84 throw MakeTypeError('symbol_data_undefined', []) |
| 85 } |
| 86 return symbol; |
67 } | 87 } |
68 return %_ValueOf(this); | |
69 } | 88 } |
70 | 89 |
71 //------------------------------------------------------------------- | 90 //------------------------------------------------------------------- |
72 | 91 |
73 function SetUpSymbol() { | 92 function SetUpSymbol() { |
74 %CheckIsBootstrapping(); | 93 %CheckIsBootstrapping(); |
75 | 94 |
| 95 // The global function for creating new Symbol values. |
76 %SetCode($Symbol, SymbolConstructor); | 96 %SetCode($Symbol, SymbolConstructor); |
77 %FunctionSetPrototype($Symbol, new $Symbol()); | 97 %FunctionSetPrototype($Symbol, $Function.prototype); |
78 %SetProperty($Symbol.prototype, "constructor", $Symbol, DONT_ENUM); | |
79 | 98 |
80 InstallGetter($Symbol.prototype, "name", SymbolGetName); | 99 // A [[Class]] of "Symbol" is assumed for wrapper objects. |
81 InstallFunctions($Symbol.prototype, DONT_ENUM, $Array( | 100 %FunctionSetInstanceClassName($SymbolWrapper, "Symbol"); |
| 101 %SetProperty($SymbolWrapper.prototype, "constructor", $Symbol, DONT_ENUM); |
| 102 |
| 103 InstallFunctions($SymbolWrapper.prototype, DONT_ENUM, $Array( |
82 "toString", SymbolToString, | 104 "toString", SymbolToString, |
83 "valueOf", SymbolValueOf | 105 "valueOf", SymbolValueOf |
84 )); | 106 )); |
85 } | 107 } |
86 | 108 |
87 SetUpSymbol(); | 109 SetUpSymbol(); |
OLD | NEW |