Skip to content

Add Iterator.prototype bindings #7506

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

Merged
merged 16 commits into from
Jun 16, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Polyfill iterator
  • Loading branch information
nojaf committed May 23, 2025
commit c40bf3618100865eb7213e638df6a401afdbbb69
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"@types/node": "^20.14.9",
"@types/semver": "^7.7.0",
"@yarnpkg/types": "^4.0.1",
"es-iterator-helpers": "1.2.1",
"mocha": "10.8.2",
"nyc": "15.0.0",
"semver": "^7.7.2",
Expand Down
7 changes: 6 additions & 1 deletion tests/docstring_tests/DocTest.res
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,12 @@ let main = async () => {
})

if codeExamples->Array.length > 0 {
let content = `describe("${key}", () => {
let content = `
%%raw(\`
import 'es-iterator-helpers/auto';
\`)

describe("${key}", () => {
${codeExamples->Array.join("\n")}
})`
output->Array.push(content)
Expand Down
2 changes: 1 addition & 1 deletion tests/docstring_tests/DocTest.res.js

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

29 changes: 7 additions & 22 deletions tests/tests/src/core/Core_IteratorTests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import * as Test from "./Test.mjs";
import * as Primitive_object from "rescript/lib/es6/Primitive_object.js";
import * as Stdlib_AsyncIterator from "rescript/lib/es6/Stdlib_AsyncIterator.js";

import 'es-iterator-helpers/auto';
;

let eq = Primitive_object.equal;

let iterator = ((() => {
Expand All @@ -16,24 +19,6 @@ let syncResult = {
contents: undefined
};

if (!Iterator.prototype.forEach) {
Iterator.prototype.forEach = function forEach(callback, thisArg) {
if (typeof callback !== 'function') {
throw new TypeError(callback + ' is not a function');
}

let index = 0;
let result = this.next();

while (!result.done) {
callback.call(thisArg, result.value, index, this);
result = this.next();
index++;
}
};
}
;

iterator.forEach(v => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nojaf

Can't do make test locally anymore after this:

file:///Users/cristianocalcagno/GitHub/rescript-compiler/tests/tests/src/core/Core_IteratorTests.mjs:19
iterator.forEach(v => {
         ^

TypeError: iterator.forEach is not a function
    at file:///Users/cristianocalcagno/GitHub/rescript-compiler/tests/tests/src/core/Core_IteratorTests.mjs:19:10
    at ModuleJob.run (node:internal/modules/esm/module_job:263:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:540:24)
    at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:117:5)

Node.js v20.19.0

file:///Users/cristianocalcagno/GitHub/rescript-compiler/tests/tests/src/core/Core_IteratorTests.mjs:19
iterator.forEach(v => {
^

TypeError: iterator.forEach is not a function
at file:///Users/cristianocalcagno/GitHub/rescript-compiler/tests/tests/src/core/Core_IteratorTests.mjs:19:10
at ModuleJob.run (node:internal/modules/esm/module_job:263:25)
at async ModuleLoader.import (node:internal/modules/esm/loader:540:24)
at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:117:5)

Node.js v20.19.0


And node `v20.x` I believe is the recommended version but this might require a newer version?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cristianoc to run the unit tests, you'll need to update to node.js v22, which supports Iterator.prototype.forEach.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like CONTRIBUTING.md needs updating

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made a PR for updating CONTRIBUTING.md here: #7558

if (v === "b") {
syncResult.contents = "b";
Expand All @@ -45,7 +30,7 @@ iterator.forEach(v => {
Test.run([
[
"Core_IteratorTests.res",
38,
23,
20,
34
],
Expand Down Expand Up @@ -77,7 +62,7 @@ await Stdlib_AsyncIterator.forEach(asyncIterator, v => {
Test.run([
[
"Core_IteratorTests.res",
61,
46,
20,
35
],
Expand Down Expand Up @@ -113,7 +98,7 @@ await Stdlib_AsyncIterator.forEach(asyncIterator$1, v => {
Test.run([
[
"Core_IteratorTests.res",
86,
71,
20,
54
],
Expand All @@ -127,4 +112,4 @@ export {
asyncResult,
asyncIterator$1 as asyncIterator,
}
/* iterator Not a pure module */
/* Not a pure module */
23 changes: 4 additions & 19 deletions tests/tests/src/core/Core_IteratorTests.res
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
%%raw(`
import 'es-iterator-helpers/auto';
`)

let eq = (a, b) => a == b

let iterator: Iterator.t<string> = %raw(`
Expand All @@ -10,25 +14,6 @@ let iterator: Iterator.t<string> = %raw(`

let syncResult = ref(None)

%%raw(`
if (!Iterator.prototype.forEach) {
Iterator.prototype.forEach = function forEach(callback, thisArg) {
if (typeof callback !== 'function') {
throw new TypeError(callback + ' is not a function');
}

let index = 0;
let result = this.next();

while (!result.done) {
callback.call(thisArg, result.value, index, this);
result = this.next();
index++;
}
};
}
`)

iterator->Iterator.forEach(v => {
if v == "b" {
syncResult.contents = Some("b")
Expand Down
3 changes: 3 additions & 0 deletions tests/tests/src/core/Core_TempTests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import * as Core_IntlTests from "./intl/Core_IntlTests.mjs";
import * as Primitive_bigint from "rescript/lib/es6/Primitive_bigint.js";
import * as Primitive_option from "rescript/lib/es6/Primitive_option.js";

import 'es-iterator-helpers/auto';
;

console.info("");

console.info("Array");
Expand Down
4 changes: 4 additions & 0 deletions tests/tests/src/core/Core_TempTests.res
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
include Core_IntlTests

%%raw(`
import 'es-iterator-helpers/auto';
`)

Console.info("")
Console.info("Array")
Console.info("---")
Expand Down
Loading