aboutsummaryrefslogtreecommitdiff
path: root/test/addons/callback-scope
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 /test/addons/callback-scope
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 'test/addons/callback-scope')
-rw-r--r--test/addons/callback-scope/binding.cc39
-rw-r--r--test/addons/callback-scope/binding.gyp9
-rw-r--r--test/addons/callback-scope/test-async-hooks.js22
-rw-r--r--test/addons/callback-scope/test.js17
4 files changed, 87 insertions, 0 deletions
diff --git a/test/addons/callback-scope/binding.cc b/test/addons/callback-scope/binding.cc
new file mode 100644
index 0000000000..31317e5dae
--- /dev/null
+++ b/test/addons/callback-scope/binding.cc
@@ -0,0 +1,39 @@
+#include "node.h"
+#include "v8.h"
+
+#include <assert.h>
+#include <vector>
+
+namespace {
+
+void RunInCallbackScope(const v8::FunctionCallbackInfo<v8::Value>& args) {
+ v8::Isolate* isolate = args.GetIsolate();
+
+ assert(args.Length() == 4);
+ assert(args[0]->IsObject());
+ assert(args[1]->IsNumber());
+ assert(args[2]->IsNumber());
+ assert(args[3]->IsFunction());
+
+ node::async_context asyncContext = {
+ args[1].As<v8::Number>()->Value(),
+ args[2].As<v8::Number>()->Value()
+ };
+
+ node::CallbackScope scope(isolate, args[0].As<v8::Object>(), asyncContext);
+ v8::Local<v8::Function> fn = args[3].As<v8::Function>();
+
+ v8::MaybeLocal<v8::Value> ret =
+ fn->Call(isolate->GetCurrentContext(), args[0], 0, nullptr);
+
+ if (!ret.IsEmpty())
+ args.GetReturnValue().Set(ret.ToLocalChecked());
+}
+
+void Initialize(v8::Local<v8::Object> exports) {
+ NODE_SET_METHOD(exports, "runInCallbackScope", RunInCallbackScope);
+}
+
+} // namespace
+
+NODE_MODULE(binding, Initialize)
diff --git a/test/addons/callback-scope/binding.gyp b/test/addons/callback-scope/binding.gyp
new file mode 100644
index 0000000000..7ede63d94a
--- /dev/null
+++ b/test/addons/callback-scope/binding.gyp
@@ -0,0 +1,9 @@
+{
+ 'targets': [
+ {
+ 'target_name': 'binding',
+ 'defines': [ 'V8_DEPRECATION_WARNINGS=1' ],
+ 'sources': [ 'binding.cc' ]
+ }
+ ]
+}
diff --git a/test/addons/callback-scope/test-async-hooks.js b/test/addons/callback-scope/test-async-hooks.js
new file mode 100644
index 0000000000..94b53efc53
--- /dev/null
+++ b/test/addons/callback-scope/test-async-hooks.js
@@ -0,0 +1,22 @@
+'use strict';
+
+const common = require('../../common');
+const assert = require('assert');
+const async_hooks = require('async_hooks');
+const { runInCallbackScope } = require(`./build/${common.buildType}/binding`);
+
+let insideHook = false;
+async_hooks.createHook({
+ before: common.mustCall((id) => {
+ assert.strictEqual(id, 1000);
+ insideHook = true;
+ }),
+ after: common.mustCall((id) => {
+ assert.strictEqual(id, 1000);
+ insideHook = false;
+ })
+}).enable();
+
+runInCallbackScope({}, 1000, 1000, () => {
+ assert(insideHook);
+});
diff --git a/test/addons/callback-scope/test.js b/test/addons/callback-scope/test.js
new file mode 100644
index 0000000000..2f2efe5f47
--- /dev/null
+++ b/test/addons/callback-scope/test.js
@@ -0,0 +1,17 @@
+'use strict';
+
+const common = require('../../common');
+const assert = require('assert');
+const { runInCallbackScope } = require(`./build/${common.buildType}/binding`);
+
+assert.strictEqual(runInCallbackScope({}, 0, 0, () => 42), 42);
+
+{
+ process.once('uncaughtException', common.mustCall((err) => {
+ assert.strictEqual(err.message, 'foo');
+ }));
+
+ runInCallbackScope({}, 0, 0, () => {
+ throw new Error('foo');
+ });
+}