forked from nodejs/node-addon-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallbackscope.js
More file actions
48 lines (43 loc) · 1.15 KB
/
callbackscope.js
File metadata and controls
48 lines (43 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'use strict';
const buildType = process.config.target_defaults.default_configuration;
const assert = require('assert');
const common = require('./common');
// we only check async hooks on 8.x an higher were
// they are closer to working properly
const nodeVersion = process.versions.node.split('.')[0]
let async_hooks = undefined;
function checkAsyncHooks() {
if (nodeVersion >= 8) {
if (async_hooks == undefined) {
async_hooks = require('async_hooks');
}
return true;
}
return false;
}
test(require(`./build/${buildType}/binding.node`));
test(require(`./build/${buildType}/binding_noexcept.node`));
function test(binding) {
if (!checkAsyncHooks())
return;
let id;
let insideHook = false;
async_hooks.createHook({
init(asyncId, type, triggerAsyncId, resource) {
if (id === undefined && type === 'callback_scope_test') {
id = asyncId;
}
},
before(asyncId) {
if (asyncId === id)
insideHook = true;
},
after(asyncId) {
if (asyncId === id)
insideHook = false;
}
}).enable();
binding.callbackscope.runInCallbackScope(function() {
assert(insideHook);
});
}