Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Breaking] ES2018+: GetIterator, IterableToList, IteratorNext, IteratorStep, IteratorClose: use iterator records instead #68

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,6 @@
2018/DeletePropertyOrThrow.js spackled linguist-generated=true
2018/FromPropertyDescriptor.js spackled linguist-generated=true
2018/Get.js spackled linguist-generated=true
2018/GetIterator.js spackled linguist-generated=true
2018/GetMethod.js spackled linguist-generated=true
2018/GetOwnPropertyKeys.js spackled linguist-generated=true
2018/GetPrototypeFromConstructor.js spackled linguist-generated=true
Expand All @@ -296,11 +295,7 @@
2018/IsPromise.js spackled linguist-generated=true
2018/IsPropertyKey.js spackled linguist-generated=true
2018/IsRegExp.js spackled linguist-generated=true
2018/IterableToList.js spackled linguist-generated=true
2018/IteratorClose.js spackled linguist-generated=true
2018/IteratorComplete.js spackled linguist-generated=true
2018/IteratorNext.js spackled linguist-generated=true
2018/IteratorStep.js spackled linguist-generated=true
2018/IteratorValue.js spackled linguist-generated=true
2018/MakeDate.js spackled linguist-generated=true
2018/MakeDay.js spackled linguist-generated=true
Expand Down
63 changes: 49 additions & 14 deletions 2018/GetIterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,69 @@

var GetIntrinsic = require('../GetIntrinsic');

var $asyncIterator = GetIntrinsic('%Symbol.asyncIterator%', true);
var $SyntaxError = GetIntrinsic('%SyntaxError%');
var $TypeError = GetIntrinsic('%TypeError%');

var getIteratorMethod = require('../helpers/getIteratorMethod');
var AdvanceStringIndex = require('./AdvanceStringIndex');
var Call = require('./Call');
var CreateAsyncFromSyncIterator = require('./CreateAsyncFromSyncIterator');
var GetMethod = require('./GetMethod');
var GetV = require('./GetV');
var IsArray = require('./IsArray');
var Type = require('./Type');

// https://ecma-international.org/ecma-262/6.0/#sec-getiterator

module.exports = function GetIterator(obj, method) {
var actualMethod = method;
if (arguments.length < 2) {
actualMethod = getIteratorMethod(
{
AdvanceStringIndex: AdvanceStringIndex,
GetMethod: GetMethod,
IsArray: IsArray,
Type: Type
},
obj
);
module.exports = function GetIterator(obj) {
var hint = arguments.length > 1 ? arguments[1] : 'sync';
if (hint !== 'sync' && hint !== 'async') {
throw new $TypeError('Assertion failed: `hint` must be either ~sync~ or ~async~');
}
var iterator = Call(actualMethod, obj);
var method;
if (arguments.length < 3) {
if (hint === 'async') {
if (!$asyncIterator) {
method = GetMethod(obj, $asyncIterator);
}
if (typeof method !== 'undefined') {
var syncMethod = getIteratorMethod(
{
AdvanceStringIndex: AdvanceStringIndex,
GetMethod: GetMethod,
IsArray: IsArray,
Type: Type
},
obj
);
var syncIteratorRecord = GetIterator(obj, 'sync', syncMethod);
return CreateAsyncFromSyncIterator(syncIteratorRecord);
}
} else {
method = getIteratorMethod(
{
AdvanceStringIndex: AdvanceStringIndex,
GetMethod: GetMethod,
IsArray: IsArray,
Type: Type
},
obj
);
}
}
var iterator = Call(method, obj);
if (Type(iterator) !== 'Object') {
throw new $TypeError('iterator must return an object');
}

return iterator;
var nextMethod = GetV(iterator, 'next');

var iteratorRecord = {
'[[Iterator]]': iterator,
'[[NextMethod]]': nextMethod,
'[[Done]]': false
};

return iteratorRecord;
};
6 changes: 3 additions & 3 deletions 2018/IterableToList.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ var GetIterator = require('./GetIterator');
var IteratorStep = require('./IteratorStep');
var IteratorValue = require('./IteratorValue');

// https://www.ecma-international.org/ecma-262/8.0/#sec-iterabletolist
// https://www.ecma-international.org/ecma-262/9.0/#sec-iterabletolist

module.exports = function IterableToList(items, method) {
var iterator = GetIterator(items, method);
var iteratorRecord = GetIterator(items, 'sync', method);
var values = [];
var next = true;
while (next) {
next = IteratorStep(iterator);
next = IteratorStep(iteratorRecord);
if (next) {
var nextValue = IteratorValue(next);
$arrayPush(values, nextValue);
Expand Down
7 changes: 4 additions & 3 deletions 2018/IteratorClose.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@ var GetMethod = require('./GetMethod');
var IsCallable = require('./IsCallable');
var Type = require('./Type');

// https://ecma-international.org/ecma-262/6.0/#sec-iteratorclose
// https://ecma-international.org/ecma-262/9.0/#sec-iteratorclose

module.exports = function IteratorClose(iterator, completion) {
if (Type(iterator) !== 'Object') {
module.exports = function IteratorClose(iteratorRecord, completion) {
if (Type(iteratorRecord['[[Iterator]]']) !== 'Object') {
throw new $TypeError('Assertion failed: Type(iterator) is not Object');
}
if (!IsCallable(completion)) {
throw new $TypeError('Assertion failed: completion is not a thunk for a Completion Record');
}
var completionThunk = completion;

var iterator = iteratorRecord['[[Iterator]]'];
var iteratorReturn = GetMethod(iterator, 'return');

if (typeof iteratorReturn === 'undefined') {
Expand Down
14 changes: 10 additions & 4 deletions 2018/IteratorNext.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ var GetIntrinsic = require('../GetIntrinsic');

var $TypeError = GetIntrinsic('%TypeError%');

var Invoke = require('./Invoke');
var Call = require('./Call');
var Type = require('./Type');

// https://ecma-international.org/ecma-262/6.0/#sec-iteratornext
// https://ecma-international.org/ecma-262/9.0/#sec-iteratornext

module.exports = function IteratorNext(iterator, value) {
var result = Invoke(iterator, 'next', arguments.length < 2 ? [] : [value]);
module.exports = function IteratorNext(iteratorRecord) {
var value;
if (arguments.length > 1) {
value = arguments[1];
}
var result = arguments.length < 2
? Call(iteratorRecord['[[NextMethod]]'], iteratorRecord['[[Iterator]]'], [])
: Call(iteratorRecord['[[NextMethod]]'], iteratorRecord['[[Iterator]]'], [value]);
if (Type(result) !== 'Object') {
throw new $TypeError('iterator next must return an object');
}
Expand Down
7 changes: 3 additions & 4 deletions 2018/IteratorStep.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
var IteratorComplete = require('./IteratorComplete');
var IteratorNext = require('./IteratorNext');

// https://ecma-international.org/ecma-262/6.0/#sec-iteratorstep
// https://ecma-international.org/ecma-262/9.0/#sec-iteratorstep

module.exports = function IteratorStep(iterator) {
var result = IteratorNext(iterator);
module.exports = function IteratorStep(iteratorRecord) {
var result = IteratorNext(iteratorRecord);
var done = IteratorComplete(result);
return done === true ? false : result;
};

59 changes: 45 additions & 14 deletions 2019/GetIterator.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions 2019/IterableToList.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions 2019/IteratorClose.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 10 additions & 4 deletions 2019/IteratorNext.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions 2019/IteratorStep.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading