From 0642a146b36265c6cea042c90951207fe4b8b9ee Mon Sep 17 00:00:00 2001 From: Trevor Norris Date: Mon, 22 Feb 2016 16:34:35 -0700 Subject: [PATCH] async_wrap: setupHooks now accepts object The number of callbacks accepted to setupHooks was getting unwieldy. Instead change the implementation to accept an object with all callbacks Ref: https://github.com/nodejs/node/pull/7048 PR-URL: https://github.com/nodejs/node/pull/5756 Reviewed-By: Ben Noordhuis Reviewed-By: Andreas Madsen --- src/async-wrap.cc | 35 ++++++++++++++----- .../test-async-wrap-check-providers.js | 2 +- ...st-async-wrap-disabled-propagate-parent.js | 2 +- .../test-async-wrap-propagate-parent.js | 2 +- .../parallel/test-async-wrap-throw-no-init.js | 4 +-- test/parallel/test-async-wrap-uid.js | 2 +- 6 files changed, 32 insertions(+), 15 deletions(-) diff --git a/src/async-wrap.cc b/src/async-wrap.cc index dde07aa0756a3e..570ed2f165b2ba 100644 --- a/src/async-wrap.cc +++ b/src/async-wrap.cc @@ -121,18 +121,35 @@ static void SetupHooks(const FunctionCallbackInfo& args) { if (env->async_hooks()->callbacks_enabled()) return env->ThrowError("hooks should not be set while also enabled"); - - if (!args[0]->IsFunction()) + if (!args[0]->IsObject()) + return env->ThrowTypeError("first argument must be an object"); + + Local fn_obj = args[0].As(); + + Local init_v = fn_obj->Get( + env->context(), + FIXED_ONE_BYTE_STRING(env->isolate(), "init")).ToLocalChecked(); + Local pre_v = fn_obj->Get( + env->context(), + FIXED_ONE_BYTE_STRING(env->isolate(), "pre")).ToLocalChecked(); + Local post_v = fn_obj->Get( + env->context(), + FIXED_ONE_BYTE_STRING(env->isolate(), "post")).ToLocalChecked(); + Local destroy_v = fn_obj->Get( + env->context(), + FIXED_ONE_BYTE_STRING(env->isolate(), "destroy")).ToLocalChecked(); + + if (!init_v->IsFunction()) return env->ThrowTypeError("init callback must be a function"); - env->set_async_hooks_init_function(args[0].As()); + env->set_async_hooks_init_function(init_v.As()); - if (args[1]->IsFunction()) - env->set_async_hooks_pre_function(args[1].As()); - if (args[2]->IsFunction()) - env->set_async_hooks_post_function(args[2].As()); - if (args[3]->IsFunction()) - env->set_async_hooks_destroy_function(args[3].As()); + if (pre_v->IsFunction()) + env->set_async_hooks_pre_function(pre_v.As()); + if (post_v->IsFunction()) + env->set_async_hooks_post_function(post_v.As()); + if (destroy_v->IsFunction()) + env->set_async_hooks_destroy_function(destroy_v.As()); } diff --git a/test/parallel/test-async-wrap-check-providers.js b/test/parallel/test-async-wrap-check-providers.js index 3a2c80d9cbe173..5ae8654d337303 100644 --- a/test/parallel/test-async-wrap-check-providers.js +++ b/test/parallel/test-async-wrap-check-providers.js @@ -36,7 +36,7 @@ function init(id, provider) { function noop() { } -async_wrap.setupHooks(init, noop, noop); +async_wrap.setupHooks({ init }); async_wrap.enable(); diff --git a/test/parallel/test-async-wrap-disabled-propagate-parent.js b/test/parallel/test-async-wrap-disabled-propagate-parent.js index 152af4bcde80fc..f0daaeb06d0031 100644 --- a/test/parallel/test-async-wrap-disabled-propagate-parent.js +++ b/test/parallel/test-async-wrap-disabled-propagate-parent.js @@ -30,7 +30,7 @@ function init(uid, type, parentUid, parentHandle) { function noop() { } -async_wrap.setupHooks(init, noop, noop); +async_wrap.setupHooks({ init }); async_wrap.enable(); const server = net.createServer(function(c) { diff --git a/test/parallel/test-async-wrap-propagate-parent.js b/test/parallel/test-async-wrap-propagate-parent.js index e6cb5fa77bdaf7..34a00c22eeeafd 100644 --- a/test/parallel/test-async-wrap-propagate-parent.js +++ b/test/parallel/test-async-wrap-propagate-parent.js @@ -30,7 +30,7 @@ function init(uid, type, parentUid, parentHandle) { function noop() { } -async_wrap.setupHooks(init, noop, noop); +async_wrap.setupHooks({ init }); async_wrap.enable(); const server = net.createServer(function(c) { diff --git a/test/parallel/test-async-wrap-throw-no-init.js b/test/parallel/test-async-wrap-throw-no-init.js index 768e38e8eff389..ccf77f66dc7232 100644 --- a/test/parallel/test-async-wrap-throw-no-init.js +++ b/test/parallel/test-async-wrap-throw-no-init.js @@ -7,14 +7,14 @@ const async_wrap = process.binding('async_wrap'); assert.throws(function() { async_wrap.setupHooks(null); -}, /init callback must be a function/); +}, /first argument must be an object/); assert.throws(function() { async_wrap.enable(); }, /init callback is not assigned to a function/); // Should not throw -async_wrap.setupHooks(() => {}); +async_wrap.setupHooks({ init: () => {} }); async_wrap.enable(); assert.throws(function() { diff --git a/test/parallel/test-async-wrap-uid.js b/test/parallel/test-async-wrap-uid.js index 4e664f1bd46ebb..5bf3a8856e0e3f 100644 --- a/test/parallel/test-async-wrap-uid.js +++ b/test/parallel/test-async-wrap-uid.js @@ -6,7 +6,7 @@ const assert = require('assert'); const async_wrap = process.binding('async_wrap'); const storage = new Map(); -async_wrap.setupHooks(init, pre, post, destroy); +async_wrap.setupHooks({ init, pre, post, destroy }); async_wrap.enable(); function init(uid) {