-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(napi): add napi_async_init and napi_async_destroy (#19234)
We don't have support for "AsyncContext" in "node:async_hooks" module, so these two APIs are just noops. Towards #18610.
- Loading branch information
1 parent
0bb5bbc
commit e56695d
Showing
4 changed files
with
152 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
|
||
import { assertEquals, loadTestLibrary } from "./common.js"; | ||
|
||
const mc = loadTestLibrary(); | ||
|
||
Deno.test("napi makeCallback1", function () { | ||
const resource = {}; | ||
|
||
let callCount = 0; | ||
function cb() { | ||
callCount++; | ||
assertEquals(arguments.length, 0); | ||
assertEquals(this, globalThis); | ||
return 42; | ||
} | ||
assertEquals(mc.makeCallback(resource, globalThis, cb), 42); | ||
assertEquals(callCount, 1); | ||
}); | ||
|
||
Deno.test("napi makeCallback2", function () { | ||
const resource = {}; | ||
|
||
let callCount = 0; | ||
function cb(x) { | ||
callCount++; | ||
assertEquals(arguments.length, 1); | ||
assertEquals(this, globalThis); | ||
assertEquals(x, 1337); | ||
return 42; | ||
} | ||
assertEquals(mc.makeCallback(resource, globalThis, cb, 1337), 42); | ||
assertEquals(callCount, 1); | ||
}); | ||
|
||
Deno.test("napi makeCallback3", function () { | ||
const resource = {}; | ||
|
||
let callCount = 0; | ||
|
||
function multiArgFunc(arg1, arg2, arg3) { | ||
callCount++; | ||
assertEquals(arg1, 1); | ||
assertEquals(arg2, 2); | ||
assertEquals(arg3, 3); | ||
return 42; | ||
} | ||
assertEquals( | ||
mc.makeCallback(resource, globalThis, multiArgFunc, 1, 2, 3), | ||
42, | ||
); | ||
assertEquals(callCount, 1); | ||
}); |
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,85 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
|
||
use crate::assert_napi_ok; | ||
use crate::cstr; | ||
use napi_sys::ValueType::napi_function; | ||
use napi_sys::*; | ||
use std::ptr; | ||
|
||
extern "C" fn make_callback( | ||
env: napi_env, | ||
info: napi_callback_info, | ||
) -> napi_value { | ||
const MAX_ARGUMENTS: usize = 10; | ||
const RESERVED_ARGUMENTS: usize = 3; | ||
|
||
let mut args = [std::ptr::null_mut(); MAX_ARGUMENTS]; | ||
let mut argc = MAX_ARGUMENTS; | ||
assert_napi_ok!(napi_get_cb_info( | ||
env, | ||
info, | ||
&mut argc, | ||
args.as_mut_ptr(), | ||
ptr::null_mut(), | ||
ptr::null_mut(), | ||
)); | ||
|
||
assert!(argc > 0); | ||
let resource = args[0]; | ||
let recv = args[1]; | ||
let func = args[2]; | ||
|
||
let mut argv: Vec<napi_value> = Vec::new(); | ||
argv.resize(MAX_ARGUMENTS - RESERVED_ARGUMENTS, ptr::null_mut()); | ||
for i in RESERVED_ARGUMENTS..argc { | ||
argv[i - RESERVED_ARGUMENTS] = args[i]; | ||
} | ||
|
||
let mut func_type: napi_valuetype = -1; | ||
assert_napi_ok!(napi_typeof(env, func, &mut func_type)); | ||
|
||
let mut resource_name = ptr::null_mut(); | ||
assert_napi_ok!(napi_create_string_utf8( | ||
env, | ||
cstr!("test"), | ||
usize::MAX, | ||
&mut resource_name | ||
)); | ||
|
||
let mut context: napi_async_context = ptr::null_mut(); | ||
assert_napi_ok!(napi_async_init(env, resource, resource_name, &mut context)); | ||
|
||
let mut result = ptr::null_mut(); | ||
assert_eq!(func_type, napi_function); | ||
assert_napi_ok!(napi_make_callback( | ||
env, | ||
context, | ||
recv, | ||
func, | ||
argc - RESERVED_ARGUMENTS, | ||
argv.as_mut_ptr(), | ||
&mut result | ||
)); | ||
|
||
assert_napi_ok!(napi_async_destroy(env, context)); | ||
result | ||
} | ||
|
||
pub fn init(env: napi_env, exports: napi_value) { | ||
let mut fn_: napi_value = ptr::null_mut(); | ||
|
||
assert_napi_ok!(napi_create_function( | ||
env, | ||
ptr::null_mut(), | ||
usize::MAX, | ||
Some(make_callback), | ||
ptr::null_mut(), | ||
&mut fn_, | ||
)); | ||
assert_napi_ok!(napi_set_named_property( | ||
env, | ||
exports, | ||
cstr!("makeCallback"), | ||
fn_ | ||
)); | ||
} |