Skip to content

Commit

Permalink
add test for getters and setters
Browse files Browse the repository at this point in the history
  • Loading branch information
Sharktheone committed Feb 2, 2024
1 parent a57858a commit a936d2b
Showing 1 changed file with 96 additions and 4 deletions.
100 changes: 96 additions & 4 deletions src/web_executor/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ use std::ops::Add;
//use webinterop::{web_fns, web_interop};
use crate::types::Result;
use crate::web_executor::js::v8::{
V8Context, V8Function, V8FunctionVariadic, V8Value, V8VariadicArgsInternal,
GetterCallback, SetterCallback, V8Context, V8Function, V8FunctionVariadic, V8Value,
V8VariadicArgsInternal,
};
use crate::web_executor::js::{
Args, JSContext, JSFunction, JSFunctionCallBack, JSFunctionCallBackVariadic,
JSFunctionVariadic, JSObject, JSRuntime, JSValue, ValueConversion, VariadicArgs,
VariadicArgsInternal,
JSFunctionVariadic, JSGetterCallback, JSObject, JSRuntime, JSSetterCallback, JSValue,
ValueConversion, VariadicArgs, VariadicArgsInternal,
};

//#[web_interop]
Expand Down Expand Up @@ -90,6 +91,7 @@ fn ref_size_slice(slice: &[i32; 3]) {}

fn mut_size_slice(slice: &mut [i32; 3]) {}

#[derive(Debug)]
struct Test2 {
field: i32,
other_field: String,
Expand Down Expand Up @@ -124,6 +126,88 @@ impl Test2 {
fn implement(s: Rc<RefCell<Self>>, mut ctx: V8Context) -> Result<()> {
let obj = ctx.new_global_object("test2")?; //#name

{
//field getter and setter
let getter = {
let s = Rc::clone(&s);
Box::new(move |cb: &mut GetterCallback| {
let ctx = cb.context();
let value = s.borrow().field;
println!("got a call to getter: {}", value);
let value = match value.to_js_value(ctx.clone()) {
Ok(value) => value,
Err(e) => {
// cb.error(e); TODO
return;
}
};
cb.ret(value);
})
};

let setter = {
let s = Rc::clone(&s);
Box::new(move |cb: &mut SetterCallback| {
let ctx = cb.context();
let value = cb.value();
let value = match value.as_number() {
Ok(value) => value,
Err(e) => {
// cb.error(e); TODO
return;
}
};

println!("got a call to setter: {}", value);

s.borrow_mut().field = value as i32;
})
};

obj.set_property_accessor("field", getter, setter)?;
}

{
//other_field getter and setter
let getter = {
let s = Rc::clone(&s);
Box::new(move |cb: &mut GetterCallback| {
let ctx = cb.context();
let value = s.borrow().other_field.clone();
println!("got a call to getter: {}", value);
let value = match value.to_js_value(ctx.clone()) {
Ok(value) => value,
Err(e) => {
// cb.error(e); TODO
return;
}
};
cb.ret(value);
})
};

let setter = {
let s = Rc::clone(&s);
Box::new(move |cb: &mut SetterCallback| {
let ctx = cb.context();
let value = cb.value();
let value = match value.as_string() {
Ok(value) => value,
Err(e) => {
// cb.error(e); TODO
return;
}
};

println!("got a call to setter: {}", value);

s.borrow_mut().other_field = value;
})
};

obj.set_property_accessor("other_field", getter, setter)?;
}

let cool_fn = {
let s = Rc::clone(&s);
V8Function::new(ctx.clone(), move |cb| {
Expand Down Expand Up @@ -277,7 +361,7 @@ fn manual_js_inop() {
other_field: "Hello, ".to_string(),
}));

Test2::implement(t2, context.clone()).unwrap();
Test2::implement(t2.clone(), context.clone()).unwrap();

let out = context
.run(
Expand All @@ -286,11 +370,19 @@ fn manual_js_inop() {
test2.cool_fn() // \
test2.add(3) // |-> functions defined in rust
test2.cool_fn() // /
test2.variadic(test2, test2.cool_fn, test2.cool_fn(), test2.field, test2.other_field)
test2.field += 5
test2.field = 33
test2.field
test2.other_field += "World!"
test2.other_field
"#,
)
.expect("no value")
.as_string()
.unwrap();

println!("JS: {}", out);
println!("Rust: {:?}", t2.borrow())
}

0 comments on commit a936d2b

Please sign in to comment.