Skip to content

Commit f7be6bb

Browse files
authored
Normative: add constructor extensions (#54)
1 parent ad550db commit f7be6bb

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,18 @@ logically-connected sync/async code execution.
157157
class AsyncContext<T> {
158158
static wrap<R>(callback: (...args: any[]) => R): (...args: any[]) => R;
159159

160+
constructor(options: AsyncContextOptions<T>);
161+
162+
get name(): string;
163+
160164
run<R>(value: T, callback: () => R): R;
161165

162-
get(): T;
166+
get(): T | undefined;
167+
}
168+
169+
interface AsyncContextOptions<T> {
170+
name?: string;
171+
defaultValue?: T;
163172
}
164173
```
165174

spec.html

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -372,13 +372,21 @@ <h1>The AsyncContext Constructor</h1>
372372
</ul>
373373

374374
<emu-clause id="sec-asynccontext">
375-
<h1>AsyncContext ( )</h1>
375+
<h1>AsyncContext ( _options_ )</h1>
376376
<p>This function performs the following steps when called:</p>
377377

378378
<emu-alg>
379379
1. If NewTarget is *undefined*, throw a *TypeError* exception.
380+
1. If _options_ is an Object, then
381+
1. Let _name_ be ? Get(_options_, *"name"*).
382+
1. Let _nameStr_ be ? ToString(_name_).
383+
1. Let _defaultValue_ be ? Get(_options_, *"defaultValue"*).
384+
1. Else,
385+
1. Let _nameStr_ be the empty String.
386+
1. Let _defaultValue_ be *undefined*.
380387
1. Let _asyncContext_ be ? OrdinaryCreateFromConstructor(NewTarget, *"%AsyncContext.prototype%"*, « [[AsyncContextKey]] »).
381-
1. Set _asyncContext_.[[AsyncContextKey]] to a new Symbol.
388+
1. Set _asyncContext_.[[AsyncContextKey]] to a new Symbol whose [[Description]] is _nameStr_.
389+
1. Set _asyncContext_.[[AsyncContextDefaultValue]] to _defaultValue_.
382390
1. Return _asyncContext_.
383391
</emu-alg>
384392
</emu-clause>
@@ -463,11 +471,23 @@ <h1>AsyncContext.prototype.get ( )</h1>
463471
1. Let _asyncContextMapping_ be _agentRecord_.[[AsyncContextMapping]].
464472
1. For each Record { [[AsyncContextKey]], [[AsyncContextValue]] } _p_ of _asyncContextMapping_, do
465473
1. If SameValueZero(_p_.[[AsyncContextKey]], _asyncContext_.[[AsyncContextKey]]) is *true*, return _p_.[[AsyncContextValue]].
466-
1. Return *undefined*.
474+
1. Return _asyncContext_.[[AsyncContextDefaultValue]].
467475
</emu-alg>
468476
</emu-clause>
469477
</emu-clause>
470478

479+
<emu-clause id="sec-asynccontext.prototype.name">
480+
<h1>get AsyncContext.prototype.name</h1>
481+
<p>`AsyncContext.prototype.name` is an accessor property whose set accessor function is *undefined*. Its get accessor function performs the following steps when called:</p>
482+
<emu-alg>
483+
1. Let _asyncContext_ be the *this* value.
484+
1. Perform ? RequireInternalSlot(_asyncContext_, [[AsyncContextKey]]).
485+
1. Let _asyncContextKey_ be _asyncContext_.[[AsyncContextKey]].
486+
1. Assert: _asyncContextKey_ is a Symbol.
487+
1. Return _asyncContextKey_.[[Description]].
488+
</emu-alg>
489+
</emu-clause>
490+
471491
<emu-clause id="sec-properties-of-asynccontext-instances">
472492
<h1>Properties of AsyncContext Instances</h1>
473493
<p>AsyncContext instances are ordinary objects that inherit properties from the AsyncContext prototype object (the intrinsic, %AsyncContext.prototype%). AsyncContext instances are initially created with the internal slots described in <emu-xref href="#table-internal-slots-of-asynccontext-instances"></emu-xref>.</p>
@@ -496,6 +516,17 @@ <h1>Properties of AsyncContext Instances</h1>
496516
Represents the AsyncContext instance in the surrounding agent's Agent Record's [[AsyncContextMapping]].
497517
</td>
498518
</tr>
519+
<tr>
520+
<td>
521+
[[AsyncContextDefaultValue]]
522+
</td>
523+
<td>
524+
an ECMAScript language value
525+
</td>
526+
<td>
527+
The default value of the AsyncContext instance when no entry is found in the mapping.
528+
</td>
529+
</tr>
499530
</table>
500531
</emu-table>
501532
</emu-clause>

0 commit comments

Comments
 (0)