11'use strict' ;
22
33const {
4- ObjectSetPrototypeOf,
54 SafeMap,
65} = primordials ;
76
@@ -10,45 +9,22 @@ const {
109 setContinuationPreservedEmbedderData,
1110} = internalBinding ( 'async_context_frame' ) ;
1211
13- let enabled_ ;
14-
15- class ActiveAsyncContextFrame extends SafeMap {
16- static get enabled ( ) {
17- return true ;
18- }
19-
20- static current ( ) {
21- return getContinuationPreservedEmbedderData ( ) ;
22- }
23-
24- static set ( frame ) {
25- setContinuationPreservedEmbedderData ( frame ) ;
26- }
27-
28- static exchange ( frame ) {
29- const prior = this . current ( ) ;
30- this . set ( frame ) ;
31- return prior ;
32- }
33-
34- static disable ( store ) {
35- const frame = this . current ( ) ;
36- frame ?. disable ( store ) ;
37- }
12+ function activeCurrent ( ) {
13+ return getContinuationPreservedEmbedderData ( ) ;
3814}
3915
40- function checkEnabled ( ) {
41- const enabled = require ( 'internal/options' )
42- . getOptionValue ( '--async-context-frame' ) ;
16+ function activeSet ( frame ) {
17+ setContinuationPreservedEmbedderData ( frame ) ;
18+ }
4319
44- // If enabled, swap to active prototype so we don't need to check status
45- // on every interaction with the async context frame.
46- if ( enabled ) {
47- // eslint-disable-next-line no-use-before-define
48- ObjectSetPrototypeOf ( AsyncContextFrame , ActiveAsyncContextFrame ) ;
49- }
20+ function activeExchange ( frame ) {
21+ const prior = getContinuationPreservedEmbedderData ( ) ;
22+ setContinuationPreservedEmbedderData ( frame ) ;
23+ return prior ;
24+ }
5025
51- return enabled ;
26+ function activeDisable ( store ) {
27+ getContinuationPreservedEmbedderData ( ) ?. disable ( store ) ;
5228}
5329
5430class InactiveAsyncContextFrame extends SafeMap {
@@ -74,4 +50,22 @@ class AsyncContextFrame extends InactiveAsyncContextFrame {
7450 }
7551}
7652
53+ let enabled_ ;
54+
55+ function checkEnabled ( ) {
56+ const enabled = require ( 'internal/options' )
57+ . getOptionValue ( '--async-context-frame' ) ;
58+
59+ // If enabled, install the active implementations directly. We use props
60+ // rather than a prototype replacement to preserve V8 optimizations.
61+ if ( enabled ) {
62+ AsyncContextFrame . current = activeCurrent ;
63+ AsyncContextFrame . set = activeSet ;
64+ AsyncContextFrame . exchange = activeExchange ;
65+ AsyncContextFrame . disable = activeDisable ;
66+ }
67+
68+ return enabled ;
69+ }
70+
7771module . exports = AsyncContextFrame ;
0 commit comments