-
Notifications
You must be signed in to change notification settings - Fork 4
Translate function pointers passed in variadic arguments #83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fafc336
Translate func ptr passed in variadic func in unsafe
lucic71 d3c570a
Translate func ptr passed in variadic func in refcount
lucic71 ccf1f07
Add vaarg func pointer test
lucic71 127afb3
Use char literal for single char string
lucic71 a44644e
Update tests
lucic71 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| extern crate libcc2rs; | ||
| use libcc2rs::*; | ||
| use std::cell::RefCell; | ||
| use std::collections::BTreeMap; | ||
| use std::io::prelude::*; | ||
| use std::io::{Read, Seek, Write}; | ||
| use std::os::fd::AsFd; | ||
| use std::rc::{Rc, Weak}; | ||
| pub fn square_0(x: i32) -> i32 { | ||
| let x: Value<i32> = Rc::new(RefCell::new(x)); | ||
| return ((*x.borrow()) * (*x.borrow())); | ||
| } | ||
| pub fn negate_1(x: i32) -> i32 { | ||
| let x: Value<i32> = Rc::new(RefCell::new(x)); | ||
| return -(*x.borrow()); | ||
| } | ||
| pub fn add_2(a: i32, b: i32) -> i32 { | ||
| let a: Value<i32> = Rc::new(RefCell::new(a)); | ||
| let b: Value<i32> = Rc::new(RefCell::new(b)); | ||
| return ((*a.borrow()) + (*b.borrow())); | ||
| } | ||
| pub fn apply_unary_3(x: i32, __args: &[VaArg]) -> i32 { | ||
| let x: Value<i32> = Rc::new(RefCell::new(x)); | ||
| let ap: Value<VaList> = Rc::new(RefCell::new(VaList::default())); | ||
| (*ap.borrow_mut()) = VaList::new(__args); | ||
| let fn_: Value<FnPtr<fn(i32) -> i32>> = Rc::new(RefCell::new( | ||
| ((*ap.borrow_mut()).arg::<FnPtr<fn(i32) -> i32>>()).clone(), | ||
| )); | ||
| let result: Value<i32> = Rc::new(RefCell::new( | ||
| ({ | ||
| let _arg0: i32 = (*x.borrow()); | ||
| (*(*fn_.borrow()))(_arg0) | ||
| }), | ||
| )); | ||
| return (*result.borrow()); | ||
| } | ||
| pub fn apply_binary_4(a: i32, b: i32, __args: &[VaArg]) -> i32 { | ||
| let a: Value<i32> = Rc::new(RefCell::new(a)); | ||
| let b: Value<i32> = Rc::new(RefCell::new(b)); | ||
| let ap: Value<VaList> = Rc::new(RefCell::new(VaList::default())); | ||
| (*ap.borrow_mut()) = VaList::new(__args); | ||
| let fn_: Value<FnPtr<fn(i32, i32) -> i32>> = Rc::new(RefCell::new( | ||
| ((*ap.borrow_mut()).arg::<FnPtr<fn(i32, i32) -> i32>>()).clone(), | ||
| )); | ||
| let result: Value<i32> = Rc::new(RefCell::new( | ||
| ({ | ||
| let _arg0: i32 = (*a.borrow()); | ||
| let _arg1: i32 = (*b.borrow()); | ||
| (*(*fn_.borrow()))(_arg0, _arg1) | ||
| }), | ||
| )); | ||
| return (*result.borrow()); | ||
| } | ||
| pub fn main() { | ||
| std::process::exit(main_0()); | ||
| } | ||
| fn main_0() -> i32 { | ||
| assert!( | ||
| (((({ | ||
| let _x: i32 = 5; | ||
| apply_unary_3(_x, &[FnPtr::<fn(i32) -> i32>::new(square_0).into()]) | ||
| }) == 25) as i32) | ||
| != 0) | ||
| ); | ||
| assert!( | ||
| (((({ | ||
| let _x: i32 = 7; | ||
| apply_unary_3(_x, &[FnPtr::<fn(i32) -> i32>::new(negate_1).into()]) | ||
| }) == -7_i32) as i32) | ||
| != 0) | ||
| ); | ||
| assert!( | ||
| (((({ | ||
| let _a: i32 = 3; | ||
| let _b: i32 = 4; | ||
| apply_binary_4(_a, _b, &[FnPtr::<fn(i32, i32) -> i32>::new(add_2).into()]) | ||
| }) == 7) as i32) | ||
| != 0) | ||
| ); | ||
| return 0; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| extern crate libc; | ||
| use libc::*; | ||
| extern crate libcc2rs; | ||
| use libcc2rs::*; | ||
| use std::collections::BTreeMap; | ||
| use std::io::{Read, Seek, Write}; | ||
| use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; | ||
| use std::rc::Rc; | ||
| pub unsafe fn square_0(mut x: i32) -> i32 { | ||
| return ((x) * (x)); | ||
| } | ||
| pub unsafe fn negate_1(mut x: i32) -> i32 { | ||
| return -x; | ||
| } | ||
| pub unsafe fn add_2(mut a: i32, mut b: i32) -> i32 { | ||
| return ((a) + (b)); | ||
| } | ||
| pub unsafe fn apply_unary_3(mut x: i32, __args: &[VaArg]) -> i32 { | ||
| let mut ap: VaList = VaList::default(); | ||
| ap = VaList::new(__args); | ||
| let mut fn_: Option<unsafe fn(i32) -> i32> = std::mem::transmute::< | ||
| *mut ::libc::c_void, | ||
| Option<unsafe fn(i32) -> i32>, | ||
| >(ap.arg::<*mut ::libc::c_void>()); | ||
| let mut result: i32 = (unsafe { | ||
| let _arg0: i32 = x; | ||
| (fn_).unwrap()(_arg0) | ||
| }); | ||
| return result; | ||
| } | ||
| pub unsafe fn apply_binary_4(mut a: i32, mut b: i32, __args: &[VaArg]) -> i32 { | ||
| let mut ap: VaList = VaList::default(); | ||
| ap = VaList::new(__args); | ||
| let mut fn_: Option<unsafe fn(i32, i32) -> i32> = std::mem::transmute::< | ||
| *mut ::libc::c_void, | ||
| Option<unsafe fn(i32, i32) -> i32>, | ||
| >(ap.arg::<*mut ::libc::c_void>()); | ||
| let mut result: i32 = (unsafe { | ||
| let _arg0: i32 = a; | ||
| let _arg1: i32 = b; | ||
| (fn_).unwrap()(_arg0, _arg1) | ||
| }); | ||
| return result; | ||
| } | ||
| pub fn main() { | ||
| unsafe { | ||
| std::process::exit(main_0() as i32); | ||
| } | ||
| } | ||
| unsafe fn main_0() -> i32 { | ||
| assert!( | ||
| ((((unsafe { | ||
| let _x: i32 = 5; | ||
| apply_unary_3( | ||
| _x, | ||
| &[ | ||
| (Some(square_0).map_or(::std::ptr::null_mut(), |f| f as *mut ::libc::c_void)) | ||
| .into(), | ||
| ], | ||
| ) | ||
| }) == (25)) as i32) | ||
| != 0) | ||
| ); | ||
| assert!( | ||
| ((((unsafe { | ||
| let _x: i32 = 7; | ||
| apply_unary_3( | ||
| _x, | ||
| &[ | ||
| (Some(negate_1).map_or(::std::ptr::null_mut(), |f| f as *mut ::libc::c_void)) | ||
| .into(), | ||
| ], | ||
| ) | ||
| }) == (-7_i32)) as i32) | ||
| != 0) | ||
| ); | ||
| assert!( | ||
| ((((unsafe { | ||
| let _a: i32 = 3; | ||
| let _b: i32 = 4; | ||
| apply_binary_4( | ||
| _a, | ||
| _b, | ||
| &[ | ||
| (Some(add_2).map_or(::std::ptr::null_mut(), |f| f as *mut ::libc::c_void)) | ||
| .into(), | ||
| ], | ||
| ) | ||
| }) == (7)) as i32) | ||
| != 0) | ||
| ); | ||
| return 0; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| #include <assert.h> | ||
| #include <stdarg.h> | ||
|
|
||
| typedef int (*unary)(int); | ||
| typedef int (*binary)(int, int); | ||
|
|
||
| int square(int x) { return x * x; } | ||
| int negate(int x) { return -x; } | ||
| int add(int a, int b) { return a + b; } | ||
|
|
||
| int apply_unary(int x, ...) { | ||
| va_list ap; | ||
| va_start(ap, x); | ||
| unary fn = va_arg(ap, unary); | ||
| int result = fn(x); | ||
| va_end(ap); | ||
| return result; | ||
| } | ||
|
|
||
| int apply_binary(int a, int b, ...) { | ||
| va_list ap; | ||
| va_start(ap, b); | ||
| binary fn = va_arg(ap, binary); | ||
| int result = fn(a, b); | ||
| va_end(ap); | ||
| return result; | ||
| } | ||
|
|
||
| int main() { | ||
| assert(apply_unary(5, square) == 25); | ||
| assert(apply_unary(7, negate) == -7); | ||
| assert(apply_binary(3, 4, add) == 7); | ||
| return 0; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is Option needed here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We translate functions pointers to
Option<fn>in unsafe. Option is needed to encode nullability