Repository navigation
Reduce regenerator helper size - #17268
Conversation
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/59264 |
|
Wow |
We run test262 tests, and now SpiderMonkey auto-syncs their tests to test262. We just need to make sure we are running |
87a8c64 to
5005658
Compare
| keep_fnames: mangleFns | ||
| ? noMangleFns.length | ||
| ? new RegExp(noMangleFns.join("|")) | ||
| ? new RegExp("^" + noMangleFns.join("|") + "$") |
There was a problem hiding this comment.
This should be with parentheses, otherwise ^/$ will only apply to the first/last of the options
62bbbed to
8f50130
Compare
|
|
||
| return function invoke(method: "next" | "throw" | "return", arg: any) { | ||
| function invoke( | ||
| _methodName: "next" | "throw" | "return", |
There was a problem hiding this comment.
Do we need to keep the _methodName parameter here?
There was a problem hiding this comment.
It's not used, but we still keep the first argument as a string name for the sake of the shared defineIteratorMethods .
| method = _method; | ||
| arg = _arg; | ||
|
|
||
| while (!done || (_ = undefined)) { |
There was a problem hiding this comment.
Does moving _ = undefined after the while make the code bigger? It seems like an optimization Terser should be able to make by itself.
There was a problem hiding this comment.
If we move it to after the while, it needs to be if (done) _ = undefined because we have a break inside the loop.
| // Call delegate.iterator[context.method](context.arg) and handle the result | ||
|
|
||
| if ( | ||
| (_ = delegateIterator[FunctionNameStrings[method as 0 | 1 | 2]]) |
There was a problem hiding this comment.
We can use _methodName here and in a bunch of other places in this function.
There was a problem hiding this comment.
I tried that and it results in an error, I'll look into it again. :)
| if ( | ||
| (_ = delegateIterator[FunctionNameStrings[method as 0 | 1 | 2]]) | ||
| ) { | ||
| if ((_ = _.call(delegateIterator, arg))) { |
There was a problem hiding this comment.
Is it possible that this call re-calls one of the iterator method, causing method to be set to something different, and thus the various method checks further down break?
There was a problem hiding this comment.
Something like this maybe:
const innerIterable = {
[Symbol.iterator]() {
let i = 0;
return {
return() {
it.throw();
return this.next();
},
next() {
if (i++ < 2) return { done: false, value: "Hi 1" };
return { done: true, value: "Hi 2" };
}
};
}
};
function* gen() {
yield* innerIterable;
yield 1;
yield 2;
}
var it = gen();
console.log(it.next());
console.log(it.return());There was a problem hiding this comment.
It seems like this will throw Generator is already running. :)
There was a problem hiding this comment.
I don't get that error if I copy-paste this code in the repl for this PR and then run it: https://babeljs.io/repl/build/59215#?browsers=chrome%2058&build=&builtIns=false&corejs=false&spec=false&loose=false&code_lz=MYewdgzgLgBAlmMBTATgSSqghgIwDZIwC8MA3gFAwwDaAygJ4C2OIeAdHJillCCgLoAKAJRlKVGAVhxiMAAwBucVRRIoAVxRgxEiao1aRO3RM5soACxQgA7iKUm9azdstwIbZAA8o95RIBfABp_Km9fUQpHKjgAMxhBOABqJJgAHhgAJlF9FzIYABNwJAAuGFisPAgkIJgAN0r1UpgAIgAJGQBGFpgAh2jcrXyi5DKoFCbahrwmsvaZTJ6-0N7_ZaoA8mXyWPUwYCg4cAAqGABzJDAjKJh6OCQ8AtOEZHQuXAJ-u4eCmE6v-6PLJKTbkBooeCwEgXK7CJSgSCsJBsPAgM6JKCeJA-ERw8gIiBIlFojFsQawuFAA&forceAllTransforms=true&modules=false&shippedProposals=false&evaluate=false&fileSize=false&timeTravel=false&sourceType=module&lineWrap=false&presets=env%2Cstage-2%2Cstage-3%2Ctypescript&prettier=false&targets=&version=7.27.0%2Bpr.17268&externalPlugins=&assumptions=%7B%7D
It logs twice.
There was a problem hiding this comment.
Oh, thanks! I haven't tried transformed code before.
This seems to be an existing bug, but I will try to fix it anyway.
There was a problem hiding this comment.
Oh then I would be fine with it happening in a separate PR. This PR looks good otherwise.
e423cc4 to
59944bb
Compare
|
We haven't run the test262 tests in I'm not sure if we should include more tests (perhaps v8) to increase confidence in this PR. |
|
V8 has some in this folder: https://github.com/v8/v8/tree/main/test/mjsunit/harmony (search for "generator") |
|
@liuxingbaoyu You need to rebase for CI to pass, but it seems like all the new tests are passing :) |
50fa1b7 to
d18276c
Compare
51924d7 to
2c5d4ae
Compare
|
One day we should figure out why on old Node.js version whenever there is a test failure then there is also that unrelated false error about |
commented
May 1, 2025
|
jestjs/jest#10451 |
| toStringTagSymbol, | ||
| "GeneratorFunction", | ||
| ); | ||
| GeneratorFunction.displayName = "GeneratorFunction"; |
There was a problem hiding this comment.
Let's add an exec test for the displayName here?
Fixes #1, Fixes #2This looks like it would be hard to review, maybe we can use meta/regenerator#21 to reduce the risk of regression.
Although the existing tests look great.
Unfortunately after this our helpers are still much larger than TS's (2700 bytes vs 1083 bytes), but most of them are prototype definitions, which cannot be optimized away unless we decide to remove them in Babel 8.
Special thanks to TS for this PR, without their implementation I didn't even know the generator helper could be so small.
This PR still has some issues with Babel 7 compatibility, I'll fix them later.