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
|
|
|
|
2011-10-25 14:14:56 +00:00
|
|
|
function SetConstructor() {
|
|
|
|
if (%_IsConstructCall()) {
|
|
|
|
%SetInitialize(this);
|
|
|
|
} else {
|
2013-08-01 09:18:28 +00:00
|
|
|
throw MakeTypeError('constructor_not_function', ['Set']);
|
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-05-21 08:47:02 +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-05-21 08:47:02 +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]);
|
|
|
|
}
|
2012-06-19 15:23:03 +00:00
|
|
|
if (%SetHas(this, key)) {
|
|
|
|
%SetDelete(this, key);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
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]);
|
|
|
|
}
|
|
|
|
return %SetGetSize(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-04-17 17:45:32 +00:00
|
|
|
%SetClear(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
|
|
|
|
var iterator = %SetCreateIterator(this, ITERATOR_KIND_VALUES);
|
|
|
|
var entry;
|
2014-05-20 14:22:05 +00:00
|
|
|
while (!(entry = %SetIteratorNext(iterator)).done) {
|
|
|
|
%_CallFunction(receiver, entry.value, entry.value, 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());
|
2013-04-11 12:15:25 +00:00
|
|
|
%SetProperty($Set.prototype, "constructor", $Set, DONT_ENUM);
|
|
|
|
|
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
|
|
|
|
|
2011-10-25 14:14:56 +00:00
|
|
|
function MapConstructor() {
|
|
|
|
if (%_IsConstructCall()) {
|
|
|
|
%MapInitialize(this);
|
|
|
|
} else {
|
2013-08-01 09:18:28 +00:00
|
|
|
throw MakeTypeError('constructor_not_function', ['Map']);
|
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-05-21 08:47:02 +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-05-21 08:47:02 +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-05-21 08:47:02 +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-05-21 08:47:02 +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]);
|
|
|
|
}
|
|
|
|
return %MapGetSize(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-04-17 17:45:32 +00:00
|
|
|
%MapClear(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
|
|
|
|
var iterator = %MapCreateIterator(this, ITERATOR_KIND_ENTRIES);
|
|
|
|
var entry;
|
2014-05-20 14:22:05 +00:00
|
|
|
while (!(entry = %MapIteratorNext(iterator)).done) {
|
|
|
|
%_CallFunction(receiver, entry.value[1], entry.value[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());
|
2013-04-11 12:15:25 +00:00
|
|
|
%SetProperty($Map.prototype, "constructor", $Map, DONT_ENUM);
|
|
|
|
|
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();
|