Skip to content

Instantly share code, notes, and snippets.

@dherman
Created August 23, 2016 22:13
Show Gist options
  • Save dherman/ed1e03eaced7a30b5f68758a34957171 to your computer and use it in GitHub Desktop.
Save dherman/ed1e03eaced7a30b5f68758a34957171 to your computer and use it in GitHub Desktop.
#[repr(C)]
pub struct FunctionKernel<T: Value>(fn(Call) -> JsResult<T>);
impl<T: Value> Kernel<()> for FunctionKernel<T> {
extern "C" fn callback(info: &CallbackInfo) {
info.scope().exec(info, |call| {
let data = info.data();
let FunctionKernel(kernel) = unsafe { Self::from_wrapper(data.to_raw()) };
// error: the parameter type `T` might not live long enough
// note: the parameter type `T` must be valid for the anonymous lifetime #1 defined on the block at 6:39...
if let Ok(value) = kernel(call) {
// ~~~~~~~~~~~~
info.set_return(value);
}
})
}
// ...
}
impl<'a> RootScopeInternal<'a> for RootScope<'a> {
fn exec<T: 'a, U: This, F: FnOnce(FunctionCall<U>) -> T>(&'a mut self, info: &'a CallbackInfo, f: F) -> T {
let mut v8_scope = raw::HandleScope::new();
unsafe {
neon_sys::scope::enter(&mut v8_scope, self.isolate().to_raw());
}
let call = info.as_call(self);
let result = f(call);
unsafe {
neon_sys::scope::exit(&mut v8_scope);
}
result
}
// ...
}
#[repr(C)]
pub struct FunctionKernel<T: Value>(fn(Call) -> JsResult<T>);
impl<T: Value> Kernel<()> for FunctionKernel<T> {
extern "C" fn callback(info: &CallbackInfo) {
let mut scope = info.scope();
let mut v8_scope = raw::HandleScope::new();
unsafe {
neon_sys::scope::enter(&mut v8_scope, scope.isolate().to_raw());
}
let data = info.data();
let FunctionKernel(kernel) = unsafe { Self::from_wrapper(data.to_raw()) };
if let Ok(value) = kernel(info.as_call(&mut scope)) {
info.set_return(value);
}
unsafe {
neon_sys::scope::exit(&mut v8_scope);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment