Skip to content
Permalink
Browse files

n-api: refactor a previous commit

This is a refactoring of #27628
following #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: #28848
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com>
  • Loading branch information...
octaviansoldea authored and gabrielschulhof committed Jul 18, 2019
1 parent 70bb570 commit db1c4a7592a896053a8f791d7cc38a6de5c4a059
@@ -0,0 +1,51 @@
#include <js_native_api.h>
#include "common.h"

#include <stdio.h>

void add_returned_status(napi_env env,
const char* key,
napi_value object,
char* expected_message,
napi_status expected_status,
napi_status actual_status) {

char napi_message_string[100] = "";
napi_value prop_value;

if (actual_status != expected_status) {
snprintf(napi_message_string, sizeof(napi_message_string), "Invalid status [%d]", actual_status);
}

NAPI_CALL_RETURN_VOID(env,
napi_create_string_utf8(
env,
(actual_status == expected_status ?
expected_message :
napi_message_string),
NAPI_AUTO_LENGTH,
&prop_value));
NAPI_CALL_RETURN_VOID(env,
napi_set_named_property(env,
object,
key,
prop_value));
}

void add_last_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));
}
@@ -1,3 +1,5 @@
#include <js_native_api.h>

// Empty value so that macros here are able to return NULL or void
#define NAPI_RETVAL_NOTHING // Intentionally blank #define

@@ -58,3 +60,12 @@

#define DECLARE_NAPI_GETTER(name, func) \
{ (name), NULL, NULL, (func), NULL, NULL, napi_default, NULL }

void add_returned_status(napi_env env,
const char* key,
napi_value object,
char* expected_message,
napi_status expected_status,
napi_status actual_status);

void add_last_status(napi_env env, const char* key, napi_value return_value);
@@ -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;
}
@@ -3,6 +3,7 @@
{
"target_name": "test_object",
"sources": [
"../common.c",
"../entry_point.c",
"test_object.c"
]
@@ -229,26 +229,26 @@ assert.strictEqual(newObject.test_string, 'test string');
// Verify that passing NULL to napi_set_property() results in the correct
// error.
assert.deepStrictEqual(test_object.TestSetProperty(), {
envIsNull: 'pass',
objectIsNull: 'pass',
keyIsNull: 'pass',
valueIsNull: 'pass'
envIsNull: 'Invalid argument',
objectIsNull: 'Invalid argument',
keyIsNull: 'Invalid argument',
valueIsNull: 'Invalid argument'
});

// Verify that passing NULL to napi_has_property() results in the correct
// error.
assert.deepStrictEqual(test_object.TestHasProperty(), {
envIsNull: 'pass',
objectIsNull: 'pass',
keyIsNull: 'pass',
resultIsNull: 'pass'
envIsNull: 'Invalid argument',
objectIsNull: 'Invalid argument',
keyIsNull: 'Invalid argument',
resultIsNull: 'Invalid argument'
});

// Verify that passing NULL to napi_get_property() results in the correct
// error.
assert.deepStrictEqual(test_object.TestGetProperty(), {
envIsNull: 'pass',
objectIsNull: 'pass',
keyIsNull: 'pass',
resultIsNull: 'pass'
envIsNull: 'Invalid argument',
objectIsNull: 'Invalid argument',
keyIsNull: 'Invalid argument',
resultIsNull: 'Invalid argument'
});

0 comments on commit db1c4a7

Please sign in to comment.
You can’t perform that action at this time.