Skip to content

Commit

Permalink
[Fix] properly implement algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Dec 18, 2023
1 parent 2ce4f30 commit 2d27685
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 41 deletions.
62 changes: 24 additions & 38 deletions implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ 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');
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
Expand All @@ -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;
}
Expand All @@ -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
};
5 changes: 2 additions & 3 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 2d27685

Please sign in to comment.