Skip to content

Commit

Permalink
[squash] remove new MakeCallback, AsyncInit, AsyncDestroy
Browse files Browse the repository at this point in the history
  • Loading branch information
ofrobots committed Feb 7, 2018
1 parent 622a125 commit b583db3
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 293 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ LINT_SOURCES = \
test/cpp/json-parse.cpp \
test/cpp/json-stringify.cpp \
test/cpp/makecallback.cpp \
test/cpp/makecallbackcontext.cpp \
test/cpp/morenews.cpp \
test/cpp/multifile1.cpp \
test/cpp/multifile2.cpp \
Expand Down
71 changes: 12 additions & 59 deletions doc/node_misc.md
Original file line number Diff line number Diff line change
@@ -1,54 +1,20 @@
## Miscellaneous Node Helpers

- <a href="#api_nan_async_init"><b><code>Nan::AsyncInit()</code></b></a>
- <a href="#api_nan_async_destory"><b><code>Nan::AsyncDestory()</code></b></a>
- <a href="#api_nan_asyncresource"><b><code>Nan::AsyncResource</code></b></a>
- <a href="#api_nan_make_callback"><b><code>Nan::MakeCallback()</code></b></a>
- <a href="#api_nan_module_init"><b><code>NAN_MODULE_INIT()</code></b></a>
- <a href="#api_nan_export"><b><code>Nan::Export()</code></b></a>


<a name="api_nan_async_init"></a>
### Nan::AsyncInit()

When calling back into JavaScript asynchornously, special care must be taken to ensure that the runtime can properly track
async hops. `Nan::AsyncInit` is an object that wraps `node::EmitAsyncInit` and returns a wrapper for the
`node::async_context` structure. The `async_context` can be provided to `Nan::MakeCallback` to properly restore the correct
async execution context.

Signatures:

```c++
Nan::async_context AsyncInit(v8::MaybeLocal<v8::Object> maybe_resource,
const char* name);
Nan::async_context AsyncInit(v8::MaybeLocal<v8::Object> maybe_resource,
v8::Local<v8::String> name);
```
* `maybe_resource`: An optional object associated with the async work that will be passed to the possible [async_hooks][]
`init` hook.
* `name`: Identified for the kind of resource that is being provided for diagnostics information exposed by the [async_hooks][]
API. This will be passed to the possible `init` hook as the `type`. To avoid name collisions with other modules we recommend
that the name include the name of the owning module as a prefix. For example `mysql` module could use something like
`mysql:batch-db-query-resource`.
* An opaque `async_context` structure is returned. This should be passed to any `Nan::MakeCallback` operations done later.
For more details, see the Node [async_hooks][] documentation. You might also want to take a look at the documentation for the
[N-API counterpart][napi]. For example usage, see the `makecallbackcontext.cpp` example in the `test/cpp` directory.
Note: It might be more convenient to use `Nan::AsyncResource` instead of using this directly.
<a name="api_nan_async_destory"></a>
### Nan::AsyncDestroy()
Wrapper around `node::EmitAsyncDestroy`.
Note: It might be more convenient to use `Nan::AsyncResource` instead of using this directly.
<a name="api_nan_asyncresource"></a>
### Nan::AsyncResource

`Nan::AsyncResouce` is a convenience class that provides RAII wrapper around `Nan::AsyncInit`, `Nan::AsyncDestroy` and `Nan::MakeCallback`. It is analogous to the `AsyncResource` JavaScript class exposed by Node's [async_hooks][] API.
This class is analogous to the `AsyncResource` JavaScript class exposed by Node's [async_hooks][] API.

When calling back into JavaScript asynchornously, special care must be taken to ensure that the runtime can properly track
async hops. `Nan::AsyncResource` is a class that provides an RAII wrapper around `node::EmitAsyncInit`, `node::EmitAsyncDestroy`,
and `node::MakeCallback`. Using this mechanism to call back into JavaScript, as opposed to `Nan::MakeCallback` or
`v8::Function::Call` ensures that the callback is executed in the correct async context. This ensures that async mechanisms
such as domains and [async_hooks][] function correctly.

Definition:

Expand Down Expand Up @@ -76,6 +42,7 @@ class AsyncResource {
Nan::async_context async_context);
};
```
* `maybe_resource`: An optional object associated with the async work that will be passed to the possible [async_hooks][]
`init` hook.
* `name`: Identified for the kind of resource that is being provided for diagnostics information exposed by the [async_hooks][]
Expand All @@ -86,7 +53,8 @@ class AsyncResource {
correct async execution context.
* `AsyncDestroy` is automatically called when an AsyncResource object is destroyed.
For example usage, see the `asyncresource.cpp` example in the `test/cpp` directory.
For more details, see the Node [async_hooks][] documentation. You might also want to take a look at the documentation for the
[N-API counterpart][napi]. For example usage, see the `asyncresource.cpp` example in the `test/cpp` directory.
<a name="api_nan_make_callback"></a>
### Nan::MakeCallback()
Expand All @@ -100,23 +68,8 @@ Use `MakeCallback()` rather than using `v8::Function#Call()` directly in order t
Signatures:
```c++
v8::MaybeLocal<v8::Value> Nan::MakeCallback(v8::Local<v8::Object> target,
v8::Local<v8::Function> func,
int argc,
v8::Local<v8::Value>* argv,
Nan::async_context async_context);
v8::MaybeLocal<v8::Value> Nan::MakeCallback(v8::Local<v8::Object> target,
v8::Local<v8::String> symbol,
int argc,
v8::Local<v8::Value>* argv,
Nan::async_context async_context);
v8::MaybeLocal<v8::Value> Nan::MakeCallback(v8::Local<v8::Object> target,
const char* method,
int argc,
v8::Local<v8::Value>* argv,
Nan::async_context async_context);

// Legacy versions. We recommend the async context preserving versions above.
// Legacy versions. We recommend the AsyncResource class and
// AsyncResource::runInAsyncScope instead.
v8::Local<v8::Value> Nan::MakeCallback(v8::Local<v8::Object> target,
v8::Local<v8::Function> func,
int argc,
Expand Down
141 changes: 52 additions & 89 deletions nan.h
Original file line number Diff line number Diff line change
Expand Up @@ -1273,138 +1273,101 @@ class Utf8String {

#endif // NODE_MODULE_VERSION

//=== async_context and context aware MakeCallback =============================
//=== async_context ============================================================

#if NODE_MODULE_VERSION >= NODE_8_0_MODULE_VERSION
typedef node::async_context async_context;
#else
struct async_context {};
#endif

inline async_context AsyncInit(
MaybeLocal<v8::Object> maybe_resource
, v8::Local<v8::String> resource_name) {
#if NODE_MODULE_VERSION < NODE_8_0_MODULE_VERSION
return async_context();
#else
v8::Isolate* isolate = v8::Isolate::GetCurrent();

v8::Local<v8::Object> resource =
maybe_resource.IsEmpty() ? New<v8::Object>()
: maybe_resource.ToLocalChecked();

node::async_context context =
node::EmitAsyncInit(isolate, resource, resource_name);
return static_cast<async_context>(context);
#endif
}

inline async_context AsyncInit(
MaybeLocal<v8::Object> maybe_resource
, const char* name) {
return AsyncInit(maybe_resource, New<v8::String>(name).ToLocalChecked());
}

inline void AsyncDestroy(async_context context) {
#if NODE_MODULE_VERSION >= NODE_8_0_MODULE_VERSION
v8::Isolate* isolate = v8::Isolate::GetCurrent();
node::async_context node_context =
static_cast<node::async_context>(context);
node::EmitAsyncDestroy(isolate, node_context);
#endif
}

inline MaybeLocal<v8::Value> MakeCallback(
v8::Local<v8::Object> target
, v8::Local<v8::Function> func
, int argc
, v8::Local<v8::Value>* argv
, async_context asyncContext) {
#if NODE_MODULE_VERSION < NODE_8_0_MODULE_VERSION
// Ignore the async_context value.
return MakeCallback(target, func, argc, argv);
#else
return node::MakeCallback(
v8::Isolate::GetCurrent(), target, func, argc, argv,
static_cast<node::async_context>(asyncContext));
#endif
}

inline MaybeLocal<v8::Value> MakeCallback(
v8::Local<v8::Object> target
, v8::Local<v8::String> symbol
, int argc
, v8::Local<v8::Value>* argv
, async_context asyncContext) {
#if NODE_MODULE_VERSION < NODE_8_0_MODULE_VERSION
// Ignore the async_context value.
return MakeCallback(target, symbol, argc, argv);
#else
return node::MakeCallback(
v8::Isolate::GetCurrent(), target, symbol, argc, argv,
static_cast<node::async_context>(asyncContext));
#endif
}

inline MaybeLocal<v8::Value> MakeCallback(
v8::Local<v8::Object> target
, const char* method
, int argc
, v8::Local<v8::Value>* argv
, async_context asyncContext) {
#if NODE_MODULE_VERSION < NODE_8_0_MODULE_VERSION
// Ignore the async_context value.
return MakeCallback(target, method, argc, argv);
#else
return node::MakeCallback(
v8::Isolate::GetCurrent(), target, method, argc, argv,
static_cast<node::async_context>(asyncContext));
#endif
}

// === AsyncResource ===========================================================

class AsyncResource {
public:
AsyncResource(
MaybeLocal<v8::Object> maybe_resource
, v8::Local<v8::String> resource_name) {
asyncContext = AsyncInit(maybe_resource, resource_name);
#if NODE_MODULE_VERSION >= NODE_8_0_MODULE_VERSION
v8::Isolate* isolate = v8::Isolate::GetCurrent();

v8::Local<v8::Object> resource =
maybe_resource.IsEmpty() ? New<v8::Object>()
: maybe_resource.ToLocalChecked();

node::async_context context =
node::EmitAsyncInit(isolate, resource, resource_name);
asyncContext = static_cast<async_context>(context);
#endif
}

AsyncResource(MaybeLocal<v8::Object> maybe_resource, const char* name) {
asyncContext = AsyncInit(maybe_resource, name);
#if NODE_MODULE_VERSION >= NODE_8_0_MODULE_VERSION
v8::Isolate* isolate = v8::Isolate::GetCurrent();

v8::Local<v8::Object> resource =
maybe_resource.IsEmpty() ? New<v8::Object>()
: maybe_resource.ToLocalChecked();
v8::Local<v8::String> name_string =
New<v8::String>(name).ToLocalChecked();
node::async_context context =
node::EmitAsyncInit(isolate, resource, name_string);
asyncContext = static_cast<async_context>(context);
#endif
}

~AsyncResource() {
AsyncDestroy(asyncContext);
#if NODE_MODULE_VERSION >= NODE_8_0_MODULE_VERSION
v8::Isolate* isolate = v8::Isolate::GetCurrent();
node::async_context node_context =
static_cast<node::async_context>(asyncContext);
node::EmitAsyncDestroy(isolate, node_context);
#endif
}

inline MaybeLocal<v8::Value> runInAsyncScope(
v8::Local<v8::Object> target
, v8::Local<v8::Function> func
, int argc
, v8::Local<v8::Value>* argv) {
return MakeCallback(target, func, argc, argv, asyncContext);
#if NODE_MODULE_VERSION < NODE_8_0_MODULE_VERSION
return MakeCallback(target, func, argc, argv);
#else
return node::MakeCallback(
v8::Isolate::GetCurrent(), target, func, argc, argv,
static_cast<node::async_context>(asyncContext));
#endif
}

inline MaybeLocal<v8::Value> runInAsyncScope(
v8::Local<v8::Object> target
, v8::Local<v8::String> symbol
, int argc
, v8::Local<v8::Value>* argv) {
return MakeCallback(target, symbol, argc, argv, asyncContext);
#if NODE_MODULE_VERSION < NODE_8_0_MODULE_VERSION
return MakeCallback(target, symbol, argc, argv);
#else
return node::MakeCallback(
v8::Isolate::GetCurrent(), target, symbol, argc, argv,
static_cast<node::async_context>(asyncContext));
#endif
}

inline MaybeLocal<v8::Value> runInAsyncScope(
v8::Local<v8::Object> target
, const char* method
, int argc
, v8::Local<v8::Value>* argv) {
return MakeCallback(target, method, argc, argv, asyncContext);
#if NODE_MODULE_VERSION < NODE_8_0_MODULE_VERSION
return MakeCallback(target, method, argc, argv);
#else
return node::MakeCallback(
v8::Isolate::GetCurrent(), target, method, argc, argv,
static_cast<node::async_context>(asyncContext));
#endif
}

protected:
private:
async_context asyncContext;
};

Expand Down
4 changes: 0 additions & 4 deletions test/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,6 @@
"target_name" : "makecallback"
, "sources" : [ "cpp/makecallback.cpp" ]
}
, {
"target_name" : "makecallbackcontext"
, "sources" : [ "cpp/makecallbackcontext.cpp" ]
}
, {
"target_name" : "asyncresource"
, "sources" : [ "cpp/asyncresource.cpp" ]
Expand Down
70 changes: 0 additions & 70 deletions test/cpp/makecallbackcontext.cpp

This file was deleted.

Loading

0 comments on commit b583db3

Please sign in to comment.