Skip to content

Commit 7723333

Browse files
author
Octavian Soldea
committed
n-api: refactoring a previous commit
This is a refactoring of PR-URL: nodejs#27628 following PR-URL: nodejs#28505
1 parent 1721034 commit 7723333

7 files changed

Lines changed: 129 additions & 201 deletions

File tree

test/js-native-api/common.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <js_native_api.h>
2+
#include "common.h"
3+
4+
#include <stdio.h>
5+
6+
void add_returned_status(napi_env env,
7+
const char* key,
8+
napi_value object,
9+
napi_status expected_status,
10+
napi_status actual_status) {
11+
12+
char p_napi_message[100] = "";
13+
napi_value prop_value;
14+
15+
if (actual_status == expected_status) {
16+
snprintf(p_napi_message, 99, "Invalid argument");
17+
} else {
18+
snprintf(p_napi_message, 99, "Invalid status [%d]", actual_status);
19+
}
20+
21+
NAPI_CALL_RETURN_VOID(env,
22+
napi_create_string_utf8(env,
23+
p_napi_message,
24+
NAPI_AUTO_LENGTH,
25+
&prop_value));
26+
NAPI_CALL_RETURN_VOID(env,
27+
napi_set_named_property(env,
28+
object,
29+
key,
30+
prop_value));
31+
}
32+
33+
void add_last_status(napi_env env, const char* key, napi_value return_value) {
34+
napi_value prop_value;
35+
const napi_extended_error_info* p_last_error;
36+
NAPI_CALL_RETURN_VOID(env, napi_get_last_error_info(env, &p_last_error));
37+
38+
NAPI_CALL_RETURN_VOID(env,
39+
napi_create_string_utf8(env,
40+
(p_last_error->error_message == NULL ?
41+
"napi_ok" :
42+
p_last_error->error_message),
43+
NAPI_AUTO_LENGTH,
44+
&prop_value));
45+
NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env,
46+
return_value,
47+
key,
48+
prop_value));
49+
}

test/js-native-api/common.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,11 @@
5858

5959
#define DECLARE_NAPI_GETTER(name, func) \
6060
{ (name), NULL, NULL, (func), NULL, NULL, napi_default, NULL }
61+
62+
void add_returned_status(napi_env env,
63+
const char* key,
64+
napi_value object,
65+
napi_status expected_status,
66+
napi_status actual_status);
67+
68+
void add_last_status(napi_env env, const char* key, napi_value return_value);

test/js-native-api/test_constructor/binding.gyp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
{
44
"target_name": "test_constructor",
55
"sources": [
6+
"../common.c",
67
"../entry_point.c",
78
"test_constructor.c"
89
]

test/js-native-api/test_constructor/test_constructor.c

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,13 @@
11
#include <js_native_api.h>
22
#include "../common.h"
33

4-
#include <stdio.h>
5-
64
static double value_ = 1;
75
static double static_value_ = 10;
86

9-
static void
10-
add_named_status(napi_env env, const char* key, napi_value return_value) {
11-
napi_value prop_value;
12-
const napi_extended_error_info* p_last_error;
13-
NAPI_CALL_RETURN_VOID(env, napi_get_last_error_info(env, &p_last_error));
14-
15-
NAPI_CALL_RETURN_VOID(env,
16-
napi_create_string_utf8(env,
17-
(p_last_error->error_message == NULL ?
18-
"napi_ok" :
19-
p_last_error->error_message),
20-
NAPI_AUTO_LENGTH,
21-
&prop_value));
22-
NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env,
23-
return_value,
24-
key,
25-
prop_value));
26-
}
27-
287
static napi_value TestDefineClass(napi_env env,
298
napi_callback_info info) {
309
napi_status status;
31-
napi_value result, return_value, prop_value;
32-
char p_napi_message[100] = "";
10+
napi_value result, return_value;
3311

3412
napi_property_descriptor property_descriptor = {
3513
"TestDefineClass",
@@ -52,20 +30,7 @@ static napi_value TestDefineClass(napi_env env,
5230
&property_descriptor,
5331
&result);
5432

55-
if (status == napi_invalid_arg) {
56-
snprintf(p_napi_message, 99, "Invalid argument");
57-
} else {
58-
snprintf(p_napi_message, 99, "Invalid status [%d]", status);
59-
}
60-
61-
NAPI_CALL(env, napi_create_string_utf8(env,
62-
p_napi_message,
63-
NAPI_AUTO_LENGTH,
64-
&prop_value));
65-
NAPI_CALL(env, napi_set_named_property(env,
66-
return_value,
67-
"envIsNull",
68-
prop_value));
33+
add_returned_status(env, "envIsNull", return_value, napi_invalid_arg, status);
6934

7035
napi_define_class(env,
7136
NULL,
@@ -76,7 +41,7 @@ static napi_value TestDefineClass(napi_env env,
7641
&property_descriptor,
7742
&result);
7843

79-
add_named_status(env, "nameIsNull", return_value);
44+
add_last_status(env, "nameIsNull", return_value);
8045

8146
napi_define_class(env,
8247
"TrackedFunction",
@@ -87,7 +52,7 @@ static napi_value TestDefineClass(napi_env env,
8752
&property_descriptor,
8853
&result);
8954

90-
add_named_status(env, "cbIsNull", return_value);
55+
add_last_status(env, "cbIsNull", return_value);
9156

9257
napi_define_class(env,
9358
"TrackedFunction",
@@ -98,7 +63,7 @@ static napi_value TestDefineClass(napi_env env,
9863
&property_descriptor,
9964
&result);
10065

101-
add_named_status(env, "cbDataIsNull", return_value);
66+
add_last_status(env, "cbDataIsNull", return_value);
10267

10368
napi_define_class(env,
10469
"TrackedFunction",
@@ -109,7 +74,7 @@ static napi_value TestDefineClass(napi_env env,
10974
NULL,
11075
&result);
11176

112-
add_named_status(env, "propertiesIsNull", return_value);
77+
add_last_status(env, "propertiesIsNull", return_value);
11378

11479

11580
napi_define_class(env,
@@ -121,7 +86,7 @@ static napi_value TestDefineClass(napi_env env,
12186
&property_descriptor,
12287
NULL);
12388

124-
add_named_status(env, "resultIsNull", return_value);
89+
add_last_status(env, "resultIsNull", return_value);
12590

12691
return return_value;
12792
}

test/js-native-api/test_object/binding.gyp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
{
44
"target_name": "test_object",
55
"sources": [
6+
"../common.c",
67
"../entry_point.c",
78
"test_object.c"
89
]

test/js-native-api/test_object/test.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -229,26 +229,26 @@ assert.strictEqual(newObject.test_string, 'test string');
229229
// Verify that passing NULL to napi_set_property() results in the correct
230230
// error.
231231
assert.deepStrictEqual(test_object.TestSetProperty(), {
232-
envIsNull: 'pass',
233-
objectIsNull: 'pass',
234-
keyIsNull: 'pass',
235-
valueIsNull: 'pass'
232+
envIsNull: 'Invalid argument',
233+
objectIsNull: 'Invalid argument',
234+
keyIsNull: 'Invalid argument',
235+
valueIsNull: 'Invalid argument'
236236
});
237237

238238
// Verify that passing NULL to napi_has_property() results in the correct
239239
// error.
240240
assert.deepStrictEqual(test_object.TestHasProperty(), {
241-
envIsNull: 'pass',
242-
objectIsNull: 'pass',
243-
keyIsNull: 'pass',
244-
resultIsNull: 'pass'
241+
envIsNull: 'Invalid argument',
242+
objectIsNull: 'Invalid argument',
243+
keyIsNull: 'Invalid argument',
244+
resultIsNull: 'Invalid argument'
245245
});
246246

247247
// Verify that passing NULL to napi_get_property() results in the correct
248248
// error.
249249
assert.deepStrictEqual(test_object.TestGetProperty(), {
250-
envIsNull: 'pass',
251-
objectIsNull: 'pass',
252-
keyIsNull: 'pass',
253-
resultIsNull: 'pass'
250+
envIsNull: 'Invalid argument',
251+
objectIsNull: 'Invalid argument',
252+
keyIsNull: 'Invalid argument',
253+
resultIsNull: 'Invalid argument'
254254
});

0 commit comments

Comments
 (0)