|
| 1 | +extern crate libcc2rs; |
| 2 | +use libcc2rs::*; |
| 3 | +use std::cell::RefCell; |
| 4 | +use std::collections::BTreeMap; |
| 5 | +use std::io::prelude::*; |
| 6 | +use std::io::Seek; |
| 7 | +use std::io::{Read, Write}; |
| 8 | +use std::os::fd::AsFd; |
| 9 | +use std::rc::{Rc, Weak}; |
| 10 | +pub fn double_it_0(x: i32) -> i32 { |
| 11 | + let x: Value<i32> = Rc::new(RefCell::new(x)); |
| 12 | + return ((*x.borrow()) * 2); |
| 13 | +} |
| 14 | +pub fn test_roundtrip_1() { |
| 15 | + let fn_: Value<FnPtr<fn(i32) -> i32>> = |
| 16 | + Rc::new(RefCell::new(fn_ptr!(double_it_0, fn(i32) -> i32))); |
| 17 | + assert!( |
| 18 | + (({ |
| 19 | + let _arg0: i32 = 5; |
| 20 | + (*(*fn_.borrow()))(_arg0) |
| 21 | + }) == 10) |
| 22 | + ); |
| 23 | + let gfn: Value<FnPtr<fn()>> = |
| 24 | + Rc::new(RefCell::new(((*fn_.borrow()).cast::<fn()>(None)).clone())); |
| 25 | + assert!(!((*gfn.borrow()).is_null())); |
| 26 | + let fn2: Value<FnPtr<fn(i32) -> i32>> = Rc::new(RefCell::new( |
| 27 | + ((*gfn.borrow()).cast::<fn(i32) -> i32>(None)).clone(), |
| 28 | + )); |
| 29 | + assert!( |
| 30 | + (({ |
| 31 | + let _arg0: i32 = 5; |
| 32 | + (*(*fn2.borrow()))(_arg0) |
| 33 | + }) == 10) |
| 34 | + ); |
| 35 | + assert!({ |
| 36 | + let _lhs = (*fn2.borrow()).clone(); |
| 37 | + _lhs == (*fn_.borrow()).clone() |
| 38 | + }); |
| 39 | +} |
| 40 | +pub fn test_double_cast_2() { |
| 41 | + let fn_: Value<FnPtr<fn(i32) -> i32>> = |
| 42 | + Rc::new(RefCell::new(fn_ptr!(double_it_0, fn(i32) -> i32))); |
| 43 | + let fn2: Value<FnPtr<fn(i32) -> i32>> = Rc::new(RefCell::new( |
| 44 | + ((*fn_.borrow()) |
| 45 | + .cast::<fn()>(None) |
| 46 | + .cast::<fn(i32) -> i32>(None)) |
| 47 | + .clone(), |
| 48 | + )); |
| 49 | + assert!( |
| 50 | + (({ |
| 51 | + let _arg0: i32 = 5; |
| 52 | + (*(*fn2.borrow()))(_arg0) |
| 53 | + }) == 10) |
| 54 | + ); |
| 55 | + assert!({ |
| 56 | + let _lhs = (*fn2.borrow()).clone(); |
| 57 | + _lhs == (*fn_.borrow()).clone() |
| 58 | + }); |
| 59 | +} |
| 60 | +#[derive(Default)] |
| 61 | +pub struct Command { |
| 62 | + pub data: Value<AnyPtr>, |
| 63 | +} |
| 64 | +impl Clone for Command { |
| 65 | + fn clone(&self) -> Self { |
| 66 | + let mut this = Self { |
| 67 | + data: Rc::new(RefCell::new((*self.data.borrow()).clone())), |
| 68 | + }; |
| 69 | + this |
| 70 | + } |
| 71 | +} |
| 72 | +impl ByteRepr for Command {} |
| 73 | +pub fn test_void_ptr_to_fn_3() { |
| 74 | + let cmd: Value<Command> = Rc::new(RefCell::new(<Command>::default())); |
| 75 | + (*(*cmd.borrow()).data.borrow_mut()) = fn_ptr!(double_it_0, fn(i32) -> i32).to_any(); |
| 76 | + let fn_: Value<FnPtr<fn(i32) -> i32>> = Rc::new(RefCell::new( |
| 77 | + ((*(*cmd.borrow()).data.borrow()) |
| 78 | + .cast_fn::<fn(i32) -> i32>() |
| 79 | + .expect("ub:wrong fn type")) |
| 80 | + .clone(), |
| 81 | + )); |
| 82 | + assert!( |
| 83 | + (({ |
| 84 | + let _arg0: i32 = 5; |
| 85 | + (*(*fn_.borrow()))(_arg0) |
| 86 | + }) == 10) |
| 87 | + ); |
| 88 | +} |
| 89 | +pub fn add_offset_4(base: Ptr<i32>, offset: i32) -> i32 { |
| 90 | + let base: Value<Ptr<i32>> = Rc::new(RefCell::new(base)); |
| 91 | + let offset: Value<i32> = Rc::new(RefCell::new(offset)); |
| 92 | + return { |
| 93 | + let _lhs = ((*base.borrow()).read()); |
| 94 | + _lhs + (*offset.borrow()) |
| 95 | + }; |
| 96 | +} |
| 97 | +pub fn test_call_through_cast_5() { |
| 98 | + let gfn: Value<FnPtr<fn(AnyPtr, i32) -> i32>> = Rc::new(RefCell::new( |
| 99 | + fn_ptr!(add_offset_4, fn(Ptr::<i32>, i32) -> i32).cast::<fn(AnyPtr, i32) -> i32>(Some( |
| 100 | + (|a0: AnyPtr, a1: i32| -> i32 { add_offset_4(a0.cast::<i32>().unwrap(), a1) }) |
| 101 | + as fn(AnyPtr, i32) -> i32, |
| 102 | + )), |
| 103 | + )); |
| 104 | + let val: Value<i32> = Rc::new(RefCell::new(100)); |
| 105 | + let result: Value<i32> = Rc::new(RefCell::new( |
| 106 | + ({ |
| 107 | + let _arg0: AnyPtr = ((val.as_pointer()) as Ptr<i32>).to_any(); |
| 108 | + let _arg1: i32 = 42; |
| 109 | + (*(*gfn.borrow()))(_arg0, _arg1) |
| 110 | + }), |
| 111 | + )); |
| 112 | + assert!(((*result.borrow()) == 142)); |
| 113 | +} |
| 114 | +pub fn main() { |
| 115 | + std::process::exit(main_0()); |
| 116 | +} |
| 117 | +fn main_0() -> i32 { |
| 118 | + ({ test_roundtrip_1() }); |
| 119 | + ({ test_double_cast_2() }); |
| 120 | + ({ test_void_ptr_to_fn_3() }); |
| 121 | + ({ test_call_through_cast_5() }); |
| 122 | + return 0; |
| 123 | +} |
0 commit comments