GeezerVison is showing github.com — src: add MakeCallback() taking an Environment by nigrosimone · Pull Request #66510 · nodejs/node
src: add MakeCallback() taking an Environment
#66510
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and
privacy statement . We’ll occasionally send you account related emails.
Already on GitHub?
Sign in
to your account
Uh oh!
There was an error while loading. Please reload this page.
Show file tree
Hide file tree
Changes from all commits
Commits
File filter
Filter by extension
.cc
(2)
.gyp
(1)
.h
(1)
.js
(1)
dotfile
(1)
All 5 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Hide whitespace
Diff view
Hide whitespace
src: add MakeCallback() taking an Environment
node::MakeCallback() looks up the Environment from the creation context
of the callback on every call. An addon that calls into the same
Environment many times, a server calling one handler per request for
example, can now take it once with GetCurrentEnvironment() and pass it.
The callback must belong to that Environment.
Add a benchmark, napi/make_callback_env, comparing the two overloads.
Refs: nodejs/performance#24
Signed-off-by: Nigro Simone <[email protected] >
Loading branch information
Uh oh!
There was an error while loading. Please reload this page.
commit 3278f895b6ce9be2670c7e19d6892d6daee9fea5
There are no files selected for viewing
Original file line number
Diff line number
Diff line change
@@ -0,0 +1 @@
build /
Original file line number
Diff line number
Diff line change
@@ -0,0 +1,71 @@
#include < node.h>
#include < uv.h>
#include < v8.h>
using v8::Context;
using v8::Function;
using v8::FunctionCallbackInfo;
using v8::Global;
using v8::HandleScope;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::Value;
// Same order as the types in index.js.
enum Type { kMakeCallback , kMakeCallbackEnv };
struct State {
uv_timer_t timer;
Isolate* isolate;
node::Environment* env;
int64_t n;
Type type;
Global<Function> fn;
Global<Function> done;
};
static void OnTimer (uv_timer_t * handle) {
State* state = static_cast <State*>(handle->data );
Isolate* isolate = state->isolate ;
HandleScope handle_scope (isolate);
Local<Function> fn = state->fn .Get (isolate);
Local<Context> context = fn->GetCreationContextChecked (isolate);
Context::Scope context_scope (context);
Local<Object> recv = context->Global ();
for (int64_t i = 0 ; i < state->n ; i++) {
HandleScope inner_scope (isolate);
if (state->type == kMakeCallbackEnv ) {
(void )node::MakeCallback (state->env , recv, fn, 0 , nullptr , {0 , 0 });
} else {
(void )node::MakeCallback (isolate, recv, fn, 0 , nullptr , {0 , 0 });
}
}
Local<Function> done = state->done .Get (isolate);
(void )node::MakeCallback (isolate, recv, done, 0 , nullptr , {0 , 0 });
uv_close (reinterpret_cast <uv_handle_t *>(&state->timer ),
[](uv_handle_t * h) { delete static_cast <State*>(h->data ); });
}
// run(n, fn, done, type): calls fn n times from a timer, then calls done.
static void Run (const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate ();
Local<Context> context = isolate->GetCurrentContext ();
State* state = new State;
state->isolate = isolate;
// Found once, the point of the MakeCallbackEnv type.
state->env = node::GetCurrentEnvironment (context);
state->n = args[0 ]->IntegerValue (context).FromJust ();
state->fn .Reset (isolate, args[1 ].As <Function>());
state->done .Reset (isolate, args[2 ].As <Function>());
state->type = static_cast <Type>(args[3 ]->Int32Value (context).FromJust ());
state->timer .data = state;
uv_timer_init (node::GetCurrentEventLoop (isolate), &state->timer );
uv_timer_start (&state->timer , OnTimer, 0 , 0 );
}
static void Initialize (Local<Object> target, Local<Value> module , void * data) {
NODE_SET_METHOD (target, " run" , Run);
}
NODE_MODULE (NODE_GYP_MODULE_NAME , Initialize)
Original file line number
Diff line number
Diff line change
@@ -0,0 +1,15 @@
{
'targets': [
{
'target_name': 'binding',
'sources': [ 'binding.cc' ],
'conditions': [
# node-gyp does not pass the include dirs of the Node.js build, and
# the addon headers have uv.h only when libuv is bundled.
[ 'node_shared_libuv=="true"', {
'include_dirs': [ '<@(node_shared_libuv_include_dirs)' ],
}],
],
}
]
}
Original file line number
Diff line number
Diff line change
@@ -0,0 +1,26 @@
'use strict' ;
const common = require ( '../../common.js' ) ;
// node::MakeCallback from a libuv timer, the Environment looked up from the
// callback on every call (MakeCallback) or passed in (MakeCallbackEnv).
let binding ;
try {
binding = require ( `./build/${ common . buildType } /binding` ) ;
} catch {
console . error ( 'napi/make_callback_env/index.js Binding failed to load' ) ;
process . exit ( 0 ) ;
}
const types = [ 'MakeCallback' , 'MakeCallbackEnv' ] ;
const bench = common . createBenchmark ( main , {
type : types ,
n : [ 1e6 , 1e7 ] ,
} ) ;
function main ( { type, n } ) {
bench . start ( ) ;
binding . run ( n , ( ) => { } , ( ) => bench . end ( n ) , types . indexOf ( type ) ) ;
}
Original file line number
Diff line number
Diff line change
Expand Up
@@ -393,6 +393,17 @@ MaybeLocal<Value> InternalMakeCallback(Isolate* isolate,
env, recv, callback, argc, argv, asyncContext, context_frame);
}
MaybeLocal<Value> MakeCallback(Environment* env,
Local<Object> recv,
Local<Function> callback,
int argc,
Local<Value> argv[],
async_context asyncContext) {
CHECK_NOT_NULL(env);
return MakeCallbackInEnvironment(
env, recv, callback, argc, argv, asyncContext, Undefined(env->isolate()));
}
MaybeLocal<Value> MakeCallbackInEnvironment(Environment* env,
Local<Object> recv,
const Local<Function> callback,
Expand Down
11 changes: 11 additions & 0 deletions
11
src/node.h
Original file line number
Diff line number
Diff line change
Expand Up
@@ -1674,6 +1674,17 @@ v8::MaybeLocal<v8::Value> MakeCallback(v8::Isolate* isolate,
int argc,
v8::Local<v8::Value>* argv,
async_context asyncContext);
/* The same, for an addon that calls into the same Environment many times:
* `env` is passed in, for example from GetCurrentEnvironment() once, instead
* of being looked up from the creation context of `callback` on every call.
* `callback` must belong to `env`. */
NODE_EXTERN
v8::MaybeLocal<v8::Value> MakeCallback(Environment* env,
v8::Local<v8::Object> recv,
v8::Local<v8::Function> callback,
int argc,
v8::Local<v8::Value>* argv,
async_context asyncContext);
NODE_EXTERN
v8::MaybeLocal<v8::Value> MakeCallback(v8::Isolate* isolate,
v8::Local<v8::Object> recv,
Expand Down
Loading
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.