2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2022-10-05 10:06:44 -04:00
|
|
|
use crate::*;
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct CallbackInfo {
|
|
|
|
pub env: napi_env,
|
|
|
|
pub cb: napi_callback,
|
|
|
|
pub cb_info: napi_callback_info,
|
|
|
|
pub args: *const c_void,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CallbackInfo {
|
|
|
|
#[inline]
|
|
|
|
pub fn new_raw(
|
|
|
|
env: napi_env,
|
|
|
|
cb: napi_callback,
|
|
|
|
cb_info: napi_callback_info,
|
|
|
|
) -> *mut Self {
|
|
|
|
Box::into_raw(Box::new(Self {
|
|
|
|
env,
|
|
|
|
cb,
|
|
|
|
cb_info,
|
|
|
|
args: std::ptr::null(),
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" fn call_fn(info: *const v8::FunctionCallbackInfo) {
|
2022-10-20 15:01:49 -04:00
|
|
|
let info = unsafe { &*info };
|
|
|
|
let args = v8::FunctionCallbackArguments::from_function_callback_info(info);
|
|
|
|
let mut rv = v8::ReturnValue::from_function_callback_info(info);
|
2022-10-05 10:06:44 -04:00
|
|
|
// SAFETY: create_function guarantees that the data is a CallbackInfo external.
|
|
|
|
let info_ptr: *mut CallbackInfo = unsafe {
|
2022-10-20 15:01:49 -04:00
|
|
|
let external_value = v8::Local::<v8::External>::cast(args.data());
|
2022-10-05 10:06:44 -04:00
|
|
|
external_value.value() as _
|
|
|
|
};
|
|
|
|
|
|
|
|
// SAFETY: pointer from Box::into_raw.
|
|
|
|
let mut info = unsafe { &mut *info_ptr };
|
|
|
|
info.args = &args as *const _ as *const c_void;
|
|
|
|
|
|
|
|
if let Some(f) = info.cb {
|
|
|
|
// SAFETY: calling user provided function pointer.
|
|
|
|
let value = unsafe { f(info.env, info_ptr as *mut _) };
|
|
|
|
// SAFETY: napi_value is reprsented as v8::Local<v8::Value> internally.
|
|
|
|
rv.set(unsafe { transmute::<napi_value, v8::Local<v8::Value>>(value) });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
|
|
|
pub fn create_function<'a>(
|
|
|
|
env_ptr: *mut Env,
|
2023-01-22 04:34:18 -05:00
|
|
|
name: Option<v8::Local<v8::String>>,
|
2022-10-05 10:06:44 -04:00
|
|
|
cb: napi_callback,
|
|
|
|
cb_info: napi_callback_info,
|
|
|
|
) -> v8::Local<'a, v8::Function> {
|
|
|
|
let env: &mut Env = unsafe { &mut *env_ptr };
|
|
|
|
let scope = &mut env.scope();
|
|
|
|
|
|
|
|
let external = v8::External::new(
|
|
|
|
scope,
|
|
|
|
CallbackInfo::new_raw(env_ptr as _, cb, cb_info) as *mut _,
|
|
|
|
);
|
|
|
|
let function = v8::Function::builder_raw(call_fn)
|
|
|
|
.data(external.into())
|
|
|
|
.build(scope)
|
|
|
|
.unwrap();
|
|
|
|
|
2023-01-22 04:34:18 -05:00
|
|
|
if let Some(v8str) = name {
|
2022-10-05 10:06:44 -04:00
|
|
|
function.set_name(v8str);
|
|
|
|
}
|
|
|
|
|
|
|
|
function
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
|
|
|
pub fn create_function_template<'a>(
|
|
|
|
env_ptr: *mut Env,
|
|
|
|
name: Option<&str>,
|
|
|
|
cb: napi_callback,
|
|
|
|
cb_info: napi_callback_info,
|
|
|
|
) -> v8::Local<'a, v8::FunctionTemplate> {
|
|
|
|
let env: &mut Env = unsafe { &mut *env_ptr };
|
|
|
|
let scope = &mut env.scope();
|
|
|
|
|
|
|
|
let external = v8::External::new(
|
|
|
|
scope,
|
|
|
|
CallbackInfo::new_raw(env_ptr as _, cb, cb_info) as *mut _,
|
|
|
|
);
|
|
|
|
let function = v8::FunctionTemplate::builder_raw(call_fn)
|
|
|
|
.data(external.into())
|
|
|
|
.build(scope);
|
|
|
|
|
|
|
|
if let Some(name) = name {
|
|
|
|
let v8str = v8::String::new(scope, name).unwrap();
|
|
|
|
function.set_class_name(v8str);
|
|
|
|
}
|
|
|
|
|
|
|
|
function
|
|
|
|
}
|