diff --git a/implementation.js b/implementation.js index d56379b..c6369d0 100644 --- a/implementation.js +++ b/implementation.js @@ -13,11 +13,13 @@ var GetIteratorFromMethod = require('es-abstract/2023/GetIteratorFromMethod'); var GetSetRecord = require('./aos/GetSetRecord'); var IteratorStep = require('es-abstract/2023/IteratorStep'); var IteratorValue = require('es-abstract/2023/IteratorValue'); -// var SetDataHas = require('./aos/SetDataHas'); +var SetDataHas = require('./aos/SetDataHas'); var ToBoolean = require('es-abstract/2023/ToBoolean'); -// var forEach = require('es-abstract/helpers/forEach'); +var forEach = require('es-abstract/helpers/forEach'); +var callBind = require('call-bind'); +var callBound = require('call-bind/callBound'); var isSet = require('is-set'); var tools = require('es-set/tools'); @@ -25,10 +27,8 @@ var $setForEach = tools.forEach; var $setAdd = tools.add; var $setSize = tools.size; -/* var $push = callBound('Array.prototype.push'); -var $sort = callBound('Array.prototype.sort'); -*/ +var $setHas = callBind($Set.prototype.has); module.exports = function intersection(other) { var O = this; // step 1 @@ -40,41 +40,33 @@ module.exports = function intersection(other) { var otherRec = GetSetRecord(other); // step 3 - // var resultSetData = []; // step 4 + var resultSetData = []; // step 4 var thisSize = $setSize(O); // step 5 - var result = new $Set(); - - // if (thisSize <= otherRec['[[Size]]']) { // step 6 - $setForEach(O, function (e) { - var inOther = ToBoolean(Call(otherRec['[[Has]]'], otherRec['[[Set]]'], [e])); // step 6.b.iii.1 - if (!inOther && e === 0) { - inOther = ToBoolean(Call(otherRec['[[Has]]'], otherRec['[[Set]]'], [-e])); - } - if (inOther) { // step 6.b.iii.2 - // var alreadyInResult = SetDataHas(resultSetData, e); // step 6.b.iii.2.b - // if (alreadyInResult) { // step 6.b.iii.2.c - // $push(resultSetData, e); // step 6.b.iii.c.i - $setAdd(result, e); // step 6.b.iii.c.i - // } - } - }); - - // return result; // this is an optimization to avoid iterating `resultSetData` - /* - // eslint-disable-next-line no-else-return + if (thisSize <= otherRec['[[Size]]']) { // step 6 + var index = 0; // step 6.a + $setForEach(O, function (e) { + if (index < thisSize) { // step 6.b + index += 1; // step 6.b.ii + var inOther = ToBoolean(Call(otherRec['[[Has]]'], otherRec['[[Set]]'], [e])); // step 6.b.iii.1 + if (inOther) { // step 6.b.iii.2 + var alreadyInResult = SetDataHas(resultSetData, e); // step 6.b.iii.2.b + if (!alreadyInResult) { // step 6.b.iii.2.c + $push(resultSetData, e); // step 6.b.iii.c.i + thisSize += 1; + } + } + } + }); } else { // step 7 - */ - if (thisSize > otherRec['[[Size]]']) { var keysIter = GetIteratorFromMethod(otherRec['[[Set]]'], otherRec['[[Keys]]']); // step 7.a var next = true; // step 7.b while (next) { // step 7.c next = IteratorStep(keysIter); // step 7.c.i if (next) { // step 7.c.ii - // var nextValue = IteratorValue(next); // step 7.c.ii.1 - IteratorValue(next); - /* + var nextValue = IteratorValue(next); // step 7.c.ii.1 + if (nextValue === 0) { // step 7.c.ii.2 nextValue = +0; } @@ -83,23 +75,17 @@ module.exports = function intersection(other) { if (!alreadyInResult && inThis) { // step 7.c.ii.6 $push(resultSetData, nextValue); // step 7.c.ii.6.a } - */ } } - /* - $sort(resultSetData, function (a, b) { // step 7.e - }); - */ } // var result = OrdinaryObjectCreate(%Set.prototype%, « [[SetData]] »); // step 8 + var result = new $Set(); // result.[[SetData]] = resultSetData; // step 9 - /* forEach(resultSetData, function (e) { $setAdd(result, e); }); - */ return result; // step 10 }; diff --git a/test/tests.js b/test/tests.js index e8d465d..6ab5f31 100644 --- a/test/tests.js +++ b/test/tests.js @@ -343,10 +343,9 @@ module.exports = function (intersection, t) { t.test('test262: test/built-ins/Set/prototype/intersection/converts-negative-zero', function (st) { var setlikeWithMinusZero = { size: 1, - has: function (x) { + has: function () { // impossible to avoid this call since we do not have internal set data access - // throw new EvalError('Set.prototype.intersection should not invoke .has on its argument when this.size > arg.size'); - return debug(x) === '-0'; + throw new EvalError('Set.prototype.intersection should not invoke .has on its argument when this.size > arg.size'); }, keys: function () { var done = false;