mirror of
https://github.com/denoland/rusty_v8.git
synced 2025-02-02 04:37:35 -05:00
add FunctionCallbackInfo.get_argument() (#150)
This commit is contained in:
parent
233160f926
commit
6ea175c065
3 changed files with 62 additions and 3 deletions
|
@ -582,6 +582,12 @@ void v8__FunctionCallbackInfo__GetReturnValue(
|
||||||
*out = self.GetReturnValue();
|
*out = self.GetReturnValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
v8::Value* v8__FunctionCallbackInfo__GetArgument(
|
||||||
|
const v8::FunctionCallbackInfo<v8::Value>& self,
|
||||||
|
int i) {
|
||||||
|
return local_to_ptr(self[i]);
|
||||||
|
}
|
||||||
|
|
||||||
void v8__ReturnValue__Set(v8::ReturnValue<v8::Value>& self,
|
void v8__ReturnValue__Set(v8::ReturnValue<v8::Value>& self,
|
||||||
v8::Local<v8::Value> value) {
|
v8::Local<v8::Value> value) {
|
||||||
self.Set(value);
|
self.Set(value);
|
||||||
|
|
|
@ -43,6 +43,10 @@ extern "C" {
|
||||||
info: &FunctionCallbackInfo,
|
info: &FunctionCallbackInfo,
|
||||||
out: *mut ReturnValue,
|
out: *mut ReturnValue,
|
||||||
);
|
);
|
||||||
|
fn v8__FunctionCallbackInfo__GetArgument(
|
||||||
|
info: &FunctionCallbackInfo,
|
||||||
|
i: int,
|
||||||
|
) -> *mut Value;
|
||||||
|
|
||||||
fn v8__ReturnValue__Set(rv: &mut ReturnValue, value: *mut Value);
|
fn v8__ReturnValue__Set(rv: &mut ReturnValue, value: *mut Value);
|
||||||
fn v8__ReturnValue__Get(rv: &ReturnValue) -> *mut Value;
|
fn v8__ReturnValue__Get(rv: &ReturnValue) -> *mut Value;
|
||||||
|
@ -117,6 +121,13 @@ impl FunctionCallbackInfo {
|
||||||
pub fn length(&self) -> int {
|
pub fn length(&self) -> int {
|
||||||
unsafe { v8__FunctionCallbackInfo__Length(self) }
|
unsafe { v8__FunctionCallbackInfo__Length(self) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Accessor for the available arguments.
|
||||||
|
pub fn get_argument<'sc>(&mut self, i: int) -> Local<'sc, Value> {
|
||||||
|
unsafe {
|
||||||
|
Local::from_raw(v8__FunctionCallbackInfo__GetArgument(self, i)).unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FunctionTemplate {
|
impl FunctionTemplate {
|
||||||
|
|
|
@ -865,6 +865,40 @@ extern "C" fn fn_callback(info: &FunctionCallbackInfo) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extern "C" fn fn_callback2(info: &FunctionCallbackInfo) {
|
||||||
|
#[allow(mutable_transmutes)]
|
||||||
|
#[allow(clippy::transmute_ptr_to_ptr)]
|
||||||
|
let info: &mut FunctionCallbackInfo = unsafe { std::mem::transmute(info) };
|
||||||
|
assert_eq!(info.length(), 2);
|
||||||
|
let isolate = info.get_isolate();
|
||||||
|
let mut locker = v8::Locker::new(&isolate);
|
||||||
|
let arg1 = info.get_argument(0);
|
||||||
|
let arg2 = info.get_argument(1);
|
||||||
|
let rv = &mut info.get_return_value();
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut hs = v8::HandleScope::new(&mut locker);
|
||||||
|
let scope = hs.enter();
|
||||||
|
let mut context = v8::Context::new(scope);
|
||||||
|
context.enter();
|
||||||
|
|
||||||
|
let arg1_val = v8::String::new(scope, "arg1").unwrap();
|
||||||
|
assert!(arg1.is_string());
|
||||||
|
assert!(arg1.strict_equals(arg1_val.into()));
|
||||||
|
|
||||||
|
let arg2_val = v8::Integer::new(scope, 2);
|
||||||
|
assert!(arg2.is_number());
|
||||||
|
assert!(arg2.strict_equals(arg2_val.into()));
|
||||||
|
|
||||||
|
let s = v8::String::new(scope, "Hello callback!").unwrap();
|
||||||
|
let value: Local<v8::Value> = s.into();
|
||||||
|
let rv_value = rv.get(scope);
|
||||||
|
assert!(rv_value.is_undefined());
|
||||||
|
rv.set(value);
|
||||||
|
context.exit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn function() {
|
fn function() {
|
||||||
setup();
|
setup();
|
||||||
|
@ -888,10 +922,18 @@ fn function() {
|
||||||
let _value =
|
let _value =
|
||||||
v8::Function::call(&mut *function, scope, context, recv, 0, vec![]);
|
v8::Function::call(&mut *function, scope, context, recv, 0, vec![]);
|
||||||
// create function without a template
|
// create function without a template
|
||||||
let mut function = v8::Function::new(scope, context, fn_callback)
|
let mut function = v8::Function::new(scope, context, fn_callback2)
|
||||||
.expect("Unable to create function");
|
.expect("Unable to create function");
|
||||||
let maybe_value =
|
let arg1 = v8::String::new(scope, "arg1").unwrap();
|
||||||
v8::Function::call(&mut *function, scope, context, recv, 0, vec![]);
|
let arg2 = v8::Integer::new(scope, 2);
|
||||||
|
let maybe_value = v8::Function::call(
|
||||||
|
&mut *function,
|
||||||
|
scope,
|
||||||
|
context,
|
||||||
|
recv,
|
||||||
|
2,
|
||||||
|
vec![arg1.into(), arg2.into()],
|
||||||
|
);
|
||||||
let value = maybe_value.unwrap();
|
let value = maybe_value.unwrap();
|
||||||
let value_str = unsafe { Local::<v8::String>::cast(value) };
|
let value_str = unsafe { Local::<v8::String>::cast(value) };
|
||||||
let rust_str = value_str.to_rust_string_lossy(scope);
|
let rust_str = value_str.to_rust_string_lossy(scope);
|
||||||
|
|
Loading…
Add table
Reference in a new issue