diff options
author | Octavian Soldea <octavian.soldea@intel.com> | 2019-06-27 00:09:24 -0700 |
---|---|---|
committer | Rich Trott <rtrott@gmail.com> | 2019-07-18 13:27:06 -0700 |
commit | 3da44b0b52617556a1c3816a0ae9167a1848655f (patch) | |
tree | 3f8f4e5fca4b7b3f891a597c963a5331b794fbcf /test/js-native-api | |
parent | 3b063dc21cb55f8f967cf9de3f3bbbcef58083ee (diff) | |
download | android-node-v8-3da44b0b52617556a1c3816a0ae9167a1848655f.tar.gz android-node-v8-3da44b0b52617556a1c3816a0ae9167a1848655f.tar.bz2 android-node-v8-3da44b0b52617556a1c3816a0ae9167a1848655f.zip |
test: propagate napi_status to JS
Re: https://github.com/nodejs/node/pull/27945#discussion_r288833979
This commit regards reporting to the JS level an actual event
that happens when using suspected improper null arguments. It is better
to report the exact reason from N-API to the JS level.
PR-URL: https://github.com/nodejs/node/pull/28505
Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Diffstat (limited to 'test/js-native-api')
-rw-r--r-- | test/js-native-api/test_constructor/test.js | 12 | ||||
-rw-r--r-- | test/js-native-api/test_constructor/test_constructor.c | 188 |
2 files changed, 94 insertions, 106 deletions
diff --git a/test/js-native-api/test_constructor/test.js b/test/js-native-api/test_constructor/test.js index a58eac81ad..c5be4c93bf 100644 --- a/test/js-native-api/test_constructor/test.js +++ b/test/js-native-api/test_constructor/test.js @@ -53,10 +53,10 @@ assert.strictEqual(test_object.staticReadonlyAccessor1, undefined); // Verify that passing NULL to napi_define_class() results in the correct // error. assert.deepStrictEqual(TestConstructor.TestDefineClass(), { - envIsNull: 'pass', - nameIsNull: 'pass', - cbIsNull: 'pass', - cbDataIsNull: 'pass', - propertiesIsNull: 'pass', - resultIsNull: 'pass' + envIsNull: 'Invalid argument', + nameIsNull: 'Invalid argument', + cbIsNull: 'Invalid argument', + cbDataIsNull: 'napi_ok', + propertiesIsNull: 'Invalid argument', + resultIsNull: 'Invalid argument' }); diff --git a/test/js-native-api/test_constructor/test_constructor.c b/test/js-native-api/test_constructor/test_constructor.c index dfcad1450b..19e00fbf1e 100644 --- a/test/js-native-api/test_constructor/test_constructor.c +++ b/test/js-native-api/test_constructor/test_constructor.c @@ -1,13 +1,35 @@ #include <js_native_api.h> #include "../common.h" +#include <stdio.h> + static double value_ = 1; static double static_value_ = 10; +static void +add_named_status(napi_env env, const char* key, napi_value return_value) { + napi_value prop_value; + const napi_extended_error_info* p_last_error; + NAPI_CALL_RETURN_VOID(env, napi_get_last_error_info(env, &p_last_error)); + + NAPI_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)); + NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, + return_value, + key, + prop_value)); +} + static napi_value TestDefineClass(napi_env env, napi_callback_info info) { - napi_status ret[7]; + napi_status status; napi_value result, return_value, prop_value; + char p_napi_message[100] = ""; napi_property_descriptor property_descriptor = { "TestDefineClass", @@ -19,34 +41,9 @@ static napi_value TestDefineClass(napi_env env, napi_enumerable | napi_static, NULL}; - ret[0] = napi_define_class(NULL, - "TrackedFunction", - NAPI_AUTO_LENGTH, - TestDefineClass, - NULL, - 1, - &property_descriptor, - &result); - - ret[1] = napi_define_class(env, - NULL, - NAPI_AUTO_LENGTH, - TestDefineClass, - NULL, - 1, - &property_descriptor, - &result); - - ret[2] = napi_define_class(env, - "TrackedFunction", - NAPI_AUTO_LENGTH, - NULL, - NULL, - 1, - &property_descriptor, - &result); + NAPI_CALL(env, napi_create_object(env, &return_value)); - ret[3] = napi_define_class(env, + status = napi_define_class(NULL, "TrackedFunction", NAPI_AUTO_LENGTH, TestDefineClass, @@ -55,85 +52,76 @@ static napi_value TestDefineClass(napi_env env, &property_descriptor, &result); - ret[4] = napi_define_class(env, - "TrackedFunction", - NAPI_AUTO_LENGTH, - TestDefineClass, - NULL, - 1, - NULL, - &result); - - ret[5] = napi_define_class(env, - "TrackedFunction", - NAPI_AUTO_LENGTH, - TestDefineClass, - NULL, - 1, - &property_descriptor, - NULL); - - NAPI_CALL(env, napi_create_object(env, &return_value)); + if (status == napi_invalid_arg) { + snprintf(p_napi_message, 99, "Invalid argument"); + } else { + snprintf(p_napi_message, 99, "Invalid status [%d]", status); + } NAPI_CALL(env, napi_create_string_utf8(env, - (ret[0] == napi_invalid_arg ? - "pass" : "fail"), + p_napi_message, NAPI_AUTO_LENGTH, &prop_value)); NAPI_CALL(env, napi_set_named_property(env, return_value, "envIsNull", - prop_value)); - - NAPI_CALL(env, napi_create_string_utf8(env, - (ret[1] == napi_invalid_arg ? - "pass" : "fail"), - NAPI_AUTO_LENGTH, - &prop_value)); - NAPI_CALL(env, napi_set_named_property(env, - return_value, - "nameIsNull", - prop_value)); - - NAPI_CALL(env, napi_create_string_utf8(env, - (ret[2] == napi_invalid_arg ? - "pass" : "fail"), - NAPI_AUTO_LENGTH, - &prop_value)); - NAPI_CALL(env, napi_set_named_property(env, - return_value, - "cbIsNull", - prop_value)); - - NAPI_CALL(env, napi_create_string_utf8(env, - (ret[3] == napi_ok ? - "pass" : "fail"), - NAPI_AUTO_LENGTH, - &prop_value)); - NAPI_CALL(env, napi_set_named_property(env, - return_value, - "cbDataIsNull", - prop_value)); - - NAPI_CALL(env, napi_create_string_utf8(env, - (ret[4] == napi_invalid_arg ? - "pass" : "fail"), - NAPI_AUTO_LENGTH, - &prop_value)); - NAPI_CALL(env, napi_set_named_property(env, - return_value, - "propertiesIsNull", - prop_value)); - - NAPI_CALL(env, napi_create_string_utf8(env, - (ret[5] == napi_invalid_arg ? - "pass" : "fail"), - NAPI_AUTO_LENGTH, - &prop_value)); - NAPI_CALL(env, napi_set_named_property(env, - return_value, - "resultIsNull", - prop_value)); + prop_value)); + + napi_define_class(env, + NULL, + NAPI_AUTO_LENGTH, + TestDefineClass, + NULL, + 1, + &property_descriptor, + &result); + + add_named_status(env, "nameIsNull", return_value); + + napi_define_class(env, + "TrackedFunction", + NAPI_AUTO_LENGTH, + NULL, + NULL, + 1, + &property_descriptor, + &result); + + add_named_status(env, "cbIsNull", return_value); + + napi_define_class(env, + "TrackedFunction", + NAPI_AUTO_LENGTH, + TestDefineClass, + NULL, + 1, + &property_descriptor, + &result); + + add_named_status(env, "cbDataIsNull", return_value); + + napi_define_class(env, + "TrackedFunction", + NAPI_AUTO_LENGTH, + TestDefineClass, + NULL, + 1, + NULL, + &result); + + add_named_status(env, "propertiesIsNull", return_value); + + + napi_define_class(env, + "TrackedFunction", + NAPI_AUTO_LENGTH, + TestDefineClass, + NULL, + 1, + &property_descriptor, + NULL); + + add_named_status(env, "resultIsNull", return_value); return return_value; } |