Debian Codegraph @

rhino/src/org/mozilla/javascript/NativeSymbol.java

raw symbols SCIP
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 package org.mozilla.javascript;
8
9 import java.util.HashMap;
10 import java.util.Map;
11
12 /**
13 * This is an implementation of the standard "Symbol" type that implements
14 * all of its weird properties. One of them is that some objects can have
15 * an "internal data slot" that makes them a Symbol and others cannot.
16 */
17
18 public class NativeSymbol
19 extends IdScriptableObject
20 implements Symbol
21 {
22 private static final long serialVersionUID = -589539749749830003L;
23
24 public static final String CLASS_NAME = "Symbol";
25 public static final String TYPE_NAME = "symbol";
26
27 private static final Object GLOBAL_TABLE_KEY = new Object();
28 private static final Object CONSTRUCTOR_SLOT = new Object();
29
30 private final SymbolKey key;
31 private final NativeSymbol symbolData;
32
33 public static void init(Context cx, Scriptable scope, boolean sealed) {
34 NativeSymbol obj = new NativeSymbol("");
35 ScriptableObject ctor = obj.exportAsJSClass(MAX_PROTOTYPE_ID, scope, sealed);
36
37 cx.putThreadLocal(CONSTRUCTOR_SLOT, Boolean.TRUE);
38 try {
39 createStandardSymbol(cx, scope, ctor, "iterator", SymbolKey.ITERATOR);
40 createStandardSymbol(cx, scope, ctor, "species", SymbolKey.SPECIES);
41 createStandardSymbol(cx, scope, ctor, "toStringTag", SymbolKey.TO_STRING_TAG);
42 createStandardSymbol(cx, scope, ctor, "hasInstance", SymbolKey.HAS_INSTANCE);
43 createStandardSymbol(cx, scope, ctor, "isConcatSpreadable", SymbolKey.IS_CONCAT_SPREADABLE);
44 createStandardSymbol(cx, scope, ctor, "isRegExp", SymbolKey.IS_REGEXP);
45 createStandardSymbol(cx, scope, ctor, "toPrimitive", SymbolKey.TO_PRIMITIVE);
46 createStandardSymbol(cx, scope, ctor, "match", SymbolKey.MATCH);
47 createStandardSymbol(cx, scope, ctor, "replace", SymbolKey.REPLACE);
48 createStandardSymbol(cx, scope, ctor, "search", SymbolKey.SEARCH);
49 createStandardSymbol(cx, scope, ctor, "split", SymbolKey.SPLIT);
50 createStandardSymbol(cx, scope, ctor, "unscopables", SymbolKey.UNSCOPABLES);
51 } finally {
52 cx.removeThreadLocal(CONSTRUCTOR_SLOT);
53 }
54 }
55
56 private NativeSymbol(String desc) {
57 this.key = new SymbolKey(desc);
58 this.symbolData = this;
59 }
60
61 private NativeSymbol(SymbolKey key) {
62 this.key = key;
63 this.symbolData = this;
64 }
65
66 public NativeSymbol(NativeSymbol s) {
67 this.key = s.key;
68 this.symbolData = s.symbolData;
69 }
70
71 /**
72 * Use this when we need to create symbols internally because of the convoluted way we have to
73 * construct them.
74 */
75 public static NativeSymbol construct(Context cx, Scriptable scope, Object[] args)
76 {
77 cx.putThreadLocal(CONSTRUCTOR_SLOT, Boolean.TRUE);
78 try {
79 return (NativeSymbol)cx.newObject(scope, CLASS_NAME, args);
80 } finally {
81 cx.removeThreadLocal(CONSTRUCTOR_SLOT);
82 }
83 }
84
85 @Override
86 public String getClassName() {
87 return CLASS_NAME;
88 }
89
90 @Override
91 protected void fillConstructorProperties(IdFunctionObject ctor) {
92 super.fillConstructorProperties(ctor);
93 addIdFunctionProperty(ctor, CLASS_NAME, ConstructorId_for, "for", 1);
94 addIdFunctionProperty(ctor, CLASS_NAME, ConstructorId_keyFor, "keyFor", 1);
95 }
96
97 private static void createStandardSymbol(Context cx, Scriptable scope,
98 ScriptableObject ctor,
99 String name, SymbolKey key)
100 {
101 Scriptable sym = cx.newObject(scope, CLASS_NAME, new Object[] { name, key });
102 ctor.defineProperty(name, sym, DONTENUM | READONLY | PERMANENT);
103 }
104
105 // #string_id_map#
106
107 @Override
108 protected int findPrototypeId(String s) {
109 int id = 0;
110 // #generated# Last update: 2016-01-26 16:39:41 PST
111 L0: { id = 0; String X = null;
112 int s_length = s.length();
113 if (s_length==7) { X="valueOf";id=Id_valueOf; }
114 else if (s_length==8) { X="toString";id=Id_toString; }
115 else if (s_length==11) { X="constructor";id=Id_constructor; }
116 if (X!=null && X!=s && !X.equals(s)) id = 0;
117 break L0;
118 }
119 // #/generated#
120 return id;
121 }
122
123 @Override
124 protected int findPrototypeId(Symbol key) {
125 if (SymbolKey.TO_STRING_TAG.equals(key)) {
126 return SymbolId_toStringTag;
127 } else if (SymbolKey.TO_PRIMITIVE.equals(key)) {
128 return SymbolId_toPrimitive;
129 }
130 return 0;
131 }
132
133 private static final int
134 ConstructorId_keyFor = -2,
135 ConstructorId_for = -1,
136 Id_constructor = 1,
137 Id_toString = 2,
138 Id_valueOf = 4,
139 SymbolId_toStringTag = 3,
140 SymbolId_toPrimitive = 5,
141 MAX_PROTOTYPE_ID = SymbolId_toPrimitive;
142
143 // #/string_id_map#
144
145
146 @Override
147 protected void initPrototypeId(int id)
148 {
149 String s = null;
150 int arity = -1;
151 switch (id) {
152 case Id_constructor:
153 initPrototypeMethod(CLASS_NAME, id, "constructor", 1);
154 break;
155 case Id_toString:
156 initPrototypeMethod(CLASS_NAME, id, "toString", 0);
157 break;
158 case Id_valueOf:
159 initPrototypeMethod(CLASS_NAME, id, "valueOf", 0);
160 break;
161 case SymbolId_toStringTag:
162 initPrototypeValue(id, SymbolKey.TO_STRING_TAG, CLASS_NAME, DONTENUM | READONLY);
163 break;
164 case SymbolId_toPrimitive:
165 initPrototypeMethod(CLASS_NAME, id, SymbolKey.TO_PRIMITIVE, "Symbol.toPrimitive", 1);
166 break;
167 default:
168 super.initPrototypeId(id);
169 break;
170 }
171 }
172
173 @Override
174 public Object execIdCall(IdFunctionObject f, Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
175 if (!f.hasTag(CLASS_NAME)) {
176 return super.execIdCall(f, cx, scope, thisObj, args);
177 }
178 int id = f.methodId();
179 switch (id) {
180 case ConstructorId_for:
181 return js_for(cx, scope, args);
182 case ConstructorId_keyFor:
183 return js_keyFor(cx, scope, args);
184
185 case Id_constructor:
186 if (thisObj == null) {
187 if (cx.getThreadLocal(CONSTRUCTOR_SLOT) == null) {
188 // We should never get to this via "new".
189 throw ScriptRuntime.typeError0("msg.no.symbol.new");
190 } else {
191 // Unless we are being called by our own internal "new"
192 return js_constructor(args);
193 }
194 } else {
195 return construct(cx, scope, args);
196 }
197
198 case Id_toString:
199 return getSelf(thisObj).toString();
200 case Id_valueOf:
201 case SymbolId_toPrimitive:
202 return getSelf(thisObj).js_valueOf();
203 default:
204 return super.execIdCall(f, cx, scope, thisObj, args);
205 }
206 }
207
208 private NativeSymbol getSelf(Object thisObj) {
209 try {
210 return (NativeSymbol)thisObj;
211 } catch (ClassCastException cce) {
212 throw ScriptRuntime.typeError1("msg.invalid.type", thisObj.getClass().getName());
213 }
214 }
215
216 private static NativeSymbol js_constructor(Object[] args) {
217 String desc;
218 if (args.length > 0) {
219 if (Undefined.instance.equals(args[0])) {
220 desc = "";
221 } else {
222 desc = ScriptRuntime.toString(args[0]);
223 }
224 } else {
225 desc = "";
226 }
227
228 if (args.length > 1) {
229 return new NativeSymbol((SymbolKey) args[1]);
230 }
231
232 return new NativeSymbol(desc);
233 }
234
235 private Object js_valueOf() {
236 // In the case that "Object()" was called we actually have a different "internal slot"
237 return symbolData;
238 }
239
240 private Object js_for(Context cx, Scriptable scope, Object[] args) {
241 String name = (args.length > 0 ? ScriptRuntime.toString(args[0]) : ScriptRuntime.toString(Undefined.instance));
242
243 Map<String, NativeSymbol> table = getGlobalMap();
244 NativeSymbol ret = table.get(name);
245
246 if (ret == null) {
247 ret = construct(cx, scope, new Object[]{name});
248 table.put(name, ret);
249 }
250 return ret;
251 }
252
253 private Object js_keyFor(Context cx, Scriptable scope, Object[] args) {
254 Object s = (args.length > 0 ? args[0] : Undefined.instance);
255 if (!(s instanceof NativeSymbol)) {
256 throw ScriptRuntime.throwCustomError(cx, scope, "TypeError", "Not a Symbol");
257 }
258 NativeSymbol sym = (NativeSymbol)s;
259
260 Map<String, NativeSymbol> table = getGlobalMap();
261 for (Map.Entry<String, NativeSymbol> e : table.entrySet()) {
262 if (e.getValue().key == sym.key) {
263 return e.getKey();
264 }
265 }
266 return Undefined.instance;
267 }
268
269 @Override
270 public String toString() {
271 return key.toString();
272 }
273
274 // Symbol objects have a special property that one cannot add properties.
275
276 @Override
277 public void put(String name, Scriptable start, Object value)
278 {
279 if (!isSymbol()) {
280 super.put(name, start, value);
281 } else if (Context.getCurrentContext().isStrictMode()) {
282 throw ScriptRuntime.typeError0("msg.no.assign.symbol.strict");
283 }
284 }
285
286 @Override
287 public void put(int index, Scriptable start, Object value)
288 {
289 if (!isSymbol()) {
290 super.put(index, start, value);
291 } else if (Context.getCurrentContext().isStrictMode()) {
292 throw ScriptRuntime.typeError0("msg.no.assign.symbol.strict");
293 }
294 }
295
296 @Override
297 public void put(Symbol key, Scriptable start, Object value)
298 {
299 if (!isSymbol()) {
300 super.put(key, start, value);
301 } else if (Context.getCurrentContext().isStrictMode()) {
302 throw ScriptRuntime.typeError0("msg.no.assign.symbol.strict");
303 }
304 }
305
306 /**
307 * Object() on a Symbol constructs an object which is NOT a symbol, but which has an "internal data slot"
308 * that is. Furthermore, such an object has the Symbol prototype so this particular object is still used.
309 * Account for that here: an "Object" that was created from a Symbol has a different value of the slot.
310 */
311 public boolean isSymbol()
312 {
313 return (symbolData == this);
314 }
315
316 @Override
317 public String getTypeOf()
318 {
319 return (isSymbol() ? TYPE_NAME : super.getTypeOf());
320 }
321
322 @Override
323 public int hashCode() {
324 return key.hashCode();
325 }
326
327 @Override
328 public boolean equals(Object x) {
329 return key.equals(x);
330 }
331
332 SymbolKey getKey() {
333 return key;
334 }
335
336 private Map<String, NativeSymbol> getGlobalMap() {
337 ScriptableObject top = (ScriptableObject)getTopLevelScope(this);
338 Map<String, NativeSymbol> map = (Map<String, NativeSymbol>)top.getAssociatedValue(GLOBAL_TABLE_KEY);
339 if (map == null) {
340 map = new HashMap<String, NativeSymbol>();
341 top.associateValue(GLOBAL_TABLE_KEY, map);
342 }
343 return map;
344 }
345 }