Home | History | Annotate | Download | only in js
      1 // Copyright 2014 the V8 project authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 (function(global, utils) {
      6 
      7 "use strict";
      8 
      9 %CheckIsBootstrapping();
     10 
     11 // -------------------------------------------------------------------
     12 // Imports
     13 
     14 var GlobalMap = global.Map;
     15 var GlobalSet = global.Set;
     16 var iteratorSymbol = utils.ImportNow("iterator_symbol");
     17 var MakeTypeError;
     18 var MapIterator = utils.ImportNow("MapIterator");
     19 var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol");
     20 var SetIterator = utils.ImportNow("SetIterator");
     21 
     22 utils.Import(function(from) {
     23   MakeTypeError = from.MakeTypeError;
     24 });
     25 
     26 // -------------------------------------------------------------------
     27 
     28 function SetIteratorConstructor(set, kind) {
     29   %SetIteratorInitialize(this, set, kind);
     30 }
     31 
     32 
     33 function SetIteratorNextJS() {
     34   if (!IS_SET_ITERATOR(this)) {
     35     throw MakeTypeError(kIncompatibleMethodReceiver,
     36                         'Set Iterator.prototype.next', this);
     37   }
     38 
     39   var value_array = [UNDEFINED, UNDEFINED];
     40   var result = %_CreateIterResultObject(value_array, false);
     41   switch (%SetIteratorNext(this, value_array)) {
     42     case 0:
     43       result.value = UNDEFINED;
     44       result.done = true;
     45       break;
     46     case ITERATOR_KIND_VALUES:
     47       result.value = value_array[0];
     48       break;
     49     case ITERATOR_KIND_ENTRIES:
     50       value_array[1] = value_array[0];
     51       break;
     52   }
     53 
     54   return result;
     55 }
     56 
     57 
     58 function SetEntries() {
     59   if (!IS_SET(this)) {
     60     throw MakeTypeError(kIncompatibleMethodReceiver,
     61                         'Set.prototype.entries', this);
     62   }
     63   return new SetIterator(this, ITERATOR_KIND_ENTRIES);
     64 }
     65 
     66 
     67 function SetValues() {
     68   if (!IS_SET(this)) {
     69     throw MakeTypeError(kIncompatibleMethodReceiver,
     70                         'Set.prototype.values', this);
     71   }
     72   return new SetIterator(this, ITERATOR_KIND_VALUES);
     73 }
     74 
     75 // -------------------------------------------------------------------
     76 
     77 %SetCode(SetIterator, SetIteratorConstructor);
     78 %FunctionSetInstanceClassName(SetIterator, 'Set Iterator');
     79 utils.InstallFunctions(SetIterator.prototype, DONT_ENUM, [
     80   'next', SetIteratorNextJS
     81 ]);
     82 
     83 %AddNamedProperty(SetIterator.prototype, toStringTagSymbol,
     84     "Set Iterator", READ_ONLY | DONT_ENUM);
     85 
     86 utils.InstallFunctions(GlobalSet.prototype, DONT_ENUM, [
     87   'entries', SetEntries,
     88   'keys', SetValues,
     89   'values', SetValues
     90 ]);
     91 
     92 %AddNamedProperty(GlobalSet.prototype, iteratorSymbol, SetValues, DONT_ENUM);
     93 
     94 // -------------------------------------------------------------------
     95 
     96 function MapIteratorConstructor(map, kind) {
     97   %MapIteratorInitialize(this, map, kind);
     98 }
     99 
    100 
    101 function MapIteratorNextJS() {
    102   if (!IS_MAP_ITERATOR(this)) {
    103     throw MakeTypeError(kIncompatibleMethodReceiver,
    104                         'Map Iterator.prototype.next', this);
    105   }
    106 
    107   var value_array = [UNDEFINED, UNDEFINED];
    108   var result = %_CreateIterResultObject(value_array, false);
    109   switch (%MapIteratorNext(this, value_array)) {
    110     case 0:
    111       result.value = UNDEFINED;
    112       result.done = true;
    113       break;
    114     case ITERATOR_KIND_KEYS:
    115       result.value = value_array[0];
    116       break;
    117     case ITERATOR_KIND_VALUES:
    118       result.value = value_array[1];
    119       break;
    120     // ITERATOR_KIND_ENTRIES does not need any processing.
    121   }
    122 
    123   return result;
    124 }
    125 
    126 
    127 function MapEntries() {
    128   if (!IS_MAP(this)) {
    129     throw MakeTypeError(kIncompatibleMethodReceiver,
    130                         'Map.prototype.entries', this);
    131   }
    132   return new MapIterator(this, ITERATOR_KIND_ENTRIES);
    133 }
    134 
    135 
    136 function MapKeys() {
    137   if (!IS_MAP(this)) {
    138     throw MakeTypeError(kIncompatibleMethodReceiver,
    139                         'Map.prototype.keys', this);
    140   }
    141   return new MapIterator(this, ITERATOR_KIND_KEYS);
    142 }
    143 
    144 
    145 function MapValues() {
    146   if (!IS_MAP(this)) {
    147     throw MakeTypeError(kIncompatibleMethodReceiver,
    148                         'Map.prototype.values', this);
    149   }
    150   return new MapIterator(this, ITERATOR_KIND_VALUES);
    151 }
    152 
    153 // -------------------------------------------------------------------
    154 
    155 %SetCode(MapIterator, MapIteratorConstructor);
    156 %FunctionSetInstanceClassName(MapIterator, 'Map Iterator');
    157 utils.InstallFunctions(MapIterator.prototype, DONT_ENUM, [
    158   'next', MapIteratorNextJS
    159 ]);
    160 
    161 %AddNamedProperty(MapIterator.prototype, toStringTagSymbol,
    162     "Map Iterator", READ_ONLY | DONT_ENUM);
    163 
    164 
    165 utils.InstallFunctions(GlobalMap.prototype, DONT_ENUM, [
    166   'entries', MapEntries,
    167   'keys', MapKeys,
    168   'values', MapValues
    169 ]);
    170 
    171 %AddNamedProperty(GlobalMap.prototype, iteratorSymbol, MapEntries, DONT_ENUM);
    172 
    173 // -------------------------------------------------------------------
    174 // Exports
    175 
    176 utils.Export(function(to) {
    177   to.MapEntries = MapEntries;
    178   to.MapIteratorNext = MapIteratorNextJS;
    179   to.SetIteratorNext = SetIteratorNextJS;
    180   to.SetValues = SetValues;
    181 });
    182 
    183 })
    184