mirror of
https://github.com/nodejs/node.git
synced 2026-11-29 14:27:57 -05:00
n-api: refactor a previous commit
This is a refactoring of https://github.com/nodejs/node/issues/27628 following https://github.com/nodejs/node/pull/28505. This change factors out functions `add_last_status()` and `add_returned_status()` so they may be reused in the tests for passing information about the last error status and/or a returned `napi_status` to JavaScript. PR-URL: https://github.com/nodejs/node/pull/28848 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com>
This commit is contained in:
1 parent
ffc7a00c10
commit
e6b3bfe111
7 files changed
+133
-180
No files matched your search
@@ -3,6 +3,7 @@
|
||||
{
|
||||
"target_name": "test_constructor",
|
||||
"sources": [
|
||||
"../common.c",
|
||||
"../entry_point.c",
|
||||
"test_constructor.c"
|
||||
]
|
||||
|
||||
@@ -1,35 +1,13 @@
|
||||
#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 status;
|
||||
napi_value result, return_value, prop_value;
|
||||
char p_napi_message[100] = "";
|
||||
napi_value result, return_value;
|
||||
|
||||
napi_property_descriptor property_descriptor = {
|
||||
"TestDefineClass",
|
||||
@@ -52,20 +30,12 @@ static napi_value TestDefineClass(napi_env env,
|
||||
&property_descriptor,
|
||||
&result);
|
||||
|
||||
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,
|
||||
p_napi_message,
|
||||
NAPI_AUTO_LENGTH,
|
||||
&prop_value));
|
||||
NAPI_CALL(env, napi_set_named_property(env,
|
||||
return_value,
|
||||
"envIsNull",
|
||||
prop_value));
|
||||
add_returned_status(env,
|
||||
"envIsNull",
|
||||
return_value,
|
||||
"Invalid argument",
|
||||
napi_invalid_arg,
|
||||
status);
|
||||
|
||||
napi_define_class(env,
|
||||
NULL,
|
||||
@@ -76,7 +46,7 @@ static napi_value TestDefineClass(napi_env env,
|
||||
&property_descriptor,
|
||||
&result);
|
||||
|
||||
add_named_status(env, "nameIsNull", return_value);
|
||||
add_last_status(env, "nameIsNull", return_value);
|
||||
|
||||
napi_define_class(env,
|
||||
"TrackedFunction",
|
||||
@@ -87,7 +57,7 @@ static napi_value TestDefineClass(napi_env env,
|
||||
&property_descriptor,
|
||||
&result);
|
||||
|
||||
add_named_status(env, "cbIsNull", return_value);
|
||||
add_last_status(env, "cbIsNull", return_value);
|
||||
|
||||
napi_define_class(env,
|
||||
"TrackedFunction",
|
||||
@@ -98,7 +68,7 @@ static napi_value TestDefineClass(napi_env env,
|
||||
&property_descriptor,
|
||||
&result);
|
||||
|
||||
add_named_status(env, "cbDataIsNull", return_value);
|
||||
add_last_status(env, "cbDataIsNull", return_value);
|
||||
|
||||
napi_define_class(env,
|
||||
"TrackedFunction",
|
||||
@@ -109,7 +79,7 @@ static napi_value TestDefineClass(napi_env env,
|
||||
NULL,
|
||||
&result);
|
||||
|
||||
add_named_status(env, "propertiesIsNull", return_value);
|
||||
add_last_status(env, "propertiesIsNull", return_value);
|
||||
|
||||
|
||||
napi_define_class(env,
|
||||
@@ -121,7 +91,7 @@ static napi_value TestDefineClass(napi_env env,
|
||||
&property_descriptor,
|
||||
NULL);
|
||||
|
||||
add_named_status(env, "resultIsNull", return_value);
|
||||
add_last_status(env, "resultIsNull", return_value);
|
||||
|
||||
return return_value;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user