Browse Source

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

PR-URL: https://github.com/nodejs/node/pull/5756
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Andreas Madsen <amwebdk@gmail.com>
process-exit-stdio-flushing
Trevor Norris 9 years ago
parent
commit
f9938b6141
  1. 35
      src/async-wrap.cc
  2. 2
      test/parallel/test-async-wrap-check-providers.js
  3. 2
      test/parallel/test-async-wrap-disabled-propagate-parent.js
  4. 2
      test/parallel/test-async-wrap-propagate-parent.js
  5. 4
      test/parallel/test-async-wrap-throw-no-init.js
  6. 2
      test/parallel/test-async-wrap-uid.js

35
src/async-wrap.cc

@ -121,18 +121,35 @@ static void SetupHooks(const FunctionCallbackInfo<Value>& args) {
if (env->async_hooks()->callbacks_enabled()) if (env->async_hooks()->callbacks_enabled())
return env->ThrowError("hooks should not be set while also enabled"); return env->ThrowError("hooks should not be set while also enabled");
if (!args[0]->IsObject())
if (!args[0]->IsFunction()) return env->ThrowTypeError("first argument must be an object");
Local<Object> fn_obj = args[0].As<Object>();
Local<Value> init_v = fn_obj->Get(
env->context(),
FIXED_ONE_BYTE_STRING(env->isolate(), "init")).ToLocalChecked();
Local<Value> pre_v = fn_obj->Get(
env->context(),
FIXED_ONE_BYTE_STRING(env->isolate(), "pre")).ToLocalChecked();
Local<Value> post_v = fn_obj->Get(
env->context(),
FIXED_ONE_BYTE_STRING(env->isolate(), "post")).ToLocalChecked();
Local<Value> 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"); return env->ThrowTypeError("init callback must be a function");
env->set_async_hooks_init_function(args[0].As<Function>()); env->set_async_hooks_init_function(init_v.As<Function>());
if (args[1]->IsFunction()) if (pre_v->IsFunction())
env->set_async_hooks_pre_function(args[1].As<Function>()); env->set_async_hooks_pre_function(pre_v.As<Function>());
if (args[2]->IsFunction()) if (post_v->IsFunction())
env->set_async_hooks_post_function(args[2].As<Function>()); env->set_async_hooks_post_function(post_v.As<Function>());
if (args[3]->IsFunction()) if (destroy_v->IsFunction())
env->set_async_hooks_destroy_function(args[3].As<Function>()); env->set_async_hooks_destroy_function(destroy_v.As<Function>());
} }

2
test/parallel/test-async-wrap-check-providers.js

@ -36,7 +36,7 @@ function init(id, provider) {
function noop() { } function noop() { }
async_wrap.setupHooks(init, noop, noop); async_wrap.setupHooks({ init });
async_wrap.enable(); async_wrap.enable();

2
test/parallel/test-async-wrap-disabled-propagate-parent.js

@ -31,7 +31,7 @@ function init(uid, type, parentUid, parentHandle) {
function noop() { } function noop() { }
async_wrap.setupHooks(init, noop, noop); async_wrap.setupHooks({ init });
async_wrap.enable(); async_wrap.enable();
server = net.createServer(function(c) { server = net.createServer(function(c) {

2
test/parallel/test-async-wrap-propagate-parent.js

@ -31,7 +31,7 @@ function init(uid, type, parentUid, parentHandle) {
function noop() { } function noop() { }
async_wrap.setupHooks(init, noop, noop); async_wrap.setupHooks({ init });
async_wrap.enable(); async_wrap.enable();
server = net.createServer(function(c) { server = net.createServer(function(c) {

4
test/parallel/test-async-wrap-throw-no-init.js

@ -7,14 +7,14 @@ const async_wrap = process.binding('async_wrap');
assert.throws(function() { assert.throws(function() {
async_wrap.setupHooks(null); async_wrap.setupHooks(null);
}, /init callback must be a function/); }, /first argument must be an object/);
assert.throws(function() { assert.throws(function() {
async_wrap.enable(); async_wrap.enable();
}, /init callback is not assigned to a function/); }, /init callback is not assigned to a function/);
// Should not throw // Should not throw
async_wrap.setupHooks(() => {}); async_wrap.setupHooks({ init: () => {} });
async_wrap.enable(); async_wrap.enable();
assert.throws(function() { assert.throws(function() {

2
test/parallel/test-async-wrap-uid.js

@ -6,7 +6,7 @@ const assert = require('assert');
const async_wrap = process.binding('async_wrap'); const async_wrap = process.binding('async_wrap');
const storage = new Map(); const storage = new Map();
async_wrap.setupHooks(init, pre, post, destroy); async_wrap.setupHooks({ init, pre, post, destroy });
async_wrap.enable(); async_wrap.enable();
function init(uid) { function init(uid) {

Loading…
Cancel
Save