Skip to content

Commit 45f84c7

Browse files
authored
Merge pull request nodejs#142 from boingoing/remove_set_function_name
napi: Remove napi_set_function_name since not all engines can rename functions
2 parents 960ed1d + 5011ee1 commit 45f84c7

File tree

4 files changed

+22
-35
lines changed

4 files changed

+22
-35
lines changed

src/node_jsvmapi.cc

+15-18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*******************************************************************************
1+
/*******************************************************************************
22
* Experimental prototype for demonstrating VM agnostic and ABI stable API
33
* for native modules to use instead of using Nan and V8 APIs directly.
44
*
@@ -99,11 +99,11 @@ namespace v8impl {
9999
return u.l;
100100
}
101101

102-
v8::Local<v8::Value> V8LocalValueFromJsPropertyName(napi_propertyname pn) {
102+
v8::Local<v8::String> V8LocalStringFromJsPropertyName(napi_propertyname pn) {
103103
// Likewise awkward
104104
union U {
105105
napi_propertyname pn;
106-
v8::Local<v8::Value> l;
106+
v8::Local<v8::String> l;
107107
U(napi_propertyname _pn) : pn(_pn) { }
108108
} u(pn);
109109
assert(sizeof(u.pn) == sizeof(u.l));
@@ -637,6 +637,7 @@ napi_status napi_set_last_error(napi_status error_code,
637637

638638
napi_status napi_create_function(
639639
napi_env e,
640+
const char* utf8name,
640641
napi_callback cb,
641642
void* data,
642643
napi_value* result) {
@@ -645,7 +646,7 @@ napi_status napi_create_function(
645646

646647
v8::Isolate *isolate = v8impl::V8IsolateFromJsEnv(e);
647648
v8::Local<v8::Context> context = isolate->GetCurrentContext();
648-
v8::Local<v8::Object> retval;
649+
v8::Local<v8::Function> retval;
649650

650651
v8::EscapableHandleScope scope(isolate);
651652

@@ -658,6 +659,13 @@ napi_status napi_create_function(
658659
isolate, v8impl::FunctionCallbackWrapper::Invoke, cbdata);
659660

660661
retval = scope.Escape(tpl->GetFunction());
662+
663+
if (utf8name) {
664+
v8::Local<v8::String> namestring;
665+
CHECK_NEW_FROM_UTF8(isolate, namestring, utf8name);
666+
retval->SetName(namestring);
667+
}
668+
661669
*result = v8impl::JsValueFromV8LocalValue(retval);
662670

663671
return GET_RETURN_STATUS();
@@ -766,17 +774,6 @@ napi_status napi_define_class(
766774
return GET_RETURN_STATUS();
767775
}
768776

769-
napi_status napi_set_function_name(napi_env e, napi_value func,
770-
napi_propertyname name) {
771-
NAPI_PREAMBLE(e);
772-
773-
v8::Local<v8::Function> v8func = v8impl::V8LocalFunctionFromJsValue(func);
774-
v8func->SetName(
775-
v8impl::V8LocalValueFromJsPropertyName(name).As<v8::String>());
776-
777-
return GET_RETURN_STATUS();
778-
}
779-
780777
napi_status napi_set_return_value(napi_env e,
781778
napi_callback_info cbinfo, napi_value v) {
782779
NAPI_PREAMBLE(e);
@@ -833,7 +830,7 @@ napi_status napi_set_property(napi_env e,
833830

834831
CHECK_TO_OBJECT(context, obj, o);
835832

836-
v8::Local<v8::Value> key = v8impl::V8LocalValueFromJsPropertyName(k);
833+
v8::Local<v8::String> key = v8impl::V8LocalStringFromJsPropertyName(k);
837834
v8::Local<v8::Value> val = v8impl::V8LocalValueFromJsValue(v);
838835

839836
v8::Maybe<bool> set_maybe = obj->Set(context, key, val);
@@ -852,7 +849,7 @@ napi_status napi_has_property(napi_env e, napi_value o, napi_propertyname k, boo
852849

853850
CHECK_TO_OBJECT(context, obj, o);
854851

855-
v8::Local<v8::Value> key = v8impl::V8LocalValueFromJsPropertyName(k);
852+
v8::Local<v8::String> key = v8impl::V8LocalStringFromJsPropertyName(k);
856853
v8::Maybe<bool> has_maybe = obj->Has(context, key);
857854

858855
CHECK_MAYBE_NOTHING(has_maybe, napi_generic_failure);
@@ -870,7 +867,7 @@ napi_status napi_get_property(napi_env e,
870867

871868
v8::Isolate *isolate = v8impl::V8IsolateFromJsEnv(e);
872869
v8::Local<v8::Context> context = isolate->GetCurrentContext();
873-
v8::Local<v8::Value> key = v8impl::V8LocalValueFromJsPropertyName(k);
870+
v8::Local<v8::String> key = v8impl::V8LocalStringFromJsPropertyName(k);
874871
v8::Local<v8::Object> obj;
875872

876873
CHECK_TO_OBJECT(context, obj, o);

src/node_jsvmapi.h

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*******************************************************************************
1+
/*******************************************************************************
22
* Experimental prototype for demonstrating VM agnostic and ABI stable API
33
* for native modules to use instead of using Nan and V8 APIs directly.
44
*
@@ -105,6 +105,7 @@ NODE_EXTERN napi_status napi_create_symbol(napi_env e,
105105
const char* s,
106106
napi_value* result);
107107
NODE_EXTERN napi_status napi_create_function(napi_env e,
108+
const char* utf8name,
108109
napi_callback cb,
109110
void* data,
110111
napi_value* result);
@@ -236,9 +237,6 @@ NODE_EXTERN napi_status napi_strict_equals(napi_env e,
236237
bool* result);
237238

238239
// Methods to work with Functions
239-
NODE_EXTERN napi_status napi_set_function_name(napi_env e,
240-
napi_value func,
241-
napi_propertyname napi_value);
242240
NODE_EXTERN napi_status napi_call_function(napi_env e,
243241
napi_value recv,
244242
napi_value func,

test/addons-abi/5_function_factory/binding.cc

+3-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include <node_jsvmapi.h>
1+
#include <node_jsvmapi.h>
22

33
void MyFunction(napi_env env, napi_callback_info info) {
44
napi_status status;
@@ -15,15 +15,7 @@ void CreateFunction(napi_env env, napi_callback_info info) {
1515
napi_status status;
1616

1717
napi_value fn;
18-
status = napi_create_function(env, MyFunction, nullptr, &fn);
19-
if (status != napi_ok) return;
20-
21-
napi_propertyname name;
22-
status = napi_property_name(env, "theFunction", &name);
23-
if (status != napi_ok) return;
24-
25-
// omit this to make it anonymous
26-
status = napi_set_function_name(env, fn, name);
18+
status = napi_create_function(env, "theFunction", MyFunction, nullptr, &fn);
2719
if (status != napi_ok) return;
2820

2921
status = napi_set_return_value(env, info, fn);
@@ -32,7 +24,7 @@ void CreateFunction(napi_env env, napi_callback_info info) {
3224

3325
void Init(napi_env env, napi_value exports, napi_value module) {
3426
napi_status status;
35-
napi_property_descriptor desc = { "exports", CreateFunction };
27+
napi_property_descriptor desc = { "exports", CreateFunction, nullptr, nullptr, nullptr, napi_default, nullptr };
3628
status = napi_define_properties(env, module, 1, &desc);
3729
if (status != napi_ok) return;
3830
}

test/addons-abi/test_function/test_function.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include <node_jsvmapi.h>
1+
#include <node_jsvmapi.h>
22

33
void Test(napi_env env, napi_callback_info info) {
44
napi_status status;
@@ -49,7 +49,7 @@ void Init(napi_env env, napi_value exports, napi_value module) {
4949
if (status != napi_ok) return;
5050

5151
napi_value fn;
52-
status = napi_create_function(env, Test, nullptr, &fn);
52+
status = napi_create_function(env, Test, nullptr, nullptr, &fn);
5353
if (status != napi_ok) return;
5454

5555
status = napi_set_property(env, exports, name, fn);

0 commit comments

Comments
 (0)