mirror of
https://github.com/denoland/rusty_v8.git
synced 2024-12-23 15:50:11 -05:00
Add v8::Function::new_with_data to attach associated data (#310)
This commit is contained in:
parent
c5348817b7
commit
747f513cba
3 changed files with 64 additions and 0 deletions
|
@ -970,6 +970,12 @@ v8::Function* v8__Function__New(v8::Local<v8::Context> context,
|
|||
return maybe_local_to_ptr(v8::Function::New(context, callback));
|
||||
}
|
||||
|
||||
v8::Function* v8__Function__NewWithData(v8::Local<v8::Context> context,
|
||||
v8::FunctionCallback callback,
|
||||
v8::Local<v8::Value> data) {
|
||||
return maybe_local_to_ptr(v8::Function::New(context, callback, data));
|
||||
}
|
||||
|
||||
v8::Value* v8__Function__Call(v8::Function* self,
|
||||
v8::Local<v8::Context> context,
|
||||
v8::Local<v8::Value> recv, int argc,
|
||||
|
@ -1017,6 +1023,11 @@ v8::Value* v8__FunctionCallbackInfo__GetArgument(
|
|||
return local_to_ptr(self[i]);
|
||||
}
|
||||
|
||||
v8::Value* v8__FunctionCallbackInfo__Data(
|
||||
const v8::FunctionCallbackInfo<v8::Value>& self) {
|
||||
return local_to_ptr(self.Data());
|
||||
}
|
||||
|
||||
void v8__ReturnValue__Set(v8::ReturnValue<v8::Value>& self,
|
||||
v8::Local<v8::Value> value) {
|
||||
self.Set(value);
|
||||
|
|
|
@ -22,6 +22,11 @@ extern "C" {
|
|||
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>,
|
||||
|
@ -42,6 +47,9 @@ extern "C" {
|
|||
info: *const FunctionCallbackInfo,
|
||||
i: int,
|
||||
) -> *mut Value;
|
||||
fn v8__FunctionCallbackInfo__Data(
|
||||
info: *const FunctionCallbackInfo,
|
||||
) -> *mut Value;
|
||||
|
||||
fn v8__PropertyCallbackInfo__GetReturnValue(
|
||||
info: *const PropertyCallbackInfo,
|
||||
|
@ -145,6 +153,11 @@ impl<'s> FunctionCallbackArguments<'s> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Returns the data argument specified when creating the callback.
|
||||
pub fn data(&self) -> Option<Local<Value>> {
|
||||
unsafe { Local::from_raw(v8__FunctionCallbackInfo__Data(self.info)) }
|
||||
}
|
||||
|
||||
/// The number of available arguments.
|
||||
pub fn length(&self) -> int {
|
||||
unsafe {
|
||||
|
@ -283,6 +296,23 @@ impl Function {
|
|||
}
|
||||
}
|
||||
|
||||
/// Create a function in the current execution context
|
||||
/// for a given FunctionCallback and associated data.
|
||||
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>,
|
||||
|
|
|
@ -1336,6 +1336,17 @@ fn fortytwo_callback(
|
|||
rv.set(v8::Integer::new(scope, 42).into());
|
||||
}
|
||||
|
||||
fn data_is_true_callback(
|
||||
_scope: v8::FunctionCallbackScope,
|
||||
args: v8::FunctionCallbackArguments,
|
||||
_rv: v8::ReturnValue,
|
||||
) {
|
||||
let data = args.data();
|
||||
assert!(data.is_some());
|
||||
let data = data.unwrap();
|
||||
assert!(data.is_true());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn function() {
|
||||
let _setup_guard = setup();
|
||||
|
@ -1373,6 +1384,18 @@ fn function() {
|
|||
let value_str = value.to_string(scope).unwrap();
|
||||
let rust_str = value_str.to_rust_string_lossy(scope);
|
||||
assert_eq!(rust_str, "Hello callback!".to_string());
|
||||
// create a function with associated data
|
||||
let true_data = v8::Boolean::new(scope, true);
|
||||
let function = v8::Function::new_with_data(
|
||||
scope,
|
||||
context,
|
||||
true_data.into(),
|
||||
data_is_true_callback,
|
||||
)
|
||||
.expect("Unable to create function with data");
|
||||
function
|
||||
.call(scope, context, recv, &[])
|
||||
.expect("Function call failed");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue