2012-06-25 13:33:48 +00:00
|
|
|
// Copyright 2012 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2011-08-03 11:55:13 +00:00
|
|
|
|
2012-02-20 13:48:24 +00:00
|
|
|
"use strict";
|
2011-08-03 11:55:13 +00:00
|
|
|
|
2013-04-11 12:15:25 +00:00
|
|
|
// This file relies on the fact that the following declaration has been made
|
|
|
|
// in runtime.js:
|
|
|
|
// var $Array = global.Array;
|
|
|
|
|
2012-02-20 13:48:24 +00:00
|
|
|
var $Set = global.Set;
|
|
|
|
var $Map = global.Map;
|
2011-08-03 11:55:13 +00:00
|
|
|
|
2014-01-29 07:27:05 +00:00
|
|
|
|
2013-04-11 12:15:25 +00:00
|
|
|
// -------------------------------------------------------------------
|
|
|
|
// Harmony Set
|
2011-11-03 14:33:58 +00:00
|
|
|
|
2014-06-23 18:05:57 +00:00
|
|
|
function SetConstructor(iterable) {
|
|
|
|
if (!%_IsConstructCall()) {
|
2013-08-01 09:18:28 +00:00
|
|
|
throw MakeTypeError('constructor_not_function', ['Set']);
|
2011-10-25 14:14:56 +00:00
|
|
|
}
|
2014-06-23 18:05:57 +00:00
|
|
|
|
2015-02-24 19:12:35 +00:00
|
|
|
%_SetInitialize(this);
|
2014-06-23 18:05:57 +00:00
|
|
|
|
|
|
|
if (!IS_NULL_OR_UNDEFINED(iterable)) {
|
2015-02-24 19:12:35 +00:00
|
|
|
var adder = this.add;
|
2014-06-23 18:05:57 +00:00
|
|
|
if (!IS_SPEC_FUNCTION(adder)) {
|
|
|
|
throw MakeTypeError('property_not_function', ['add', this]);
|
|
|
|
}
|
|
|
|
|
2015-02-24 19:12:35 +00:00
|
|
|
for (var value of iterable) {
|
|
|
|
%_CallFunction(this, value, adder);
|
2014-06-23 18:05:57 +00:00
|
|
|
}
|
|
|
|
}
|
2011-10-25 14:14:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-14 08:51:10 +00:00
|
|
|
function SetAddJS(key) {
|
2012-01-05 12:55:06 +00:00
|
|
|
if (!IS_SET(this)) {
|
|
|
|
throw MakeTypeError('incompatible_method_receiver',
|
|
|
|
['Set.prototype.add', this]);
|
|
|
|
}
|
2014-08-19 15:15:41 +00:00
|
|
|
// Normalize -0 to +0 as required by the spec.
|
|
|
|
// Even though we use SameValueZero as the comparison for the keys we don't
|
|
|
|
// want to ever store -0 as the key since the key is directly exposed when
|
|
|
|
// doing iteration.
|
|
|
|
if (key === 0) {
|
|
|
|
key = 0;
|
|
|
|
}
|
2014-12-08 18:03:28 +00:00
|
|
|
return %_SetAdd(this, key);
|
2011-10-25 14:14:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-14 08:51:10 +00:00
|
|
|
function SetHasJS(key) {
|
2012-01-05 12:55:06 +00:00
|
|
|
if (!IS_SET(this)) {
|
|
|
|
throw MakeTypeError('incompatible_method_receiver',
|
|
|
|
['Set.prototype.has', this]);
|
|
|
|
}
|
2014-12-02 23:06:44 +00:00
|
|
|
return %_SetHas(this, key);
|
2011-10-25 14:14:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-14 08:51:10 +00:00
|
|
|
function SetDeleteJS(key) {
|
2012-01-05 12:55:06 +00:00
|
|
|
if (!IS_SET(this)) {
|
|
|
|
throw MakeTypeError('incompatible_method_receiver',
|
|
|
|
['Set.prototype.delete', this]);
|
|
|
|
}
|
2014-12-08 18:03:28 +00:00
|
|
|
return %_SetDelete(this, key);
|
2011-10-25 14:14:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-14 08:51:10 +00:00
|
|
|
function SetGetSizeJS() {
|
2012-11-06 18:14:45 +00:00
|
|
|
if (!IS_SET(this)) {
|
|
|
|
throw MakeTypeError('incompatible_method_receiver',
|
|
|
|
['Set.prototype.size', this]);
|
|
|
|
}
|
2014-12-02 23:06:44 +00:00
|
|
|
return %_SetGetSize(this);
|
2012-11-06 18:14:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-14 08:51:10 +00:00
|
|
|
function SetClearJS() {
|
2012-11-09 09:01:29 +00:00
|
|
|
if (!IS_SET(this)) {
|
|
|
|
throw MakeTypeError('incompatible_method_receiver',
|
|
|
|
['Set.prototype.clear', this]);
|
|
|
|
}
|
2014-12-10 22:16:12 +00:00
|
|
|
%_SetClear(this);
|
2014-04-17 17:45:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function SetForEach(f, receiver) {
|
|
|
|
if (!IS_SET(this)) {
|
|
|
|
throw MakeTypeError('incompatible_method_receiver',
|
|
|
|
['Set.prototype.forEach', this]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!IS_SPEC_FUNCTION(f)) {
|
|
|
|
throw MakeTypeError('called_non_callable', [f]);
|
|
|
|
}
|
Array.prototype.{every, filter, find, findIndex, forEach, map, some}: Use fresh primitive wrapper for calls.
When the receiver is a primitive value, it's cast to an Object before entering the loop. Instead, it should be cast to an Object for each function call while in the loop.
BUG=v8:3536
LOG=Y
R=arv@chromium.org, svenpanne@chromium.org, wingo@igalia.com
Review URL: https://codereview.chromium.org/553413002
Patch from Diego Pino <dpino@igalia.com>.
git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@24620 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2014-10-15 09:11:32 +00:00
|
|
|
var needs_wrapper = false;
|
|
|
|
if (IS_NULL_OR_UNDEFINED(receiver)) {
|
|
|
|
receiver = %GetDefaultReceiver(f) || receiver;
|
|
|
|
} else {
|
|
|
|
needs_wrapper = SHOULD_CREATE_WRAPPER(f, receiver);
|
|
|
|
}
|
2014-04-17 17:45:32 +00:00
|
|
|
|
2014-06-03 00:34:01 +00:00
|
|
|
var iterator = new SetIterator(this, ITERATOR_KIND_VALUES);
|
2014-06-26 00:40:45 +00:00
|
|
|
var key;
|
2014-06-23 07:10:25 +00:00
|
|
|
var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(f);
|
2014-06-26 00:40:45 +00:00
|
|
|
var value_array = [UNDEFINED];
|
|
|
|
while (%SetIteratorNext(iterator, value_array)) {
|
2014-05-21 09:25:50 +00:00
|
|
|
if (stepping) %DebugPrepareStepInIfStepping(f);
|
2014-06-26 00:40:45 +00:00
|
|
|
key = value_array[0];
|
Array.prototype.{every, filter, find, findIndex, forEach, map, some}: Use fresh primitive wrapper for calls.
When the receiver is a primitive value, it's cast to an Object before entering the loop. Instead, it should be cast to an Object for each function call while in the loop.
BUG=v8:3536
LOG=Y
R=arv@chromium.org, svenpanne@chromium.org, wingo@igalia.com
Review URL: https://codereview.chromium.org/553413002
Patch from Diego Pino <dpino@igalia.com>.
git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@24620 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2014-10-15 09:11:32 +00:00
|
|
|
var new_receiver = needs_wrapper ? ToObject(receiver) : receiver;
|
|
|
|
%_CallFunction(new_receiver, key, key, this, f);
|
2014-04-17 17:45:32 +00:00
|
|
|
}
|
2012-11-09 09:01:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-11 12:15:25 +00:00
|
|
|
// -------------------------------------------------------------------
|
|
|
|
|
|
|
|
function SetUpSet() {
|
|
|
|
%CheckIsBootstrapping();
|
|
|
|
|
|
|
|
%SetCode($Set, SetConstructor);
|
2013-04-11 13:31:51 +00:00
|
|
|
%FunctionSetPrototype($Set, new $Object());
|
2014-07-14 14:05:30 +00:00
|
|
|
%AddNamedProperty($Set.prototype, "constructor", $Set, DONT_ENUM);
|
2014-10-21 17:21:32 +00:00
|
|
|
%AddNamedProperty(
|
|
|
|
$Set.prototype, symbolToStringTag, "Set", DONT_ENUM | READ_ONLY);
|
2013-04-11 12:15:25 +00:00
|
|
|
|
2014-04-17 17:45:32 +00:00
|
|
|
%FunctionSetLength(SetForEach, 1);
|
|
|
|
|
2013-04-11 12:15:25 +00:00
|
|
|
// Set up the non-enumerable functions on the Set prototype object.
|
2014-05-14 08:51:10 +00:00
|
|
|
InstallGetter($Set.prototype, "size", SetGetSizeJS);
|
2013-04-11 12:15:25 +00:00
|
|
|
InstallFunctions($Set.prototype, DONT_ENUM, $Array(
|
2014-05-14 08:51:10 +00:00
|
|
|
"add", SetAddJS,
|
|
|
|
"has", SetHasJS,
|
|
|
|
"delete", SetDeleteJS,
|
|
|
|
"clear", SetClearJS,
|
2014-04-17 17:45:32 +00:00
|
|
|
"forEach", SetForEach
|
2013-04-11 12:15:25 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
SetUpSet();
|
|
|
|
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------
|
|
|
|
// Harmony Map
|
|
|
|
|
2014-06-23 18:05:57 +00:00
|
|
|
function MapConstructor(iterable) {
|
|
|
|
if (!%_IsConstructCall()) {
|
2013-08-01 09:18:28 +00:00
|
|
|
throw MakeTypeError('constructor_not_function', ['Map']);
|
2011-10-25 14:14:56 +00:00
|
|
|
}
|
2014-06-23 18:05:57 +00:00
|
|
|
|
2015-02-24 19:12:35 +00:00
|
|
|
%_MapInitialize(this);
|
2014-06-23 18:05:57 +00:00
|
|
|
|
|
|
|
if (!IS_NULL_OR_UNDEFINED(iterable)) {
|
2015-02-24 19:12:35 +00:00
|
|
|
var adder = this.set;
|
2014-06-23 18:05:57 +00:00
|
|
|
if (!IS_SPEC_FUNCTION(adder)) {
|
|
|
|
throw MakeTypeError('property_not_function', ['set', this]);
|
|
|
|
}
|
|
|
|
|
2015-02-24 19:12:35 +00:00
|
|
|
for (var nextItem of iterable) {
|
|
|
|
if (!IS_SPEC_OBJECT(nextItem)) {
|
|
|
|
throw MakeTypeError('iterator_value_not_an_object', [nextItem]);
|
|
|
|
}
|
|
|
|
%_CallFunction(this, nextItem[0], nextItem[1], adder);
|
2014-06-23 18:05:57 +00:00
|
|
|
}
|
|
|
|
}
|
2011-10-25 14:14:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-14 08:51:10 +00:00
|
|
|
function MapGetJS(key) {
|
2012-01-05 12:55:06 +00:00
|
|
|
if (!IS_MAP(this)) {
|
|
|
|
throw MakeTypeError('incompatible_method_receiver',
|
|
|
|
['Map.prototype.get', this]);
|
|
|
|
}
|
2014-12-02 23:06:44 +00:00
|
|
|
return %_MapGet(this, key);
|
2011-10-25 14:14:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-14 08:51:10 +00:00
|
|
|
function MapSetJS(key, value) {
|
2012-01-05 12:55:06 +00:00
|
|
|
if (!IS_MAP(this)) {
|
|
|
|
throw MakeTypeError('incompatible_method_receiver',
|
|
|
|
['Map.prototype.set', this]);
|
|
|
|
}
|
2014-08-19 15:15:41 +00:00
|
|
|
// Normalize -0 to +0 as required by the spec.
|
|
|
|
// Even though we use SameValueZero as the comparison for the keys we don't
|
|
|
|
// want to ever store -0 as the key since the key is directly exposed when
|
|
|
|
// doing iteration.
|
|
|
|
if (key === 0) {
|
|
|
|
key = 0;
|
|
|
|
}
|
2014-12-08 18:03:28 +00:00
|
|
|
return %_MapSet(this, key, value);
|
2011-10-25 14:14:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-14 08:51:10 +00:00
|
|
|
function MapHasJS(key) {
|
2012-01-05 12:55:06 +00:00
|
|
|
if (!IS_MAP(this)) {
|
|
|
|
throw MakeTypeError('incompatible_method_receiver',
|
|
|
|
['Map.prototype.has', this]);
|
|
|
|
}
|
2014-12-02 23:06:44 +00:00
|
|
|
return %_MapHas(this, key);
|
2011-10-25 14:14:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-14 08:51:10 +00:00
|
|
|
function MapDeleteJS(key) {
|
2012-01-05 12:55:06 +00:00
|
|
|
if (!IS_MAP(this)) {
|
|
|
|
throw MakeTypeError('incompatible_method_receiver',
|
|
|
|
['Map.prototype.delete', this]);
|
|
|
|
}
|
2014-12-08 18:03:28 +00:00
|
|
|
return %_MapDelete(this, key);
|
2011-10-25 14:14:56 +00:00
|
|
|
}
|
|
|
|
|
2011-08-03 11:55:13 +00:00
|
|
|
|
2014-05-14 08:51:10 +00:00
|
|
|
function MapGetSizeJS() {
|
2012-11-06 18:14:45 +00:00
|
|
|
if (!IS_MAP(this)) {
|
|
|
|
throw MakeTypeError('incompatible_method_receiver',
|
|
|
|
['Map.prototype.size', this]);
|
|
|
|
}
|
2014-12-02 23:06:44 +00:00
|
|
|
return %_MapGetSize(this);
|
2012-11-06 18:14:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-14 08:51:10 +00:00
|
|
|
function MapClearJS() {
|
2012-11-09 09:01:29 +00:00
|
|
|
if (!IS_MAP(this)) {
|
|
|
|
throw MakeTypeError('incompatible_method_receiver',
|
|
|
|
['Map.prototype.clear', this]);
|
|
|
|
}
|
2014-12-10 22:16:12 +00:00
|
|
|
%_MapClear(this);
|
2014-04-17 17:45:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function MapForEach(f, receiver) {
|
|
|
|
if (!IS_MAP(this)) {
|
|
|
|
throw MakeTypeError('incompatible_method_receiver',
|
|
|
|
['Map.prototype.forEach', this]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!IS_SPEC_FUNCTION(f)) {
|
|
|
|
throw MakeTypeError('called_non_callable', [f]);
|
|
|
|
}
|
Array.prototype.{every, filter, find, findIndex, forEach, map, some}: Use fresh primitive wrapper for calls.
When the receiver is a primitive value, it's cast to an Object before entering the loop. Instead, it should be cast to an Object for each function call while in the loop.
BUG=v8:3536
LOG=Y
R=arv@chromium.org, svenpanne@chromium.org, wingo@igalia.com
Review URL: https://codereview.chromium.org/553413002
Patch from Diego Pino <dpino@igalia.com>.
git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@24620 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2014-10-15 09:11:32 +00:00
|
|
|
var needs_wrapper = false;
|
|
|
|
if (IS_NULL_OR_UNDEFINED(receiver)) {
|
|
|
|
receiver = %GetDefaultReceiver(f) || receiver;
|
|
|
|
} else {
|
|
|
|
needs_wrapper = SHOULD_CREATE_WRAPPER(f, receiver);
|
|
|
|
}
|
2014-04-17 17:45:32 +00:00
|
|
|
|
2014-06-03 00:34:01 +00:00
|
|
|
var iterator = new MapIterator(this, ITERATOR_KIND_ENTRIES);
|
2014-06-23 07:10:25 +00:00
|
|
|
var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(f);
|
2014-06-26 00:40:45 +00:00
|
|
|
var value_array = [UNDEFINED, UNDEFINED];
|
|
|
|
while (%MapIteratorNext(iterator, value_array)) {
|
2014-05-21 09:25:50 +00:00
|
|
|
if (stepping) %DebugPrepareStepInIfStepping(f);
|
Array.prototype.{every, filter, find, findIndex, forEach, map, some}: Use fresh primitive wrapper for calls.
When the receiver is a primitive value, it's cast to an Object before entering the loop. Instead, it should be cast to an Object for each function call while in the loop.
BUG=v8:3536
LOG=Y
R=arv@chromium.org, svenpanne@chromium.org, wingo@igalia.com
Review URL: https://codereview.chromium.org/553413002
Patch from Diego Pino <dpino@igalia.com>.
git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@24620 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2014-10-15 09:11:32 +00:00
|
|
|
var new_receiver = needs_wrapper ? ToObject(receiver) : receiver;
|
|
|
|
%_CallFunction(new_receiver, value_array[1], value_array[0], this, f);
|
2014-04-17 17:45:32 +00:00
|
|
|
}
|
2012-11-09 09:01:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-11 12:15:25 +00:00
|
|
|
// -------------------------------------------------------------------
|
|
|
|
|
|
|
|
function SetUpMap() {
|
|
|
|
%CheckIsBootstrapping();
|
|
|
|
|
|
|
|
%SetCode($Map, MapConstructor);
|
2013-04-11 13:31:51 +00:00
|
|
|
%FunctionSetPrototype($Map, new $Object());
|
2014-07-14 14:05:30 +00:00
|
|
|
%AddNamedProperty($Map.prototype, "constructor", $Map, DONT_ENUM);
|
2014-10-21 17:21:32 +00:00
|
|
|
%AddNamedProperty(
|
|
|
|
$Map.prototype, symbolToStringTag, "Map", DONT_ENUM | READ_ONLY);
|
2013-04-11 12:15:25 +00:00
|
|
|
|
2014-04-17 17:45:32 +00:00
|
|
|
%FunctionSetLength(MapForEach, 1);
|
|
|
|
|
2013-04-11 12:15:25 +00:00
|
|
|
// Set up the non-enumerable functions on the Map prototype object.
|
2014-05-14 08:51:10 +00:00
|
|
|
InstallGetter($Map.prototype, "size", MapGetSizeJS);
|
2013-04-11 12:15:25 +00:00
|
|
|
InstallFunctions($Map.prototype, DONT_ENUM, $Array(
|
2014-05-14 08:51:10 +00:00
|
|
|
"get", MapGetJS,
|
|
|
|
"set", MapSetJS,
|
|
|
|
"has", MapHasJS,
|
|
|
|
"delete", MapDeleteJS,
|
|
|
|
"clear", MapClearJS,
|
2014-04-17 17:45:32 +00:00
|
|
|
"forEach", MapForEach
|
2013-04-11 12:15:25 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
SetUpMap();
|