Skip to content
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

test: pass null params to napi_create_function() #26998

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions test/js-native-api/test_function/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,10 @@ let tracked_function = test_function.MakeTrackedFunction(common.mustCall());
assert(!!tracked_function);
tracked_function = null;
global.gc();

assert.deepStrictEqual(test_function.TestCreateFunctionParameters(), {
envIsNull: 'pass',
nameIsNull: 'pass',
cbIsNull: 'pass',
resultIsNull: 'pass'
});
92 changes: 92 additions & 0 deletions test/js-native-api/test_function/test_function.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,85 @@
#include <js_native_api.h>
#include "../common.h"

static napi_value TestCreateFunctionParameters(napi_env env,
napi_callback_info info) {
napi_status ret[4];
napi_value result, return_value = NULL, prop_value;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: we can forego the initalizer here.



ret[0] = napi_create_function(NULL,
"TrackedFunction",
NAPI_AUTO_LENGTH,
TestCreateFunctionParameters,
NULL,
&result);

ret[1] = napi_create_function(env,
NULL,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: this and the lines below need a few more spaces of indentation.

NAPI_AUTO_LENGTH,
TestCreateFunctionParameters,
NULL,
&result);

ret[2] = napi_create_function(env,
"TrackedFunction",
NAPI_AUTO_LENGTH,
NULL,
NULL,
&result);

ret[3] = napi_create_function(env,
"TrackedFunction",
NAPI_AUTO_LENGTH,
TestCreateFunctionParameters,
NULL,
NULL);

NAPI_CALL(env, napi_create_object(env, &return_value));

NAPI_CALL(env, napi_create_string_utf8(env,
(ret[0] == napi_invalid_arg ?
"pass" : "fail"),
NAPI_AUTO_LENGTH,
&prop_value));
NAPI_CALL(env, napi_set_named_property(env,
return_value,
"envIsNull",
prop_value));

NAPI_CALL(env, napi_create_string_utf8(env,
(ret[1] == napi_ok ?
"pass" : "fail"),
NAPI_AUTO_LENGTH,
&prop_value));
NAPI_CALL(env, napi_set_named_property(env,
return_value,
"nameIsNull",
prop_value));

NAPI_CALL(env, napi_create_string_utf8(env,
(ret[2] == napi_invalid_arg ?
"pass" : "fail"),
NAPI_AUTO_LENGTH,
&prop_value));
NAPI_CALL(env, napi_set_named_property(env,
return_value,
"cbIsNull",
prop_value));

NAPI_CALL(env, napi_create_string_utf8(env,
(ret[3] == napi_invalid_arg ?
"pass" : "fail"),
NAPI_AUTO_LENGTH,
&prop_value));
NAPI_CALL(env, napi_set_named_property(env,
return_value,
"resultIsNull",
prop_value));

return return_value;
}

static napi_value TestCallFunction(napi_env env, napi_callback_info info) {
size_t argc = 10;
napi_value args[10];
Expand Down Expand Up @@ -124,6 +203,14 @@ napi_value Init(napi_env env, napi_value exports) {
NULL,
&fn4));

napi_value fn5;
NAPI_CALL(env, napi_create_function(env,
"TestCreateFunctionParameters",
NAPI_AUTO_LENGTH,
TestCreateFunctionParameters,
NULL,
&fn5));

NAPI_CALL(env, napi_set_named_property(env, exports, "TestCall", fn1));
NAPI_CALL(env, napi_set_named_property(env, exports, "TestName", fn2));
NAPI_CALL(env, napi_set_named_property(env, exports, "TestNameShort", fn3));
Expand All @@ -132,6 +219,11 @@ napi_value Init(napi_env env, napi_value exports) {
"MakeTrackedFunction",
fn4));

NAPI_CALL(env, napi_set_named_property(env,
exports,
"TestCreateFunctionParameters",
fn5));

return exports;
}
EXTERN_C_END