Skip to content

Commit

Permalink
Make ocaml! functions return results directly, instead of wrapped in …
Browse files Browse the repository at this point in the history
…Result.

panic! on unexpected exceptions.
  • Loading branch information
tizoc committed Dec 19, 2020
1 parent 34c141b commit 3832ad7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 25 deletions.
18 changes: 9 additions & 9 deletions src/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl OCamlClosure {
get_named(name).map(OCamlClosure)
}

pub fn call<'a, T, R>(&self, cr: &'a mut OCamlRuntime, arg: &OCamlRooted<T>) -> Result<OCaml<'a, R>, OCamlError> {
pub fn call<'a, T, R>(&self, cr: &'a mut OCamlRuntime, arg: &OCamlRooted<T>) -> OCaml<'a, R> {
let result = unsafe { caml_callback_exn(*self.0, arg.get_raw()) };
self.handle_call_result(cr, result)
}
Expand All @@ -71,7 +71,7 @@ impl OCamlClosure {
cr: &'a mut OCamlRuntime,
arg1: &OCamlRooted<T>,
arg2: &OCamlRooted<U>,
) -> Result<OCaml<'a, R>, OCamlError> {
) -> OCaml<'a, R> {
let result = unsafe { caml_callback2_exn(*self.0, arg1.get_raw(), arg2.get_raw()) };
self.handle_call_result(cr, result)
}
Expand All @@ -82,25 +82,25 @@ impl OCamlClosure {
arg1: &OCamlRooted<T>,
arg2: &OCamlRooted<U>,
arg3: &OCamlRooted<V>,
) -> Result<OCaml<'a, R>, OCamlError> {
) -> OCaml<'a, R> {
let result = unsafe { caml_callback3_exn(*self.0, arg1.get_raw(), arg2.get_raw(), arg3.get_raw()) };
self.handle_call_result(cr, result)
}

pub fn call_n<'a, R>(&self, cr: &'a mut OCamlRuntime, args: &mut [RawOCaml]) -> Result<OCaml<'a, R>, OCamlError> {
pub fn call_n<'a, R>(&self, cr: &'a mut OCamlRuntime, args: &mut [RawOCaml]) -> OCaml<'a, R> {
let len = args.len();
let result = unsafe { caml_callbackN_exn(*self.0, len, args.as_mut_ptr()) };
self.handle_call_result(cr, result)
}

#[inline]
fn handle_call_result<'a, R>(&self, cr: &'a mut OCamlRuntime, result: RawOCaml) -> Result<OCaml<'a, R>, OCamlError> {
fn handle_call_result<'a, R>(&self, cr: &'a mut OCamlRuntime, result: RawOCaml) -> OCaml<'a, R> {
if is_exception_result(result) {
let ex = extract_exception(result);
Err(OCamlError::Exception(OCamlException::of(ex)))
//let ex = extract_exception(result);
//Err(OCamlError::Exception(OCamlException::of(ex)))
panic!("OCaml exception")
} else {
let gv = unsafe { OCaml::new(cr, result) };
Ok(gv)
unsafe { OCaml::new(cr, result) }
}
}
}
8 changes: 4 additions & 4 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ macro_rules! ocaml {
$vis fn $name<'a>(
cr: &'a mut $crate::OCamlRuntime,
$arg: &$crate::OCamlRooted<$typ>,
) -> Result<$crate::OCaml<'a, $crate::default_to_unit!($($rtyp)?)>, $crate::OCamlError> {
) -> $crate::OCaml<'a, $crate::default_to_unit!($($rtyp)?)> {
$crate::ocaml_closure_reference!(F, $name);
F.call(cr, $arg)
}
Expand All @@ -105,7 +105,7 @@ macro_rules! ocaml {
cr: &'a mut $crate::OCamlRuntime,
$arg1: &$crate::OCamlRooted<$typ1>,
$arg2: &$crate::OCamlRooted<$typ2>,
) -> Result<$crate::OCaml<'a, $crate::default_to_unit!($($rtyp)?)>, $crate::OCamlError> {
) -> $crate::OCaml<'a, $crate::default_to_unit!($($rtyp)?)> {
$crate::ocaml_closure_reference!(F, $name);
F.call2(cr, $arg1, $arg2)
}
Expand All @@ -123,7 +123,7 @@ macro_rules! ocaml {
$arg1: &$crate::OCamlRooted<$typ1>,
$arg2: &$crate::OCamlRooted<$typ2>,
$arg3: &$crate::OCamlRooted<$typ3>,
) -> Result<$crate::OCaml<'a, $crate::default_to_unit!($($rtyp)?)>, $crate::OCamlError> {
) -> $crate::OCaml<'a, $crate::default_to_unit!($($rtyp)?)> {
$crate::ocaml_closure_reference!(F, $name);
F.call3(cr, $arg1, $arg2, $arg3)
}
Expand All @@ -137,7 +137,7 @@ macro_rules! ocaml {
$vis fn $name<'a>(
cr: &'a mut $crate::OCamlRuntime,
$($arg: &$crate::OCamlRooted<$typ>),+
) -> Result<$crate::OCaml<'a, $crate::default_to_unit!($($rtyp)?)>, $crate::OCamlError> {
) -> $crate::OCaml<'a, $crate::default_to_unit!($($rtyp)?)> {
$crate::ocaml_closure_reference!(F, $name);
F.call_n(cr, &mut [$($arg.get_raw()),+])
}
Expand Down
13 changes: 1 addition & 12 deletions testing/rust-caller/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub fn increment_bytes(cr: &mut OCamlRuntime, bytes: &str, first_n: usize) -> St
ocaml_frame!(cr, (bytes_root, first_n_root), {
let bytes = to_ocaml!(cr, bytes, bytes_root);
let first_n = to_ocaml!(cr, first_n as i64, first_n_root);
let result = ocaml::increment_bytes(cr, &bytes, &first_n).unwrap();
let result = ocaml::increment_bytes(cr, &bytes, &first_n);
result.to_rust()
})
}
Expand All @@ -74,8 +74,6 @@ pub fn increment_ints_list(cr: &mut OCamlRuntime, ints: &Vec<i64>) -> Vec<i64> {
ocaml_frame!(cr, (root), {
let ints = to_ocaml!(cr, ints, root);
let result = ocaml::increment_ints_list(cr, &ints);
let result: OCaml<OCamlList<OCamlInt>> =
result.expect("Error in 'increment_ints_list' call result");
result.to_rust()
})
}
Expand All @@ -85,7 +83,6 @@ pub fn twice(cr: &mut OCamlRuntime, num: i64) -> i64 {
let num = unsafe { OCaml::of_i64_unchecked(num) };
let num = root.keep(num);
let result = ocaml::twice(cr, &num);
let result: OCaml<OCamlInt> = result.expect("Error in 'twice' call result");
result.to_rust()
})
}
Expand All @@ -96,7 +93,6 @@ pub fn make_tuple(cr: &mut OCamlRuntime, fst: String, snd: i64) -> (String, i64)
let num = num_root.keep(num);
let str = to_ocaml!(cr, fst, str_root);
let result = ocaml::make_tuple(cr, &str, &num);
let result: OCaml<(String, OCamlInt)> = result.expect("Error in 'make_tuple' call result");
result.to_rust()
})
}
Expand All @@ -105,7 +101,6 @@ pub fn make_some(cr: &mut OCamlRuntime, value: String) -> Option<String> {
ocaml_frame!(cr, (root), {
let str = to_ocaml!(cr, value, root);
let result = ocaml::make_some(cr, &str);
let result: OCaml<Option<String>> = result.expect("Error in 'make_some' call result");
result.to_rust()
})
}
Expand All @@ -114,8 +109,6 @@ pub fn make_ok(cr: &mut OCamlRuntime, value: i64) -> Result<i64, String> {
ocaml_frame!(cr, (root), {
let result = to_ocaml!(cr, value, root);
let result = ocaml::make_ok(cr, &result);
let result: OCaml<Result<OCamlInt, String>> =
result.expect("Error in 'make_ok' call result");
result.to_rust()
})
}
Expand All @@ -124,8 +117,6 @@ pub fn make_error(cr: &mut OCamlRuntime, value: String) -> Result<i64, String> {
ocaml_frame!(cr, (root), {
let result = to_ocaml!(cr, value, root);
let result = ocaml::make_error(cr, &result);
let result: OCaml<Result<OCamlInt, String>> =
result.expect("Error in 'make_error' call result");
result.to_rust()
})
}
Expand All @@ -134,7 +125,6 @@ pub fn verify_record_test(cr: &mut OCamlRuntime, record: ocaml::TestRecord) -> S
ocaml_frame!(cr, (root), {
let ocaml_record = to_ocaml!(cr, record, root);
let result = ocaml::stringify_record(cr, &ocaml_record);
let result: OCaml<String> = result.expect("Error in 'stringify_record' call result");
result.to_rust()
})
}
Expand All @@ -143,7 +133,6 @@ pub fn verify_variant_test(cr: &mut OCamlRuntime, variant: ocaml::Movement) -> S
ocaml_frame!(cr, (root), {
let ocaml_variant = to_ocaml!(cr, variant, root);
let result = ocaml::stringify_variant(cr, &ocaml_variant);
let result: OCaml<String> = result.expect("Error in 'stringify_variant' call result");
result.to_rust()
})
}
Expand Down

0 comments on commit 3832ad7

Please sign in to comment.