From 2956b0127fa8c4c5a8a20ea510d1d7266085d096 Mon Sep 17 00:00:00 2001 From: toyobayashi Date: Sat, 22 Apr 2023 14:24:49 +0800 Subject: [PATCH] test: passing NULL to number APIs --- packages/test/CMakeLists.txt | 2 +- packages/test/number/binding.c | 3 + packages/test/number/number-null.test.js | 19 ++++++ packages/test/number/test_null.c | 77 ++++++++++++++++++++++++ packages/test/number/test_null.h | 8 +++ 5 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 packages/test/number/number-null.test.js create mode 100644 packages/test/number/test_null.c create mode 100644 packages/test/number/test_null.h diff --git a/packages/test/CMakeLists.txt b/packages/test/CMakeLists.txt index d4cca5db..68e1cba6 100644 --- a/packages/test/CMakeLists.txt +++ b/packages/test/CMakeLists.txt @@ -292,7 +292,7 @@ add_test("ref_double_free" "./ref_double_free/binding.c" ON OFF "") add_test("function" "./function/binding.c" ON OFF "") add_test("scope" "./scope/binding.c" ON OFF "") add_test("newtarget" "./newtarget/binding.c" ON OFF "") -add_test("number" "./number/binding.c" ON OFF "") +add_test("number" "./number/binding.c;./number/test_null.c" ON OFF "") add_test("symbol" "./symbol/binding.c" ON OFF "") add_test("typedarray" "./typedarray/binding.c" ON OFF "") add_test("buffer" "./buffer/binding.c" OFF OFF "") diff --git a/packages/test/number/binding.c b/packages/test/number/binding.c index d49bac29..0e146f4d 100644 --- a/packages/test/number/binding.c +++ b/packages/test/number/binding.c @@ -1,5 +1,6 @@ #include #include "../common.h" +#include "test_null.h" static napi_value Test(napi_env env, napi_callback_info info) { size_t argc = 1; @@ -101,6 +102,8 @@ napi_value Init(napi_env env, napi_value exports) { NAPI_CALL(env, napi_define_properties( env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors)); + init_test_null(env, exports); + return exports; } EXTERN_C_END diff --git a/packages/test/number/number-null.test.js b/packages/test/number/number-null.test.js new file mode 100644 index 00000000..645fbd68 --- /dev/null +++ b/packages/test/number/number-null.test.js @@ -0,0 +1,19 @@ +'use strict' +const assert = require('assert') +const { load } = require('../util') + +module.exports = load('number').then(({ testNull }) => { + const expectedCreateResult = { + envIsNull: 'Invalid argument', + resultIsNull: 'Invalid argument' + } + const expectedGetValueResult = { + envIsNull: 'Invalid argument', + resultIsNull: 'Invalid argument', + valueIsNull: 'Invalid argument' + }; + ['Double', 'Int32', 'Uint32', 'Int64'].forEach((typeName) => { + assert.deepStrictEqual(testNull['create' + typeName](), expectedCreateResult) + assert.deepStrictEqual(testNull['getValue' + typeName](), expectedGetValueResult) + }) +}) diff --git a/packages/test/number/test_null.c b/packages/test/number/test_null.c new file mode 100644 index 00000000..8e3fc30f --- /dev/null +++ b/packages/test/number/test_null.c @@ -0,0 +1,77 @@ +#include + +#include "../common.h" + +// Unifies the way the macros declare values. +typedef double double_t; + +#define BINDING_FOR_CREATE(initial_capital, lowercase) \ + static napi_value Create##initial_capital(napi_env env, \ + napi_callback_info info) { \ + napi_value return_value, call_result; \ + lowercase##_t value = 42; \ + NAPI_CALL(env, napi_create_object(env, &return_value)); \ + add_returned_status(env, \ + "envIsNull", \ + return_value, \ + "Invalid argument", \ + napi_invalid_arg, \ + napi_create_##lowercase(NULL, value, &call_result)); \ + napi_create_##lowercase(env, value, NULL); \ + add_last_status(env, "resultIsNull", return_value); \ + return return_value; \ + } + +#define BINDING_FOR_GET_VALUE(initial_capital, lowercase) \ + static napi_value GetValue##initial_capital(napi_env env, \ + napi_callback_info info) { \ + napi_value return_value, call_result; \ + lowercase##_t value = 42; \ + NAPI_CALL(env, napi_create_object(env, &return_value)); \ + NAPI_CALL(env, napi_create_##lowercase(env, value, &call_result)); \ + add_returned_status( \ + env, \ + "envIsNull", \ + return_value, \ + "Invalid argument", \ + napi_invalid_arg, \ + napi_get_value_##lowercase(NULL, call_result, &value)); \ + napi_get_value_##lowercase(env, NULL, &value); \ + add_last_status(env, "valueIsNull", return_value); \ + napi_get_value_##lowercase(env, call_result, NULL); \ + add_last_status(env, "resultIsNull", return_value); \ + return return_value; \ + } + +BINDING_FOR_CREATE(Double, double) +BINDING_FOR_CREATE(Int32, int32) +BINDING_FOR_CREATE(Uint32, uint32) +BINDING_FOR_CREATE(Int64, int64) +BINDING_FOR_GET_VALUE(Double, double) +BINDING_FOR_GET_VALUE(Int32, int32) +BINDING_FOR_GET_VALUE(Uint32, uint32) +BINDING_FOR_GET_VALUE(Int64, int64) + +void init_test_null(napi_env env, napi_value exports) { + const napi_property_descriptor test_null_props[] = { + DECLARE_NAPI_PROPERTY("createDouble", CreateDouble), + DECLARE_NAPI_PROPERTY("createInt32", CreateInt32), + DECLARE_NAPI_PROPERTY("createUint32", CreateUint32), + DECLARE_NAPI_PROPERTY("createInt64", CreateInt64), + DECLARE_NAPI_PROPERTY("getValueDouble", GetValueDouble), + DECLARE_NAPI_PROPERTY("getValueInt32", GetValueInt32), + DECLARE_NAPI_PROPERTY("getValueUint32", GetValueUint32), + DECLARE_NAPI_PROPERTY("getValueInt64", GetValueInt64), + }; + napi_value test_null; + + NAPI_CALL_RETURN_VOID(env, napi_create_object(env, &test_null)); + NAPI_CALL_RETURN_VOID( + env, + napi_define_properties(env, + test_null, + sizeof(test_null_props) / sizeof(*test_null_props), + test_null_props)); + NAPI_CALL_RETURN_VOID( + env, napi_set_named_property(env, exports, "testNull", test_null)); +} diff --git a/packages/test/number/test_null.h b/packages/test/number/test_null.h new file mode 100644 index 00000000..d5f6bf01 --- /dev/null +++ b/packages/test/number/test_null.h @@ -0,0 +1,8 @@ +#ifndef TEST_JS_NATIVE_API_TEST_NUMBER_TEST_NULL_H_ +#define TEST_JS_NATIVE_API_TEST_NUMBER_TEST_NULL_H_ + +#include + +void init_test_null(napi_env env, napi_value exports); + +#endif // TEST_JS_NATIVE_API_TEST_NUMBER_TEST_NULL_H_