mirror of
https://github.com/napi-rs/napi-rs.git
synced 2025-12-08 19:56:07 +00:00
chore(napi): remove depracated get_js_function (#2101)
This commit is contained in:
parent
d2c80d28d3
commit
1e781a589c
@ -16,7 +16,7 @@ use std::thread::ThreadId;
|
||||
|
||||
use once_cell::sync::Lazy;
|
||||
|
||||
use crate::{check_status, sys, Env, JsFunction, Property, Result, Value, ValueType};
|
||||
use crate::{check_status, sys, Property, Result};
|
||||
#[cfg(not(feature = "noop"))]
|
||||
use crate::{check_status_or_throw, JsError};
|
||||
|
||||
@ -156,58 +156,6 @@ pub fn register_class(
|
||||
});
|
||||
}
|
||||
|
||||
#[inline]
|
||||
/// Get `JsFunction` from defined Rust `fn`
|
||||
/// ```rust
|
||||
/// #[napi]
|
||||
/// fn some_fn() -> u32 {
|
||||
/// 1
|
||||
/// }
|
||||
///
|
||||
/// #[napi]
|
||||
/// fn return_some_fn() -> Result<JsFunction> {
|
||||
/// get_js_function(some_fn_js_function)
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// ```js
|
||||
/// returnSomeFn()(); // 1
|
||||
/// ```
|
||||
///
|
||||
pub fn get_js_function(env: &Env, raw_fn: ExportRegisterCallback) -> Result<JsFunction> {
|
||||
FN_REGISTER_MAP.borrow_mut(|inner| {
|
||||
inner
|
||||
.get(&raw_fn)
|
||||
.and_then(|(cb, name)| {
|
||||
let mut function = ptr::null_mut();
|
||||
let name_len = name.len() - 1;
|
||||
let fn_name = unsafe { CStr::from_bytes_with_nul_unchecked(name.as_bytes()) };
|
||||
check_status!(unsafe {
|
||||
sys::napi_create_function(
|
||||
env.0,
|
||||
fn_name.as_ptr(),
|
||||
name_len,
|
||||
*cb,
|
||||
ptr::null_mut(),
|
||||
&mut function,
|
||||
)
|
||||
})
|
||||
.ok()?;
|
||||
Some(JsFunction(Value {
|
||||
env: env.0,
|
||||
value: function,
|
||||
value_type: ValueType::Function,
|
||||
}))
|
||||
})
|
||||
.ok_or_else(|| {
|
||||
crate::Error::new(
|
||||
crate::Status::InvalidArg,
|
||||
"JavaScript function does not exist".to_owned(),
|
||||
)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/// Get `C Callback` from defined Rust `fn`
|
||||
/// ```rust
|
||||
/// #[napi]
|
||||
|
||||
@ -621,8 +621,6 @@ Generated by [AVA](https://avajs.dev).
|
||||
␊
|
||||
export function returnFromSharedCrate(): Shared␊
|
||||
␊
|
||||
export function returnJsFunction(): (...args: any[]) => any␊
|
||||
␊
|
||||
export function returnNull(): null␊
|
||||
␊
|
||||
export function returnObjectOnlyToJs(): ObjectOnlyToJs␊
|
||||
|
||||
Binary file not shown.
@ -121,7 +121,6 @@ import {
|
||||
JsClassForEither,
|
||||
receiveMutClassOrNumber,
|
||||
getStrFromObject,
|
||||
returnJsFunction,
|
||||
testSerdeRoundtrip,
|
||||
testSerdeBigNumberPrecision,
|
||||
testSerdeBufferBytes,
|
||||
@ -466,16 +465,6 @@ test('callback', (t) => {
|
||||
)
|
||||
})
|
||||
|
||||
test('return function', (t) => {
|
||||
return new Promise<void>((resolve) => {
|
||||
returnJsFunction()((err: Error | undefined, content: string) => {
|
||||
t.is(err, undefined)
|
||||
t.is(content, 'hello world')
|
||||
resolve()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Napi4Test('callback function return Promise', async (t) => {
|
||||
const cbSpy = spy()
|
||||
await callbackReturnPromise<string>(() => '1', spy)
|
||||
|
||||
@ -540,7 +540,6 @@ module.exports.referenceAsCallback = nativeBinding.referenceAsCallback
|
||||
module.exports.returnEither = nativeBinding.returnEither
|
||||
module.exports.returnEitherClass = nativeBinding.returnEitherClass
|
||||
module.exports.returnFromSharedCrate = nativeBinding.returnFromSharedCrate
|
||||
module.exports.returnJsFunction = nativeBinding.returnJsFunction
|
||||
module.exports.returnNull = nativeBinding.returnNull
|
||||
module.exports.returnObjectOnlyToJs = nativeBinding.returnObjectOnlyToJs
|
||||
module.exports.returnUndefined = nativeBinding.returnUndefined
|
||||
|
||||
@ -611,8 +611,6 @@ export function returnEitherClass(input: number): number | JsClassForEither
|
||||
|
||||
export function returnFromSharedCrate(): Shared
|
||||
|
||||
export function returnJsFunction(): (...args: any[]) => any
|
||||
|
||||
export function returnNull(): null
|
||||
|
||||
export function returnObjectOnlyToJs(): ObjectOnlyToJs
|
||||
|
||||
@ -46,11 +46,6 @@ fn read_file_content() -> Result<String> {
|
||||
Ok("hello world".to_string())
|
||||
}
|
||||
|
||||
#[napi]
|
||||
fn return_js_function(env: Env) -> Result<JsFunction> {
|
||||
get_js_function(&env, read_file_js_function)
|
||||
}
|
||||
|
||||
#[napi(
|
||||
ts_generic_types = "T",
|
||||
ts_args_type = "functionInput: () => T | Promise<T>, callback: (err: Error | null, result: T) => void",
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user