OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 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 15 matching lines...) Expand all Loading... |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 "use strict"; | 28 "use strict"; |
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 $Set = global.Set; | 34 var $Set = global.Set; |
35 var $Map = global.Map; | 35 var $Map = global.Map; |
36 var $WeakMap = global.WeakMap; | |
37 var $WeakSet = global.WeakSet; | |
38 | 36 |
39 // Global sentinel to be used instead of undefined keys, which are not | 37 // Global sentinel to be used instead of undefined keys, which are not |
40 // supported internally but required for Harmony sets and maps. | 38 // supported internally but required for Harmony sets and maps. |
41 var undefined_sentinel = {}; | 39 var undefined_sentinel = {}; |
42 | 40 |
43 | 41 |
44 // Map and Set uses SameValueZero which means that +0 and -0 should be treated | 42 // Map and Set uses SameValueZero which means that +0 and -0 should be treated |
45 // as the same value. | 43 // as the same value. |
46 function NormalizeKey(key) { | 44 function NormalizeKey(key) { |
47 if (IS_UNDEFINED(key)) { | 45 if (IS_UNDEFINED(key)) { |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 InstallFunctions($Map.prototype, DONT_ENUM, $Array( | 221 InstallFunctions($Map.prototype, DONT_ENUM, $Array( |
224 "get", MapGet, | 222 "get", MapGet, |
225 "set", MapSet, | 223 "set", MapSet, |
226 "has", MapHas, | 224 "has", MapHas, |
227 "delete", MapDelete, | 225 "delete", MapDelete, |
228 "clear", MapClear | 226 "clear", MapClear |
229 )); | 227 )); |
230 } | 228 } |
231 | 229 |
232 SetUpMap(); | 230 SetUpMap(); |
233 | |
234 | |
235 // ------------------------------------------------------------------- | |
236 // Harmony WeakMap | |
237 | |
238 function WeakMapConstructor() { | |
239 if (%_IsConstructCall()) { | |
240 %WeakCollectionInitialize(this); | |
241 } else { | |
242 throw MakeTypeError('constructor_not_function', ['WeakMap']); | |
243 } | |
244 } | |
245 | |
246 | |
247 function WeakMapGet(key) { | |
248 if (!IS_WEAKMAP(this)) { | |
249 throw MakeTypeError('incompatible_method_receiver', | |
250 ['WeakMap.prototype.get', this]); | |
251 } | |
252 if (!(IS_SPEC_OBJECT(key) || IS_SYMBOL(key))) { | |
253 throw %MakeTypeError('invalid_weakmap_key', [this, key]); | |
254 } | |
255 return %WeakCollectionGet(this, key); | |
256 } | |
257 | |
258 | |
259 function WeakMapSet(key, value) { | |
260 if (!IS_WEAKMAP(this)) { | |
261 throw MakeTypeError('incompatible_method_receiver', | |
262 ['WeakMap.prototype.set', this]); | |
263 } | |
264 if (!(IS_SPEC_OBJECT(key) || IS_SYMBOL(key))) { | |
265 throw %MakeTypeError('invalid_weakmap_key', [this, key]); | |
266 } | |
267 return %WeakCollectionSet(this, key, value); | |
268 } | |
269 | |
270 | |
271 function WeakMapHas(key) { | |
272 if (!IS_WEAKMAP(this)) { | |
273 throw MakeTypeError('incompatible_method_receiver', | |
274 ['WeakMap.prototype.has', this]); | |
275 } | |
276 if (!(IS_SPEC_OBJECT(key) || IS_SYMBOL(key))) { | |
277 throw %MakeTypeError('invalid_weakmap_key', [this, key]); | |
278 } | |
279 return %WeakCollectionHas(this, key); | |
280 } | |
281 | |
282 | |
283 function WeakMapDelete(key) { | |
284 if (!IS_WEAKMAP(this)) { | |
285 throw MakeTypeError('incompatible_method_receiver', | |
286 ['WeakMap.prototype.delete', this]); | |
287 } | |
288 if (!(IS_SPEC_OBJECT(key) || IS_SYMBOL(key))) { | |
289 throw %MakeTypeError('invalid_weakmap_key', [this, key]); | |
290 } | |
291 return %WeakCollectionDelete(this, key); | |
292 } | |
293 | |
294 | |
295 function WeakMapClear() { | |
296 if (!IS_WEAKMAP(this)) { | |
297 throw MakeTypeError('incompatible_method_receiver', | |
298 ['WeakMap.prototype.clear', this]); | |
299 } | |
300 // Replace the internal table with a new empty table. | |
301 %WeakCollectionInitialize(this); | |
302 } | |
303 | |
304 | |
305 // ------------------------------------------------------------------- | |
306 | |
307 function SetUpWeakMap() { | |
308 %CheckIsBootstrapping(); | |
309 | |
310 %SetCode($WeakMap, WeakMapConstructor); | |
311 %FunctionSetPrototype($WeakMap, new $Object()); | |
312 %SetProperty($WeakMap.prototype, "constructor", $WeakMap, DONT_ENUM); | |
313 | |
314 // Set up the non-enumerable functions on the WeakMap prototype object. | |
315 InstallFunctions($WeakMap.prototype, DONT_ENUM, $Array( | |
316 "get", WeakMapGet, | |
317 "set", WeakMapSet, | |
318 "has", WeakMapHas, | |
319 "delete", WeakMapDelete, | |
320 "clear", WeakMapClear | |
321 )); | |
322 } | |
323 | |
324 SetUpWeakMap(); | |
325 | |
326 | |
327 // ------------------------------------------------------------------- | |
328 // Harmony WeakSet | |
329 | |
330 function WeakSetConstructor() { | |
331 if (%_IsConstructCall()) { | |
332 %WeakCollectionInitialize(this); | |
333 } else { | |
334 throw MakeTypeError('constructor_not_function', ['WeakSet']); | |
335 } | |
336 } | |
337 | |
338 | |
339 function WeakSetAdd(value) { | |
340 if (!IS_WEAKSET(this)) { | |
341 throw MakeTypeError('incompatible_method_receiver', | |
342 ['WeakSet.prototype.add', this]); | |
343 } | |
344 if (!(IS_SPEC_OBJECT(value) || IS_SYMBOL(value))) { | |
345 throw %MakeTypeError('invalid_weakset_value', [this, value]); | |
346 } | |
347 return %WeakCollectionSet(this, value, true); | |
348 } | |
349 | |
350 | |
351 function WeakSetHas(value) { | |
352 if (!IS_WEAKSET(this)) { | |
353 throw MakeTypeError('incompatible_method_receiver', | |
354 ['WeakSet.prototype.has', this]); | |
355 } | |
356 if (!(IS_SPEC_OBJECT(value) || IS_SYMBOL(value))) { | |
357 throw %MakeTypeError('invalid_weakset_value', [this, value]); | |
358 } | |
359 return %WeakCollectionHas(this, value); | |
360 } | |
361 | |
362 | |
363 function WeakSetDelete(value) { | |
364 if (!IS_WEAKSET(this)) { | |
365 throw MakeTypeError('incompatible_method_receiver', | |
366 ['WeakSet.prototype.delete', this]); | |
367 } | |
368 if (!(IS_SPEC_OBJECT(value) || IS_SYMBOL(value))) { | |
369 throw %MakeTypeError('invalid_weakset_value', [this, value]); | |
370 } | |
371 return %WeakCollectionDelete(this, value); | |
372 } | |
373 | |
374 | |
375 function WeakSetClear() { | |
376 if (!IS_WEAKSET(this)) { | |
377 throw MakeTypeError('incompatible_method_receiver', | |
378 ['WeakSet.prototype.clear', this]); | |
379 } | |
380 // Replace the internal table with a new empty table. | |
381 %WeakCollectionInitialize(this); | |
382 } | |
383 | |
384 | |
385 // ------------------------------------------------------------------- | |
386 | |
387 function SetUpWeakSet() { | |
388 %CheckIsBootstrapping(); | |
389 | |
390 %SetCode($WeakSet, WeakSetConstructor); | |
391 %FunctionSetPrototype($WeakSet, new $Object()); | |
392 %SetProperty($WeakSet.prototype, "constructor", $WeakSet, DONT_ENUM); | |
393 | |
394 // Set up the non-enumerable functions on the WeakSet prototype object. | |
395 InstallFunctions($WeakSet.prototype, DONT_ENUM, $Array( | |
396 "add", WeakSetAdd, | |
397 "has", WeakSetHas, | |
398 "delete", WeakSetDelete, | |
399 "clear", WeakSetClear | |
400 )); | |
401 } | |
402 | |
403 SetUpWeakSet(); | |
OLD | NEW |