Skip to content

Commit e6b3bfe

Browse files
Octavian Soldeatargos
Octavian Soldea
authored andcommitted
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 <[email protected]> Reviewed-By: Gabriel Schulhof <[email protected]>
1 parent ffc7a00 commit e6b3bfe

File tree

7 files changed

+154
-201
lines changed

7 files changed

+154
-201
lines changed

test/js-native-api/common.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
char* expected_message,
10+
napi_status expected_status,
11+
napi_status actual_status) {
12+
13+
char napi_message_string[100] = "";
14+
napi_value prop_value;
15+
16+
if (actual_status != expected_status) {
17+
snprintf(napi_message_string, sizeof(napi_message_string), "Invalid status [%d]", actual_status);
18+
}
19+
20+
NAPI_CALL_RETURN_VOID(env,
21+
napi_create_string_utf8(
22+
env,
23+
(actual_status == expected_status ?
24+
expected_message :
25+
napi_message_string),
26+
NAPI_AUTO_LENGTH,
27+
&prop_value));
28+
NAPI_CALL_RETURN_VOID(env,
29+
napi_set_named_property(env,
30+
object,
31+
key,
32+
prop_value));
33+
}
34+
35+
void add_last_status(napi_env env, const char* key, napi_value return_value) {
36+
napi_value prop_value;
37+
const napi_extended_error_info* p_last_error;
38+
NAPI_CALL_RETURN_VOID(env, napi_get_last_error_info(env, &p_last_error));
39+
40+
NAPI_CALL_RETURN_VOID(env,
41+
napi_create_string_utf8(env,
42+
(p_last_error->error_message == NULL ?
43+
"napi_ok" :
44+
p_last_error->error_message),
45+
NAPI_AUTO_LENGTH,
46+
&prop_value));
47+
NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env,
48+
return_value,
49+
key,
50+
prop_value));
51+
}

test/js-native-api/common.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include <js_native_api.h>
2+
13
// Empty value so that macros here are able to return NULL or void
24
#define NAPI_RETVAL_NOTHING // Intentionally blank #define
35

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

5961
#define DECLARE_NAPI_GETTER(name, func) \
6062
{ (name), NULL, NULL, (func), NULL, NULL, napi_default, NULL }
63+
64+
void add_returned_status(napi_env env,
65+
const char* key,
66+
napi_value object,
67+
char* expected_message,
68+
napi_status expected_status,
69+
napi_status actual_status);
70+
71+
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: 12 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,12 @@ 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,
34+
"envIsNull",
35+
return_value,
36+
"Invalid argument",
37+
napi_invalid_arg,
38+
status);
6939

7040
napi_define_class(env,
7141
NULL,
@@ -76,7 +46,7 @@ static napi_value TestDefineClass(napi_env env,
7646
&property_descriptor,
7747
&result);
7848

79-
add_named_status(env, "nameIsNull", return_value);
49+
add_last_status(env, "nameIsNull", return_value);
8050

8151
napi_define_class(env,
8252
"TrackedFunction",
@@ -87,7 +57,7 @@ static napi_value TestDefineClass(napi_env env,
8757
&property_descriptor,
8858
&result);
8959

90-
add_named_status(env, "cbIsNull", return_value);
60+
add_last_status(env, "cbIsNull", return_value);
9161

9262
napi_define_class(env,
9363
"TrackedFunction",
@@ -98,7 +68,7 @@ static napi_value TestDefineClass(napi_env env,
9868
&property_descriptor,
9969
&result);
10070

101-
add_named_status(env, "cbDataIsNull", return_value);
71+
add_last_status(env, "cbDataIsNull", return_value);
10272

10373
napi_define_class(env,
10474
"TrackedFunction",
@@ -109,7 +79,7 @@ static napi_value TestDefineClass(napi_env env,
10979
NULL,
11080
&result);
11181

112-
add_named_status(env, "propertiesIsNull", return_value);
82+
add_last_status(env, "propertiesIsNull", return_value);
11383

11484

11585
napi_define_class(env,
@@ -121,7 +91,7 @@ static napi_value TestDefineClass(napi_env env,
12191
&property_descriptor,
12292
NULL);
12393

124-
add_named_status(env, "resultIsNull", return_value);
94+
add_last_status(env, "resultIsNull", return_value);
12595

12696
return return_value;
12797
}

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)