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

n-api: refactoring a previous commit #28848

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
51 changes: 51 additions & 0 deletions test/js-native-api/common.c
Original file line number Diff line number Diff line change
@@ -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,
gabrielschulhof marked this conversation as resolved.
Show resolved Hide resolved
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));
}
11 changes: 11 additions & 0 deletions test/js-native-api/common.h
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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);
1 change: 1 addition & 0 deletions test/js-native-api/test_constructor/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{
"target_name": "test_constructor",
"sources": [
"../common.c",
"../entry_point.c",
"test_constructor.c"
]
Expand Down
54 changes: 12 additions & 42 deletions test/js-native-api/test_constructor/test_constructor.c
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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,
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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,
Expand All @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions test/js-native-api/test_object/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{
"target_name": "test_object",
"sources": [
"../common.c",
"../entry_point.c",
"test_object.c"
]
Expand Down
24 changes: 12 additions & 12 deletions test/js-native-api/test_object/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
});
Loading