forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also adds a missing check for deferred being NULL during resolution. Signed-off-by: Gabriel Schulhof <gabrielschulhof@gmail.com> PR-URL: nodejs#47553 Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Michael Dawson <midawson@redhat.com>
- Loading branch information
1 parent
86a8335
commit ef17b4c
Showing
6 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#include <js_native_api.h> | ||
|
||
#include "../common.h" | ||
#include "test_null.h" | ||
|
||
static napi_value CreatePromise(napi_env env, napi_callback_info info) { | ||
napi_value return_value, promise; | ||
napi_deferred deferred; | ||
|
||
NODE_API_CALL(env, napi_create_object(env, &return_value)); | ||
|
||
add_returned_status(env, | ||
"envIsNull", | ||
return_value, | ||
"Invalid argument", | ||
napi_invalid_arg, | ||
napi_create_promise(NULL, &deferred, &promise)); | ||
|
||
napi_create_promise(env, NULL, &promise); | ||
add_last_status(env, "deferredIsNull", return_value); | ||
|
||
napi_create_promise(env, &deferred, NULL); | ||
add_last_status(env, "promiseIsNull", return_value); | ||
|
||
return return_value; | ||
} | ||
|
||
static napi_value test_resolution_api(napi_env env, | ||
napi_callback_info info, | ||
napi_status (*api)(napi_env, | ||
napi_deferred, | ||
napi_value)) { | ||
napi_value return_value, promise, undefined; | ||
napi_deferred deferred; | ||
|
||
NODE_API_CALL(env, napi_create_object(env, &return_value)); | ||
NODE_API_CALL(env, napi_create_promise(env, &deferred, &promise)); | ||
NODE_API_CALL(env, napi_get_undefined(env, &undefined)); | ||
|
||
add_returned_status(env, | ||
"envIsNull", | ||
return_value, | ||
"Invalid argument", | ||
napi_invalid_arg, | ||
api(NULL, deferred, undefined)); | ||
|
||
api(env, NULL, undefined); | ||
add_last_status(env, "deferredIsNull", return_value); | ||
|
||
api(env, deferred, NULL); | ||
add_last_status(env, "valueIsNull", return_value); | ||
|
||
// We need to call the api will all parameters given because doing so frees a | ||
// reference the implementation keeps internally. | ||
napi_status status = api(env, deferred, undefined); | ||
if (status != napi_ok) { | ||
// This will make the test fail since the test doesn't expect that the | ||
// nothing-is-null case will be recorded. | ||
add_last_status(env, "nothingIsNull", return_value); | ||
} | ||
|
||
NODE_API_CALL(env, | ||
napi_set_named_property(env, return_value, "promise", promise)); | ||
|
||
return return_value; | ||
} | ||
|
||
static napi_value ResolveDeferred(napi_env env, napi_callback_info info) { | ||
return test_resolution_api(env, info, napi_resolve_deferred); | ||
} | ||
|
||
static napi_value RejectDeferred(napi_env env, napi_callback_info info) { | ||
return test_resolution_api(env, info, napi_reject_deferred); | ||
} | ||
|
||
void init_test_null(napi_env env, napi_value exports) { | ||
napi_value test_null; | ||
|
||
const napi_property_descriptor test_null_props[] = { | ||
DECLARE_NODE_API_PROPERTY("createPromise", CreatePromise), | ||
DECLARE_NODE_API_PROPERTY("resolveDeferred", ResolveDeferred), | ||
DECLARE_NODE_API_PROPERTY("rejectDeferred", RejectDeferred), | ||
}; | ||
|
||
NODE_API_CALL_RETURN_VOID(env, napi_create_object(env, &test_null)); | ||
NODE_API_CALL_RETURN_VOID( | ||
env, | ||
napi_define_properties(env, | ||
test_null, | ||
sizeof(test_null_props) / sizeof(*test_null_props), | ||
test_null_props)); | ||
|
||
NODE_API_CALL_RETURN_VOID( | ||
env, napi_set_named_property(env, exports, "testNull", test_null)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#ifndef TEST_JS_NATIVE_API_TEST_PROMISE_TEST_NULL_H_ | ||
#define TEST_JS_NATIVE_API_TEST_PROMISE_TEST_NULL_H_ | ||
|
||
#include <js_native_api.h> | ||
|
||
void init_test_null(napi_env env, napi_value exports); | ||
|
||
#endif // TEST_JS_NATIVE_API_TEST_PROMISE_TEST_NULL_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
'use strict'; | ||
const common = require('../../common'); | ||
const assert = require('assert'); | ||
|
||
// Test passing NULL to object-related N-APIs. | ||
const { testNull } = require(`./build/${common.buildType}/test_promise`); | ||
|
||
const expectedForCreatePromise = { | ||
envIsNull: 'Invalid argument', | ||
deferredIsNull: 'Invalid argument', | ||
promiseIsNull: 'Invalid argument', | ||
}; | ||
assert.deepStrictEqual(testNull.createPromise(), expectedForCreatePromise); | ||
|
||
function testPromiseResolution(resultObject) { | ||
const expectedForResolution = { | ||
envIsNull: 'Invalid argument', | ||
deferredIsNull: 'Invalid argument', | ||
valueIsNull: 'Invalid argument', | ||
}; | ||
const promise = resultObject.promise; | ||
|
||
delete resultObject.promise; | ||
assert.deepStrictEqual(resultObject, expectedForResolution); | ||
|
||
// Must avoid an unhandled rejection. | ||
promise.catch(() => {}); | ||
} | ||
|
||
testPromiseResolution(testNull.resolveDeferred()); | ||
testPromiseResolution(testNull.rejectDeferred()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters