async-browser-context 0.2.3 → 0.2.4

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # Change history
2
2
 
3
+ ## 0.2.4
4
+
5
+ ### Corrections
6
+
7
+ - `yield*` in a transformed async generator gave a promise in place of each object of the delegate. Version 0.2.3 had this error. The driver does not change the result of a `yield*` step at this time.
8
+ - Transformed code can await a thenable that is not a native promise, or give it as the result of a function. Its `then` method operates in the context of the body at this time. Before, only the read of `then` had the context, and the call had the root context. A query builder that starts its request in `then`, for example of supabase-js, gets the context, as on Node.js.
9
+ - An `enterWith()` in the synchronous part of a transformed async function stays for the caller, as on Node.js.
10
+ - An async generator of transformed code is closer to a native async generator:
11
+ - `return(value)` at a `yield` awaits the value before the `finally` blocks. A rejection goes into the body at the `yield`.
12
+ - `next()` of a complete generator settles before a later microtask.
13
+ - Its methods give a rejected promise for an object that is not an async generator.
14
+ - The object inherits from `%AsyncIteratorPrototype%`, so `Symbol.asyncDispose` and the async iterator helpers are available, and its methods are not enumerable.
15
+ - An error of the `then` getter of an awaited object goes into the body at the `await`.
16
+
3
17
  ## 0.2.3
4
18
 
5
19
  ### Corrections
@@ -8,29 +8,49 @@
8
8
  *
9
9
  * The protocol is the protocol of the Babel 8 helper: the body gives
10
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`.
11
+ * step of `yield*`. Each other value is a `yield`. Where the helper differs from a native async
12
+ * generator, the driver does as the native generator. Thus `return()` at a `yield` awaits its
13
+ * value first, and a request on a complete generator settles at once.
12
14
  */
13
15
  /**
14
16
  * The value that the body gives for an `await` (kind 0) or for a step of `yield*` (kind 1).
15
17
  * The Babel helpers `awaitAsyncGenerator` and `asyncGeneratorDelegate` make it with `new`.
16
18
  */
17
19
  export declare class OverloadYield {
18
- v: unknown;
20
+ readonly v: unknown;
19
21
  readonly k: 0 | 1;
20
22
  constructor(value: unknown, kind: 0 | 1);
21
23
  }
24
+ /**
25
+ * A value that is not a thenable, in a box. The browser runtime reads the `then` property of an
26
+ * awaited object one time, in the context of the body. When the object has no `then` method,
27
+ * the driver awaits the box. Thus the engine does not read `then` a second time, and the
28
+ * `await` takes the same microtask turns.
29
+ */
30
+ export declare class PlainValue {
31
+ readonly value: unknown;
32
+ constructor(value: unknown);
33
+ }
34
+ /** This function gives the value in a box, or the value itself. */
35
+ export declare function unbox(value: unknown): unknown;
22
36
  /** The methods of a generator that a request of the consumer starts. */
23
37
  export type ResumeKey = "next" | "throw" | "return";
24
38
  /**
25
39
  * This function does one step of the body: `generator[key](argument)`. The browser runtime sets
26
40
  * the context of the generator around the step. `resumed` is `true` for a step after an
27
- * `await` of the driver.
41
+ * `await` of the driver. The function gives the result of the generator without a change.
28
42
  */
29
43
  export type AsyncGeneratorStep = (key: ResumeKey, argument: unknown, resumed: boolean) => IteratorResult<unknown, unknown>;
44
+ /**
45
+ * This function gives the value that the driver awaits in place of `value`. The browser runtime
46
+ * reads the `then` property in the context of the generator. The Node.js runtime gives the
47
+ * value without a change.
48
+ */
49
+ export type Prepare = (value: unknown) => unknown;
30
50
  /**
31
51
  * 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.
52
+ * consumer, and starts one request at a time, as a native async generator does. `enter`
53
+ * starts the driver: the Node.js runtime starts it in the context of the generator.
34
54
  */
35
- export declare function createAsyncGenerator(step: AsyncGeneratorStep, NativePromise: PromiseConstructor, enter?: (start: () => Promise<void>) => Promise<void>): AsyncGenerator<unknown, unknown, unknown>;
55
+ export declare function createAsyncGenerator(step: AsyncGeneratorStep, PromiseConstructor: PromiseConstructor, enter?: (start: () => Promise<void>) => Promise<void>, prepare?: Prepare): AsyncGenerator<unknown, unknown, unknown>;
36
56
  //# sourceMappingURL=async-generator.d.ts.map
@@ -1 +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"}
1
+ {"version":3,"file":"async-generator.d.ts","sourceRoot":"","sources":["../../src/core/async-generator.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAKH;;;GAGG;AACH,qBAAa,aAAa;IACtB,QAAQ,CAAC,CAAC,EAAG,OAAO,CAAC;IACrB,QAAQ,CAAC,CAAC,EAAG,CAAC,GAAG,CAAC,CAAC;gBACP,KAAK,EAAG,OAAO,EAAE,IAAI,EAAG,CAAC,GAAG,CAAC;CAI5C;AAED;;;;;GAKG;AACH,qBAAa,UAAU;IACnB,QAAQ,CAAC,KAAK,EAAG,OAAO,CAAC;gBACb,KAAK,EAAG,OAAO;CAG9B;AAED,mEAAmE;AACnE,wBAAgB,KAAK,CAAC,KAAK,EAAG,OAAO,GAAI,OAAO,CAE/C;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;AAE9H;;;;GAIG;AACH,MAAM,MAAM,OAAO,GAAG,CAAC,KAAK,EAAG,OAAO,KAAK,OAAO,CAAC;AA8DnD;;;;GAIG;AACH,wBAAgB,oBAAoB,CAChC,IAAI,EAAG,kBAAkB,EACzB,kBAAkB,EAAG,kBAAkB,EACvC,KAAK,GAAG,CAAC,KAAK,EAAG,MAAM,OAAO,CAAC,IAAI,CAAC,KAAK,OAAO,CAAC,IAAI,CAAsB,EAC3E,OAAO,GAAG,OAA0B,GACpC,cAAc,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,CA8G5C"}
@@ -8,8 +8,12 @@
8
8
  *
9
9
  * The protocol is the protocol of the Babel 8 helper: the body gives
10
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`.
11
+ * step of `yield*`. Each other value is a `yield`. Where the helper differs from a native async
12
+ * generator, the driver does as the native generator. Thus `return()` at a `yield` awaits its
13
+ * value first, and a request on a complete generator settles at once.
12
14
  */
15
+ /** The Promise of the time when the module loads, for the rejections of the methods. */
16
+ const NativePromise = Promise;
13
17
  /**
14
18
  * The value that the body gives for an `await` (kind 0) or for a step of `yield*` (kind 1).
15
19
  * The Babel helpers `awaitAsyncGenerator` and `asyncGeneratorDelegate` make it with `new`.
@@ -22,42 +26,83 @@ export class OverloadYield {
22
26
  this.k = kind;
23
27
  }
24
28
  }
29
+ /**
30
+ * A value that is not a thenable, in a box. The browser runtime reads the `then` property of an
31
+ * awaited object one time, in the context of the body. When the object has no `then` method,
32
+ * the driver awaits the box. Thus the engine does not read `then` a second time, and the
33
+ * `await` takes the same microtask turns.
34
+ */
35
+ export class PlainValue {
36
+ value;
37
+ constructor(value) {
38
+ this.value = value;
39
+ }
40
+ }
41
+ /** This function gives the value in a box, or the value itself. */
42
+ export function unbox(value) {
43
+ return value instanceof PlainValue ? value.value : value;
44
+ }
25
45
  /** The key of the request function of each async generator object. */
26
46
  const INVOKE = Symbol("async-browser-context/invoke");
27
- function invokeOf(target) {
28
- const invoke = target[INVOKE];
47
+ /** This function starts a request. For an object that is not an async generator of the runtime, it gives a rejected promise, as a native method does. */
48
+ function request(target, key, argument) {
49
+ const invoke = typeof target === "object" && target !== null ? target[INVOKE] : undefined;
29
50
  if (invoke === undefined) {
30
- throw new TypeError("The object is not an async generator of async-browser-context");
51
+ return NativePromise.reject(new TypeError(`AsyncGenerator.prototype.${key} started on an object that is not an async generator`));
52
+ }
53
+ return invoke(key, argument);
54
+ }
55
+ /**
56
+ * This function gives %AsyncIteratorPrototype% of the engine, which gives `Symbol.asyncIterator`,
57
+ * `Symbol.asyncDispose` and the async iterator helpers. If a compiler changed the async
58
+ * generator below, the function gives `undefined`.
59
+ */
60
+ function asyncIteratorPrototype() {
61
+ try {
62
+ const generatorFunction = async function* () {
63
+ // The directive tells the Babel preset not to change this function (src/babel/skip-runtime.ts)
64
+ // Stryker disable next-line all: the directive matters only when a bundler applies the preset to the runtime
65
+ "async-browser-context runtime";
66
+ };
67
+ const prototype = Object.getPrototypeOf(Object.getPrototypeOf(generatorFunction.prototype));
68
+ return prototype !== null && typeof prototype[Symbol.asyncIterator] === "function" ? prototype : undefined;
69
+ }
70
+ catch {
71
+ return undefined;
31
72
  }
32
- return invoke;
33
73
  }
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
- };
74
+ /** The prototype of the async generator objects of transformed code. Its methods are not enumerable, as native methods. */
75
+ const AsyncGeneratorPrototype = (() => {
76
+ const parent = asyncIteratorPrototype();
77
+ const prototype = Object.create(parent ?? Object.prototype);
78
+ const methods = {
79
+ next(value) { return request(this, "next", value); },
80
+ throw(error) { return request(this, "throw", error); },
81
+ return(value) { return request(this, "return", value); },
82
+ };
83
+ for (const key of ["next", "throw", "return"]) {
84
+ Object.defineProperty(prototype, key, { value: methods[key], writable: true, configurable: true });
85
+ }
86
+ if (parent === undefined) {
87
+ Object.defineProperty(prototype, Symbol.asyncIterator, { value: function asyncIterator() { return this; }, writable: true, configurable: true });
88
+ }
89
+ Object.defineProperty(prototype, Symbol.toStringTag, { value: "AsyncGenerator", configurable: true });
90
+ return prototype;
91
+ })();
50
92
  /**
51
93
  * 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.
94
+ * consumer, and starts one request at a time, as a native async generator does. `enter`
95
+ * starts the driver: the Node.js runtime starts it in the context of the generator.
54
96
  */
55
- export function createAsyncGenerator(step, NativePromise, enter = (start) => start()) {
97
+ export function createAsyncGenerator(step, PromiseConstructor, enter = (start) => start(), prepare = (value) => value) {
56
98
  let first;
57
99
  let last;
100
+ /** The state of the body: not started, suspended at a `yield`, or complete. While a step operates, the requests wait in the queue. */
101
+ let state = "start";
58
102
  /** This function starts the requests in the queue, one after the other, until the queue is empty. */
59
103
  const run = async (key, argument) => {
60
104
  // The directive tells the Babel preset not to change this function (src/babel/skip-runtime.ts)
105
+ // Stryker disable next-line all: the directive matters only when a bundler applies the preset to the runtime
61
106
  "async-browser-context runtime";
62
107
  let resumed = false;
63
108
  for (;;) {
@@ -65,47 +110,81 @@ export function createAsyncGenerator(step, NativePromise, enter = (start) => sta
65
110
  let settled;
66
111
  let done = false;
67
112
  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);
113
+ if (state === "done" || (state === "start" && key !== "next")) {
114
+ // The body does not operate: it is complete, or return() or throw() came before the start
115
+ state = "done";
116
+ done = true;
117
+ if (key === "throw") {
118
+ throw argument;
75
119
  }
76
- catch (error) {
120
+ if (key === "return") {
121
+ // As AsyncGeneratorAwaitReturn, return() awaits its value
122
+ settled = await argument;
123
+ }
124
+ }
125
+ else {
126
+ if (key === "return") {
127
+ // At a yield, return() awaits its value first. A rejection goes into the body at the yield.
128
+ try {
129
+ argument = unbox(await prepare(argument));
130
+ }
131
+ catch (error) {
132
+ key = "throw";
133
+ argument = error;
134
+ }
77
135
  resumed = true;
78
- key = "throw";
79
- result = step("throw", error, true);
80
- continue;
81
136
  }
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);
137
+ let result = step(key, argument, resumed);
138
+ for (;;) {
139
+ const yielded = result.value;
140
+ const overloaded = yielded instanceof OverloadYield;
141
+ let value;
142
+ try {
143
+ value = unbox(await prepare(overloaded ? yielded.v : yielded));
144
+ }
145
+ catch (error) {
146
+ resumed = true;
147
+ key = "throw";
148
+ result = step("throw", error, true);
88
149
  continue;
89
150
  }
90
- value = step(nextKey, value, true).value;
151
+ resumed = true;
152
+ if (overloaded) {
153
+ const nextKey = key === "return" && yielded.k === 1 ? "return" : "next";
154
+ if (yielded.k === 0 || Boolean(value.done)) {
155
+ key = nextKey;
156
+ result = step(nextKey, value, true);
157
+ continue;
158
+ }
159
+ // A step of yield*: the body gives the result of the delegate back without a change.
160
+ // Its value is not awaited, as with a native yield*.
161
+ const passed = step(nextKey, value, true);
162
+ if (passed !== value) {
163
+ result = passed;
164
+ continue;
165
+ }
166
+ value = passed.value;
167
+ }
168
+ settled = value;
169
+ done = result.done === true;
170
+ break;
91
171
  }
92
- settled = value;
93
- done = result.done === true;
94
- break;
172
+ state = done ? "done" : "yield";
95
173
  }
96
174
  }
97
175
  catch (error) {
176
+ state = "done";
98
177
  failed = true;
99
178
  settled = error;
100
179
  }
101
180
  // A request always waits in the queue while the driver runs
102
- const request = first;
103
- first = request.next;
181
+ const current = first;
182
+ first = current.next;
104
183
  if (failed) {
105
- request.reject(settled);
184
+ current.reject(settled);
106
185
  }
107
186
  else {
108
- request.resolve({ value: settled, done });
187
+ current.resolve({ value: settled, done });
109
188
  }
110
189
  if (first === undefined) {
111
190
  last = undefined;
@@ -115,14 +194,14 @@ export function createAsyncGenerator(step, NativePromise, enter = (start) => sta
115
194
  argument = first.argument;
116
195
  }
117
196
  };
118
- const invoke = (key, argument) => new NativePromise((resolve, reject) => {
119
- const request = { key, argument, resolve, reject, next: undefined };
197
+ const invoke = (key, argument) => new PromiseConstructor((resolve, reject) => {
198
+ const added = { key, argument, resolve, reject, next: undefined };
120
199
  if (last === undefined) {
121
- first = last = request;
200
+ first = last = added;
122
201
  void enter(() => run(key, argument));
123
202
  }
124
203
  else {
125
- last = last.next = request;
204
+ last = last.next = added;
126
205
  }
127
206
  });
128
207
  const generator = Object.create(AsyncGeneratorPrototype);
@@ -1 +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,GA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AC,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,CAoCvE;AAED,KAAK,WAAW,GAAG,QAAQ,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,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,4 +1,4 @@
1
- import { createAsyncGenerator, OverloadYield } from "./async-generator.js";
1
+ import { createAsyncGenerator, PlainValue, unbox } from "./async-generator.js";
2
2
  import { applyWithHooks, store } from "./store.js";
3
3
  /**
4
4
  * The code from the Babel preset uses these runtime functions. Do not use them
@@ -21,13 +21,65 @@ function namedForHooks(generatorFunction, name) {
21
21
  }
22
22
  return generatorFunction;
23
23
  }
24
+ /**
25
+ * This function gives an async function that operates `generatorFunction`. The preset gives
26
+ * this function to `@babel/plugin-transform-async-to-generator` as its
27
+ * `method` option, and adds `name`, the name of the async function, if it has one.
28
+ */
29
+ /** This function gives `true` for a value that can have a `then` property: an object or a function. */
30
+ function isObject(value) {
31
+ return (typeof value === "object" && value !== null) || typeof value === "function";
32
+ }
33
+ /**
34
+ * This function gives the value that a driver awaits or gives as its result, in place of `value`. It
35
+ * operates in the context of the body, as `await` reads the `then` property there:
36
+ *
37
+ * - A primitive value and a native promise stay the same. `await` does not read a `then`
38
+ * property of them.
39
+ * - For another object, the function reads `then` one time. A `then` method gets the current
40
+ * frame for its later call. Thus the method operates in the context of the body, as with
41
+ * the native `AsyncLocalStorage`. With `box`, an object without a `then` method goes into a
42
+ * box, so that the engine does not read `then` a second time.
43
+ * - When the read throws, the function gives a rejected promise: the error goes into the body.
44
+ */
45
+ function awaitable(value, NativePromise, box) {
46
+ if (!isObject(value)) {
47
+ return value;
48
+ }
49
+ let then;
50
+ try {
51
+ if (value instanceof NativePromise && value.constructor === NativePromise) {
52
+ return value;
53
+ }
54
+ then = value.then;
55
+ }
56
+ catch (error) {
57
+ return new NativePromise((_resolve, reject) => reject(error));
58
+ }
59
+ if (typeof then !== "function") {
60
+ return box ? new PlainValue(value) : value;
61
+ }
62
+ const frame = store.current;
63
+ return {
64
+ then(resolve, reject) {
65
+ const previous = store.current;
66
+ store.current = frame;
67
+ try {
68
+ return Reflect.apply(then, value, [resolve, reject]);
69
+ }
70
+ finally {
71
+ store.current = previous;
72
+ }
73
+ },
74
+ };
75
+ }
24
76
  /**
25
77
  * This function gives an async function that operates `generatorFunction`. The preset gives
26
78
  * this function to `@babel/plugin-transform-async-to-generator` as its
27
79
  * `method` option, and adds `name`, the name of the async function, if it has one.
28
80
  */
29
81
  export function coroutine(generatorFunction, name) {
30
- const { promiseResolve } = store.intrinsics;
82
+ const NativePromise = store.intrinsics.Promise;
31
83
  return function asyncFunction(...args) {
32
84
  // The context of the call. The body can change it with AsyncLocalStorage.enterWith.
33
85
  let frame = store.current;
@@ -35,9 +87,11 @@ export function coroutine(generatorFunction, name) {
35
87
  const next = (value) => generator.next(value);
36
88
  const throwInto = (error) => generator.throw(error);
37
89
  /**
38
- * This function does one step of the body in the context of the async function, and sets
39
- * the previous context again. `resumed` is `true` for a step after an `await`. Only these
40
- * steps start the hook methods. The function gives the value to await.
90
+ * This function does one step of the body in the context of the async function. `resumed`
91
+ * is `true` for a step after an `await`. Only these steps start the hook methods. After a
92
+ * resumed step, the function sets the previous context again. The first step operates in
93
+ * the code of the caller, so an `enterWith` of that step stays for the caller, as on
94
+ * Node.js. The function gives the value to await, or the result of the function.
41
95
  */
42
96
  const step = (resume, argument, resumed) => {
43
97
  const previous = store.current;
@@ -47,17 +101,13 @@ export function coroutine(generatorFunction, name) {
47
101
  const result = hooks === undefined
48
102
  ? resume(argument)
49
103
  : applyWithHooks(hooks, namedForHooks(generatorFunction, name), "await", resume, undefined, [argument]);
50
- const value = result.value;
51
- if (!result.done && ((typeof value === "object" && value !== null) || typeof value === "function")) {
52
- // Promise.resolve reads the "then" property of an object. Do it in the context of the body, as await does.
53
- // The result object is new for each step, so the change is not visible to other code.
54
- result.value = promiseResolve(value);
55
- }
104
+ // The result object is new for each step, so the change is not visible to other code
105
+ result.value = awaitable(result.value, NativePromise, result.done !== true);
56
106
  return result;
57
107
  }
58
108
  finally {
59
109
  frame = store.current;
60
- store.current = previous;
110
+ store.current = resumed ? previous : frame;
61
111
  }
62
112
  };
63
113
  /**
@@ -67,14 +117,15 @@ export function coroutine(generatorFunction, name) {
67
117
  */
68
118
  const drive = async () => {
69
119
  // The directive tells the Babel preset not to change this function (src/babel/skip-runtime.ts)
120
+ // Stryker disable next-line all: the directive matters only when a bundler applies the preset to the runtime
70
121
  "async-browser-context runtime";
71
122
  // In the driver, an error of the parameters rejects the promise, as with a native async function
72
- generator = generatorFunction.apply(this, args);
123
+ generator = Reflect.apply(generatorFunction, this, args);
73
124
  let result = step(next, undefined, false);
74
125
  while (result.done !== true) {
75
126
  let value;
76
127
  try {
77
- value = await result.value;
128
+ value = unbox(await result.value);
78
129
  }
79
130
  catch (error) {
80
131
  result = step(throwInto, error, true);
@@ -87,10 +138,6 @@ export function coroutine(generatorFunction, name) {
87
138
  return drive();
88
139
  };
89
140
  }
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
141
  /**
95
142
  * This function gives a function that makes an async generator object, which operates
96
143
  * `generatorFunction`. The preset gives this function in place of the helper
@@ -100,41 +147,42 @@ function isObject(value) {
100
147
  * (rule C5). The driver continues each step with a native `await` (`async-generator.ts`).
101
148
  */
102
149
  export function asyncGenerator(generatorFunction) {
103
- const { Promise: NativePromise, promiseResolve } = store.intrinsics;
150
+ const NativePromise = store.intrinsics.Promise;
104
151
  return function makeAsyncGenerator(...args) {
105
152
  let frame = store.current;
106
- const generator = generatorFunction.apply(this, args);
153
+ const generator = Reflect.apply(generatorFunction, this, args);
107
154
  const methods = {
108
155
  next: (argument) => generator.next(argument),
109
156
  throw: (argument) => generator.throw(argument),
110
157
  return: (argument) => generator.return(argument),
111
158
  };
159
+ /** One step of the body in the context of the generator. The result stays the same: `yield*` gives the result of the delegate back. */
112
160
  const step = (key, argument, resumed) => {
113
161
  const previous = store.current;
114
162
  store.current = frame;
115
163
  try {
116
164
  const hooks = resumed ? store.hooks : undefined;
117
- const result = hooks === undefined
165
+ return hooks === undefined
118
166
  ? methods[key](argument)
119
167
  : 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
168
  }
132
169
  finally {
133
170
  frame = store.current;
134
171
  store.current = previous;
135
172
  }
136
173
  };
137
- return createAsyncGenerator(step, NativePromise);
174
+ /** The value to await, in the context of the generator: `await` and `yield` read the `then` property of an object there. */
175
+ const prepare = (value) => {
176
+ const previous = store.current;
177
+ store.current = frame;
178
+ try {
179
+ return awaitable(value, NativePromise, true);
180
+ }
181
+ finally {
182
+ store.current = previous;
183
+ }
184
+ };
185
+ return createAsyncGenerator(step, NativePromise, undefined, prepare);
138
186
  };
139
187
  }
140
188
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"coroutine.js","sourceRoot":"","sources":["../../src/core/coroutine.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,aAAa,EAAE,MAAM,sBAAsB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c,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"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "async-browser-context",
3
- "version": "0.2.3",
3
+ "version": "0.2.4",
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",
@@ -8,15 +8,20 @@
8
8
  *
9
9
  * The protocol is the protocol of the Babel 8 helper: the body gives
10
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`.
11
+ * step of `yield*`. Each other value is a `yield`. Where the helper differs from a native async
12
+ * generator, the driver does as the native generator. Thus `return()` at a `yield` awaits its
13
+ * value first, and a request on a complete generator settles at once.
12
14
  */
13
15
 
16
+ /** The Promise of the time when the module loads, for the rejections of the methods. */
17
+ const NativePromise : PromiseConstructor = Promise;
18
+
14
19
  /**
15
20
  * The value that the body gives for an `await` (kind 0) or for a step of `yield*` (kind 1).
16
21
  * The Babel helpers `awaitAsyncGenerator` and `asyncGeneratorDelegate` make it with `new`.
17
22
  */
18
23
  export class OverloadYield {
19
- v : unknown;
24
+ readonly v : unknown;
20
25
  readonly k : 0 | 1;
21
26
  constructor(value : unknown, kind : 0 | 1) {
22
27
  this.v = value;
@@ -24,16 +29,41 @@ export class OverloadYield {
24
29
  }
25
30
  }
26
31
 
32
+ /**
33
+ * A value that is not a thenable, in a box. The browser runtime reads the `then` property of an
34
+ * awaited object one time, in the context of the body. When the object has no `then` method,
35
+ * the driver awaits the box. Thus the engine does not read `then` a second time, and the
36
+ * `await` takes the same microtask turns.
37
+ */
38
+ export class PlainValue {
39
+ readonly value : unknown;
40
+ constructor(value : unknown) {
41
+ this.value = value;
42
+ }
43
+ }
44
+
45
+ /** This function gives the value in a box, or the value itself. */
46
+ export function unbox(value : unknown) : unknown {
47
+ return value instanceof PlainValue ? value.value : value;
48
+ }
49
+
27
50
  /** The methods of a generator that a request of the consumer starts. */
28
51
  export type ResumeKey = "next" | "throw" | "return";
29
52
 
30
53
  /**
31
54
  * This function does one step of the body: `generator[key](argument)`. The browser runtime sets
32
55
  * the context of the generator around the step. `resumed` is `true` for a step after an
33
- * `await` of the driver.
56
+ * `await` of the driver. The function gives the result of the generator without a change.
34
57
  */
35
58
  export type AsyncGeneratorStep = (key : ResumeKey, argument : unknown, resumed : boolean) => IteratorResult<unknown, unknown>;
36
59
 
60
+ /**
61
+ * This function gives the value that the driver awaits in place of `value`. The browser runtime
62
+ * reads the `then` property in the context of the generator. The Node.js runtime gives the
63
+ * value without a change.
64
+ */
65
+ export type Prepare = (value : unknown) => unknown;
66
+
37
67
  interface Request {
38
68
  readonly key : ResumeKey;
39
69
  readonly argument : unknown;
@@ -47,47 +77,73 @@ type Invoke = (key : ResumeKey, argument : unknown) => Promise<IteratorResult<un
47
77
  /** The key of the request function of each async generator object. */
48
78
  const INVOKE : unique symbol = Symbol("async-browser-context/invoke");
49
79
 
50
- function invokeOf(target : object) : Invoke {
51
- const invoke = (target as { [INVOKE]? : Invoke })[INVOKE];
80
+ /** This function starts a request. For an object that is not an async generator of the runtime, it gives a rejected promise, as a native method does. */
81
+ function request(target : unknown, key : ResumeKey, argument : unknown) : Promise<IteratorResult<unknown, unknown>> {
82
+ const invoke = typeof target === "object" && target !== null ? (target as { [INVOKE]? : Invoke })[INVOKE] : undefined;
52
83
  if (invoke === undefined) {
53
- throw new TypeError("The object is not an async generator of async-browser-context");
84
+ return NativePromise.reject(new TypeError(`AsyncGenerator.prototype.${key} started on an object that is not an async generator`));
85
+ }
86
+ return invoke(key, argument);
87
+ }
88
+
89
+ /**
90
+ * This function gives %AsyncIteratorPrototype% of the engine, which gives `Symbol.asyncIterator`,
91
+ * `Symbol.asyncDispose` and the async iterator helpers. If a compiler changed the async
92
+ * generator below, the function gives `undefined`.
93
+ */
94
+ function asyncIteratorPrototype() : object | undefined {
95
+ try {
96
+ const generatorFunction = async function* () {
97
+ // The directive tells the Babel preset not to change this function (src/babel/skip-runtime.ts)
98
+ // Stryker disable next-line all: the directive matters only when a bundler applies the preset to the runtime
99
+ "async-browser-context runtime";
100
+ };
101
+ const prototype = Object.getPrototypeOf(Object.getPrototypeOf(generatorFunction.prototype) as object) as Record<symbol, unknown> | null;
102
+ return prototype !== null && typeof prototype[Symbol.asyncIterator] === "function" ? prototype : undefined;
103
+ } catch {
104
+ return undefined;
54
105
  }
55
- return invoke;
56
106
  }
57
107
 
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
- };
108
+ /** The prototype of the async generator objects of transformed code. Its methods are not enumerable, as native methods. */
109
+ const AsyncGeneratorPrototype : object = (() => {
110
+ const parent = asyncIteratorPrototype();
111
+ const prototype = Object.create(parent ?? Object.prototype) as object;
112
+ const methods : Record<ResumeKey, (this : unknown, argument? : unknown) => Promise<IteratorResult<unknown, unknown>>> = {
113
+ next(value) { return request(this, "next", value); },
114
+ throw(error) { return request(this, "throw", error); },
115
+ return(value) { return request(this, "return", value); },
116
+ };
117
+ for (const key of ["next", "throw", "return"] as const) {
118
+ Object.defineProperty(prototype, key, { value : methods[key], writable : true, configurable : true });
119
+ }
120
+ if (parent === undefined) {
121
+ Object.defineProperty(prototype, Symbol.asyncIterator, { value : function asyncIterator(this : unknown) { return this; }, writable : true, configurable : true });
122
+ }
123
+ Object.defineProperty(prototype, Symbol.toStringTag, { value : "AsyncGenerator", configurable : true });
124
+ return prototype;
125
+ })();
74
126
 
75
127
  /**
76
128
  * 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.
129
+ * consumer, and starts one request at a time, as a native async generator does. `enter`
130
+ * starts the driver: the Node.js runtime starts it in the context of the generator.
79
131
  */
80
132
  export function createAsyncGenerator(
81
133
  step : AsyncGeneratorStep,
82
- NativePromise : PromiseConstructor,
134
+ PromiseConstructor : PromiseConstructor,
83
135
  enter : (start : () => Promise<void>) => Promise<void> = (start) => start(),
136
+ prepare : Prepare = (value) => value,
84
137
  ) : AsyncGenerator<unknown, unknown, unknown> {
85
138
  let first : Request | undefined;
86
139
  let last : Request | undefined;
140
+ /** The state of the body: not started, suspended at a `yield`, or complete. While a step operates, the requests wait in the queue. */
141
+ let state : "start" | "yield" | "done" = "start";
87
142
 
88
143
  /** This function starts the requests in the queue, one after the other, until the queue is empty. */
89
144
  const run = async (key : ResumeKey, argument : unknown) : Promise<void> => {
90
145
  // The directive tells the Babel preset not to change this function (src/babel/skip-runtime.ts)
146
+ // Stryker disable next-line all: the directive matters only when a bundler applies the preset to the runtime
91
147
  "async-browser-context runtime";
92
148
  let resumed = false;
93
149
  for (;;) {
@@ -95,44 +151,76 @@ export function createAsyncGenerator(
95
151
  let settled : unknown;
96
152
  let done = false;
97
153
  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) {
154
+ if (state === "done" || (state === "start" && key !== "next")) {
155
+ // The body does not operate: it is complete, or return() or throw() came before the start
156
+ state = "done";
157
+ done = true;
158
+ if (key === "throw") {
159
+ throw argument;
160
+ }
161
+ if (key === "return") {
162
+ // As AsyncGeneratorAwaitReturn, return() awaits its value
163
+ settled = await argument;
164
+ }
165
+ } else {
166
+ if (key === "return") {
167
+ // At a yield, return() awaits its value first. A rejection goes into the body at the yield.
168
+ try {
169
+ argument = unbox(await prepare(argument));
170
+ } catch (error) {
171
+ key = "throw";
172
+ argument = error;
173
+ }
106
174
  resumed = true;
107
- key = "throw";
108
- result = step("throw", error, true);
109
- continue;
110
175
  }
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);
176
+ let result = step(key, argument, resumed);
177
+ for (;;) {
178
+ const yielded : unknown = result.value;
179
+ const overloaded = yielded instanceof OverloadYield;
180
+ let value : unknown;
181
+ try {
182
+ value = unbox(await prepare(overloaded ? yielded.v : yielded));
183
+ } catch (error) {
184
+ resumed = true;
185
+ key = "throw";
186
+ result = step("throw", error, true);
117
187
  continue;
118
188
  }
119
- value = step(nextKey, value, true).value;
189
+ resumed = true;
190
+ if (overloaded) {
191
+ const nextKey : ResumeKey = key === "return" && yielded.k === 1 ? "return" : "next";
192
+ if (yielded.k === 0 || Boolean((value as { done? : unknown }).done)) {
193
+ key = nextKey;
194
+ result = step(nextKey, value, true);
195
+ continue;
196
+ }
197
+ // A step of yield*: the body gives the result of the delegate back without a change.
198
+ // Its value is not awaited, as with a native yield*.
199
+ const passed = step(nextKey, value, true);
200
+ if (passed !== value) {
201
+ result = passed;
202
+ continue;
203
+ }
204
+ value = passed.value;
205
+ }
206
+ settled = value;
207
+ done = result.done === true;
208
+ break;
120
209
  }
121
- settled = value;
122
- done = result.done === true;
123
- break;
210
+ state = done ? "done" : "yield";
124
211
  }
125
212
  } catch (error) {
213
+ state = "done";
126
214
  failed = true;
127
215
  settled = error;
128
216
  }
129
217
  // A request always waits in the queue while the driver runs
130
- const request = first as Request;
131
- first = request.next;
218
+ const current = first as Request;
219
+ first = current.next;
132
220
  if (failed) {
133
- request.reject(settled);
221
+ current.reject(settled);
134
222
  } else {
135
- request.resolve({ value : settled, done });
223
+ current.resolve({ value : settled, done });
136
224
  }
137
225
  if (first === undefined) {
138
226
  last = undefined;
@@ -143,13 +231,13 @@ export function createAsyncGenerator(
143
231
  }
144
232
  };
145
233
 
146
- const invoke : Invoke = (key, argument) => new NativePromise((resolve, reject) => {
147
- const request : Request = { key, argument, resolve, reject, next : undefined };
234
+ const invoke : Invoke = (key, argument) => new PromiseConstructor((resolve, reject) => {
235
+ const added : Request = { key, argument, resolve, reject, next : undefined };
148
236
  if (last === undefined) {
149
- first = last = request;
237
+ first = last = added;
150
238
  void enter(() => run(key, argument));
151
239
  } else {
152
- last = last.next = request;
240
+ last = last.next = added;
153
241
  }
154
242
  });
155
243
 
@@ -1,4 +1,4 @@
1
- import { createAsyncGenerator, OverloadYield } from "./async-generator.js";
1
+ import { createAsyncGenerator, PlainValue, unbox } from "./async-generator.js";
2
2
  import { applyWithHooks, store, type Frame } from "./store.js";
3
3
 
4
4
  /**
@@ -24,6 +24,58 @@ function namedForHooks(generatorFunction : Function, name : string | undefined)
24
24
  return generatorFunction;
25
25
  }
26
26
 
27
+ /**
28
+ * This function gives an async function that operates `generatorFunction`. The preset gives
29
+ * this function to `@babel/plugin-transform-async-to-generator` as its
30
+ * `method` option, and adds `name`, the name of the async function, if it has one.
31
+ */
32
+ /** This function gives `true` for a value that can have a `then` property: an object or a function. */
33
+ function isObject(value : unknown) : value is object {
34
+ return (typeof value === "object" && value !== null) || typeof value === "function";
35
+ }
36
+
37
+ /**
38
+ * This function gives the value that a driver awaits or gives as its result, in place of `value`. It
39
+ * operates in the context of the body, as `await` reads the `then` property there:
40
+ *
41
+ * - A primitive value and a native promise stay the same. `await` does not read a `then`
42
+ * property of them.
43
+ * - For another object, the function reads `then` one time. A `then` method gets the current
44
+ * frame for its later call. Thus the method operates in the context of the body, as with
45
+ * the native `AsyncLocalStorage`. With `box`, an object without a `then` method goes into a
46
+ * box, so that the engine does not read `then` a second time.
47
+ * - When the read throws, the function gives a rejected promise: the error goes into the body.
48
+ */
49
+ function awaitable(value : unknown, NativePromise : PromiseConstructor, box : boolean) : unknown {
50
+ if (!isObject(value)) {
51
+ return value;
52
+ }
53
+ let then : unknown;
54
+ try {
55
+ if (value instanceof NativePromise && value.constructor === NativePromise) {
56
+ return value;
57
+ }
58
+ then = (value as { then? : unknown }).then;
59
+ } catch (error) {
60
+ return new NativePromise((_resolve, reject) => reject(error));
61
+ }
62
+ if (typeof then !== "function") {
63
+ return box ? new PlainValue(value) : value;
64
+ }
65
+ const frame = store.current;
66
+ return {
67
+ then(this : unknown, resolve : unknown, reject : unknown) : unknown {
68
+ const previous = store.current;
69
+ store.current = frame;
70
+ try {
71
+ return Reflect.apply(then, value, [resolve, reject]);
72
+ } finally {
73
+ store.current = previous;
74
+ }
75
+ },
76
+ };
77
+ }
78
+
27
79
  /**
28
80
  * This function gives an async function that operates `generatorFunction`. The preset gives
29
81
  * this function to `@babel/plugin-transform-async-to-generator` as its
@@ -33,7 +85,7 @@ export function coroutine<T, A extends unknown[], R>(
33
85
  generatorFunction : (this : T, ...args : A) => Generator<unknown, R, unknown>,
34
86
  name? : string,
35
87
  ) : (this : T, ...args : A) => Promise<R> {
36
- const { promiseResolve } = store.intrinsics;
88
+ const NativePromise = store.intrinsics.Promise;
37
89
  return function asyncFunction(this : T, ...args : A) : Promise<R> {
38
90
  // The context of the call. The body can change it with AsyncLocalStorage.enterWith.
39
91
  let frame : Frame = store.current;
@@ -42,9 +94,11 @@ export function coroutine<T, A extends unknown[], R>(
42
94
  const throwInto = (error : unknown) : IteratorResult<unknown, R> => generator.throw(error);
43
95
 
44
96
  /**
45
- * This function does one step of the body in the context of the async function, and sets
46
- * the previous context again. `resumed` is `true` for a step after an `await`. Only these
47
- * steps start the hook methods. The function gives the value to await.
97
+ * This function does one step of the body in the context of the async function. `resumed`
98
+ * is `true` for a step after an `await`. Only these steps start the hook methods. After a
99
+ * resumed step, the function sets the previous context again. The first step operates in
100
+ * the code of the caller, so an `enterWith` of that step stays for the caller, as on
101
+ * Node.js. The function gives the value to await, or the result of the function.
48
102
  */
49
103
  const step = (resume : (argument : unknown) => IteratorResult<unknown, R>, argument : unknown, resumed : boolean) : IteratorResult<unknown, R> => {
50
104
  const previous = store.current;
@@ -54,16 +108,12 @@ export function coroutine<T, A extends unknown[], R>(
54
108
  const result = hooks === undefined
55
109
  ? resume(argument)
56
110
  : applyWithHooks(hooks, namedForHooks(generatorFunction, name), "await", resume, undefined, [argument]);
57
- const value : unknown = result.value;
58
- if (!result.done && ((typeof value === "object" && value !== null) || typeof value === "function")) {
59
- // Promise.resolve reads the "then" property of an object. Do it in the context of the body, as await does.
60
- // The result object is new for each step, so the change is not visible to other code.
61
- (result as { value : unknown }).value = promiseResolve(value);
62
- }
111
+ // The result object is new for each step, so the change is not visible to other code
112
+ (result as { value : unknown }).value = awaitable(result.value, NativePromise, result.done !== true);
63
113
  return result;
64
114
  } finally {
65
115
  frame = store.current;
66
- store.current = previous;
116
+ store.current = resumed ? previous : frame;
67
117
  }
68
118
  };
69
119
 
@@ -74,14 +124,15 @@ export function coroutine<T, A extends unknown[], R>(
74
124
  */
75
125
  const drive = async () : Promise<R> => {
76
126
  // The directive tells the Babel preset not to change this function (src/babel/skip-runtime.ts)
127
+ // Stryker disable next-line all: the directive matters only when a bundler applies the preset to the runtime
77
128
  "async-browser-context runtime";
78
129
  // In the driver, an error of the parameters rejects the promise, as with a native async function
79
- generator = generatorFunction.apply(this, args);
130
+ generator = Reflect.apply(generatorFunction, this, args);
80
131
  let result = step(next, undefined, false);
81
132
  while (result.done !== true) {
82
133
  let value : unknown;
83
134
  try {
84
- value = await result.value;
135
+ value = unbox(await result.value);
85
136
  } catch (error) {
86
137
  result = step(throwInto, error, true);
87
138
  continue;
@@ -94,11 +145,6 @@ export function coroutine<T, A extends unknown[], R>(
94
145
  };
95
146
  }
96
147
 
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
148
  /**
103
149
  * This function gives a function that makes an async generator object, which operates
104
150
  * `generatorFunction`. The preset gives this function in place of the helper
@@ -110,39 +156,40 @@ function isObject(value : unknown) : value is object {
110
156
  export function asyncGenerator<T, A extends unknown[]>(
111
157
  generatorFunction : (this : T, ...args : A) => Generator<unknown, unknown, unknown>,
112
158
  ) : (this : T, ...args : A) => AsyncGenerator<unknown, unknown, unknown> {
113
- const { Promise : NativePromise, promiseResolve } = store.intrinsics;
159
+ const NativePromise = store.intrinsics.Promise;
114
160
  return function makeAsyncGenerator(this : T, ...args : A) : AsyncGenerator<unknown, unknown, unknown> {
115
161
  let frame : Frame = store.current;
116
- const generator = generatorFunction.apply(this, args);
162
+ const generator = Reflect.apply(generatorFunction, this, args);
117
163
  const methods : Record<ResumeMethod, (argument : unknown) => IteratorResult<unknown, unknown>> = {
118
164
  next : (argument) => generator.next(argument),
119
165
  throw : (argument) => generator.throw(argument),
120
166
  return : (argument) => generator.return(argument),
121
167
  };
168
+ /** One step of the body in the context of the generator. The result stays the same: `yield*` gives the result of the delegate back. */
122
169
  const step = (key : ResumeMethod, argument : unknown, resumed : boolean) : IteratorResult<unknown, unknown> => {
123
170
  const previous = store.current;
124
171
  store.current = frame;
125
172
  try {
126
173
  const hooks = resumed ? store.hooks : undefined;
127
- const result = hooks === undefined
174
+ return hooks === undefined
128
175
  ? methods[key](argument)
129
176
  : 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
177
  } finally {
141
178
  frame = store.current;
142
179
  store.current = previous;
143
180
  }
144
181
  };
145
- return createAsyncGenerator(step, NativePromise);
182
+ /** The value to await, in the context of the generator: `await` and `yield` read the `then` property of an object there. */
183
+ const prepare = (value : unknown) : unknown => {
184
+ const previous = store.current;
185
+ store.current = frame;
186
+ try {
187
+ return awaitable(value, NativePromise, true);
188
+ } finally {
189
+ store.current = previous;
190
+ }
191
+ };
192
+ return createAsyncGenerator(step, NativePromise, undefined, prepare);
146
193
  };
147
194
  }
148
195