summaryrefslogtreecommitdiff
path: root/src/node.h
diff options
context:
space:
mode:
authorAnna Henningsen <anna@addaleax.net>2017-08-08 20:02:55 +0200
committerAnna Henningsen <anna@addaleax.net>2017-09-14 17:38:44 +0200
commit64616bb76932f8a1b05cc616e82dba84c2a7f87e (patch)
tree9cf828e999cc751321ac01c170273f0f543fa4d5 /src/node.h
parentbe6d807bbf09ab09cfabf7e941fbaedbb1ff7456 (diff)
downloadandroid-node-v8-64616bb76932f8a1b05cc616e82dba84c2a7f87e.tar.gz
android-node-v8-64616bb76932f8a1b05cc616e82dba84c2a7f87e.tar.bz2
android-node-v8-64616bb76932f8a1b05cc616e82dba84c2a7f87e.zip
src: refactor async callback handling
- Merge the two almost-but-not-quite identical `MakeCallback()` implementations - Provide a public `CallbackScope` class for embedders in order to enable `MakeCallback()`-like behaviour without tying that to calling a JS function PR-URL: https://github.com/nodejs/node/pull/14697 Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'src/node.h')
-rw-r--r--src/node.h40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/node.h b/src/node.h
index 5fe7bde428..6558a9ee6d 100644
--- a/src/node.h
+++ b/src/node.h
@@ -570,6 +570,36 @@ NODE_EXTERN async_context EmitAsyncInit(v8::Isolate* isolate,
NODE_EXTERN void EmitAsyncDestroy(v8::Isolate* isolate,
async_context asyncContext);
+class InternalCallbackScope;
+
+/* This class works like `MakeCallback()` in that it sets up a specific
+ * asyncContext as the current one and informs the async_hooks and domains
+ * modules that this context is currently active.
+ *
+ * `MakeCallback()` is a wrapper around this class as well as
+ * `Function::Call()`. Either one of these mechanisms needs to be used for
+ * top-level calls into JavaScript (i.e. without any existing JS stack).
+ *
+ * This object should be stack-allocated to ensure that it is contained in a
+ * valid HandleScope.
+ */
+class NODE_EXTERN CallbackScope {
+ public:
+ CallbackScope(v8::Isolate* isolate,
+ v8::Local<v8::Object> resource,
+ async_context asyncContext);
+ ~CallbackScope();
+
+ private:
+ InternalCallbackScope* private_;
+ v8::TryCatch try_catch_;
+
+ void operator=(const CallbackScope&) = delete;
+ void operator=(CallbackScope&&) = delete;
+ CallbackScope(const CallbackScope&) = delete;
+ CallbackScope(CallbackScope&&) = delete;
+};
+
/* An API specific to emit before/after callbacks is unnecessary because
* MakeCallback will automatically call them for you.
*
@@ -659,6 +689,16 @@ class AsyncResource {
async_id get_trigger_async_id() const {
return async_context_.trigger_async_id;
}
+
+ protected:
+ class CallbackScope : public node::CallbackScope {
+ public:
+ explicit CallbackScope(AsyncResource* res)
+ : node::CallbackScope(res->isolate_,
+ res->resource_.Get(res->isolate_),
+ res->async_context_) {}
+ };
+
private:
v8::Isolate* isolate_;
v8::Persistent<v8::Object> resource_;