diff --git a/lib/async_hooks.js b/lib/async_hooks.js index 8638bb4d7b34..3b0730c37ec0 100644 --- a/lib/async_hooks.js +++ b/lib/async_hooks.js @@ -27,6 +27,8 @@ const { } = require('internal/validators'); const internal_async_hooks = require('internal/async_hooks'); +const { AsyncLocalStorage } = internalBinding('async_local_storage'); + // Get functions // For userland AsyncResources, make sure to emit a destroy event when the // resource gets gced. @@ -262,97 +264,6 @@ class AsyncResource { } } -const storageList = []; -const storageHook = createHook({ - init(asyncId, type, triggerAsyncId, resource) { - const currentResource = executionAsyncResource(); - // Value of currentResource is always a non null object - for (let i = 0; i < storageList.length; ++i) { - storageList[i]._propagate(resource, currentResource); - } - } -}); - -class AsyncLocalStorage { - constructor() { - this.kResourceStore = Symbol('kResourceStore'); - this.enabled = false; - } - - disable() { - if (this.enabled) { - this.enabled = false; - // If this.enabled, the instance must be in storageList - ArrayPrototypeSplice(storageList, - ArrayPrototypeIndexOf(storageList, this), 1); - if (storageList.length === 0) { - storageHook.disable(); - } - } - } - - _enable() { - if (!this.enabled) { - this.enabled = true; - ArrayPrototypePush(storageList, this); - storageHook.enable(); - } - } - - // Propagate the context from a parent resource to a child one - _propagate(resource, triggerResource) { - const store = triggerResource[this.kResourceStore]; - if (this.enabled) { - resource[this.kResourceStore] = store; - } - } - - enterWith(store) { - this._enable(); - const resource = executionAsyncResource(); - resource[this.kResourceStore] = store; - } - - run(store, callback, ...args) { - // Avoid creation of an AsyncResource if store is already active - if (ObjectIs(store, this.getStore())) { - return ReflectApply(callback, null, args); - } - - this._enable(); - - const resource = executionAsyncResource(); - const oldStore = resource[this.kResourceStore]; - - resource[this.kResourceStore] = store; - - try { - return ReflectApply(callback, null, args); - } finally { - resource[this.kResourceStore] = oldStore; - } - } - - exit(callback, ...args) { - if (!this.enabled) { - return ReflectApply(callback, null, args); - } - this.disable(); - try { - return ReflectApply(callback, null, args); - } finally { - this._enable(); - } - } - - getStore() { - if (this.enabled) { - const resource = executionAsyncResource(); - return resource[this.kResourceStore]; - } - } -} - // Placing all exports down here because the exported classes won't export // otherwise. module.exports = { diff --git a/node.gyp b/node.gyp index d063a6408072..3d3229106163 100644 --- a/node.gyp +++ b/node.gyp @@ -469,10 +469,12 @@ 'src/api/exceptions.cc', 'src/api/hooks.cc', 'src/api/utils.cc', + 'src/async_local_storage.cc', 'src/async_wrap.cc', 'src/cares_wrap.cc', 'src/connect_wrap.cc', 'src/connection_wrap.cc', + 'src/context_storage.cc', 'src/debug_utils.cc', 'src/env.cc', 'src/fs_event_wrap.cc', @@ -564,6 +566,7 @@ 'src/aliased_buffer.h', 'src/aliased_struct.h', 'src/aliased_struct-inl.h', + 'src/async_local_storage.h', 'src/async_wrap.h', 'src/async_wrap-inl.h', 'src/base_object.h', @@ -574,6 +577,7 @@ 'src/callback_queue-inl.h', 'src/connect_wrap.h', 'src/connection_wrap.h', + 'src/context_storage.h', 'src/debug_utils.h', 'src/debug_utils-inl.h', 'src/env.h', diff --git a/src/api/callback.cc b/src/api/callback.cc index 1287eb466fdd..33ec8170f936 100644 --- a/src/api/callback.cc +++ b/src/api/callback.cc @@ -1,4 +1,5 @@ #include "node.h" +#include "async_local_storage.h" #include "async_wrap-inl.h" #include "env-inl.h" #include "v8.h" @@ -52,7 +53,8 @@ InternalCallbackScope::InternalCallbackScope(Environment* env, async_context_(asyncContext), object_(object), skip_hooks_(flags & kSkipAsyncHooks), - skip_task_queues_(flags & kSkipTaskQueues) { + skip_task_queues_(flags & kSkipTaskQueues), + context_storage_scope_(AsyncLocalStorage::GetState(object)) { CHECK_NOT_NULL(env); env->PushAsyncCallbackScope(); diff --git a/src/async_local_storage.cc b/src/async_local_storage.cc new file mode 100644 index 000000000000..ed25bb719a87 --- /dev/null +++ b/src/async_local_storage.cc @@ -0,0 +1,250 @@ +#include "async_local_storage.h" + +#include + +#include "env-inl.h" +#include "memory_tracker-inl.h" +#include "node_external_reference.h" +#include "util-inl.h" + +#include "v8.h" + +namespace node { + +using v8::Context; +using v8::Function; +using v8::FunctionCallbackInfo; +using v8::FunctionTemplate; +using v8::Isolate; +using v8::Local; +using v8::MaybeLocal; +using v8::Object; +using v8::String; +using v8::Symbol; +using v8::Value; + +AsyncLocalStorageState::AsyncLocalStorageState(Environment* env, + Local wrap) + : BaseObject(env, wrap), + state(ContextStorageState::State()) { + MakeWeak(); +} + +BaseObjectPtr AsyncLocalStorageState::Create( + Environment* env) { + Local obj; + + if (!GetConstructorTemplate(env) + ->InstanceTemplate() + ->NewInstance(env->context()) + .ToLocal(&obj)) { + return BaseObjectPtr(); + } + + return MakeBaseObject(env, obj); +} + +Local AsyncLocalStorageState::GetConstructorTemplate( + Environment* env) { + Local tmpl = env->async_local_storage_state_ctor_template(); + if (tmpl.IsEmpty()) { + tmpl = env->NewFunctionTemplate(nullptr); + Local classname = + FIXED_ONE_BYTE_STRING(env->isolate(), "AsyncLocalStorageState"); + tmpl->SetClassName(classname); + tmpl->Inherit(BaseObject::GetConstructorTemplate(env)); + + tmpl->InstanceTemplate()->SetInternalFieldCount( + AsyncLocalStorageState::kInternalFieldCount); + env->SetProtoMethod(tmpl, "restore", AsyncLocalStorageState::Restore); + env->SetProtoMethod(tmpl, "clear", AsyncLocalStorageState::Clear); + + env->set_async_local_storage_state_ctor_template(tmpl); + } + return tmpl; +} + +bool AsyncLocalStorageState::HasInstance(Environment* env, + const Local& object) { + return GetConstructorTemplate(env)->HasInstance(object); +} + +void AsyncLocalStorageState::RegisterExternalReferences( + ExternalReferenceRegistry* registry) { + registry->Register(AsyncLocalStorageState::Restore); + registry->Register(AsyncLocalStorageState::Clear); +} + +void AsyncLocalStorageState::MemoryInfo(MemoryTracker* tracker) const { + tracker->TrackFieldWithSize("state", sizeof(state)); +} + +void AsyncLocalStorageState::Restore(const FunctionCallbackInfo& args) { + AsyncLocalStorageState* state; + ASSIGN_OR_RETURN_UNWRAP(&state, args.Holder()); + state->state->Restore(); +} + +void AsyncLocalStorageState::Clear(const FunctionCallbackInfo& args) { + AsyncLocalStorageState* state; + ASSIGN_OR_RETURN_UNWRAP(&state, args.Holder()); + state->state->Clear(); +} + +AsyncLocalStorage::AsyncLocalStorage(Environment* env, Local object) + : BaseObject(env, object), + storage_(ContextStorage::Create(env->isolate())) {} + +void AsyncLocalStorage::Initialize(Local target, + Local unused, + Local context, + void* priv) { + Environment* env = Environment::GetCurrent(context); + Local t = AsyncLocalStorage::GetConstructorTemplate(env); + env->SetConstructorFunction(target, "AsyncLocalStorage", t); +} + +Local AsyncLocalStorage::GetConstructorTemplate( + Environment* env) { + Local tmpl = env->async_local_storage_ctor_template(); + if (tmpl.IsEmpty()) { + tmpl = env->NewFunctionTemplate(New); + tmpl->SetClassName( + FIXED_ONE_BYTE_STRING(env->isolate(), "AsyncLocalStorage")); + tmpl->InstanceTemplate()->SetInternalFieldCount( + BaseObject::kInternalFieldCount); + tmpl->Inherit(BaseObject::GetConstructorTemplate(env)); + env->SetProtoMethod(tmpl, "getStore", AsyncLocalStorage::GetStore); + env->SetProtoMethod(tmpl, "enterWith", AsyncLocalStorage::EnterWith); + env->SetProtoMethod(tmpl, "exit", AsyncLocalStorage::Exit); + env->SetProtoMethod(tmpl, "run", AsyncLocalStorage::Run); + env->set_async_local_storage_ctor_template(tmpl); + } + return tmpl; +} + +void AsyncLocalStorage::RegisterExternalReferences( + ExternalReferenceRegistry* registry) { + registry->Register(AsyncLocalStorage::EnterWith); + registry->Register(AsyncLocalStorage::Exit); + registry->Register(AsyncLocalStorage::GetStore); + registry->Register(AsyncLocalStorage::New); + registry->Register(AsyncLocalStorage::Run); +} + +void AsyncLocalStorage::MemoryInfo(MemoryTracker* tracker) const { + tracker->TrackFieldWithSize("storage", sizeof(storage_)); +} + +void AsyncLocalStorage::New(const FunctionCallbackInfo& args) { + Environment* env = Environment::GetCurrent(args); + CHECK(args.IsConstructCall()); + new AsyncLocalStorage(env, args.Holder()); +} + +void AsyncLocalStorage::GetStore(const FunctionCallbackInfo& args) { + AsyncLocalStorage* als; + ASSIGN_OR_RETURN_UNWRAP(&als, args.Holder()); + args.GetReturnValue().Set(als->storage_->Get()); +} + +void AsyncLocalStorage::EnterWith(const FunctionCallbackInfo& args) { + AsyncLocalStorage* als; + ASSIGN_OR_RETURN_UNWRAP(&als, args.Holder()); + std::shared_ptr storage = als->storage_; + storage->Enable(); + storage->Enter(args[0]); +} + +MaybeLocal RunFn(Local context, + Local fn, + const FunctionCallbackInfo& args, + int offset) { + int argc = args.Length() - offset; + if (argc < 1) { + return fn->Call(context, args.This(), 0, {}); + } + + MaybeStackBuffer, 16> argv(argc); + for (int i = 0; i < argc; i++) { + argv[i] = args[i + offset]; + } + + return fn->Call(context, args.This(), argc, &argv[0]); +} + +void AsyncLocalStorage::Exit(const FunctionCallbackInfo& args) { + CHECK(args[0]->IsFunction()); + + AsyncLocalStorage* storage; + ASSIGN_OR_RETURN_UNWRAP(&storage, args.Holder()); + + ContextStorageExitScope scope(storage->storage_); + + Local context = storage->env()->context(); + auto fn = args[0].As(); + + args.GetReturnValue().Set(RunFn(context, fn, args, 1).ToLocalChecked()); +} + +void AsyncLocalStorage::Run(const FunctionCallbackInfo& args) { + CHECK(args[1]->IsFunction()); + + AsyncLocalStorage* storage; + ASSIGN_OR_RETURN_UNWRAP(&storage, args.Holder()); + + ContextStorageRunScope scope(storage->storage_, args[0]); + + Local context = storage->env()->context(); + auto fn = args[1].As(); + + v8::Local result; + if (RunFn(context, fn, args, 2).ToLocal(&result)) { + args.GetReturnValue().Set(result); + } +} + +void AsyncLocalStorage::StoreState(v8::Local resource) { + Isolate* isolate = resource->GetIsolate(); + Environment* env = Environment::GetCurrent(isolate); + + USE(resource->SetPrivate(isolate->GetCurrentContext(), + env->context_storage_state_symbol(), + AsyncLocalStorageState::Create(env)->object())); +} + +std::shared_ptr AsyncLocalStorage::GetState( + v8::Local resource) { + Isolate* isolate = resource->GetIsolate(); + Environment* env = Environment::GetCurrent(isolate); + + auto maybe_local = resource->GetPrivate( + isolate->GetCurrentContext(), + env->context_storage_state_symbol()); + + Local value; + if (!maybe_local.ToLocal(&value) || + !AsyncLocalStorageState::HasInstance(env, value)) { + // fprintf(stderr, "Error: No AsyncLocalStorageState found\n"); + // DumpBacktrace(stderr); + return nullptr; + } + + AsyncLocalStorageState* state = Unwrap(value); + + return state->state; +} + +} // namespace node + +NODE_MODULE_CONTEXT_AWARE_INTERNAL( + async_local_storage, + node::AsyncLocalStorage::Initialize) + +NODE_MODULE_EXTERNAL_REFERENCE( + async_local_storage_state, + node::AsyncLocalStorageState::RegisterExternalReferences) + +NODE_MODULE_EXTERNAL_REFERENCE( + async_local_storage, + node::AsyncLocalStorage::RegisterExternalReferences) diff --git a/src/async_local_storage.h b/src/async_local_storage.h new file mode 100644 index 000000000000..6daec0fc0255 --- /dev/null +++ b/src/async_local_storage.h @@ -0,0 +1,103 @@ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +#ifndef SRC_ASYNC_LOCAL_STORAGE_H_ +#define SRC_ASYNC_LOCAL_STORAGE_H_ + +#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS + +#include "context_storage.h" +#include "base_object.h" +#include "v8.h" + +#include + +namespace node { + +class Environment; +class ExternalReferenceRegistry; + +class AsyncLocalStorageState : public BaseObject { + friend class AsyncLocalStorage; + + public: + AsyncLocalStorageState(Environment* env, v8::Local wrap); + + static v8::Local GetConstructorTemplate( + Environment* env); + static bool HasInstance(Environment* env, + const v8::Local& object); + + static BaseObjectPtr Create(Environment* env); + + static void Restore(const v8::FunctionCallbackInfo& args); + static void Clear(const v8::FunctionCallbackInfo& args); + + static void RegisterExternalReferences(ExternalReferenceRegistry* registry); + + void MemoryInfo(MemoryTracker* tracker) const override; + SET_MEMORY_INFO_NAME(AsyncLocalStorageState) + SET_SELF_SIZE(AsyncLocalStorageState) + + private: + std::shared_ptr state; +}; + +class AsyncLocalStorage : public BaseObject { + public: + // This constructor creates a reusable instance where user is responsible + // to call set_provider_type() and AsyncReset() before use. + AsyncLocalStorage(Environment* env, v8::Local object); + AsyncLocalStorage() = delete; + + static v8::Local GetConstructorTemplate( + Environment* env); + + static void Initialize(v8::Local target, + v8::Local unused, + v8::Local context, + void* priv); + + static void EnterWith(const v8::FunctionCallbackInfo& args); + static void Exit(const v8::FunctionCallbackInfo& args); + static void GetStore(const v8::FunctionCallbackInfo& args); + static void New(const v8::FunctionCallbackInfo& args); + static void Run(const v8::FunctionCallbackInfo& args); + + static void StoreState(v8::Local resource); + static std::shared_ptr GetState( + v8::Local resource); + + static void RegisterExternalReferences(ExternalReferenceRegistry* registry); + + void MemoryInfo(MemoryTracker* tracker) const override; + SET_MEMORY_INFO_NAME(AsyncLocalStorage) + SET_SELF_SIZE(AsyncLocalStorage) + + private: + std::shared_ptr storage_; +}; + +} // namespace node + +#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS + +#endif // SRC_ASYNC_LOCAL_STORAGE_H_ diff --git a/src/async_wrap.cc b/src/async_wrap.cc index 38f2eb421f48..433f71918987 100644 --- a/src/async_wrap.cc +++ b/src/async_wrap.cc @@ -20,6 +20,7 @@ // USE OR OTHER DEALINGS IN THE SOFTWARE. #include "async_wrap.h" // NOLINT(build/include_inline) +#include "async_local_storage.h" #include "async_wrap-inl.h" #include "env-inl.h" #include "node_errors.h" @@ -632,6 +633,8 @@ void AsyncWrap::EmitAsyncInit(Environment* env, CHECK(!type.IsEmpty()); AsyncHooks* async_hooks = env->async_hooks(); + AsyncLocalStorage::StoreState(object); + // Nothing to execute, so can continue normally. if (async_hooks->fields()[AsyncHooks::kInit] == 0) { return; diff --git a/src/context_storage.cc b/src/context_storage.cc new file mode 100644 index 000000000000..f19d14abee85 --- /dev/null +++ b/src/context_storage.cc @@ -0,0 +1,116 @@ +#include "context_storage.h" + +#include + +namespace node { + +std::unordered_set> context_storages; +std::unordered_map> context_storage_map; + +ContextStorage::ContextStorage(v8::Isolate* isolate) : isolate_(isolate) {} +ContextStorage::ContextStorage() : isolate_(v8::Isolate::GetCurrent()) {} + +std::shared_ptr ContextStorage::Create(v8::Isolate* isolate) { + auto storage = std::make_shared(isolate); + context_storage_map[storage.get()] = storage; + return storage; +} +std::shared_ptr ContextStorage::Create() { + auto storage = std::make_shared(v8::Isolate::GetCurrent()); + context_storage_map[storage.get()] = storage; + return storage; +} + +ContextStorage::~ContextStorage() { + context_storage_map.extract(this); + Disable(); +} + +void ContextStorage::Enter(const v8::PersistentBase& data) { + stored.Reset(isolate_, data); +} + +bool ContextStorage::Enabled() { + auto it = context_storages.find(context_storage_map[this]); + return it != context_storages.end(); +} + +void ContextStorage::Enable() { + context_storages.insert(context_storage_map[this]); +} + +void ContextStorage::Disable() { + context_storages.extract(context_storage_map[this]); +} + +void ContextStorage::Enter(v8::Local data) { + stored.Reset(isolate_, data); +} + +void ContextStorage::Exit() { + stored.Reset(); +} + +v8::Local ContextStorage::Get() { + if (!Enabled() || stored.IsEmpty()) { + return v8::Undefined(isolate_); + } + return stored.Get(isolate_); +} + +void ContextStorageState::Restore() { + for (auto storage : context_storages) { + storage->Enter(state[storage]); + } +} + +void ContextStorageState::Clear() { + for (auto storage : context_storages) { + storage->Exit(); + } +} + +std::shared_ptr ContextStorageState::State() { + std::shared_ptr state = + std::make_shared(); + + for (auto storage : context_storages) { + state->state[storage].Reset(storage->isolate_, storage->stored); + } + + return state; +} + +ContextStorageRunScope::ContextStorageRunScope(std::shared_ptr storage, + v8::Local data) + : storage_(storage) { + storage_->Enable(); + storage_->Enter(data); +} + +ContextStorageRunScope::~ContextStorageRunScope() { + storage_->Exit(); +} + +ContextStorageExitScope::ContextStorageExitScope(std::shared_ptr storage) + : storage_(storage) { + storage_->Disable(); +} + +ContextStorageExitScope::~ContextStorageExitScope() { + storage_->Enable(); +} + +ContextStorageRestoreScope::ContextStorageRestoreScope( + std::shared_ptr state) + : state(std::move(state)) { + if (!state) return; + state->Restore(); +} + +ContextStorageRestoreScope::~ContextStorageRestoreScope() { + if (!state) return; + state->Clear(); +} + +} // namespace node diff --git a/src/context_storage.h b/src/context_storage.h new file mode 100644 index 000000000000..519de8ccdcd4 --- /dev/null +++ b/src/context_storage.h @@ -0,0 +1,103 @@ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +#ifndef SRC_CONTEXT_STORAGE_H_ +#define SRC_CONTEXT_STORAGE_H_ + +#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS + +#include + +#include "v8.h" + +namespace node { + +class Environment; +class ExternalReferenceRegistry; + +class ContextStorage { + friend class ContextStorageState; + + private: + v8::Isolate* isolate_; + v8::Global stored; + + void Enter(const v8::PersistentBase& data); + + public: + ContextStorage(v8::Isolate* isolate); + ContextStorage(); + ~ContextStorage(); + + static std::shared_ptr Create(v8::Isolate* isolate); + static std::shared_ptr Create(); + + bool Enabled(); + void Enable(); + void Disable(); + void Enter(v8::Local data); + void Exit(); + v8::Local Get(); +}; + +class ContextStorageState { + private: + std::unordered_map, v8::Global> state; + + public: + void Restore(); + void Clear(); + static std::shared_ptr State(); +}; + +class ContextStorageRunScope { + private: + std::shared_ptr storage_; + + public: + ContextStorageRunScope(std::shared_ptr storage, + v8::Local data); + ~ContextStorageRunScope(); +}; + +class ContextStorageExitScope { + private: + std::shared_ptr storage_; + + public: + ContextStorageExitScope(std::shared_ptr storage); + ~ContextStorageExitScope(); +}; + +class ContextStorageRestoreScope { + private: + std::shared_ptr state; + + public: + ContextStorageRestoreScope(std::shared_ptr state); + ~ContextStorageRestoreScope(); +}; + +} // namespace node + +#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS + +#endif // SRC_CONTEXT_STORAGE_H_ diff --git a/src/env.h b/src/env.h index 921b6758ac5b..53be82cf4a30 100644 --- a/src/env.h +++ b/src/env.h @@ -167,6 +167,7 @@ class NoArrayBufferZeroFillScope { #define PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(V) \ V(alpn_buffer_private_symbol, "node:alpnBuffer") \ V(arrow_message_private_symbol, "node:arrowMessage") \ + V(context_storage_state_symbol, "node:contextStorage") \ V(contextify_context_private_symbol, "node:contextify:context") \ V(contextify_global_private_symbol, "node:contextify:global") \ V(decorated_private_symbol, "node:decorated") \ @@ -470,6 +471,8 @@ class NoArrayBufferZeroFillScope { V(zero_return_string, "ZERO_RETURN") #define ENVIRONMENT_STRONG_PERSISTENT_TEMPLATES(V) \ + V(async_local_storage_ctor_template, v8::FunctionTemplate) \ + V(async_local_storage_state_ctor_template, v8::FunctionTemplate) \ V(async_wrap_ctor_template, v8::FunctionTemplate) \ V(async_wrap_object_ctor_template, v8::FunctionTemplate) \ V(base_object_ctor_template, v8::FunctionTemplate) \ diff --git a/src/node_binding.cc b/src/node_binding.cc index 2991ee34746e..0e8780ae271e 100644 --- a/src/node_binding.cc +++ b/src/node_binding.cc @@ -39,6 +39,7 @@ // node is built as static library. No need to depend on the // __attribute__((constructor)) like mechanism in GCC. #define NODE_BUILTIN_STANDARD_MODULES(V) \ + V(async_local_storage) \ V(async_wrap) \ V(blob) \ V(block_list) \ diff --git a/src/node_external_reference.h b/src/node_external_reference.h index 58b8f4bed2fc..3c8a02392143 100644 --- a/src/node_external_reference.h +++ b/src/node_external_reference.h @@ -52,6 +52,8 @@ class ExternalReferenceRegistry { }; #define EXTERNAL_REFERENCE_BINDING_LIST_BASE(V) \ + V(async_local_storage) \ + V(async_local_storage_state) \ V(async_wrap) \ V(binding) \ V(blob) \ diff --git a/src/node_internals.h b/src/node_internals.h index f8c31b386d7b..b4a5e9d6dbb7 100644 --- a/src/node_internals.h +++ b/src/node_internals.h @@ -24,6 +24,7 @@ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS +#include "context_storage.h" #include "env.h" #include "node.h" #include "node_binding.h" @@ -234,6 +235,7 @@ class InternalCallbackScope { bool failed_ = false; bool pushed_ids_ = false; bool closed_ = false; + ContextStorageRestoreScope context_storage_scope_; }; class DebugSealHandleScope { diff --git a/test/parallel/test-bootstrap-modules.js b/test/parallel/test-bootstrap-modules.js index 495f08f716f5..0e0e99989d15 100644 --- a/test/parallel/test-bootstrap-modules.js +++ b/test/parallel/test-bootstrap-modules.js @@ -9,6 +9,7 @@ const common = require('../common'); const assert = require('assert'); const expectedModules = new Set([ + 'Internal Binding async_local_storage', 'Internal Binding async_wrap', 'Internal Binding block_list', 'Internal Binding buffer',