| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -884,8 +884,8 @@ added: REPLACEME | |||
| 884 | 884 | ||
| 885 | 885 | > Stability: 1 - Experimental | |
| 886 | 886 | ||
| 887 | - Enables the use of AsyncLocalStorage backed by AsyncContextFrame rather than | ||
| 888 | - the default implementation which relies on async\_hooks. This new model is | ||
| 887 | + Enables the use of [`AsyncLocalStorage`][] backed by `AsyncContextFrame` rather | ||
| 888 | + than the default implementation which relies on async\_hooks. This new model is | ||
| 889 | 889 | implemented very differently and so could have differences in how context data | |
| 890 | 890 | flows within the application. As such, it is presently recommended to be sure | |
| 891 | 891 | your application behaviour is unaffected by this change before using it in | |
@@ -3527,6 +3527,7 @@ node --stack-trace-limit=12 -p -e "Error.stackTraceLimit" # prints 12 | |||
| 3527 | 3527 | [`--print`]: #-p---print-script | |
| 3528 | 3528 | [`--redirect-warnings`]: #--redirect-warningsfile | |
| 3529 | 3529 | [`--require`]: #-r---require-module | |
| 3530 | + [`AsyncLocalStorage`]: async_context.md#class-asynclocalstorage | ||
| 3530 | 3531 | [`Atomics.wait()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics/wait | |
| 3531 | 3532 | [`Buffer`]: buffer.md#class-buffer | |
| 3532 | 3533 | [`CRYPTO_secure_malloc_init`]: https://www.openssl.org/docs/man3.0/man3/CRYPTO_secure_malloc_init.html | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -280,7 +280,7 @@ module.exports = { | |||
| 280 | 280 | // Public API | |
| 281 | 281 | get AsyncLocalStorage() { | |
| 282 | 282 | return AsyncContextFrame.enabled ? | |
| 283 | - require('internal/async_local_storage/native') : | ||
| 283 | + require('internal/async_local_storage/async_context_frame') : | ||
| 284 | 284 | require('internal/async_local_storage/async_hooks'); | |
| 285 | 285 | }, | |
| 286 | 286 | createHook, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,34 +1,27 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | 2 | ||
| 3 | + const { | ||
| 4 | + ObjectSetPrototypeOf, | ||
| 5 | + } = primordials; | ||
| 6 | + | ||
| 3 | 7 | const { | |
| 4 | 8 | getContinuationPreservedEmbedderData, | |
| 5 | 9 | setContinuationPreservedEmbedderData, | |
| 6 | 10 | } = internalBinding('async_context_frame'); | |
| 7 | 11 | ||
| 8 | 12 | let enabled_; | |
| 9 | 13 | ||
| 10 | - class AsyncContextFrame extends Map { | ||
| 11 | - constructor(store, data) { | ||
| 12 | - super(AsyncContextFrame.current()); | ||
| 13 | - this.set(store, data); | ||
| 14 | - } | ||
| 15 | - | ||
| 14 | + class ActiveAsyncContextFrame { | ||
| 16 | 15 | static get enabled() { | |
| 17 | - enabled_ ??= require('internal/options') | ||
| 18 | - .getOptionValue('--experimental-async-context-frame'); | ||
| 19 | - return enabled_; | ||
| 16 | + return true; | ||
| 20 | 17 | } | |
| 21 | 18 | ||
| 22 | 19 | static current() { | |
| 23 | - if (this.enabled) { | ||
| 24 | - return getContinuationPreservedEmbedderData(); | ||
| 25 | - } | ||
| 20 | + return getContinuationPreservedEmbedderData(); | ||
| 26 | 21 | } | |
| 27 | 22 | ||
| 28 | 23 | static set(frame) { | |
| 29 | - if (this.enabled) { | ||
| 30 | - setContinuationPreservedEmbedderData(frame); | ||
| 31 | - } | ||
| 24 | + setContinuationPreservedEmbedderData(frame); | ||
| 32 | 25 | } | |
| 33 | 26 | ||
| 34 | 27 | static exchange(frame) { | |
@@ -41,6 +34,37 @@ class AsyncContextFrame extends Map { | |||
| 41 | 34 | const frame = this.current(); | |
| 42 | 35 | frame?.disable(store); | |
| 43 | 36 | } | |
| 37 | + } | ||
| 38 | + | ||
| 39 | + function checkEnabled() { | ||
| 40 | + const enabled = require('internal/options') | ||
| 41 | + .getOptionValue('--experimental-async-context-frame'); | ||
| 42 | + | ||
| 43 | + // If enabled, swap to active prototype so we don't need to check status | ||
| 44 | + // on every interaction with the async context frame. | ||
| 45 | + if (enabled) { | ||
| 46 | + // eslint-disable-next-line no-use-before-define | ||
| 47 | + ObjectSetPrototypeOf(AsyncContextFrame, ActiveAsyncContextFrame); | ||
| 48 | + } | ||
| 49 | + | ||
| 50 | + return enabled; | ||
| 51 | + } | ||
| 52 | + | ||
| 53 | + class AsyncContextFrame extends Map { | ||
| 54 | + constructor(store, data) { | ||
| 55 | + super(AsyncContextFrame.current()); | ||
| 56 | + this.set(store, data); | ||
| 57 | + } | ||
| 58 | + | ||
| 59 | + static get enabled() { | ||
| 60 | + enabled_ ??= checkEnabled(); | ||
| 61 | + return enabled_; | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + static current() {} | ||
| 65 | + static set(frame) {} | ||
| 66 | + static exchange(frame) {} | ||
| 67 | + static disable(store) {} | ||
| 44 | 68 | ||
| 45 | 69 | disable(store) { | |
| 46 | 70 | this.delete(store); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -44,7 +44,7 @@ const { AsyncResource } = require('async_hooks'); | |||
| 44 | 44 | ||
| 45 | 45 | const AsyncContextFrame = require('internal/async_context_frame'); | |
| 46 | 46 | ||
| 47 | - const async_context_frame = Symbol('asyncContextFrame'); | ||
| 47 | + const async_context_frame = Symbol('kAsyncContextFrame'); | ||
| 48 | 48 | ||
| 49 | 49 | // *Must* match Environment::TickInfo::Fields in src/env.h. | |
| 50 | 50 | const kHasTickScheduled = 0; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -123,7 +123,7 @@ let debug = require('internal/util/debuglog').debuglog('timer', (fn) => { | |||
| 123 | 123 | ||
| 124 | 124 | const AsyncContextFrame = require('internal/async_context_frame'); | |
| 125 | 125 | ||
| 126 | - const async_context_frame = Symbol('asyncContextFrame'); | ||
| 126 | + const async_context_frame = Symbol('kAsyncContextFrame'); | ||
| 127 | 127 | ||
| 128 | 128 | // *Must* match Environment::ImmediateInfo::Fields in src/env.h. | |
| 129 | 129 | const kCount = 0; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -27,8 +27,8 @@ AsyncResource::AsyncResource(Isolate* isolate, | |||
| 27 | 27 | ||
| 28 | 28 | AsyncResource::~AsyncResource() { | |
| 29 | 29 | CHECK_NOT_NULL(env_); | |
| 30 | - env_->RemoveAsyncResourceContextFrame(reinterpret_cast<std::uintptr_t>(this)); | ||
| 31 | 30 | EmitAsyncDestroy(env_, async_context_); | |
| 31 | + env_->RemoveAsyncResourceContextFrame(reinterpret_cast<std::uintptr_t>(this)); | ||
| 32 | 32 | } | |
| 33 | 33 | ||
| 34 | 34 | MaybeLocal<Value> AsyncResource::MakeCallback(Local<Function> callback, | |
| Back | FazBrowse Home | New Git URL |
0 commit comments