use std::convert::TryFrom;
use std::marker::PhantomData;
use crate::scope::ScopeDefinition;
use crate::support::MapFnFrom;
use crate::support::MapFnTo;
use crate::support::ToCFn;
use crate::support::UnitType;
use crate::support::{int, Opaque};
use crate::Context;
use crate::Function;
use crate::FunctionCallbackScope;
use crate::Local;
use crate::Name;
use crate::Object;
use crate::PropertyCallbackScope;
use crate::ToLocal;
use crate::Value;
extern "C" {
fn v8__Function__New(
context: *mut Context,
callback: FunctionCallback,
) -> *mut Function;
fn v8__Function__NewWithData(
context: *mut Context,
callback: FunctionCallback,
data: Local<Value>,
) -> *mut Function;
fn v8__Function__Call(
function: *const Function,
context: Local<Context>,
recv: Local<Value>,
argc: int,
argv: *const Local<Value>,
) -> *mut Value;
fn v8__FunctionCallbackInfo__GetReturnValue(
info: *const FunctionCallbackInfo,
) -> *mut Value;
fn v8__FunctionCallbackInfo__This(
info: *const FunctionCallbackInfo,
) -> *mut Object;
fn v8__FunctionCallbackInfo__Length(info: *const FunctionCallbackInfo)
-> int;
fn v8__FunctionCallbackInfo__GetArgument(
info: *const FunctionCallbackInfo,
i: int,
) -> *mut Value;
fn v8__FunctionCallbackInfo__Data(
info: *const FunctionCallbackInfo,
) -> *mut Value;
fn v8__PropertyCallbackInfo__GetReturnValue(
info: *const PropertyCallbackInfo,
) -> *mut Value;
fn v8__PropertyCallbackInfo__This(
info: *const PropertyCallbackInfo,
) -> *mut Object;
fn v8__ReturnValue__Set(rv: &mut ReturnValue, value: *mut Value);
fn v8__ReturnValue__Get(rv: &ReturnValue) -> *mut Value;
}
#[repr(C)]
pub struct ReturnValue<'cb>(*mut Value, PhantomData<&'cb ()>);
impl<'cb> ReturnValue<'cb> {
fn from_function_callback_info(info: *const FunctionCallbackInfo) -> Self {
let slot = unsafe { v8__FunctionCallbackInfo__GetReturnValue(info) };
Self(slot, PhantomData)
}
fn from_property_callback_info(info: *const PropertyCallbackInfo) -> Self {
let slot = unsafe { v8__PropertyCallbackInfo__GetReturnValue(info) };
Self(slot, PhantomData)
}
pub fn set(&mut self, mut value: Local<Value>) {
unsafe { v8__ReturnValue__Set(&mut *self, &mut *value) }
}
pub fn get<'sc>(
&mut self,
scope: &mut impl ToLocal<'sc>,
) -> Local<'sc, Value> {
unsafe { scope.to_local(v8__ReturnValue__Get(self)) }.unwrap()
}
}
#[repr(C)]
pub struct FunctionCallbackInfo {
implicit_args: *mut Opaque,
values: *mut Value,
length: int,
}
unsafe impl<'s> ScopeDefinition<'s> for FunctionCallbackInfo {
type Args = ();
unsafe fn enter_scope(_: *mut Self, _: Self::Args) {}
}
#[repr(C)]
pub struct PropertyCallbackInfo {
args: *mut Opaque,
}
unsafe impl<'s> ScopeDefinition<'s> for PropertyCallbackInfo {
type Args = ();
unsafe fn enter_scope(_: *mut Self, _: Self::Args) {}
}
pub struct FunctionCallbackArguments<'s> {
info: *const FunctionCallbackInfo,
phantom: PhantomData<&'s ()>,
}
impl<'s> FunctionCallbackArguments<'s> {
fn from_function_callback_info(info: *const FunctionCallbackInfo) -> Self {
Self {
info,
phantom: PhantomData,
}
}
pub fn this(&self) -> Local<Object> {
unsafe {
Local::from_raw(v8__FunctionCallbackInfo__This(self.info)).unwrap()
}
}
pub fn data(&self) -> Option<Local<Value>> {
unsafe { Local::from_raw(v8__FunctionCallbackInfo__Data(self.info)) }
}
pub fn length(&self) -> int {
unsafe {
let length = (*self.info).length;
debug_assert_eq!(length, v8__FunctionCallbackInfo__Length(self.info));
length
}
}
pub fn get(&self, i: int) -> Local<Value> {
unsafe {
Local::from_raw(v8__FunctionCallbackInfo__GetArgument(self.info, i))
.unwrap()
}
}
}
pub struct PropertyCallbackArguments<'s> {
info: *const PropertyCallbackInfo,
phantom: PhantomData<&'s ()>,
}
impl<'s> PropertyCallbackArguments<'s> {
fn from_property_callback_info(info: *const PropertyCallbackInfo) -> Self {
Self {
info,
phantom: PhantomData,
}
}
pub fn this(&self) -> Local<Object> {
unsafe {
Local::from_raw(v8__PropertyCallbackInfo__This(self.info)).unwrap()
}
}
}
pub type FunctionCallback = extern "C" fn(*const FunctionCallbackInfo);
impl<F> MapFnFrom<F> for FunctionCallback
where
F: UnitType
+ Fn(FunctionCallbackScope, FunctionCallbackArguments, ReturnValue),
{
fn mapping() -> Self {
let f = |info: *const FunctionCallbackInfo| {
let scope: FunctionCallbackScope =
&mut crate::scope::Entered::new_root(info as *mut FunctionCallbackInfo);
let args = FunctionCallbackArguments::from_function_callback_info(info);
let rv = ReturnValue::from_function_callback_info(info);
(F::get())(scope, args, rv);
};
f.to_c_fn()
}
}
pub type AccessorNameGetterCallback<'s> =
extern "C" fn(Local<'s, Name>, *const PropertyCallbackInfo);
impl<F> MapFnFrom<F> for AccessorNameGetterCallback<'_>
where
F: UnitType
+ Fn(
PropertyCallbackScope,
Local<Name>,
PropertyCallbackArguments,
ReturnValue,
),
{
fn mapping() -> Self {
let f = |key: Local<Name>, info: *const PropertyCallbackInfo| {
let scope: PropertyCallbackScope =
&mut crate::scope::Entered::new_root(info as *mut PropertyCallbackInfo);
let args = PropertyCallbackArguments::from_property_callback_info(info);
let rv = ReturnValue::from_property_callback_info(info);
(F::get())(scope, key, args, rv);
};
f.to_c_fn()
}
}
impl Function {
pub fn new<'sc>(
scope: &mut impl ToLocal<'sc>,
mut context: Local<Context>,
callback: impl MapFnTo<FunctionCallback>,
) -> Option<Local<'sc, Function>> {
unsafe {
scope.to_local(v8__Function__New(&mut *context, callback.map_fn_to()))
}
}
pub fn new_with_data<'sc>(
scope: &mut impl ToLocal<'sc>,
mut context: Local<Context>,
data: Local<Value>,
callback: impl MapFnTo<FunctionCallback>,
) -> Option<Local<'sc, Function>> {
unsafe {
scope.to_local(v8__Function__NewWithData(
&mut *context,
callback.map_fn_to(),
data,
))
}
}
pub fn call<'sc>(
&self,
scope: &mut impl ToLocal<'sc>,
context: Local<Context>,
recv: Local<Value>,
args: &[Local<Value>],
) -> Option<Local<'sc, Value>> {
let argv = args.as_ptr();
let argc = int::try_from(args.len()).unwrap();
unsafe {
scope.to_local(v8__Function__Call(self, context, recv, argc, argv))
}
}
}