2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2022-10-05 10:06:44 -04:00
|
|
|
|
2023-01-11 09:47:26 -05:00
|
|
|
use crate::assert_napi_ok;
|
|
|
|
use crate::napi_get_callback_info;
|
|
|
|
use crate::napi_new_property;
|
2022-10-05 10:06:44 -04:00
|
|
|
use napi_sys::*;
|
|
|
|
use std::ptr;
|
2024-04-10 18:08:23 -04:00
|
|
|
use std::ptr::addr_of_mut;
|
2022-10-05 10:06:44 -04:00
|
|
|
|
|
|
|
static mut CURRENT_DEFERRED: napi_deferred = ptr::null_mut();
|
|
|
|
|
|
|
|
extern "C" fn test_promise_new(
|
|
|
|
env: napi_env,
|
|
|
|
_info: napi_callback_info,
|
|
|
|
) -> napi_value {
|
|
|
|
let mut value: napi_value = ptr::null_mut();
|
2024-04-10 18:08:23 -04:00
|
|
|
assert_napi_ok!(napi_create_promise(
|
|
|
|
env,
|
|
|
|
addr_of_mut!(CURRENT_DEFERRED),
|
|
|
|
&mut value
|
|
|
|
));
|
2022-10-05 10:06:44 -04:00
|
|
|
value
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" fn test_promise_resolve(
|
|
|
|
env: napi_env,
|
|
|
|
info: napi_callback_info,
|
|
|
|
) -> napi_value {
|
2023-01-11 09:47:26 -05:00
|
|
|
let (args, argc, _) = napi_get_callback_info!(env, info, 1);
|
2022-10-05 10:06:44 -04:00
|
|
|
assert_eq!(argc, 1);
|
|
|
|
|
2023-01-11 09:47:26 -05:00
|
|
|
assert_napi_ok!(napi_resolve_deferred(env, CURRENT_DEFERRED, args[0]));
|
2022-10-05 10:06:44 -04:00
|
|
|
unsafe { CURRENT_DEFERRED = ptr::null_mut() };
|
|
|
|
ptr::null_mut()
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" fn test_promise_reject(
|
|
|
|
env: napi_env,
|
|
|
|
info: napi_callback_info,
|
|
|
|
) -> napi_value {
|
2023-01-11 09:47:26 -05:00
|
|
|
let (args, argc, _) = napi_get_callback_info!(env, info, 1);
|
2022-10-05 10:06:44 -04:00
|
|
|
assert_eq!(argc, 1);
|
|
|
|
|
2023-01-11 09:47:26 -05:00
|
|
|
assert_napi_ok!(napi_reject_deferred(env, CURRENT_DEFERRED, args[0]));
|
2022-10-05 10:06:44 -04:00
|
|
|
unsafe { CURRENT_DEFERRED = ptr::null_mut() };
|
|
|
|
ptr::null_mut()
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" fn test_promise_is(
|
|
|
|
env: napi_env,
|
|
|
|
info: napi_callback_info,
|
|
|
|
) -> napi_value {
|
2023-01-11 09:47:26 -05:00
|
|
|
let (args, argc, _) = napi_get_callback_info!(env, info, 1);
|
2022-10-05 10:06:44 -04:00
|
|
|
assert_eq!(argc, 1);
|
|
|
|
|
|
|
|
let mut is_promise: bool = false;
|
2023-01-11 09:47:26 -05:00
|
|
|
assert_napi_ok!(napi_is_promise(env, args[0], &mut is_promise));
|
2022-10-05 10:06:44 -04:00
|
|
|
|
|
|
|
let mut result: napi_value = ptr::null_mut();
|
2023-01-11 09:47:26 -05:00
|
|
|
assert_napi_ok!(napi_get_boolean(env, is_promise, &mut result));
|
2022-10-05 10:06:44 -04:00
|
|
|
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn init(env: napi_env, exports: napi_value) {
|
|
|
|
let properties = &[
|
2023-01-11 09:47:26 -05:00
|
|
|
napi_new_property!(env, "test_promise_new", test_promise_new),
|
|
|
|
napi_new_property!(env, "test_promise_resolve", test_promise_resolve),
|
|
|
|
napi_new_property!(env, "test_promise_reject", test_promise_reject),
|
|
|
|
napi_new_property!(env, "test_promise_is", test_promise_is),
|
2022-10-05 10:06:44 -04:00
|
|
|
];
|
|
|
|
|
2023-01-11 09:47:26 -05:00
|
|
|
assert_napi_ok!(napi_define_properties(
|
|
|
|
env,
|
|
|
|
exports,
|
|
|
|
properties.len(),
|
|
|
|
properties.as_ptr()
|
|
|
|
));
|
2022-10-05 10:06:44 -04:00
|
|
|
}
|