async-browser-context 0.2.2 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +1 -1
  3. package/dist/babel/async-generator-helpers.d.ts +17 -0
  4. package/dist/babel/async-generator-helpers.d.ts.map +1 -0
  5. package/dist/babel/async-generator-helpers.js +40 -0
  6. package/dist/babel/async-generator-helpers.js.map +1 -0
  7. package/dist/babel/bind-generators.d.ts.map +1 -1
  8. package/dist/babel/bind-generators.js +17 -5
  9. package/dist/babel/bind-generators.js.map +1 -1
  10. package/dist/babel/preset.d.ts +6 -4
  11. package/dist/babel/preset.d.ts.map +1 -1
  12. package/dist/babel/preset.js +8 -4
  13. package/dist/babel/preset.js.map +1 -1
  14. package/dist/core/async-generator.d.ts +36 -0
  15. package/dist/core/async-generator.d.ts.map +1 -0
  16. package/dist/core/async-generator.js +132 -0
  17. package/dist/core/async-generator.js.map +1 -0
  18. package/dist/core/coroutine.d.ts +13 -3
  19. package/dist/core/coroutine.d.ts.map +1 -1
  20. package/dist/core/coroutine.js +55 -3
  21. package/dist/core/coroutine.js.map +1 -1
  22. package/dist/node/runtime.d.ts +8 -1
  23. package/dist/node/runtime.d.ts.map +1 -1
  24. package/dist/node/runtime.js +24 -0
  25. package/dist/node/runtime.js.map +1 -1
  26. package/dist/runtime.d.ts +4 -2
  27. package/dist/runtime.d.ts.map +1 -1
  28. package/dist/runtime.js +4 -2
  29. package/dist/runtime.js.map +1 -1
  30. package/package.json +1 -1
  31. package/src/babel/async-generator-helpers.ts +49 -0
  32. package/src/babel/bind-generators.ts +17 -5
  33. package/src/babel/preset.ts +8 -4
  34. package/src/core/async-generator.ts +159 -0
  35. package/src/core/coroutine.ts +57 -3
  36. package/src/node/runtime.ts +27 -0
  37. package/src/runtime.ts +4 -2
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # Change history
2
2
 
3
+ ## 0.2.3
4
+
5
+ ### Corrections
6
+
7
+ - Async generators are faster. The runtime has a driver for async generators (`asyncGenerator`), which continues each step with a native `await`. Before, the helper of Babel was in each module, and each step was a `then` callback. A `for await` loop over an async generator with 50,000 steps took 11.2 ms in Chromium, 560 ms in Firefox and 16 ms in WebKit. It takes 9.0 ms, 370 ms and 13 ms at this time (`pnpm bench:browsers`).
8
+ - The modules with async generators are smaller, because they do not contain the async generator helper of Babel. In the size measurement, idb changed from +31.9 to +8.1 percent and openai from +9.1 to +7.5 percent.
9
+ - The preset does not wrap async generators with `bindGenerator`. The driver keeps the context of the creation itself (rule C5).
10
+
11
+ ### Tests
12
+
13
+ - The tests examine the behavior of async generators: the queue of requests, `throw()`, `return()` with `await` and `yield` in a `finally` block, and `yield*`. The project without a transform starts the same tests on native async generators.
14
+
3
15
  ## 0.2.2
4
16
 
5
17
  ### Corrections
package/README.md CHANGED
@@ -276,7 +276,7 @@ When the page loads, the library installs its patches in approximately 8 ms: the
276
276
 
277
277
  In Chromium, Firefox and WebKit, an `await` in transformed code takes approximately the time of a native `await` (`pnpm bench:browsers`). In Firefox, the patch of `then` adds approximately 0.8 µs to each `await` of a promise. The performance page of the website tells more.
278
278
 
279
- The runtime is 5.9 kB with gzip, and zone.js 0.16.3 is 13.3 kB. The transform adds 4 to 9 percent to packages with much async code, and nothing to React DOM. In an example application with React DOM, TanStack Query, supabase-js and ky, it adds 2.9 percent (`pnpm bench:size`).
279
+ The runtime is 5.9 kB with gzip, and zone.js 0.16.3 is 13.3 kB. The transform adds 3 to 8 percent to packages with much async code, and nothing to React DOM. In an example application with React DOM, TanStack Query, supabase-js and ky, it adds 2.9 percent (`pnpm bench:size`).
280
280
 
281
281
  ## Tests
282
282
 
@@ -0,0 +1,17 @@
1
+ import { type PluginObject, type PluginPass } from "@babel/core";
2
+ /** The options of the plugin. */
3
+ export interface AsyncGeneratorHelpersOptions {
4
+ /** The module that exports `asyncGenerator` and `OverloadYield`. */
5
+ readonly runtime: string;
6
+ }
7
+ /**
8
+ * The Babel plugin that gives the async generators to the driver of the runtime. It sets the
9
+ * `helperGenerator` of the file, as `@babel/plugin-transform-runtime` does. Thus, the helpers
10
+ * `wrapAsyncGenerator` and `OverloadYield` of `@babel/plugin-transform-async-generator-functions`
11
+ * become imports of `asyncGenerator` and `OverloadYield` from the runtime. The helpers `awaitAsyncGenerator` and `asyncGeneratorDelegate` stay in the
12
+ * file, and they make the `OverloadYield` values of the runtime.
13
+ *
14
+ * The plugin keeps a `helperGenerator` of an earlier plugin for the other helpers.
15
+ */
16
+ export declare function asyncGeneratorHelpersPlugin(_api: unknown, options: object): PluginObject<PluginPass>;
17
+ //# sourceMappingURL=async-generator-helpers.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"async-generator-helpers.d.ts","sourceRoot":"","sources":["../../src/babel/async-generator-helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAS,KAAK,YAAY,EAAE,KAAK,UAAU,EAAE,MAAM,aAAa,CAAC;AAGxE,iCAAiC;AACjC,MAAM,WAAW,4BAA4B;IACzC,oEAAoE;IACpE,QAAQ,CAAC,OAAO,EAAG,MAAM,CAAC;CAC7B;AAUD;;;;;;;;GAQG;AACH,wBAAgB,2BAA2B,CAAC,IAAI,EAAG,OAAO,EAAE,OAAO,EAAG,MAAM,GAAI,YAAY,CAAC,UAAU,CAAC,CAsBvG"}
@@ -0,0 +1,40 @@
1
+ import { types } from "@babel/core";
2
+ import { addNamed } from "@babel/helper-module-imports";
3
+ /** The Babel helpers that the runtime replaces, and the export of the runtime for each one. */
4
+ const REPLACED_HELPERS = {
5
+ wrapAsyncGenerator: "asyncGenerator",
6
+ OverloadYield: "OverloadYield",
7
+ };
8
+ /**
9
+ * The Babel plugin that gives the async generators to the driver of the runtime. It sets the
10
+ * `helperGenerator` of the file, as `@babel/plugin-transform-runtime` does. Thus, the helpers
11
+ * `wrapAsyncGenerator` and `OverloadYield` of `@babel/plugin-transform-async-generator-functions`
12
+ * become imports of `asyncGenerator` and `OverloadYield` from the runtime. The helpers `awaitAsyncGenerator` and `asyncGeneratorDelegate` stay in the
13
+ * file, and they make the `OverloadYield` values of the runtime.
14
+ *
15
+ * The plugin keeps a `helperGenerator` of an earlier plugin for the other helpers.
16
+ */
17
+ export function asyncGeneratorHelpersPlugin(_api, options) {
18
+ const { runtime } = options;
19
+ return {
20
+ name: "async-browser-context/async-generator-helpers",
21
+ pre(file) {
22
+ const previous = file.get("helperGenerator");
23
+ const imports = new Map();
24
+ const generator = (name) => {
25
+ const exported = REPLACED_HELPERS[name];
26
+ if (exported === undefined) {
27
+ return previous?.(name);
28
+ }
29
+ let local = imports.get(exported);
30
+ if (local === undefined) {
31
+ local = addNamed(file.path, exported, runtime, { nameHint: exported });
32
+ imports.set(exported, local);
33
+ }
34
+ return types.cloneNode(local);
35
+ };
36
+ file.set("helperGenerator", generator);
37
+ },
38
+ };
39
+ }
40
+ //# sourceMappingURL=async-generator-helpers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"async-generator-helpers.js","sourceRoot":"","sources":["../../src/babel/async-generator-helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAsC,MAAM,aAAa,CAAC;AACxE,OAAO,EAAE,QAAQ,EAAE,MAAM,8BAA8B,CAAC;AAQxD,+FAA+F;AAC/F,MAAM,gBAAgB,GAAsC;IACxD,kBAAkB,EAAG,gBAAgB;IACrC,aAAa,EAAG,eAAe;CAClC,CAAC;AAIF;;;;;;;;GAQG;AACH,MAAM,UAAU,2BAA2B,CAAC,IAAc,EAAE,OAAgB;IACxE,MAAM,EAAE,OAAO,EAAE,GAAG,OAAuC,CAAC;IAC5D,OAAO;QACH,IAAI,EAAG,+CAA+C;QACtD,GAAG,CAAC,IAAyB;YACzB,MAAM,QAAQ,GAAG,IAAI,CAAC,GAAG,CAAC,iBAAiB,CAAgC,CAAC;YAC5E,MAAM,OAAO,GAAG,IAAI,GAAG,EAA4B,CAAC;YACpD,MAAM,SAAS,GAAqB,CAAC,IAAI,EAAE,EAAE;gBACzC,MAAM,QAAQ,GAAG,gBAAgB,CAAC,IAAI,CAAC,CAAC;gBACxC,IAAI,QAAQ,KAAK,SAAS,EAAE,CAAC;oBACzB,OAAO,QAAQ,EAAE,CAAC,IAAI,CAAC,CAAC;gBAC5B,CAAC;gBACD,IAAI,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;gBAClC,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;oBACtB,KAAK,GAAG,QAAQ,CAAC,IAAI,CAAC,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,EAAE,QAAQ,EAAG,QAAQ,EAAE,CAAqB,CAAC;oBAC5F,OAAO,CAAC,GAAG,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;gBACjC,CAAC;gBACD,OAAO,KAAK,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;YAClC,CAAC,CAAC;YACF,IAAI,CAAC,GAAG,CAAC,iBAAiB,EAAE,SAAS,CAAC,CAAC;QAC3C,CAAC;KACJ,CAAC;AACN,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"bind-generators.d.ts","sourceRoot":"","sources":["../../src/babel/bind-generators.ts"],"names":[],"mappings":"AAAA,OAAO,EAAwB,KAAK,YAAY,EAAE,KAAK,UAAU,EAAE,MAAM,aAAa,CAAC;AAGvF,iCAAiC;AACjC,MAAM,WAAW,qBAAqB;IAClC,+CAA+C;IAC/C,QAAQ,CAAC,OAAO,EAAG,MAAM,CAAC;CAC7B;AAED,8EAA8E;AAC9E,eAAO,MAAM,YAAY,wBAAwB,CAAC;AAsElD;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,IAAI,EAAG,OAAO,EAAE,OAAO,EAAG,MAAM,GAAI,YAAY,CAAC,UAAU,CAAC,CAoChG"}
1
+ {"version":3,"file":"bind-generators.d.ts","sourceRoot":"","sources":["../../src/babel/bind-generators.ts"],"names":[],"mappings":"AAAA,OAAO,EAAwB,KAAK,YAAY,EAAE,KAAK,UAAU,EAAE,MAAM,aAAa,CAAC;AAGvF,iCAAiC;AACjC,MAAM,WAAW,qBAAqB;IAClC,+CAA+C;IAC/C,QAAQ,CAAC,OAAO,EAAG,MAAM,CAAC;CAC7B;AAED,8EAA8E;AAC9E,eAAO,MAAM,YAAY,wBAAwB,CAAC;AAiFlD;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,IAAI,EAAG,OAAO,EAAE,OAAO,EAAG,MAAM,GAAI,YAAY,CAAC,UAAU,CAAC,CAqChG"}
@@ -2,15 +2,26 @@ import { types } from "@babel/core";
2
2
  import { addNamed } from "@babel/helper-module-imports";
3
3
  /** The key of the file metadata that tells if the preset changed the file. */
4
4
  export const METADATA_KEY = "asyncBrowserContext";
5
- /** The names of the helper of `@babel/plugin-transform-async-generator-functions`. Babel gives them an underscore. */
6
- const ASYNC_GENERATOR_HELPER = /^_wrapAsyncGenerator\d*$/;
5
+ /**
6
+ * The local names of the async generator helper: `wrapAsyncGenerator` of
7
+ * `@babel/plugin-transform-async-generator-functions`, or `asyncGenerator` of the runtime,
8
+ * which the preset gives in its place. Babel gives them an underscore.
9
+ */
10
+ const ASYNC_GENERATOR_HELPER = /^_(wrapAsyncGenerator|asyncGenerator)\d*$/;
7
11
  /** The local names that `addNamed` gives to `bindGenerator`. */
8
12
  const BIND_GENERATOR_NAME = /^_bindGenerator\d*$/;
9
13
  /** This function gives `true` if `path` is the generator that Babel gives to its async generator helper. */
10
14
  function isAsyncGeneratorHelperArgument(path) {
11
15
  const parent = path.parent;
12
- // Stryker disable next-line LogicalOperator: without the call check, the name test also fails, because a member callee has no name
13
- return types.isCallExpression(parent) && types.isIdentifier(parent.callee) && ASYNC_GENERATOR_HELPER.test(parent.callee.name);
16
+ if (!types.isCallExpression(parent)) {
17
+ return false;
18
+ }
19
+ if (types.isIdentifier(parent.callee)) {
20
+ return ASYNC_GENERATOR_HELPER.test(parent.callee.name);
21
+ }
22
+ // CommonJS output of the preset: _runtime.asyncGenerator or (0, _runtime.asyncGenerator)
23
+ const member = types.isSequenceExpression(parent.callee) ? parent.callee.expressions.at(-1) : parent.callee;
24
+ return types.isMemberExpression(member) && types.isIdentifier(member.property, { name: "asyncGenerator" });
14
25
  }
15
26
  /**
16
27
  * This function gives `true` if `callee` is the `bindGenerator` function of
@@ -83,7 +94,8 @@ export function bindGeneratorsPlugin(_api, options) {
83
94
  if (node.async) {
84
95
  changed = true;
85
96
  }
86
- if (!node.generator || isAsyncGeneratorHelperArgument(path) || isAlreadyBound(path, runtime)) {
97
+ // The driver of the runtime keeps the context of an async generator (asyncGenerator)
98
+ if (!node.generator || node.async || isAsyncGeneratorHelperArgument(path) || isAlreadyBound(path, runtime)) {
87
99
  return;
88
100
  }
89
101
  changed = true;
@@ -1 +1 @@
1
- {"version":3,"file":"bind-generators.js","sourceRoot":"","sources":["../../src/babel/bind-generators.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAqD,MAAM,aAAa,CAAC;AACvF,OAAO,EAAE,QAAQ,EAAE,MAAM,8BAA8B,CAAC;AAQxD,8EAA8E;AAC9E,MAAM,CAAC,MAAM,YAAY,GAAG,qBAAqB,CAAC;AAElD,sHAAsH;AACtH,MAAM,sBAAsB,GAAG,0BAA0B,CAAC;AAC1D,gEAAgE;AAChE,MAAM,mBAAmB,GAAG,qBAAqB,CAAC;AAIlD,4GAA4G;AAC5G,SAAS,8BAA8B,CAAC,IAAmB;IACvD,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;IAC3B,mIAAmI;IACnI,OAAO,KAAK,CAAC,gBAAgB,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,YAAY,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,sBAAsB,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;AAClI,CAAC;AAED;;;;GAIG;AACH,SAAS,qBAAqB,CAAC,MAAmB,EAAE,IAAe,EAAE,OAAg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;QAC9C,OAAO,EAAG;YACN,OAAO,CAAC,WAAW,EAAE,KAAK;gBACtB,IAAI,MAAqC,CAAC;gBAC1C,MAAM,aAAa,GAAG,GAAsB,EAAE;oBAC1C,MAAM,KAAK,QAAQ,CAAC,WAAW,EAAE,eAAe,EAAE,OAAO,EAAE,EAAE,QAAQ,EAAG,eAAe,EAAE,CAAqB,CAAC;oBAC/G,OAAO,KAAK,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;gBACnC,CAAC,CAAC;gBACF,IAAI,OAAO,GAAG,KAAK,CAAC;gBACpB,WAAW,CAAC,QAAQ,CAAC;oBACjB,QAAQ,CAAC,IAAI;wBACT,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC;wBACvB,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC;4BACb,OAAO,GAAG,IAAI,CAAC;wBACnB,CAAC;wBACD,qFAAqF;wBACrF,IAAI,CAAC,IAAI,CAAC,SAAS,IAAI,IAAI,CAAC,KAAK,IAAI,8BAA8B,CAAC,IAAI,CAAC,IAAI,cAAc,CAAC,IAAI,EAAE,OAAO,CAAC,EAAE,CAAC;4BACzG,OAAO;wBACX,CAAC;wBACD,OAAO,GAAG,IAAI,CAAC;wBACf,qBAAqB,CAAC,IAAI,EAAE,aAAa,EAAE,CAAC,CAAC;oBACjD,CAAC;oBACD,cAAc,CAAC,IAAI;wBACf,IAAI,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC;4BAClB,OAAO,GAAG,IAAI,CAAC;wBACnB,CAAC;oBACL,CAAC;iBACJ,CAAC,CAAC;gBACH,IAAI,OAAO,EAAE,CAAC;oBACT,KAAK,CAAC,IAAI,CAAC,QAA+C,CAAC,YAAY,CAAC,GAAG,IAAI,CAAC;gBACrF,CAAC;YACL,CAAC;SACJ;KACJ,CAAC;AACN,CAAC"}
@@ -24,14 +24,16 @@ export declare const DEFAULT_RUNTIME = "async-browser-context/runtime";
24
24
  *
25
25
  * 1. A plugin of this library skips the native async functions of the
26
26
  * runtime, which a directive marks.
27
- * 2. A plugin of this library binds each generator to the context of its
28
- * creation.
27
+ * 2. A plugin of this library binds each sync generator to the context of
28
+ * its creation.
29
29
  * 3. A plugin of this library gives the name of each async function to
30
30
  * `coroutine`, for the callback hooks. The option `names: false` removes
31
31
  * this step.
32
- * 4. `@babel/plugin-transform-async-generator-functions` changes async
32
+ * 4. A plugin of this library gives the async generators to `asyncGenerator`
33
+ * of the runtime, in place of the Babel helper.
34
+ * 5. `@babel/plugin-transform-async-generator-functions` changes async
33
35
  * generators and `for await` loops.
34
- * 5. `@babel/plugin-transform-async-to-generator` changes each async function
36
+ * 6. `@babel/plugin-transform-async-to-generator` changes each async function
35
37
  * into a generator that the `coroutine` function of the runtime operates.
36
38
  *
37
39
  * The preset operates with Babel 7.22 and later and with Babel 8. Put it last
@@ -1 +1 @@
1
- {"version":3,"file":"preset.d.ts","sourceRoot":"","sources":["../../src/babel/preset.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAc,SAAS,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAOvE,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAEpD,iCAAiC;AACjC,MAAM,WAAW,aAAa;IAC1B;;;OAGG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAG,MAAM,GAAG,SAAS,CAAC;IACvC;;;;;OAKG;IACH,QAAQ,CAAC,KAAK,CAAC,EAAG,OAAO,GAAG,SAAS,CAAC;CACzC;AAED,kCAAkC;AAClC,eAAO,MAAM,eAAe,kCAAkC,CAAC;AAE/D;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,CAAC,OAAO,UAAU,yBAAyB,CAAC,GAAG,EAAG,SAAS,EAAE,OAAO,GAAG,aAAkB,GAAI,YAAY,CAa9G"}
1
+ {"version":3,"file":"preset.d.ts","sourceRoot":"","sources":["../../src/babel/preset.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAc,SAAS,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAQvE,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAEpD,iCAAiC;AACjC,MAAM,WAAW,aAAa;IAC1B;;;OAGG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAG,MAAM,GAAG,SAAS,CAAC;IACvC;;;;;OAKG;IACH,QAAQ,CAAC,KAAK,CAAC,EAAG,OAAO,GAAG,SAAS,CAAC;CACzC;AAED,kCAAkC;AAClC,eAAO,MAAM,eAAe,kCAAkC,CAAC;AAE/D;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,CAAC,OAAO,UAAU,yBAAyB,CAAC,GAAG,EAAG,SAAS,EAAE,OAAO,GAAG,aAAkB,GAAI,YAAY,CAc9G"}
@@ -1,5 +1,6 @@
1
1
  import asyncGeneratorFunctions from "@babel/plugin-transform-async-generator-functions";
2
2
  import asyncToGenerator from "@babel/plugin-transform-async-to-generator";
3
+ import { asyncGeneratorHelpersPlugin } from "./async-generator-helpers.js";
3
4
  import { bindGeneratorsPlugin } from "./bind-generators.js";
4
5
  import { nameCoroutinesPlugin } from "./name-coroutines.js";
5
6
  import { skipRuntimePlugin } from "./skip-runtime.js";
@@ -13,14 +14,16 @@ export const DEFAULT_RUNTIME = "async-browser-context/runtime";
13
14
  *
14
15
  * 1. A plugin of this library skips the native async functions of the
15
16
  * runtime, which a directive marks.
16
- * 2. A plugin of this library binds each generator to the context of its
17
- * creation.
17
+ * 2. A plugin of this library binds each sync generator to the context of
18
+ * its creation.
18
19
  * 3. A plugin of this library gives the name of each async function to
19
20
  * `coroutine`, for the callback hooks. The option `names: false` removes
20
21
  * this step.
21
- * 4. `@babel/plugin-transform-async-generator-functions` changes async
22
+ * 4. A plugin of this library gives the async generators to `asyncGenerator`
23
+ * of the runtime, in place of the Babel helper.
24
+ * 5. `@babel/plugin-transform-async-generator-functions` changes async
22
25
  * generators and `for await` loops.
23
- * 5. `@babel/plugin-transform-async-to-generator` changes each async function
26
+ * 6. `@babel/plugin-transform-async-to-generator` changes each async function
24
27
  * into a generator that the `coroutine` function of the runtime operates.
25
28
  *
26
29
  * The preset operates with Babel 7.22 and later and with Babel 8. Put it last
@@ -35,6 +38,7 @@ export default function asyncBrowserContextPreset(api, options = {}) {
35
38
  skipRuntimePlugin,
36
39
  [bindGeneratorsPlugin, { runtime }],
37
40
  ...names,
41
+ [asyncGeneratorHelpersPlugin, { runtime }],
38
42
  asyncGeneratorFunctions,
39
43
  [asyncToGenerator, { module: runtime, method: "coroutine" }],
40
44
  ],
@@ -1 +1 @@
1
- {"version":3,"file":"preset.js","sourceRoot":"","sources":["../../src/babel/preset.ts"],"names":[],"mappings":"AACA,OAAO,uBAAuB,MAAM,mDAAmD,CAAC;AACxF,OAAO,gBAAgB,MAAM,4CAA4C,CAAC;AAC1E,OAAO,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAC5D,OAAO,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAC5D,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAEtD,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAkBpD,kCAAkC;AAClC,MAAM,CAAC,MAAM,eAAe,GAAG,+BAA+B,CAAC;AAE/D;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,CAAC,OAAO,UAAU,yBAAyB,CAAC,GAAe,EAAE,UAA0B,EAAE;IAC3F,GAAG,CAAC,aAAa,CAAC,mBAAmB,CAAC,CAAC;IACvC,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,IAAI,eAAe,CAAC;IACnD,MAAM,KAAK,GAAkB,OAAO,CAAC,KAAK,KAAK,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,oBAAoB,EAAE,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC;IAClG,OAAO;QACH,OAAO,EAAG;YACN,iBAAiB;YACjB,CAAC,oBAAoB,EAAE,EAAE,OAAO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E,MAAM,EAAG,OAAO,EAAE,MAAM,EAAG,WAAW,EAAE,CAAC;SACjE;KACJ,CAAC;AACN,CAAC"}
@@ -0,0 +1,36 @@
1
+ /**
2
+ * The async generators of transformed code. `@babel/plugin-transform-async-generator-functions`
3
+ * changes each async generator into a sync generator and its helper `wrapAsyncGenerator`.
4
+ * The helper continued each step in a `then` callback, and a `then` callback is slow in
5
+ * Firefox. The preset replaces the helper with `asyncGenerator` of the runtime, and the
6
+ * helper `OverloadYield` with the class of this module. The driver in this module then
7
+ * continues each step with a native `await`.
8
+ *
9
+ * The protocol is the protocol of the Babel 8 helper: the body gives
10
+ * `new OverloadYield(value, 0)` for an `await`, and `new OverloadYield(promise, 1)` for a
11
+ * step of `yield*`. Each other value is a `yield`.
12
+ */
13
+ /**
14
+ * The value that the body gives for an `await` (kind 0) or for a step of `yield*` (kind 1).
15
+ * The Babel helpers `awaitAsyncGenerator` and `asyncGeneratorDelegate` make it with `new`.
16
+ */
17
+ export declare class OverloadYield {
18
+ v: unknown;
19
+ readonly k: 0 | 1;
20
+ constructor(value: unknown, kind: 0 | 1);
21
+ }
22
+ /** The methods of a generator that a request of the consumer starts. */
23
+ export type ResumeKey = "next" | "throw" | "return";
24
+ /**
25
+ * This function does one step of the body: `generator[key](argument)`. The browser runtime sets
26
+ * the context of the generator around the step. `resumed` is `true` for a step after an
27
+ * `await` of the driver.
28
+ */
29
+ export type AsyncGeneratorStep = (key: ResumeKey, argument: unknown, resumed: boolean) => IteratorResult<unknown, unknown>;
30
+ /**
31
+ * This function makes an async generator object. The object queues the requests of the
32
+ * consumer, and starts one request at a time, as the Babel helper does. `enter` starts the
33
+ * driver: the Node.js runtime starts it in the context of the generator.
34
+ */
35
+ export declare function createAsyncGenerator(step: AsyncGeneratorStep, NativePromise: PromiseConstructor, enter?: (start: () => Promise<void>) => Promise<void>): AsyncGenerator<unknown, unknown, unknown>;
36
+ //# sourceMappingURL=async-generator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"async-generator.d.ts","sourceRoot":"","sources":["../../src/core/async-generator.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH;;;GAGG;AACH,qBAAa,aAAa;IACtB,CAAC,EAAG,OAAO,CAAC;IACZ,QAAQ,CAAC,CAAC,EAAG,CAAC,GAAG,CAAC,CAAC;gBACP,KAAK,EAAG,OAAO,EAAE,IAAI,EAAG,CAAC,GAAG,CAAC;CAI5C;AAED,wEAAwE;AACxE,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG,OAAO,GAAG,QAAQ,CAAC;AAEpD;;;;GAIG;AACH,MAAM,MAAM,kBAAkB,GAAG,CAAC,GAAG,EAAG,SAAS,EAAE,QAAQ,EAAG,OAAO,EAAE,OAAO,EAAG,OAAO,KAAK,cAAc,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;AAwC9H;;;;GAIG;AACH,wBAAgB,oBAAoB,CAChC,IAAI,EAAG,kBAAkB,EACzB,aAAa,EAAG,kBAAkB,EAClC,KAAK,GAAG,CAAC,KAAK,EAAG,MAAM,OAAO,CAAC,IAAI,CAAC,KAAK,OAAO,CAAC,IAAI,CAAsB,GAC3E,cAAc,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,CA2E5C"}
@@ -0,0 +1,132 @@
1
+ /**
2
+ * The async generators of transformed code. `@babel/plugin-transform-async-generator-functions`
3
+ * changes each async generator into a sync generator and its helper `wrapAsyncGenerator`.
4
+ * The helper continued each step in a `then` callback, and a `then` callback is slow in
5
+ * Firefox. The preset replaces the helper with `asyncGenerator` of the runtime, and the
6
+ * helper `OverloadYield` with the class of this module. The driver in this module then
7
+ * continues each step with a native `await`.
8
+ *
9
+ * The protocol is the protocol of the Babel 8 helper: the body gives
10
+ * `new OverloadYield(value, 0)` for an `await`, and `new OverloadYield(promise, 1)` for a
11
+ * step of `yield*`. Each other value is a `yield`.
12
+ */
13
+ /**
14
+ * The value that the body gives for an `await` (kind 0) or for a step of `yield*` (kind 1).
15
+ * The Babel helpers `awaitAsyncGenerator` and `asyncGeneratorDelegate` make it with `new`.
16
+ */
17
+ export class OverloadYield {
18
+ v;
19
+ k;
20
+ constructor(value, kind) {
21
+ this.v = value;
22
+ this.k = kind;
23
+ }
24
+ }
25
+ /** The key of the request function of each async generator object. */
26
+ const INVOKE = Symbol("async-browser-context/invoke");
27
+ function invokeOf(target) {
28
+ const invoke = target[INVOKE];
29
+ if (invoke === undefined) {
30
+ throw new TypeError("The object is not an async generator of async-browser-context");
31
+ }
32
+ return invoke;
33
+ }
34
+ /** The prototype of the async generator objects of transformed code. */
35
+ const AsyncGeneratorPrototype = {
36
+ next(value) {
37
+ return invokeOf(this)("next", value);
38
+ },
39
+ throw(error) {
40
+ return invokeOf(this)("throw", error);
41
+ },
42
+ return(value) {
43
+ return invokeOf(this)("return", value);
44
+ },
45
+ [Symbol.asyncIterator]() {
46
+ return this;
47
+ },
48
+ [Symbol.toStringTag]: "AsyncGenerator",
49
+ };
50
+ /**
51
+ * This function makes an async generator object. The object queues the requests of the
52
+ * consumer, and starts one request at a time, as the Babel helper does. `enter` starts the
53
+ * driver: the Node.js runtime starts it in the context of the generator.
54
+ */
55
+ export function createAsyncGenerator(step, NativePromise, enter = (start) => start()) {
56
+ let first;
57
+ let last;
58
+ /** This function starts the requests in the queue, one after the other, until the queue is empty. */
59
+ const run = async (key, argument) => {
60
+ // The directive tells the Babel preset not to change this function (src/babel/skip-runtime.ts)
61
+ "async-browser-context runtime";
62
+ let resumed = false;
63
+ for (;;) {
64
+ let failed = false;
65
+ let settled;
66
+ let done = false;
67
+ try {
68
+ let result = step(key, argument, resumed);
69
+ for (;;) {
70
+ const yielded = result.value;
71
+ const overloaded = yielded instanceof OverloadYield;
72
+ let value;
73
+ try {
74
+ value = await (overloaded ? yielded.v : yielded);
75
+ }
76
+ catch (error) {
77
+ resumed = true;
78
+ key = "throw";
79
+ result = step("throw", error, true);
80
+ continue;
81
+ }
82
+ resumed = true;
83
+ if (overloaded) {
84
+ const nextKey = key === "return" && yielded.k === 1 ? "return" : "next";
85
+ if (yielded.k === 0 || Boolean(value.done)) {
86
+ key = nextKey;
87
+ result = step(nextKey, value, true);
88
+ continue;
89
+ }
90
+ value = step(nextKey, value, true).value;
91
+ }
92
+ settled = value;
93
+ done = result.done === true;
94
+ break;
95
+ }
96
+ }
97
+ catch (error) {
98
+ failed = true;
99
+ settled = error;
100
+ }
101
+ // A request always waits in the queue while the driver runs
102
+ const request = first;
103
+ first = request.next;
104
+ if (failed) {
105
+ request.reject(settled);
106
+ }
107
+ else {
108
+ request.resolve({ value: settled, done });
109
+ }
110
+ if (first === undefined) {
111
+ last = undefined;
112
+ return;
113
+ }
114
+ key = first.key;
115
+ argument = first.argument;
116
+ }
117
+ };
118
+ const invoke = (key, argument) => new NativePromise((resolve, reject) => {
119
+ const request = { key, argument, resolve, reject, next: undefined };
120
+ if (last === undefined) {
121
+ first = last = request;
122
+ void enter(() => run(key, argument));
123
+ }
124
+ else {
125
+ last = last.next = request;
126
+ }
127
+ });
128
+ const generator = Object.create(AsyncGeneratorPrototype);
129
+ Object.defineProperty(generator, INVOKE, { value: invoke });
130
+ return generator;
131
+ }
132
+ //# sourceMappingURL=async-generator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"async-generator.js","sourceRoot":"","sources":["../../src/core/async-generator.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH;;;GAGG;AACH,MAAM,OAAO,aAAa;IACtB,CAAC,CAAW;IACH,CAAC,CAAS;IACnB,YAAY,KAAe,EAAE,IAAY;QACrC,IAAI,CAAC,CAAC,GAAG,KAAK,CAAC;QACf,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC;IAClB,CAAC;CACJ;AAsBD,sEAAsE;AACtE,MAAM,MAAM,GAAmB,MAAM,CAAC,8BAA8B,CAAC,CAAC;AAEtE,SAAS,QAAQ,CAAC,MAAe;IAC7B,MAAM,MAAM,GAAI,MAAiC,CAAC,MAAM,CAAC,CAAC;IAC1D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it has one.
5
5
  */
6
6
  export declare function coroutine<T, A extends unknown[], R>(generatorFunction: (this: T, ...args: A) => Generator<unknown, R, unknown>, name?: string): (this: T, ...args: A) => Promise<R>;
7
+ /**
8
+ * This function gives a function that makes an async generator object, which operates
9
+ * `generatorFunction`. The preset gives this function in place of the helper
10
+ * `wrapAsyncGenerator` of `@babel/plugin-transform-async-generator-functions`.
11
+ *
12
+ * Each step of the body operates in the context of the call that made the generator
13
+ * (rule C5). The driver continues each step with a native `await` (`async-generator.ts`).
14
+ */
15
+ export declare function asyncGenerator<T, A extends unknown[]>(generatorFunction: (this: T, ...args: A) => Generator<unknown, unknown, unknown>): (this: T, ...args: A) => AsyncGenerator<unknown, unknown, unknown>;
7
16
  type AnyIterator = Iterator<unknown, unknown, unknown> | AsyncIterator<unknown, unknown, unknown>;
8
17
  /**
9
18
  * This function binds a generator object to the current context. The body of
@@ -11,9 +20,10 @@ type AnyIterator = Iterator<unknown, unknown, unknown> | AsyncIterator<unknown,
11
20
  * proposal specifies (`GeneratorStart` and `GeneratorResume`). After each step,
12
21
  * the context of the caller is current again.
13
22
  *
14
- * The preset changes each generator function so that it gives
15
- * `bindGenerator(generator)`. The function works with sync generators and
16
- * with the async generators of `@babel/plugin-transform-async-generator-functions`.
23
+ * The preset changes each sync generator function so that it gives
24
+ * `bindGenerator(generator)`. An async generator does not need it, because
25
+ * `asyncGenerator` keeps the context itself. The function also works with
26
+ * async generator objects, for example in the output of older versions.
17
27
  */
18
28
  export declare function bindGenerator<I extends AnyIterator>(generator: I): I;
19
29
  export {};
@@ -1 +1 @@
1
- {"version":3,"file":"coroutine.d.ts","sourceRoot":"","sources":["../../src/core/coroutine.ts"],"names":[],"mappings":"AAyBA;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,CAAC,SAAS,OAAO,EAAE,EAAE,CAAC,EAC/C,iBAAiB,EAAG,CAAC,IAAI,EAAG,CAAC,EAAE,GAAG,IAAI,EAAG,CAAC,KAAK,SAAS,C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AAO,CAAC,GAAG,aAAa,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;AAGlG;;;;;;;;;;GAUG;AACH,wBAAgB,aAAa,CAAC,CAAC,SAAS,WAAW,EAAE,SAAS,EAAG,CAAC,GAAI,CAAC,CA2BtE"}
@@ -1,3 +1,4 @@
1
+ import { createAsyncGenerator, OverloadYield } from "./async-generator.js";
1
2
  import { applyWithHooks, store } from "./store.js";
2
3
  /**
3
4
  * The code from the Babel preset uses these runtime functions. Do not use them
@@ -86,15 +87,66 @@ export function coroutine(generatorFunction, name) {
86
87
  return drive();
87
88
  };
88
89
  }
90
+ /** This function gives `true` for a value that can have a `then` property: an object or a function. */
91
+ function isObject(value) {
92
+ return (typeof value === "object" && value !== null) || typeof value === "function";
93
+ }
94
+ /**
95
+ * This function gives a function that makes an async generator object, which operates
96
+ * `generatorFunction`. The preset gives this function in place of the helper
97
+ * `wrapAsyncGenerator` of `@babel/plugin-transform-async-generator-functions`.
98
+ *
99
+ * Each step of the body operates in the context of the call that made the generator
100
+ * (rule C5). The driver continues each step with a native `await` (`async-generator.ts`).
101
+ */
102
+ export function asyncGenerator(generatorFunction) {
103
+ const { Promise: NativePromise, promiseResolve } = store.intrinsics;
104
+ return function makeAsyncGenerator(...args) {
105
+ let frame = store.current;
106
+ const generator = generatorFunction.apply(this, args);
107
+ const methods = {
108
+ next: (argument) => generator.next(argument),
109
+ throw: (argument) => generator.throw(argument),
110
+ return: (argument) => generator.return(argument),
111
+ };
112
+ const step = (key, argument, resumed) => {
113
+ const previous = store.current;
114
+ store.current = frame;
115
+ try {
116
+ const hooks = resumed ? store.hooks : undefined;
117
+ const result = hooks === undefined
118
+ ? methods[key](argument)
119
+ : applyWithHooks(hooks, generatorFunction, "await", methods[key], undefined, [argument]);
120
+ // Read the "then" property of an awaited or yielded object in the context of the body, as await and yield do
121
+ const value = result.value;
122
+ if (value instanceof OverloadYield) {
123
+ if (isObject(value.v)) {
124
+ value.v = promiseResolve(value.v);
125
+ }
126
+ }
127
+ else if (isObject(value)) {
128
+ result.value = promiseResolve(value);
129
+ }
130
+ return result;
131
+ }
132
+ finally {
133
+ frame = store.current;
134
+ store.current = previous;
135
+ }
136
+ };
137
+ return createAsyncGenerator(step, NativePromise);
138
+ };
139
+ }
89
140
  /**
90
141
  * This function binds a generator object to the current context. The body of
91
142
  * the generator operates in the context of the call that made the generator, as the TC39
92
143
  * proposal specifies (`GeneratorStart` and `GeneratorResume`). After each step,
93
144
  * the context of the caller is current again.
94
145
  *
95
- * The preset changes each generator function so that it gives
96
- * `bindGenerator(generator)`. The function works with sync generators and
97
- * with the async generators of `@babel/plugin-transform-async-generator-functions`.
146
+ * The preset changes each sync generator function so that it gives
147
+ * `bindGenerator(generator)`. An async generator does not need it, because
148
+ * `asyncGenerator` keeps the context itself. The function also works with
149
+ * async generator objects, for example in the output of older versions.
98
150
  */
99
151
  export function bindGenerator(generator) {
100
152
  let frame = store.current;
@@ -1 +1 @@
1
- {"version":3,"file":"coroutine.js","sourceRoot":"","sources":["../../src/core/coroutine.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,KAAK,EAAc,MAAM,YAAY,CAAC;AAE/D;;;;;;;;;GASG;AAEH;;;;GAIG;AACH,SAAS,aAAa,CAAC,iBAA4B,EAAE,IAAyB;IAC1E,IAAI,IAAI,KAAK,SAAS,IAAI,iBAAiB,CAAC,IAAI,KAAK,IAAI,EAAE,CAAC;QACxD,MAAM,CAAC,cAAc,CAAC,iBAAiB,EAAE,MAAM,EAAE,EAAE,KAAK,EAAG,IAAI,EAAE,YAAY,EAAG,IAAI,EAAE,CAAC,CAAC;IAC5F,CAAC;IACD,OAAO,iBAAiB,CAAC;AAC7B,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,SAAS,CACrB,iBAA6E,EAC7E,IAAc;IAEd,MAAM,EAAE,cAAc,EAAE,GAAG,KAAK,CAAC,UAAU,CAAC;IAC5C,OAAO,SAAS,aAAa,CAAW,GAA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C,OAAO,CAAC;YACtB,KAAK,CAAC,OAAO,GAAG,QAAQ,CAAC;QAC7B,CAAC;IACL,CAAC,CAAC;IACF,wEAAwE;IACxE,8DAA8D;IAC9D,MAAM,OAAO,GAAG;QACZ,IAAI,CAAC,KAAgB,IAAc,OAAO,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC;QAClE,KAAK,CAAC,KAAgB,IAAc,OAAO,MAAM,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC;QACpE,MAAM,CAAC,KAAgB,IAAc,OAAO,MAAM,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC;KACzE,CAAC;IACF,MAAM,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,cAAc,CAAC,SAAS,CAAW,CAAM,CAAC;IAC7E,MAAM,CAAC,gBAAgB,CAAC,KAAK,EAAE;QAC3B,IAAI,EAAG,EAAE,KAAK,EAAG,OAAO,CAAC,IAAI,EAAE,QAAQ,EAAG,IAAI,EAAE,YAAY,EAAG,IAAI,EAAE;QACrE,KAAK,EAAG,EAAE,KAAK,EAAG,OAAO,CAAC,KAAK,EAAE,QAAQ,EAAG,IAAI,EAAE,YAAY,EAAG,IAAI,EAAE;QACvE,MAAM,EAAG,EAAE,KAAK,EAAG,OAAO,CAAC,MAAM,EAAE,QAAQ,EAAG,IAAI,EAAE,YAAY,EAAG,IAAI,EAAE;KAC5E,CAAC,CAAC;IACH,OAAO,KAAK,CAAC;AACjB,CAAC"}
@@ -1,11 +1,18 @@
1
+ export { OverloadYield } from "../core/async-generator.js";
1
2
  /**
2
3
  * This function gives an async function that operates `generatorFunction`. The function is
3
4
  * a native async function: each `await` of the source becomes one native `await`, with
4
5
  * the same microtask turns. An error of the parameters rejects the promise.
5
6
  */
6
7
  export declare function coroutine<T, A extends unknown[], R>(generatorFunction: (this: T, ...args: A) => Generator<unknown, R, unknown>): (this: T, ...args: A) => Promise<R>;
8
+ /**
9
+ * This function gives a function that makes an async generator object, which operates
10
+ * `generatorFunction`. The preset gives it in place of the helper `wrapAsyncGenerator` of
11
+ * Babel. The driver starts in a snapshot of the context of the creation (rule C5). The
12
+ * native `AsyncLocalStorage` keeps that context through the `await` of the driver.
13
+ */
14
+ export declare function asyncGenerator<T, A extends unknown[]>(generatorFunction: (this: T, ...args: A) => Generator<unknown, unknown, unknown>): (this: T, ...args: A) => AsyncGenerator<unknown, unknown, unknown>;
7
15
  type AnyIterator = Iterator<unknown, unknown, unknown> | AsyncIterator<unknown, unknown, unknown>;
8
16
  /** This function binds a generator object to the current context (rule C5). */
9
17
  export declare function bindGenerator<I extends AnyIterator>(generator: I): I;
10
- export {};
11
18
  //# sourceMappingURL=runtime.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../../src/node/runtime.ts"],"names":[],"mappings":"AAUA;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,CAAC,SAAS,OAAO,EAAE,EAAE,CAAC,EAAE,iBAAiB,EAAG,CAAC,IAAI,EAAG,CAAC,EAAE,GAAG,IAAI,EAAG,CAAC,KAAK,SAAS,CAAC,OAAO,EAAE,CAAC,EAAE,OAAO,CAAC,GAAI,CAAC,IAAI,EAAG,CAAC,EAAE,GAAG,IAAI,EAAG,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,CAkB1K;AAED,KAAK,WAAW,GAAG,QAAQ,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,GAAG,aAAa,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;AAGlG,+EAA+E;AAC/E,wBAAgB,aAAa,CAAC,CAAC,SAAS,WAAW,EAAE,SAAS,EAAG,CAAC,GAAI,CAAC,CAuBtE"}
1
+ {"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../../src/node/runtime.ts"],"names":[],"mappings":"AAWA,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAE3D;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,CAAC,SAAS,OAAO,EAAE,EAAE,CAAC,EAAE,iBAAiB,EAAG,CAAC,IAAI,EAAG,CAAC,EAAE,GAAG,IAAI,EAAG,CAAC,KAAK,SAAS,CAAC,OAAO,EAAE,CAAC,EAAE,OAAO,CAAC,GAAI,CAAC,IAAI,EAAG,CAAC,EAAE,GAAG,IAAI,EAAG,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,CAkB1K;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,CAAC,EAAE,CAAC,SAAS,OAAO,EAAE,EACjD,iBAAiB,EAAG,CAAC,IAAI,EAAG,CAAC,EAAE,GAAG,IAAI,EAAG,CAAC,KAAK,SAAS,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,GACnF,CAAC,IAAI,EAAG,CAAC,EAAE,GAAG,IAAI,EAAG,CAAC,KAAK,cAAc,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,CAcvE;AAED,KAAK,WAAW,GAAG,QAAQ,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,GAAG,aAAa,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;AAGlG,+EAA+E;AAC/E,wBAAgB,aAAa,CAAC,CAAC,SAAS,WAAW,EAAE,SAAS,EAAG,CAAC,GAAI,CAAC,CAuBtE"}
@@ -7,6 +7,8 @@
7
7
  * @packageDocumentation
8
8
  */
9
9
  import { AsyncLocalStorage } from "node:async_hooks";
10
+ import { createAsyncGenerator } from "../core/async-generator.js";
11
+ export { OverloadYield } from "../core/async-generator.js";
10
12
  /**
11
13
  * This function gives an async function that operates `generatorFunction`. The function is
12
14
  * a native async function: each `await` of the source becomes one native `await`, with
@@ -32,6 +34,28 @@ export function coroutine(generatorFunction) {
32
34
  return result.value;
33
35
  };
34
36
  }
37
+ /**
38
+ * This function gives a function that makes an async generator object, which operates
39
+ * `generatorFunction`. The preset gives it in place of the helper `wrapAsyncGenerator` of
40
+ * Babel. The driver starts in a snapshot of the context of the creation (rule C5). The
41
+ * native `AsyncLocalStorage` keeps that context through the `await` of the driver.
42
+ */
43
+ export function asyncGenerator(generatorFunction) {
44
+ return function makeAsyncGenerator(...args) {
45
+ let snapshot = AsyncLocalStorage.snapshot();
46
+ const generator = generatorFunction.apply(this, args);
47
+ const step = (key, argument) => {
48
+ try {
49
+ return generator[key](argument);
50
+ }
51
+ finally {
52
+ // The body can change its context with enterWith before it stops
53
+ snapshot = AsyncLocalStorage.snapshot();
54
+ }
55
+ };
56
+ return createAsyncGenerator(step, Promise, (start) => snapshot(start));
57
+ };
58
+ }
35
59
  /** This function binds a generator object to the current context (rule C5). */
36
60
  export function bindGenerator(generator) {
37
61
  let run = AsyncLocalStorage.snapshot();
@@ -1 +1 @@
1
- {"version":3,"file":"runtime.js","sourceRoot":"","sources":["../../src/node/runtime.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAErD;;;;GAIG;AACH,MAAM,UAAU,SAAS,CAA4B,iBAA6E;IAC9H,OAAO,KAAK,UAAU,aAAa,CAAW,GAAG,IAAQ;QACrD,+FAA+F;QAC/F,+BAA+B,CAAC;QAChC,MAAM,SAAS,GAAG,iBAAiB,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QACtD,IAAI,MAAM,GAAG,SAAS,CAAC,IAAI,EAAE,CAAC;QAC9B,OAAO,MAAM,CAAC,IAAI,KAAK,IAAI,EAAE,CAAC;YAC1B,IAAI,KAAe,CAAC;YACpB,IAAI,CAAC;gBACD,KAAK,GAAG,MAAM,MAAM,CAAC,KAAK,CAAC;YAC/B,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACb,MAAM,GAAG,SAAS,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;gBAChC,SAAS;YACb,CAAC;YACD,MAAM,G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QAAQ,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC;KACzE,CAAC;IACF,MAAM,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,cAAc,CAAC,SAAS,CAAW,CAAM,CAAC;IAC7E,MAAM,CAAC,gBAAgB,CAAC,KAAK,EAAE;QAC3B,IAAI,EAAG,EAAE,KAAK,EAAG,OAAO,CAAC,IAAI,EAAE,QAAQ,EAAG,IAAI,EAAE,YAAY,EAAG,IAAI,EAAE;QACrE,KAAK,EAAG,EAAE,KAAK,EAAG,OAAO,CAAC,KAAK,EAAE,QAAQ,EAAG,IAAI,EAAE,YAAY,EAAG,IAAI,EAAE;QACvE,MAAM,EAAG,EAAE,KAAK,EAAG,OAAO,CAAC,MAAM,EAAE,QAAQ,EAAG,IAAI,EAAE,YAAY,EAAG,IAAI,EAAE;KAC5E,CAAC,CAAC;IACH,OAAO,KAAK,CAAC;AACjB,CAAC"}
package/dist/runtime.d.ts CHANGED
@@ -1,10 +1,12 @@
1
1
  /**
2
2
  * The runtime of the code that the Babel preset transforms. The transformed
3
- * code imports `coroutine` and `bindGenerator` from this module. When the
3
+ * code imports `coroutine`, `bindGenerator`, `asyncGenerator` and
4
+ * `OverloadYield` from this module. When the
4
5
  * module loads, it installs the patches of the runtime.
5
6
  *
6
7
  * @packageDocumentation
7
8
  */
8
9
  import "./install.js";
9
- export { bindGenerator, coroutine } from "./core/coroutine.js";
10
+ export { OverloadYield } from "./core/async-generator.js";
11
+ export { asyncGenerator, bindGenerator, coroutine } from "./core/coroutine.js";
10
12
  //# sourceMappingURL=runtime.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../src/runtime.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,cAAc,CAAC;AAEtB,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC"}
1
+ {"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../src/runtime.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,cAAc,CAAC;AAEtB,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAC1D,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC"}
package/dist/runtime.js CHANGED
@@ -1,10 +1,12 @@
1
1
  /**
2
2
  * The runtime of the code that the Babel preset transforms. The transformed
3
- * code imports `coroutine` and `bindGenerator` from this module. When the
3
+ * code imports `coroutine`, `bindGenerator`, `asyncGenerator` and
4
+ * `OverloadYield` from this module. When the
4
5
  * module loads, it installs the patches of the runtime.
5
6
  *
6
7
  * @packageDocumentation
7
8
  */
8
9
  import "./install.js";
9
- export { bindGenerator, coroutine } from "./core/coroutine.js";
10
+ export { OverloadYield } from "./core/async-generator.js";
11
+ export { asyncGenerator, bindGenerator, coroutine } from "./core/coroutine.js";
10
12
  //# sourceMappingURL=runtime.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"runtime.js","sourceRoot":"","sources":["../src/runtime.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,cAAc,CAAC;AAEtB,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC"}
1
+ {"version":3,"file":"runtime.js","sourceRoot":"","sources":["../src/runtime.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,cAAc,CAAC;AAEtB,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAC1D,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "async-browser-context",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "description": "AsyncLocalStorage and the TC39 AsyncContext API (Variable, Snapshot) for browsers. The context stays through await, promises, timers, events and generators. A Babel preset and a Vite plugin transform async functions. On Node.js, the package uses the native AsyncLocalStorage.",
5
5
  "license": "MIT",
6
6
  "author": "Mark Russell",
@@ -0,0 +1,49 @@
1
+ import { types, type PluginObject, type PluginPass } from "@babel/core";
2
+ import { addNamed } from "@babel/helper-module-imports";
3
+
4
+ /** The options of the plugin. */
5
+ export interface AsyncGeneratorHelpersOptions {
6
+ /** The module that exports `asyncGenerator` and `OverloadYield`. */
7
+ readonly runtime : string;
8
+ }
9
+
10
+ /** The Babel helpers that the runtime replaces, and the export of the runtime for each one. */
11
+ const REPLACED_HELPERS : Readonly<Record<string, string>> = {
12
+ wrapAsyncGenerator : "asyncGenerator",
13
+ OverloadYield : "OverloadYield",
14
+ };
15
+
16
+ type HelperGenerator = (name : string) => types.Expression | undefined;
17
+
18
+ /**
19
+ * The Babel plugin that gives the async generators to the driver of the runtime. It sets the
20
+ * `helperGenerator` of the file, as `@babel/plugin-transform-runtime` does. Thus, the helpers
21
+ * `wrapAsyncGenerator` and `OverloadYield` of `@babel/plugin-transform-async-generator-functions`
22
+ * become imports of `asyncGenerator` and `OverloadYield` from the runtime. The helpers `awaitAsyncGenerator` and `asyncGeneratorDelegate` stay in the
23
+ * file, and they make the `OverloadYield` values of the runtime.
24
+ *
25
+ * The plugin keeps a `helperGenerator` of an earlier plugin for the other helpers.
26
+ */
27
+ export function asyncGeneratorHelpersPlugin(_api : unknown, options : object) : PluginObject<PluginPass> {
28
+ const { runtime } = options as AsyncGeneratorHelpersOptions;
29
+ return {
30
+ name : "async-browser-context/async-generator-helpers",
31
+ pre(file : PluginPass["file"]) {
32
+ const previous = file.get("helperGenerator") as HelperGenerator | undefined;
33
+ const imports = new Map<string, types.Expression>();
34
+ const generator : HelperGenerator = (name) => {
35
+ const exported = REPLACED_HELPERS[name];
36
+ if (exported === undefined) {
37
+ return previous?.(name);
38
+ }
39
+ let local = imports.get(exported);
40
+ if (local === undefined) {
41
+ local = addNamed(file.path, exported, runtime, { nameHint : exported }) as types.Expression;
42
+ imports.set(exported, local);
43
+ }
44
+ return types.cloneNode(local);
45
+ };
46
+ file.set("helperGenerator", generator);
47
+ },
48
+ };
49
+ }
@@ -10,8 +10,12 @@ export interface BindGeneratorsOptions {
10
10
  /** The key of the file metadata that tells if the preset changed the file. */
11
11
  export const METADATA_KEY = "asyncBrowserContext";
12
12
 
13
- /** The names of the helper of `@babel/plugin-transform-async-generator-functions`. Babel gives them an underscore. */
14
- const ASYNC_GENERATOR_HELPER = /^_wrapAsyncGenerator\d*$/;
13
+ /**
14
+ * The local names of the async generator helper: `wrapAsyncGenerator` of
15
+ * `@babel/plugin-transform-async-generator-functions`, or `asyncGenerator` of the runtime,
16
+ * which the preset gives in its place. Babel gives them an underscore.
17
+ */
18
+ const ASYNC_GENERATOR_HELPER = /^_(wrapAsyncGenerator|asyncGenerator)\d*$/;
15
19
  /** The local names that `addNamed` gives to `bindGenerator`. */
16
20
  const BIND_GENERATOR_NAME = /^_bindGenerator\d*$/;
17
21
 
@@ -20,8 +24,15 @@ type FunctionPath = NodePath<types.Function>;
20
24
  /** This function gives `true` if `path` is the generator that Babel gives to its async generator helper. */
21
25
  function isAsyncGeneratorHelperArgument(path : FunctionPath) : boolean {
22
26
  const parent = path.parent;
23
- // Stryker disable next-line LogicalOperator: without the call check, the name test also fails, because a member callee has no name
24
- return types.isCallExpression(parent) && types.isIdentifier(parent.callee) && ASYNC_GENERATOR_HELPER.test(parent.callee.name);
27
+ if (!types.isCallExpression(parent)) {
28
+ return false;
29
+ }
30
+ if (types.isIdentifier(parent.callee)) {
31
+ return ASYNC_GENERATOR_HELPER.test(parent.callee.name);
32
+ }
33
+ // CommonJS output of the preset: _runtime.asyncGenerator or (0, _runtime.asyncGenerator)
34
+ const member = types.isSequenceExpression(parent.callee) ? parent.callee.expressions.at(-1) : parent.callee;
35
+ return types.isMemberExpression(member) && types.isIdentifier(member.property, { name : "asyncGenerator" });
25
36
  }
26
37
 
27
38
  /**
@@ -101,7 +112,8 @@ export function bindGeneratorsPlugin(_api : unknown, options : object) : PluginO
101
112
  if (node.async) {
102
113
  changed = true;
103
114
  }
104
- if (!node.generator || isAsyncGeneratorHelperArgument(path) || isAlreadyBound(path, runtime)) {
115
+ // The driver of the runtime keeps the context of an async generator (asyncGenerator)
116
+ if (!node.generator || node.async || isAsyncGeneratorHelperArgument(path) || isAlreadyBound(path, runtime)) {
105
117
  return;
106
118
  }
107
119
  changed = true;
@@ -1,6 +1,7 @@
1
1
  import type { PluginItem, PresetAPI, PresetObject } from "@babel/core";
2
2
  import asyncGeneratorFunctions from "@babel/plugin-transform-async-generator-functions";
3
3
  import asyncToGenerator from "@babel/plugin-transform-async-to-generator";
4
+ import { asyncGeneratorHelpersPlugin } from "./async-generator-helpers.js";
4
5
  import { bindGeneratorsPlugin } from "./bind-generators.js";
5
6
  import { nameCoroutinesPlugin } from "./name-coroutines.js";
6
7
  import { skipRuntimePlugin } from "./skip-runtime.js";
@@ -33,14 +34,16 @@ export const DEFAULT_RUNTIME = "async-browser-context/runtime";
33
34
  *
34
35
  * 1. A plugin of this library skips the native async functions of the
35
36
  * runtime, which a directive marks.
36
- * 2. A plugin of this library binds each generator to the context of its
37
- * creation.
37
+ * 2. A plugin of this library binds each sync generator to the context of
38
+ * its creation.
38
39
  * 3. A plugin of this library gives the name of each async function to
39
40
  * `coroutine`, for the callback hooks. The option `names: false` removes
40
41
  * this step.
41
- * 4. `@babel/plugin-transform-async-generator-functions` changes async
42
+ * 4. A plugin of this library gives the async generators to `asyncGenerator`
43
+ * of the runtime, in place of the Babel helper.
44
+ * 5. `@babel/plugin-transform-async-generator-functions` changes async
42
45
  * generators and `for await` loops.
43
- * 5. `@babel/plugin-transform-async-to-generator` changes each async function
46
+ * 6. `@babel/plugin-transform-async-to-generator` changes each async function
44
47
  * into a generator that the `coroutine` function of the runtime operates.
45
48
  *
46
49
  * The preset operates with Babel 7.22 and later and with Babel 8. Put it last
@@ -55,6 +58,7 @@ export default function asyncBrowserContextPreset(api : PresetAPI, options : Pre
55
58
  skipRuntimePlugin,
56
59
  [bindGeneratorsPlugin, { runtime }],
57
60
  ...names,
61
+ [asyncGeneratorHelpersPlugin, { runtime }],
58
62
  asyncGeneratorFunctions,
59
63
  [asyncToGenerator, { module : runtime, method : "coroutine" }],
60
64
  ],
@@ -0,0 +1,159 @@
1
+ /**
2
+ * The async generators of transformed code. `@babel/plugin-transform-async-generator-functions`
3
+ * changes each async generator into a sync generator and its helper `wrapAsyncGenerator`.
4
+ * The helper continued each step in a `then` callback, and a `then` callback is slow in
5
+ * Firefox. The preset replaces the helper with `asyncGenerator` of the runtime, and the
6
+ * helper `OverloadYield` with the class of this module. The driver in this module then
7
+ * continues each step with a native `await`.
8
+ *
9
+ * The protocol is the protocol of the Babel 8 helper: the body gives
10
+ * `new OverloadYield(value, 0)` for an `await`, and `new OverloadYield(promise, 1)` for a
11
+ * step of `yield*`. Each other value is a `yield`.
12
+ */
13
+
14
+ /**
15
+ * The value that the body gives for an `await` (kind 0) or for a step of `yield*` (kind 1).
16
+ * The Babel helpers `awaitAsyncGenerator` and `asyncGeneratorDelegate` make it with `new`.
17
+ */
18
+ export class OverloadYield {
19
+ v : unknown;
20
+ readonly k : 0 | 1;
21
+ constructor(value : unknown, kind : 0 | 1) {
22
+ this.v = value;
23
+ this.k = kind;
24
+ }
25
+ }
26
+
27
+ /** The methods of a generator that a request of the consumer starts. */
28
+ export type ResumeKey = "next" | "throw" | "return";
29
+
30
+ /**
31
+ * This function does one step of the body: `generator[key](argument)`. The browser runtime sets
32
+ * the context of the generator around the step. `resumed` is `true` for a step after an
33
+ * `await` of the driver.
34
+ */
35
+ export type AsyncGeneratorStep = (key : ResumeKey, argument : unknown, resumed : boolean) => IteratorResult<unknown, unknown>;
36
+
37
+ interface Request {
38
+ readonly key : ResumeKey;
39
+ readonly argument : unknown;
40
+ readonly resolve : (result : IteratorResult<unknown, unknown>) => void;
41
+ readonly reject : (error : unknown) => void;
42
+ next : Request | undefined;
43
+ }
44
+
45
+ type Invoke = (key : ResumeKey, argument : unknown) => Promise<IteratorResult<unknown, unknown>>;
46
+
47
+ /** The key of the request function of each async generator object. */
48
+ const INVOKE : unique symbol = Symbol("async-browser-context/invoke");
49
+
50
+ function invokeOf(target : object) : Invoke {
51
+ const invoke = (target as { [INVOKE]? : Invoke })[INVOKE];
52
+ if (invoke === undefined) {
53
+ throw new TypeError("The object is not an async generator of async-browser-context");
54
+ }
55
+ return invoke;
56
+ }
57
+
58
+ /** The prototype of the async generator objects of transformed code. */
59
+ const AsyncGeneratorPrototype = {
60
+ next(this : object, value? : unknown) : Promise<IteratorResult<unknown, unknown>> {
61
+ return invokeOf(this)("next", value);
62
+ },
63
+ throw(this : object, error? : unknown) : Promise<IteratorResult<unknown, unknown>> {
64
+ return invokeOf(this)("throw", error);
65
+ },
66
+ return(this : object, value? : unknown) : Promise<IteratorResult<unknown, unknown>> {
67
+ return invokeOf(this)("return", value);
68
+ },
69
+ [Symbol.asyncIterator]<T>(this : T) : T {
70
+ return this;
71
+ },
72
+ [Symbol.toStringTag] : "AsyncGenerator",
73
+ };
74
+
75
+ /**
76
+ * This function makes an async generator object. The object queues the requests of the
77
+ * consumer, and starts one request at a time, as the Babel helper does. `enter` starts the
78
+ * driver: the Node.js runtime starts it in the context of the generator.
79
+ */
80
+ export function createAsyncGenerator(
81
+ step : AsyncGeneratorStep,
82
+ NativePromise : PromiseConstructor,
83
+ enter : (start : () => Promise<void>) => Promise<void> = (start) => start(),
84
+ ) : AsyncGenerator<unknown, unknown, unknown> {
85
+ let first : Request | undefined;
86
+ let last : Request | undefined;
87
+
88
+ /** This function starts the requests in the queue, one after the other, until the queue is empty. */
89
+ const run = async (key : ResumeKey, argument : unknown) : Promise<void> => {
90
+ // The directive tells the Babel preset not to change this function (src/babel/skip-runtime.ts)
91
+ "async-browser-context runtime";
92
+ let resumed = false;
93
+ for (;;) {
94
+ let failed = false;
95
+ let settled : unknown;
96
+ let done = false;
97
+ try {
98
+ let result = step(key, argument, resumed);
99
+ for (;;) {
100
+ const yielded : unknown = result.value;
101
+ const overloaded = yielded instanceof OverloadYield;
102
+ let value : unknown;
103
+ try {
104
+ value = await (overloaded ? yielded.v : yielded);
105
+ } catch (error) {
106
+ resumed = true;
107
+ key = "throw";
108
+ result = step("throw", error, true);
109
+ continue;
110
+ }
111
+ resumed = true;
112
+ if (overloaded) {
113
+ const nextKey : ResumeKey = key === "return" && yielded.k === 1 ? "return" : "next";
114
+ if (yielded.k === 0 || Boolean((value as { done? : unknown }).done)) {
115
+ key = nextKey;
116
+ result = step(nextKey, value, true);
117
+ continue;
118
+ }
119
+ value = step(nextKey, value, true).value;
120
+ }
121
+ settled = value;
122
+ done = result.done === true;
123
+ break;
124
+ }
125
+ } catch (error) {
126
+ failed = true;
127
+ settled = error;
128
+ }
129
+ // A request always waits in the queue while the driver runs
130
+ const request = first as Request;
131
+ first = request.next;
132
+ if (failed) {
133
+ request.reject(settled);
134
+ } else {
135
+ request.resolve({ value : settled, done });
136
+ }
137
+ if (first === undefined) {
138
+ last = undefined;
139
+ return;
140
+ }
141
+ key = first.key;
142
+ argument = first.argument;
143
+ }
144
+ };
145
+
146
+ const invoke : Invoke = (key, argument) => new NativePromise((resolve, reject) => {
147
+ const request : Request = { key, argument, resolve, reject, next : undefined };
148
+ if (last === undefined) {
149
+ first = last = request;
150
+ void enter(() => run(key, argument));
151
+ } else {
152
+ last = last.next = request;
153
+ }
154
+ });
155
+
156
+ const generator = Object.create(AsyncGeneratorPrototype) as AsyncGenerator<unknown, unknown, unknown>;
157
+ Object.defineProperty(generator, INVOKE, { value : invoke });
158
+ return generator;
159
+ }
@@ -1,3 +1,4 @@
1
+ import { createAsyncGenerator, OverloadYield } from "./async-generator.js";
1
2
  import { applyWithHooks, store, type Frame } from "./store.js";
2
3
 
3
4
  /**
@@ -93,6 +94,58 @@ export function coroutine<T, A extends unknown[], R>(
93
94
  };
94
95
  }
95
96
 
97
+ /** This function gives `true` for a value that can have a `then` property: an object or a function. */
98
+ function isObject(value : unknown) : value is object {
99
+ return (typeof value === "object" && value !== null) || typeof value === "function";
100
+ }
101
+
102
+ /**
103
+ * This function gives a function that makes an async generator object, which operates
104
+ * `generatorFunction`. The preset gives this function in place of the helper
105
+ * `wrapAsyncGenerator` of `@babel/plugin-transform-async-generator-functions`.
106
+ *
107
+ * Each step of the body operates in the context of the call that made the generator
108
+ * (rule C5). The driver continues each step with a native `await` (`async-generator.ts`).
109
+ */
110
+ export function asyncGenerator<T, A extends unknown[]>(
111
+ generatorFunction : (this : T, ...args : A) => Generator<unknown, unknown, unknown>,
112
+ ) : (this : T, ...args : A) => AsyncGenerator<unknown, unknown, unknown> {
113
+ const { Promise : NativePromise, promiseResolve } = store.intrinsics;
114
+ return function makeAsyncGenerator(this : T, ...args : A) : AsyncGenerator<unknown, unknown, unknown> {
115
+ let frame : Frame = store.current;
116
+ const generator = generatorFunction.apply(this, args);
117
+ const methods : Record<ResumeMethod, (argument : unknown) => IteratorResult<unknown, unknown>> = {
118
+ next : (argument) => generator.next(argument),
119
+ throw : (argument) => generator.throw(argument),
120
+ return : (argument) => generator.return(argument),
121
+ };
122
+ const step = (key : ResumeMethod, argument : unknown, resumed : boolean) : IteratorResult<unknown, unknown> => {
123
+ const previous = store.current;
124
+ store.current = frame;
125
+ try {
126
+ const hooks = resumed ? store.hooks : undefined;
127
+ const result = hooks === undefined
128
+ ? methods[key](argument)
129
+ : applyWithHooks(hooks, generatorFunction, "await", methods[key], undefined, [argument]);
130
+ // Read the "then" property of an awaited or yielded object in the context of the body, as await and yield do
131
+ const value : unknown = result.value;
132
+ if (value instanceof OverloadYield) {
133
+ if (isObject(value.v)) {
134
+ value.v = promiseResolve(value.v);
135
+ }
136
+ } else if (isObject(value)) {
137
+ (result as { value : unknown }).value = promiseResolve(value);
138
+ }
139
+ return result;
140
+ } finally {
141
+ frame = store.current;
142
+ store.current = previous;
143
+ }
144
+ };
145
+ return createAsyncGenerator(step, NativePromise);
146
+ };
147
+ }
148
+
96
149
  type AnyIterator = Iterator<unknown, unknown, unknown> | AsyncIterator<unknown, unknown, unknown>;
97
150
  type ResumeMethod = "next" | "throw" | "return";
98
151
 
@@ -102,9 +155,10 @@ type ResumeMethod = "next" | "throw" | "return";
102
155
  * proposal specifies (`GeneratorStart` and `GeneratorResume`). After each step,
103
156
  * the context of the caller is current again.
104
157
  *
105
- * The preset changes each generator function so that it gives
106
- * `bindGenerator(generator)`. The function works with sync generators and
107
- * with the async generators of `@babel/plugin-transform-async-generator-functions`.
158
+ * The preset changes each sync generator function so that it gives
159
+ * `bindGenerator(generator)`. An async generator does not need it, because
160
+ * `asyncGenerator` keeps the context itself. The function also works with
161
+ * async generator objects, for example in the output of older versions.
108
162
  */
109
163
  export function bindGenerator<I extends AnyIterator>(generator : I) : I {
110
164
  let frame : Frame = store.current;
@@ -7,6 +7,9 @@
7
7
  * @packageDocumentation
8
8
  */
9
9
  import { AsyncLocalStorage } from "node:async_hooks";
10
+ import { createAsyncGenerator, type ResumeKey } from "../core/async-generator.js";
11
+
12
+ export { OverloadYield } from "../core/async-generator.js";
10
13
 
11
14
  /**
12
15
  * This function gives an async function that operates `generatorFunction`. The function is
@@ -33,6 +36,30 @@ export function coroutine<T, A extends unknown[], R>(generatorFunction : (this :
33
36
  };
34
37
  }
35
38
 
39
+ /**
40
+ * This function gives a function that makes an async generator object, which operates
41
+ * `generatorFunction`. The preset gives it in place of the helper `wrapAsyncGenerator` of
42
+ * Babel. The driver starts in a snapshot of the context of the creation (rule C5). The
43
+ * native `AsyncLocalStorage` keeps that context through the `await` of the driver.
44
+ */
45
+ export function asyncGenerator<T, A extends unknown[]>(
46
+ generatorFunction : (this : T, ...args : A) => Generator<unknown, unknown, unknown>,
47
+ ) : (this : T, ...args : A) => AsyncGenerator<unknown, unknown, unknown> {
48
+ return function makeAsyncGenerator(this : T, ...args : A) : AsyncGenerator<unknown, unknown, unknown> {
49
+ let snapshot = AsyncLocalStorage.snapshot();
50
+ const generator = generatorFunction.apply(this, args);
51
+ const step = (key : ResumeKey, argument : unknown) : IteratorResult<unknown, unknown> => {
52
+ try {
53
+ return generator[key](argument);
54
+ } finally {
55
+ // The body can change its context with enterWith before it stops
56
+ snapshot = AsyncLocalStorage.snapshot();
57
+ }
58
+ };
59
+ return createAsyncGenerator(step, Promise, (start) => snapshot(start));
60
+ };
61
+ }
62
+
36
63
  type AnyIterator = Iterator<unknown, unknown, unknown> | AsyncIterator<unknown, unknown, unknown>;
37
64
  type ResumeMethod = "next" | "throw" | "return";
38
65
 
package/src/runtime.ts CHANGED
@@ -1,10 +1,12 @@
1
1
  /**
2
2
  * The runtime of the code that the Babel preset transforms. The transformed
3
- * code imports `coroutine` and `bindGenerator` from this module. When the
3
+ * code imports `coroutine`, `bindGenerator`, `asyncGenerator` and
4
+ * `OverloadYield` from this module. When the
4
5
  * module loads, it installs the patches of the runtime.
5
6
  *
6
7
  * @packageDocumentation
7
8
  */
8
9
  import "./install.js";
9
10
 
10
- export { bindGenerator, coroutine } from "./core/coroutine.js";
11
+ export { OverloadYield } from "./core/async-generator.js";
12
+ export { asyncGenerator, bindGenerator, coroutine } from "./core/coroutine.js";