mirror of https://github.com/lukechilds/node.git
Browse Source
- 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>canary-base
Anna Henningsen
7 years ago
8 changed files with 256 additions and 117 deletions
@ -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) |
@ -0,0 +1,9 @@ |
|||
{ |
|||
'targets': [ |
|||
{ |
|||
'target_name': 'binding', |
|||
'defines': [ 'V8_DEPRECATION_WARNINGS=1' ], |
|||
'sources': [ 'binding.cc' ] |
|||
} |
|||
] |
|||
} |
@ -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); |
|||
}); |
@ -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'); |
|||
}); |
|||
} |
Loading…
Reference in new issue