diff options
author | Gabriel Schulhof <gabriel.schulhof@intel.com> | 2018-07-06 10:47:48 -0400 |
---|---|---|
committer | Gabriel Schulhof <gabriel.schulhof@intel.com> | 2018-07-09 11:44:37 -0400 |
commit | 978d89f5e3cf238a0e42303acc9493993ede9c98 (patch) | |
tree | aa891f503e98073be4b760622237eb44fa286edc /src | |
parent | a478259af7671fa2794aa030f7790a9d9772be5e (diff) | |
download | android-node-v8-978d89f5e3cf238a0e42303acc9493993ede9c98.tar.gz android-node-v8-978d89f5e3cf238a0e42303acc9493993ede9c98.tar.bz2 android-node-v8-978d89f5e3cf238a0e42303acc9493993ede9c98.zip |
n-api: create functions directly
Avoid using `v8::FunctionTemplate::New()` when using
`v8::Function::New()` suffices. This ensures that individual functions
can be gc-ed and that functions can be created dynamically without
running out of memory.
PR-URL: https://github.com/nodejs/node/pull/21688
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Kyle Farnung <kfarnung@microsoft.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Yang Guo <yangguo@chromium.org>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>
Diffstat (limited to 'src')
-rw-r--r-- | src/node_api.cc | 20 |
1 files changed, 12 insertions, 8 deletions
diff --git a/src/node_api.cc b/src/node_api.cc index 2b14617175..1d42435264 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -1025,11 +1025,11 @@ napi_status napi_create_function(napi_env env, RETURN_STATUS_IF_FALSE(env, !cbdata.IsEmpty(), napi_generic_failure); - v8::Local<v8::FunctionTemplate> tpl = v8::FunctionTemplate::New( - isolate, v8impl::FunctionCallbackWrapper::Invoke, cbdata); - v8::Local<v8::Context> context = isolate->GetCurrentContext(); - v8::MaybeLocal<v8::Function> maybe_function = tpl->GetFunction(context); + v8::MaybeLocal<v8::Function> maybe_function = + v8::Function::New(context, + v8impl::FunctionCallbackWrapper::Invoke, + cbdata); CHECK_MAYBE_EMPTY(env, maybe_function, napi_generic_failure); return_value = scope.Escape(maybe_function.ToLocalChecked()); @@ -1491,13 +1491,17 @@ napi_status napi_define_properties(napi_env env, v8::Local<v8::Value> cbdata = v8impl::CreateFunctionCallbackData(env, p->method, p->data); - RETURN_STATUS_IF_FALSE(env, !cbdata.IsEmpty(), napi_generic_failure); + CHECK_MAYBE_EMPTY(env, cbdata, napi_generic_failure); + + v8::MaybeLocal<v8::Function> maybe_fn = + v8::Function::New(context, + v8impl::FunctionCallbackWrapper::Invoke, + cbdata); - v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New( - isolate, v8impl::FunctionCallbackWrapper::Invoke, cbdata); + CHECK_MAYBE_EMPTY(env, maybe_fn, napi_generic_failure); auto define_maybe = obj->DefineOwnProperty( - context, property_name, t->GetFunction(), attributes); + context, property_name, maybe_fn.ToLocalChecked(), attributes); if (!define_maybe.FromMaybe(false)) { return napi_set_last_error(env, napi_generic_failure); |