test: avoid copying test source files

Converting the helper functions to be inlined and making the helper file
header only.

PR-URL: https://github.com/nodejs/node/pull/49515
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Gabriel Schulhof <gabrielschulhof@gmail.com>
This commit is contained in:
legendecas 2023-09-06 21:11:09 +08:00 committed by Node.js GitHub Bot
parent ae115d68e0
commit 178dff255f
65 changed files with 155 additions and 147 deletions

View File

@ -1423,6 +1423,7 @@ FORMAT_CPP_FILES += $(LINT_CPP_FILES)
# C source codes.
FORMAT_CPP_FILES += $(wildcard \
benchmark/napi/*/*.c \
test/js-native-api/*.h \
test/js-native-api/*/*.c \
test/js-native-api/*/*.h \
test/node-api/*/*.c \

View File

@ -1,5 +1,6 @@
#include <js_native_api.h>
#include "../common.h"
#include "../entry_point.h"
static napi_value Add(napi_env env, napi_callback_info info) {
size_t argc = 2;

View File

@ -3,7 +3,6 @@
{
"target_name": "2_function_arguments",
"sources": [
"../entry_point.c",
"2_function_arguments.c"
]
}

View File

@ -1,6 +1,7 @@
#include <js_native_api.h>
#include "../common.h"
#include <string.h>
#include "../common.h"
#include "../entry_point.h"
static napi_value RunCallback(napi_env env, napi_callback_info info) {
size_t argc = 2;

View File

@ -3,7 +3,6 @@
{
"target_name": "3_callbacks",
"sources": [
"../entry_point.c",
"3_callbacks.c"
]
}

View File

@ -1,5 +1,6 @@
#include <js_native_api.h>
#include "../common.h"
#include "../entry_point.h"
static napi_value CreateObject(napi_env env, napi_callback_info info) {
size_t argc = 1;

View File

@ -3,7 +3,6 @@
{
"target_name": "4_object_factory",
"sources": [
"../entry_point.c",
"4_object_factory.c"
]
}

View File

@ -1,5 +1,6 @@
#include <js_native_api.h>
#include "../common.h"
#include "../entry_point.h"
static napi_value MyFunction(napi_env env, napi_callback_info info) {
napi_value str;

View File

@ -3,7 +3,6 @@
{
"target_name": "5_function_factory",
"sources": [
"../entry_point.c",
"5_function_factory.c"
]
}

View File

@ -1,4 +1,5 @@
#include "../common.h"
#include "../entry_point.h"
#include "assert.h"
#include "myobject.h"

View File

@ -3,7 +3,6 @@
{
"target_name": "6_object_wrap",
"sources": [
"../entry_point.c",
"6_object_wrap.cc"
]
}

View File

@ -1,6 +1,7 @@
#include <js_native_api.h>
#include "myobject.h"
#include "../common.h"
#include "../entry_point.h"
#include "myobject.h"
napi_value CreateObject(napi_env env, napi_callback_info info) {
size_t argc = 1;

View File

@ -3,7 +3,6 @@
{
"target_name": "7_factory_wrap",
"sources": [
"../entry_point.c",
"7_factory_wrap.cc",
"myobject.cc"
]

View File

@ -1,6 +1,7 @@
#include <js_native_api.h>
#include "myobject.h"
#include "../common.h"
#include "../entry_point.h"
#include "myobject.h"
extern size_t finalize_count;

View File

@ -3,7 +3,6 @@
{
"target_name": "8_passing_wrapped",
"sources": [
"../entry_point.c",
"8_passing_wrapped.cc",
"myobject.cc"
]

View File

@ -0,0 +1,56 @@
#ifndef JS_NATIVE_API_COMMON_INL_H_
#define JS_NATIVE_API_COMMON_INL_H_
#include <js_native_api.h>
#include "common.h"
#include <stdio.h>
inline 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);
}
NODE_API_CALL_RETURN_VOID(
env,
napi_create_string_utf8(
env,
(actual_status == expected_status ? expected_message
: napi_message_string),
NAPI_AUTO_LENGTH,
&prop_value));
NODE_API_CALL_RETURN_VOID(
env, napi_set_named_property(env, object, key, prop_value));
}
inline 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;
NODE_API_CALL_RETURN_VOID(env, napi_get_last_error_info(env, &p_last_error));
NODE_API_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));
NODE_API_CALL_RETURN_VOID(
env, napi_set_named_property(env, return_value, key, prop_value));
}
#endif // JS_NATIVE_API_COMMON_INL_H_

View File

@ -1,48 +0,0 @@
#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);
}
NODE_API_CALL_RETURN_VOID(env,
napi_create_string_utf8(
env,
(actual_status == expected_status ?
expected_message :
napi_message_string),
NAPI_AUTO_LENGTH,
&prop_value));
NODE_API_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;
NODE_API_CALL_RETURN_VOID(env,
napi_get_last_error_info(env, &p_last_error));
NODE_API_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));
NODE_API_CALL_RETURN_VOID(env,
napi_set_named_property(env, return_value, key, prop_value));
}

View File

@ -1,3 +1,6 @@
#ifndef JS_NATIVE_API_COMMON_H_
#define JS_NATIVE_API_COMMON_H_
#include <js_native_api.h>
// Empty value so that macros here are able to return NULL or void
@ -76,11 +79,17 @@
#define DECLARE_NODE_API_PROPERTY_VALUE(name, value) \
{ (name), NULL, NULL, NULL, NULL, (value), 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);
static inline 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);
static inline void add_last_status(napi_env env,
const char* key,
napi_value return_value);
#include "common-inl.h"
#endif // JS_NATIVE_API_COMMON_H_

View File

@ -1,3 +1,6 @@
#ifndef JS_NATIVE_API_ENTRY_POINT_H_
#define JS_NATIVE_API_ENTRY_POINT_H_
#include <node_api.h>
EXTERN_C_START
@ -5,3 +8,5 @@ napi_value Init(napi_env env, napi_value exports);
EXTERN_C_END
NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)
#endif // JS_NATIVE_API_ENTRY_POINT_H_

View File

@ -3,7 +3,6 @@
{
"target_name": "test_array",
"sources": [
"../entry_point.c",
"test_array.c"
]
}

View File

@ -1,6 +1,7 @@
#include <js_native_api.h>
#include <string.h>
#include "../common.h"
#include "../entry_point.h"
static napi_value TestGetElement(napi_env env, napi_callback_info info) {
size_t argc = 2;

View File

@ -3,7 +3,6 @@
{
"target_name": "test_bigint",
"sources": [
"../entry_point.c",
"test_bigint.c"
]
}

View File

@ -1,8 +1,9 @@
#include <limits.h>
#include <inttypes.h>
#include <stdio.h>
#include <js_native_api.h>
#include <limits.h>
#include <stdio.h>
#include "../common.h"
#include "../entry_point.h"
static napi_value IsLossless(napi_env env, napi_callback_info info) {
size_t argc = 2;

View File

@ -1,32 +1,18 @@
{
"targets": [
{
"target_name": "copy_entry_point",
"type": "none",
"copies": [
{
"destination": ".",
"files": [ "../entry_point.c" ]
}
]
},
{
"target_name": "test_cannot_run_js",
"sources": [
"entry_point.c",
"test_cannot_run_js.c"
],
"defines": [ "NAPI_EXPERIMENTAL" ],
"dependencies": [ "copy_entry_point" ],
},
{
"target_name": "test_pending_exception",
"sources": [
"entry_point.c",
"test_cannot_run_js.c"
],
"defines": [ "NAPI_VERSION=8" ],
"dependencies": [ "copy_entry_point" ],
}
]
}

View File

@ -1,5 +1,6 @@
#include <js_native_api.h>
#include "../common.h"
#include "../entry_point.h"
#include "stdlib.h"
static void Finalize(napi_env env, void* data, void* hint) {

View File

@ -3,8 +3,6 @@
{
"target_name": "test_constructor",
"sources": [
"../common.c",
"../entry_point.c",
"test_constructor.c",
"test_null.c",
]

View File

@ -1,5 +1,6 @@
#include <js_native_api.h>
#include "../common.h"
#include "../entry_point.h"
#include "test_null.h"
static double value_ = 1;

View File

@ -3,8 +3,6 @@
{
"target_name": "test_conversions",
"sources": [
"../entry_point.c",
"../common.c",
"test_conversions.c",
"test_null.c",
]

View File

@ -1,5 +1,6 @@
#include <js_native_api.h>
#include "../common.h"
#include "../entry_point.h"
#include "test_null.h"
static napi_value AsBool(napi_env env, napi_callback_info info) {

View File

@ -3,7 +3,6 @@
{
"target_name": "test_dataview",
"sources": [
"../entry_point.c",
"test_dataview.c"
]
}

View File

@ -1,6 +1,7 @@
#include <js_native_api.h>
#include <string.h>
#include "../common.h"
#include "../entry_point.h"
static napi_value CreateDataView(napi_env env, napi_callback_info info) {
size_t argc = 3;

View File

@ -3,7 +3,6 @@
{
"target_name": "test_date",
"sources": [
"../entry_point.c",
"test_date.c"
]
}

View File

@ -1,5 +1,6 @@
#include <js_native_api.h>
#include "../common.h"
#include "../entry_point.h"
static napi_value createDate(napi_env env, napi_callback_info info) {
size_t argc = 1;

View File

@ -3,7 +3,6 @@
{
"target_name": "test_error",
"sources": [
"../entry_point.c",
"test_error.c"
]
}

View File

@ -1,6 +1,7 @@
#define NAPI_VERSION 9
#include <js_native_api.h>
#include "../common.h"
#include "../entry_point.h"
static napi_value checkError(napi_env env, napi_callback_info info) {
size_t argc = 1;

View File

@ -3,7 +3,6 @@
{
"target_name": "test_exception",
"sources": [
"../entry_point.c",
"test_exception.c"
]
}

View File

@ -1,5 +1,6 @@
#include <js_native_api.h>
#include "../common.h"
#include "../entry_point.h"
static bool exceptionWasPending = false;
static int num = 0x23432;

View File

@ -3,8 +3,6 @@
{
"target_name": "test_function",
"sources": [
"../common.c",
"../entry_point.c",
"test_function.c"
]
}

View File

@ -1,5 +1,6 @@
#include <js_native_api.h>
#include "../common.h"
#include "../entry_point.h"
static napi_value TestCreateFunctionParameters(napi_env env,
napi_callback_info info) {

View File

@ -3,7 +3,6 @@
{
"target_name": "test_general",
"sources": [
"../entry_point.c",
"test_general.c"
]
}

View File

@ -3,11 +3,12 @@
// not related to any of the other tests
// defined in the file
#define NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED
#include <js_native_api.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <js_native_api.h>
#include "../common.h"
#include "../entry_point.h"
static napi_value testStrictEquals(napi_env env, napi_callback_info info) {
size_t argc = 2;

View File

@ -3,7 +3,6 @@
{
"target_name": "test_handle_scope",
"sources": [
"../entry_point.c",
"test_handle_scope.c"
]
}

View File

@ -1,6 +1,7 @@
#include <js_native_api.h>
#include "../common.h"
#include <string.h>
#include "../common.h"
#include "../entry_point.h"
// these tests validate the handle scope functions in the normal
// flow. Forcing gc behavior to fully validate they are doing

View File

@ -3,7 +3,6 @@
{
"target_name": "test_instance_data",
"sources": [
"../entry_point.c",
"test_instance_data.c"
]
}

View File

@ -1,7 +1,8 @@
#include <js_native_api.h>
#include <stdio.h>
#include <stdlib.h>
#include <js_native_api.h>
#include "../common.h"
#include "../entry_point.h"
typedef struct {
size_t value;

View File

@ -4,7 +4,6 @@
'target_name': 'test_new_target',
'defines': [ 'V8_DEPRECATION_WARNINGS=1' ],
'sources': [
'../entry_point.c',
'test_new_target.c'
]
}

View File

@ -1,5 +1,6 @@
#include <js_native_api.h>
#include "../common.h"
#include "../entry_point.h"
static napi_value BaseClass(napi_env env, napi_callback_info info) {
napi_value newTargetArg;

View File

@ -3,8 +3,6 @@
{
"target_name": "test_number",
"sources": [
"../common.c",
"../entry_point.c",
"test_number.c",
"test_null.c",
]

View File

@ -1,5 +1,6 @@
#include <js_native_api.h>
#include "../common.h"
#include "../entry_point.h"
#include "test_null.h"
static napi_value Test(napi_env env, napi_callback_info info) {

View File

@ -3,8 +3,6 @@
{
"target_name": "test_object",
"sources": [
"../common.c",
"../entry_point.c",
"test_null.c",
"test_object.c"
]

View File

@ -1,6 +1,7 @@
#include <js_native_api.h>
#include "../common.h"
#include <string.h>
#include "../common.h"
#include "../entry_point.h"
#include "test_null.h"
static int test_value = 3;

View File

@ -3,7 +3,6 @@
{
"target_name": "test_promise",
"sources": [
"../entry_point.c",
"test_promise.c"
]
}

View File

@ -1,5 +1,6 @@
#include <js_native_api.h>
#include "../common.h"
#include "../entry_point.h"
napi_deferred deferred = NULL;

View File

@ -3,7 +3,6 @@
{
"target_name": "test_properties",
"sources": [
"../entry_point.c",
"test_properties.c"
]
}

View File

@ -1,6 +1,7 @@
#define NAPI_VERSION 9
#include <js_native_api.h>
#include "../common.h"
#include "../entry_point.h"
static double value_ = 1;

View File

@ -3,7 +3,6 @@
{
"target_name": "test_reference",
"sources": [
"../entry_point.c",
"test_reference.c"
]
}

View File

@ -1,8 +1,9 @@
#define NAPI_VERSION 9
#include <stdlib.h>
#include <assert.h>
#include <js_native_api.h>
#include <stdlib.h>
#include "../common.h"
#include "../entry_point.h"
static int test_value = 1;
static int finalize_count = 0;
@ -51,40 +52,44 @@ static napi_value CreateExternal(napi_env env, napi_callback_info info) {
}
static napi_value CreateSymbol(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value args[1];
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL,NULL));
NODE_API_ASSERT(env, argc == 1, "Expect one argument only (symbol description)");
napi_value result_symbol;
NODE_API_CALL(env, napi_create_symbol(env, args[0], &result_symbol));
return result_symbol;
size_t argc = 1;
napi_value args[1];
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
NODE_API_ASSERT(
env, argc == 1, "Expect one argument only (symbol description)");
napi_value result_symbol;
NODE_API_CALL(env, napi_create_symbol(env, args[0], &result_symbol));
return result_symbol;
}
static napi_value CreateSymbolFor(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value args[1];
char description[256];
size_t description_length;
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL,NULL));
NODE_API_ASSERT(env, argc == 1, "Expect one argument only (symbol description)");
size_t argc = 1;
napi_value args[1];
NODE_API_CALL(env, napi_get_value_string_utf8(env, args[0], description, sizeof(description), &description_length));
NODE_API_ASSERT(env, description_length <= 255, "Cannot accommodate descriptions longer than 255 bytes");
napi_value result_symbol;
NODE_API_CALL(env, node_api_symbol_for(env,
description,
description_length,
&result_symbol));
return result_symbol;
char description[256];
size_t description_length;
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
NODE_API_ASSERT(
env, argc == 1, "Expect one argument only (symbol description)");
NODE_API_CALL(
env,
napi_get_value_string_utf8(
env, args[0], description, sizeof(description), &description_length));
NODE_API_ASSERT(env,
description_length <= 255,
"Cannot accommodate descriptions longer than 255 bytes");
napi_value result_symbol;
NODE_API_CALL(env,
node_api_symbol_for(
env, description, description_length, &result_symbol));
return result_symbol;
}
static napi_value CreateSymbolForEmptyString(napi_env env, napi_callback_info info) {

View File

@ -3,7 +3,6 @@
{
"target_name": "test_reference_double_free",
"sources": [
"../entry_point.c",
"test_reference_double_free.c"
]
}

View File

@ -1,6 +1,7 @@
#include <stdlib.h>
#include <js_native_api.h>
#include <stdlib.h>
#include "../common.h"
#include "../entry_point.h"
static size_t g_call_count = 0;

View File

@ -3,10 +3,8 @@
{
"target_name": "test_string",
"sources": [
"../entry_point.c",
"test_string.c",
"test_null.c",
"../common.c",
]
}
]

View File

@ -4,6 +4,7 @@
#define NAPI_EXPERIMENTAL
#include <js_native_api.h>
#include "../common.h"
#include "../entry_point.h"
#include "test_null.h"
enum length_type { actual_length, auto_length };

View File

@ -3,7 +3,6 @@
{
"target_name": "test_symbol",
"sources": [
"../entry_point.c",
"test_symbol.c"
]
}

View File

@ -1,5 +1,6 @@
#include <js_native_api.h>
#include "../common.h"
#include "../entry_point.h"
static napi_value New(napi_env env, napi_callback_info info) {
size_t argc = 1;

View File

@ -3,7 +3,6 @@
{
"target_name": "test_typedarray",
"sources": [
"../entry_point.c",
"test_typedarray.c"
]
}

View File

@ -1,7 +1,8 @@
#include <js_native_api.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>
#include "../common.h"
#include "../entry_point.h"
static napi_value Multiply(napi_env env, napi_callback_info info) {
size_t argc = 2;