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: propagate napi_status to JS #28505

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
12 changes: 6 additions & 6 deletions test/js-native-api/test_constructor/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ assert.strictEqual(test_object.staticReadonlyAccessor1, undefined);
// Verify that passing NULL to napi_define_class() results in the correct
// error.
assert.deepStrictEqual(TestConstructor.TestDefineClass(), {
envIsNull: 'pass',
nameIsNull: 'pass',
cbIsNull: 'pass',
cbDataIsNull: 'pass',
propertiesIsNull: 'pass',
resultIsNull: 'pass'
envIsNull: 'Invalid argument',
nameIsNull: 'Invalid argument',
cbIsNull: 'Invalid argument',
cbDataIsNull: 'napi_ok',
propertiesIsNull: 'Invalid argument',
resultIsNull: 'Invalid argument'
});
188 changes: 88 additions & 100 deletions test/js-native-api/test_constructor/test_constructor.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
#include <js_native_api.h>
#include "../common.h"

#include <stdio.h>

static double value_ = 1;
static double static_value_ = 10;

static void
add_named_status(napi_env env, const char* key, napi_value return_value) {
napi_value prop_value;
const napi_extended_error_info* p_last_error;
NAPI_CALL_RETURN_VOID(env, napi_get_last_error_info(env, &p_last_error));

NAPI_CALL_RETURN_VOID(env,
napi_create_string_utf8(env,
(p_last_error->error_message == NULL ?
"napi_ok" :
p_last_error->error_message),
NAPI_AUTO_LENGTH,
&prop_value));
NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env,
return_value,
key,
prop_value));
}

static napi_value TestDefineClass(napi_env env,
napi_callback_info info) {
napi_status ret[7];
napi_status status;
napi_value result, return_value, prop_value;
char p_napi_message[100] = "";

napi_property_descriptor property_descriptor = {
"TestDefineClass",
Expand All @@ -19,34 +41,9 @@ static napi_value TestDefineClass(napi_env env,
napi_enumerable | napi_static,
NULL};

ret[0] = napi_define_class(NULL,
"TrackedFunction",
NAPI_AUTO_LENGTH,
TestDefineClass,
NULL,
1,
&property_descriptor,
&result);

ret[1] = napi_define_class(env,
NULL,
NAPI_AUTO_LENGTH,
TestDefineClass,
NULL,
1,
&property_descriptor,
&result);

ret[2] = napi_define_class(env,
"TrackedFunction",
NAPI_AUTO_LENGTH,
NULL,
NULL,
1,
&property_descriptor,
&result);
NAPI_CALL(env, napi_create_object(env, &return_value));

ret[3] = napi_define_class(env,
status = napi_define_class(NULL,
"TrackedFunction",
NAPI_AUTO_LENGTH,
TestDefineClass,
Expand All @@ -55,85 +52,76 @@ static napi_value TestDefineClass(napi_env env,
&property_descriptor,
&result);

ret[4] = napi_define_class(env,
"TrackedFunction",
NAPI_AUTO_LENGTH,
TestDefineClass,
NULL,
1,
NULL,
&result);

ret[5] = napi_define_class(env,
"TrackedFunction",
NAPI_AUTO_LENGTH,
TestDefineClass,
NULL,
1,
&property_descriptor,
NULL);

NAPI_CALL(env, napi_create_object(env, &return_value));
if (status == napi_invalid_arg) {
snprintf(p_napi_message, 99, "Invalid argument");
} else {
snprintf(p_napi_message, 99, "Invalid status [%d]", status);
}

NAPI_CALL(env, napi_create_string_utf8(env,
(ret[0] == napi_invalid_arg ?
"pass" : "fail"),
p_napi_message,
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_invalid_arg ?
"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_ok ?
"pass" : "fail"),
NAPI_AUTO_LENGTH,
&prop_value));
NAPI_CALL(env, napi_set_named_property(env,
return_value,
"cbDataIsNull",
prop_value));

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

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

napi_define_class(env,
NULL,
NAPI_AUTO_LENGTH,
TestDefineClass,
NULL,
1,
&property_descriptor,
&result);

add_named_status(env, "nameIsNull", return_value);

napi_define_class(env,
"TrackedFunction",
NAPI_AUTO_LENGTH,
NULL,
NULL,
1,
&property_descriptor,
&result);

add_named_status(env, "cbIsNull", return_value);

napi_define_class(env,
"TrackedFunction",
NAPI_AUTO_LENGTH,
TestDefineClass,
NULL,
1,
&property_descriptor,
&result);

add_named_status(env, "cbDataIsNull", return_value);

napi_define_class(env,
"TrackedFunction",
NAPI_AUTO_LENGTH,
TestDefineClass,
NULL,
1,
NULL,
&result);

add_named_status(env, "propertiesIsNull", return_value);


napi_define_class(env,
"TrackedFunction",
NAPI_AUTO_LENGTH,
TestDefineClass,
NULL,
1,
&property_descriptor,
NULL);

add_named_status(env, "resultIsNull", return_value);

return return_value;
}
Expand Down