Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 568256d

Browse filesBrowse files
gabrielschulhoftargos
authored andcommitted
node-api: test passing NULL to number APIs
PR-URL: #47549 Reviewed-By: Michael Dawson <midawson@redhat.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
1 parent fbb6b72 commit 568256d
Copy full SHA for 568256d

File tree

Expand file treeCollapse file tree

5 files changed

+109
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

5 files changed

+109
-1
lines changed
Open diff view settings
Collapse file

‎test/js-native-api/test_number/binding.gyp‎

Copy file name to clipboardExpand all lines: test/js-native-api/test_number/binding.gyp
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
{
44
"target_name": "test_number",
55
"sources": [
6+
"../common.c",
67
"../entry_point.c",
7-
"test_number.c"
8+
"test_number.c",
9+
"test_null.c",
810
]
911
}
1012
]
Collapse file
+77Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include <js_native_api.h>
2+
3+
#include "../common.h"
4+
5+
// Unifies the way the macros declare values.
6+
typedef double double_t;
7+
8+
#define BINDING_FOR_CREATE(initial_capital, lowercase) \
9+
static napi_value Create##initial_capital(napi_env env, \
10+
napi_callback_info info) { \
11+
napi_value return_value, call_result; \
12+
lowercase##_t value = 42; \
13+
NODE_API_CALL(env, napi_create_object(env, &return_value)); \
14+
add_returned_status(env, \
15+
"envIsNull", \
16+
return_value, \
17+
"Invalid argument", \
18+
napi_invalid_arg, \
19+
napi_create_##lowercase(NULL, value, &call_result)); \
20+
napi_create_##lowercase(env, value, NULL); \
21+
add_last_status(env, "resultIsNull", return_value); \
22+
return return_value; \
23+
}
24+
25+
#define BINDING_FOR_GET_VALUE(initial_capital, lowercase) \
26+
static napi_value GetValue##initial_capital(napi_env env, \
27+
napi_callback_info info) { \
28+
napi_value return_value, call_result; \
29+
lowercase##_t value = 42; \
30+
NODE_API_CALL(env, napi_create_object(env, &return_value)); \
31+
NODE_API_CALL(env, napi_create_##lowercase(env, value, &call_result)); \
32+
add_returned_status( \
33+
env, \
34+
"envIsNull", \
35+
return_value, \
36+
"Invalid argument", \
37+
napi_invalid_arg, \
38+
napi_get_value_##lowercase(NULL, call_result, &value)); \
39+
napi_get_value_##lowercase(env, NULL, &value); \
40+
add_last_status(env, "valueIsNull", return_value); \
41+
napi_get_value_##lowercase(env, call_result, NULL); \
42+
add_last_status(env, "resultIsNull", return_value); \
43+
return return_value; \
44+
}
45+
46+
BINDING_FOR_CREATE(Double, double)
47+
BINDING_FOR_CREATE(Int32, int32)
48+
BINDING_FOR_CREATE(Uint32, uint32)
49+
BINDING_FOR_CREATE(Int64, int64)
50+
BINDING_FOR_GET_VALUE(Double, double)
51+
BINDING_FOR_GET_VALUE(Int32, int32)
52+
BINDING_FOR_GET_VALUE(Uint32, uint32)
53+
BINDING_FOR_GET_VALUE(Int64, int64)
54+
55+
void init_test_null(napi_env env, napi_value exports) {
56+
const napi_property_descriptor test_null_props[] = {
57+
DECLARE_NODE_API_PROPERTY("createDouble", CreateDouble),
58+
DECLARE_NODE_API_PROPERTY("createInt32", CreateInt32),
59+
DECLARE_NODE_API_PROPERTY("createUint32", CreateUint32),
60+
DECLARE_NODE_API_PROPERTY("createInt64", CreateInt64),
61+
DECLARE_NODE_API_PROPERTY("getValueDouble", GetValueDouble),
62+
DECLARE_NODE_API_PROPERTY("getValueInt32", GetValueInt32),
63+
DECLARE_NODE_API_PROPERTY("getValueUint32", GetValueUint32),
64+
DECLARE_NODE_API_PROPERTY("getValueInt64", GetValueInt64),
65+
};
66+
napi_value test_null;
67+
68+
NODE_API_CALL_RETURN_VOID(env, napi_create_object(env, &test_null));
69+
NODE_API_CALL_RETURN_VOID(
70+
env,
71+
napi_define_properties(env,
72+
test_null,
73+
sizeof(test_null_props) / sizeof(*test_null_props),
74+
test_null_props));
75+
NODE_API_CALL_RETURN_VOID(
76+
env, napi_set_named_property(env, exports, "testNull", test_null));
77+
}
Collapse file
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef TEST_JS_NATIVE_API_TEST_NUMBER_TEST_NULL_H_
2+
#define TEST_JS_NATIVE_API_TEST_NUMBER_TEST_NULL_H_
3+
4+
#include <js_native_api.h>
5+
6+
void init_test_null(napi_env env, napi_value exports);
7+
8+
#endif // TEST_JS_NATIVE_API_TEST_NUMBER_TEST_NULL_H_
Collapse file
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
const common = require('../../common');
3+
const assert = require('assert');
4+
const { testNull } = require(`./build/${common.buildType}/test_number`);
5+
6+
const expectedCreateResult = {
7+
envIsNull: 'Invalid argument',
8+
resultIsNull: 'Invalid argument',
9+
};
10+
const expectedGetValueResult = {
11+
envIsNull: 'Invalid argument',
12+
resultIsNull: 'Invalid argument',
13+
valueIsNull: 'Invalid argument',
14+
};
15+
[ 'Double', 'Int32', 'Uint32', 'Int64' ].forEach((typeName) => {
16+
assert.deepStrictEqual(testNull['create' + typeName](), expectedCreateResult);
17+
assert.deepStrictEqual(testNull['getValue' + typeName](), expectedGetValueResult);
18+
});
Collapse file

‎test/js-native-api/test_number/test_number.c‎

Copy file name to clipboardExpand all lines: test/js-native-api/test_number/test_number.c
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <js_native_api.h>
22
#include "../common.h"
3+
#include "test_null.h"
34

45
static napi_value Test(napi_env env, napi_callback_info info) {
56
size_t argc = 1;
@@ -101,6 +102,8 @@ napi_value Init(napi_env env, napi_value exports) {
101102
NODE_API_CALL(env, napi_define_properties(
102103
env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors));
103104

105+
init_test_null(env, exports);
106+
104107
return exports;
105108
}
106109
EXTERN_C_END

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.