-
Notifications
You must be signed in to change notification settings - Fork 29.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
async_wrap: make native API public #3504
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -101,6 +101,7 @@ | |
], | ||
|
||
'sources': [ | ||
'src/base-object.cc', | ||
'src/debug-agent.cc', | ||
'src/async-wrap.cc', | ||
'src/env.cc', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
#include "async-wrap.h" | ||
#include "async-wrap-inl.h" | ||
#include "base-object.h" | ||
#include "base-object-inl.h" | ||
#include "env.h" | ||
#include "env-inl.h" | ||
#include "util.h" | ||
|
@@ -14,9 +16,11 @@ using v8::Function; | |
using v8::FunctionCallbackInfo; | ||
using v8::HandleScope; | ||
using v8::HeapProfiler; | ||
using v8::Int32; | ||
using v8::Integer; | ||
using v8::Isolate; | ||
using v8::Local; | ||
using v8::MaybeLocal; | ||
using v8::Object; | ||
using v8::RetainedObjectInfo; | ||
using v8::TryCatch; | ||
|
@@ -101,6 +105,67 @@ RetainedObjectInfo* WrapperInfo(uint16_t class_id, Local<Value> wrapper) { | |
// end RetainedAsyncInfo | ||
|
||
|
||
void AsyncWrap::ConstructAsyncWrap(Environment* env, | ||
Local<Object> object, | ||
ProviderType provider, | ||
AsyncWrap* parent) { | ||
CHECK_NE(provider, PROVIDER_NONE); | ||
CHECK_GE(object->InternalFieldCount(), 1); | ||
|
||
// Shift provider value over to prevent id collision. | ||
persistent().SetWrapperClassId(NODE_ASYNC_ID_OFFSET + provider); | ||
|
||
Local<Function> init_fn = env->async_hooks_init_function(); | ||
|
||
// No init callback exists, no reason to go on. | ||
if (init_fn.IsEmpty()) | ||
return; | ||
|
||
// If async wrap callbacks are disabled and no parent was passed that has | ||
// run the init callback then return. | ||
if (!env->async_wrap_callbacks_enabled() && | ||
(parent == nullptr || !parent->ran_init_callback())) | ||
return; | ||
|
||
HandleScope scope(env->isolate()); | ||
|
||
Local<Value> argv[] = { | ||
Int32::New(env->isolate(), provider), | ||
Null(env->isolate()) | ||
}; | ||
|
||
if (parent != nullptr) | ||
argv[1] = parent->object(); | ||
|
||
MaybeLocal<Value> ret = | ||
init_fn->Call(env->context(), object, ARRAY_SIZE(argv), argv); | ||
|
||
if (ret.IsEmpty()) | ||
FatalError("node::AsyncWrap::AsyncWrap", "init hook threw"); | ||
|
||
bits_ |= 1; // ran_init_callback() is true now. | ||
} | ||
|
||
|
||
AsyncWrap::AsyncWrap(Environment* env, | ||
Local<Object> object, | ||
ProviderType provider, | ||
AsyncWrap* parent) | ||
: BaseObject(env, object), bits_(static_cast<uint32_t>(provider) << 1) { | ||
ConstructAsyncWrap(env, object, provider, parent); | ||
} | ||
|
||
|
||
AsyncWrap::AsyncWrap(Isolate* isolate, | ||
Local<Object> object, | ||
ProviderType provider, | ||
AsyncWrap* parent) | ||
: BaseObject(isolate, object), bits_(static_cast<uint32_t>(provider) << 1) { | ||
Environment* env = Environment::GetCurrent(isolate); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exactly! |
||
ConstructAsyncWrap(env, object, provider, parent); | ||
} | ||
|
||
|
||
static void EnableHooksJS(const FunctionCallbackInfo<Value>& args) { | ||
Environment* env = Environment::GetCurrent(args); | ||
env->async_hooks()->set_enable_callbacks(1); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,7 +33,8 @@ namespace node { | |
V(UDPWRAP) \ | ||
V(UDPSENDWRAP) \ | ||
V(WRITEWRAP) \ | ||
V(ZLIB) | ||
V(ZLIB) \ | ||
V(USER) | ||
|
||
class Environment; | ||
|
||
|
@@ -46,10 +47,16 @@ class AsyncWrap : public BaseObject { | |
#undef V | ||
}; | ||
|
||
inline AsyncWrap(Environment* env, | ||
v8::Local<v8::Object> object, | ||
ProviderType provider, | ||
AsyncWrap* parent = nullptr); | ||
AsyncWrap(v8::Isolate* isolate, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you want to make this header public then you should make all methods non-inline. Otherwise it may get hard to maintain ABI stability. |
||
v8::Local<v8::Object> object, | ||
ProviderType provider, | ||
AsyncWrap* parent = nullptr); | ||
|
||
// Private API. Users don't have access to Environment. | ||
AsyncWrap(Environment* env, | ||
v8::Local<v8::Object> object, | ||
ProviderType provider, | ||
AsyncWrap* parent = nullptr); | ||
|
||
inline virtual ~AsyncWrap() override = default; | ||
|
||
|
@@ -66,11 +73,17 @@ class AsyncWrap : public BaseObject { | |
int argc, | ||
v8::Local<v8::Value>* argv); | ||
|
||
inline bool ran_init_callback() const; | ||
|
||
virtual size_t self_size() const = 0; | ||
|
||
private: | ||
inline AsyncWrap(); | ||
inline bool ran_init_callback() const; | ||
|
||
void ConstructAsyncWrap(Environment* env, | ||
v8::Local<v8::Object> object, | ||
ProviderType provider, | ||
AsyncWrap* parent); | ||
|
||
// When the async hooks init JS function is called from the constructor it is | ||
// expected the context object will receive a _asyncQueue object property | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#include "base-object.h" | ||
#include "base-object-inl.h" | ||
#include "env.h" | ||
#include "env-inl.h" | ||
|
||
namespace node { | ||
|
||
using v8::Local; | ||
using v8::Object; | ||
|
||
BaseObject::BaseObject(Environment* env, Local<Object> handle) | ||
: handle_(env->isolate(), handle), | ||
isolate_(env->isolate()), | ||
env_(env) { | ||
CHECK_EQ(false, handle.IsEmpty()); | ||
} | ||
|
||
} // namespace node |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe make
bits_
a uintptr_t so there is room to change it to a pointer without breaking ABI.