check_call!() macro to use () not []

(cherry picked from commit 66d255af0a5d331782dc24c89bb45d3434f3c109)
This commit is contained in:
Taeer Bar-Yam 2024-07-02 16:35:32 -04:00 committed by Robert Hensing
parent da26721bea
commit cc2e640e7f
5 changed files with 64 additions and 39 deletions

View file

@ -16,7 +16,7 @@ lazy_static! {
static ref INIT: Result<()> = { static ref INIT: Result<()> = {
unsafe { unsafe {
raw::GC_allow_register_threads(); raw::GC_allow_register_threads();
check_call!(raw::libexpr_init[&mut Context::new()])?; check_call!(raw::libexpr_init(&mut Context::new()))?;
Ok(()) Ok(())
} }
}; };
@ -67,7 +67,11 @@ impl EvalState {
init()?; init()?;
let eval_state = unsafe { let eval_state = unsafe {
check_call!(raw::state_create[&mut context, lookup_path.as_mut_ptr(), store.raw_ptr()]) check_call!(raw::state_create(
&mut context,
lookup_path.as_mut_ptr(),
store.raw_ptr()
))
}?; }?;
Ok(EvalState { Ok(EvalState {
eval_state: NonNull::new(eval_state).unwrap_or_else(|| { eval_state: NonNull::new(eval_state).unwrap_or_else(|| {
@ -109,30 +113,29 @@ impl EvalState {
CString::new(path).with_context(|| "eval_from_string: path contains null byte")?; CString::new(path).with_context(|| "eval_from_string: path contains null byte")?;
unsafe { unsafe {
let value = self.new_value_uninitialized()?; let value = self.new_value_uninitialized()?;
check_call!(raw::expr_eval_from_string[ check_call!(raw::expr_eval_from_string(
&mut self.context, &mut self.context,
self.eval_state.as_ptr(), self.eval_state.as_ptr(),
expr_ptr.as_ptr(), expr_ptr.as_ptr(),
path_ptr.as_ptr(), path_ptr.as_ptr(),
value.raw_ptr() value.raw_ptr()
])?; ))?;
Ok(value) Ok(value)
} }
} }
/// Try turn any Value into a Value that isn't a Thunk. /// Try turn any Value into a Value that isn't a Thunk.
pub fn force(&mut self, v: &Value) -> Result<()> { pub fn force(&mut self, v: &Value) -> Result<()> {
unsafe { unsafe {
check_call!(raw::value_force[&mut self.context, self.eval_state.as_ptr(), v.raw_ptr()]) check_call!(raw::value_force(
&mut self.context,
self.eval_state.as_ptr(),
v.raw_ptr()
))
}?; }?;
Ok(()) Ok(())
} }
pub fn value_type_unforced(&mut self, value: &Value) -> Option<ValueType> { pub fn value_type_unforced(&mut self, value: &Value) -> Option<ValueType> {
let r = unsafe { let r = unsafe { check_call!(raw::get_type(&mut self.context, value.raw_ptr())) };
check_call!(raw::get_type[
&mut self.context,
value.raw_ptr()
])
};
// .unwrap(): no reason for this to fail, as it does not evaluate // .unwrap(): no reason for this to fail, as it does not evaluate
ValueType::from_raw(r.unwrap()) ValueType::from_raw(r.unwrap())
} }
@ -155,7 +158,7 @@ impl EvalState {
if t != ValueType::Int { if t != ValueType::Int {
bail!("expected an int, but got a {:?}", t); bail!("expected an int, but got a {:?}", t);
} }
unsafe { check_call!(raw::get_int[&mut self.context, v.raw_ptr()]) } unsafe { check_call!(raw::get_int(&mut self.context, v.raw_ptr())) }
} }
/// Evaluate, and require that the value is an attrset. /// Evaluate, and require that the value is an attrset.
/// Returns a list of the keys in the attrset. /// Returns a list of the keys in the attrset.
@ -164,16 +167,16 @@ impl EvalState {
if t != ValueType::AttrSet { if t != ValueType::AttrSet {
bail!("expected an attrset, but got a {:?}", t); bail!("expected an attrset, but got a {:?}", t);
} }
let n = unsafe { check_call!(raw::get_attrs_size[&mut self.context, v.raw_ptr()]) }?; let n = unsafe { check_call!(raw::get_attrs_size(&mut self.context, v.raw_ptr())) }?;
let mut attrs = Vec::with_capacity(n as usize); let mut attrs = Vec::with_capacity(n as usize);
for i in 0..n { for i in 0..n {
let cstr_ptr: *const i8 = unsafe { let cstr_ptr: *const i8 = unsafe {
check_call!(raw::get_attr_name_byidx[ check_call!(raw::get_attr_name_byidx(
&mut self.context, &mut self.context,
v.raw_ptr(), v.raw_ptr(),
self.eval_state.as_ptr(), self.eval_state.as_ptr(),
i as c_uint i as c_uint
]) ))
}?; }?;
let cstr = unsafe { std::ffi::CStr::from_ptr(cstr_ptr) }; let cstr = unsafe { std::ffi::CStr::from_ptr(cstr_ptr) };
let s = cstr let s = cstr
@ -193,12 +196,12 @@ impl EvalState {
let attr_name = CString::new(attr_name) let attr_name = CString::new(attr_name)
.with_context(|| "require_attrs_select: attrName contains null byte")?; .with_context(|| "require_attrs_select: attrName contains null byte")?;
let v2 = unsafe { let v2 = unsafe {
check_call!(raw::get_attr_byname[ check_call!(raw::get_attr_byname(
&mut self.context, &mut self.context,
v.raw_ptr(), v.raw_ptr(),
self.eval_state.as_ptr(), self.eval_state.as_ptr(),
attr_name.as_ptr() attr_name.as_ptr()
]) ))
}?; }?;
Ok(Value::new(v2)) Ok(Value::new(v2))
} }
@ -238,7 +241,11 @@ impl EvalState {
let s = CString::new(s).with_context(|| "new_value_str: contains null byte")?; let s = CString::new(s).with_context(|| "new_value_str: contains null byte")?;
let v = unsafe { let v = unsafe {
let value = self.new_value_uninitialized()?; let value = self.new_value_uninitialized()?;
check_call!(raw::init_string[&mut self.context, value.raw_ptr(), s.as_ptr()])?; check_call!(raw::init_string(
&mut self.context,
value.raw_ptr(),
s.as_ptr()
))?;
value value
}; };
Ok(v) Ok(v)
@ -247,7 +254,7 @@ impl EvalState {
pub fn new_value_int(&mut self, i: Int) -> Result<Value> { pub fn new_value_int(&mut self, i: Int) -> Result<Value> {
let v = unsafe { let v = unsafe {
let value = self.new_value_uninitialized()?; let value = self.new_value_uninitialized()?;
check_call!(raw::init_int[&mut self.context, value.raw_ptr(), i])?; check_call!(raw::init_int(&mut self.context, value.raw_ptr(), i))?;
value value
}; };
Ok(v) Ok(v)
@ -257,12 +264,12 @@ impl EvalState {
fn get_string(&mut self, value: &Value) -> Result<String> { fn get_string(&mut self, value: &Value) -> Result<String> {
let mut r = result_string_init!(); let mut r = result_string_init!();
unsafe { unsafe {
check_call!(raw::get_string[ check_call!(raw::get_string(
&mut self.context, &mut self.context,
value.raw_ptr(), value.raw_ptr(),
Some(callback_get_result_string), Some(callback_get_result_string),
callback_get_result_string_data(&mut r) callback_get_result_string_data(&mut r)
])?; ))?;
}; };
r r
} }
@ -285,12 +292,12 @@ impl EvalState {
} }
let rs = unsafe { let rs = unsafe {
check_call!(raw::string_realise[ check_call!(raw::string_realise(
&mut self.context, &mut self.context,
self.eval_state.as_ptr(), self.eval_state.as_ptr(),
value.raw_ptr(), value.raw_ptr(),
is_import_from_derivation is_import_from_derivation
]) ))
}?; }?;
let s = unsafe { let s = unsafe {
@ -325,13 +332,13 @@ impl EvalState {
pub fn call(&mut self, f: Value, a: Value) -> Result<Value> { pub fn call(&mut self, f: Value, a: Value) -> Result<Value> {
let value = self.new_value_uninitialized()?; let value = self.new_value_uninitialized()?;
unsafe { unsafe {
check_call!(raw::value_call[ check_call!(raw::value_call(
&mut self.context, &mut self.context,
self.eval_state.as_ptr(), self.eval_state.as_ptr(),
f.raw_ptr(), f.raw_ptr(),
a.raw_ptr(), a.raw_ptr(),
value.raw_ptr() value.raw_ptr()
]) ))
}?; }?;
Ok(value) Ok(value)
} }
@ -342,19 +349,23 @@ impl EvalState {
pub fn new_value_apply(&mut self, f: &Value, a: &Value) -> Result<Value> { pub fn new_value_apply(&mut self, f: &Value, a: &Value) -> Result<Value> {
let value = self.new_value_uninitialized()?; let value = self.new_value_uninitialized()?;
unsafe { unsafe {
check_call!(raw::init_apply[ check_call!(raw::init_apply(
&mut self.context, &mut self.context,
value.raw_ptr(), value.raw_ptr(),
f.raw_ptr(), f.raw_ptr(),
a.raw_ptr() a.raw_ptr()
]) ))
}?; }?;
Ok(value) Ok(value)
} }
fn new_value_uninitialized(&mut self) -> Result<Value> { fn new_value_uninitialized(&mut self) -> Result<Value> {
let value = let value = unsafe {
unsafe { check_call!(raw::alloc_value[&mut self.context, self.eval_state.as_ptr()]) }?; check_call!(raw::alloc_value(
&mut self.context,
self.eval_state.as_ptr()
))
}?;
Ok(Value::new(value)) Ok(Value::new(value))
} }
} }

View file

@ -77,7 +77,7 @@ impl Clone for Value {
// this is very unlikely to error, and it is not recoverable // this is very unlikely to error, and it is not recoverable
// Maybe try without, and try again with context to report details? // Maybe try without, and try again with context to report details?
unsafe { unsafe {
check_call!(raw::gc_incref[&mut Context::new(), self.inner.as_ptr()]).unwrap(); check_call!(raw::gc_incref(&mut Context::new(), self.inner.as_ptr())).unwrap();
} }
// can't return an error here, but we don't want to ignore the error either as it means we could use-after-free // can't return an error here, but we don't want to ignore the error either as it means we could use-after-free
Value { inner: self.inner } Value { inner: self.inner }

View file

@ -11,7 +11,7 @@ use std::ptr::NonNull;
/* TODO make Nix itself thread safe */ /* TODO make Nix itself thread safe */
lazy_static! { lazy_static! {
static ref INIT: Result<()> = unsafe { static ref INIT: Result<()> = unsafe {
check_call!(raw::libstore_init[&mut Context::new()])?; check_call!(raw::libstore_init(&mut Context::new()))?;
Ok(()) Ok(())
}; };
} }
@ -74,7 +74,11 @@ impl Store {
.collect(); .collect();
let store = unsafe { let store = unsafe {
check_call!(raw::store_open[&mut context, uri_ptr.as_ptr(), params.as_mut_ptr()]) check_call!(raw::store_open(
&mut context,
uri_ptr.as_ptr(),
params.as_mut_ptr()
))
}?; }?;
if store.is_null() { if store.is_null() {
panic!("nix_c_store_open returned a null pointer without an error"); panic!("nix_c_store_open returned a null pointer without an error");
@ -95,7 +99,12 @@ impl Store {
pub fn get_uri(&mut self) -> Result<String> { pub fn get_uri(&mut self) -> Result<String> {
let mut r = result_string_init!(); let mut r = result_string_init!();
unsafe { unsafe {
check_call!(raw::store_get_uri[&mut self.context, self.inner.ptr(), Some(callback_get_result_string), callback_get_result_string_data(&mut r)]) check_call!(raw::store_get_uri(
&mut self.context,
self.inner.ptr(),
Some(callback_get_result_string),
callback_get_result_string_data(&mut r)
))
}?; }?;
r r
} }

View file

@ -85,10 +85,10 @@ impl Drop for Context {
#[macro_export] #[macro_export]
macro_rules! check_call { macro_rules! check_call {
($f:path[$ctx:expr $(, $arg:expr)*]) => { ($($f:ident)::+($ctx:expr $(, $arg:expr)*)) => {
{ {
let ctx : &mut $crate::context::Context = $ctx; let ctx : &mut $crate::context::Context = $ctx;
let ret = $f(ctx.ptr() $(, $arg)*); let ret = $($f)::*(ctx.ptr() $(, $arg)*);
match ctx.check_err() { match ctx.check_err() {
Ok(_) => Ok(ret), Ok(_) => Ok(ret),
Err(e) => { Err(e) => {
@ -148,7 +148,7 @@ mod tests {
#[test] #[test]
fn check_call_dynamic_context() { fn check_call_dynamic_context() {
let r = check_call!(set_dummy_err[&mut Context::new()]); let r = check_call!(set_dummy_err(&mut Context::new()));
assert!(r.is_err()); assert!(r.is_err());
assert_eq!(r.unwrap_err().to_string(), "dummy error message"); assert_eq!(r.unwrap_err().to_string(), "dummy error message");
} }

View file

@ -11,7 +11,7 @@ pub fn set(key: &str, value: &str) -> Result<()> {
let key = std::ffi::CString::new(key)?; let key = std::ffi::CString::new(key)?;
let value = std::ffi::CString::new(value)?; let value = std::ffi::CString::new(value)?;
unsafe { unsafe {
check_call!(raw::setting_set[&mut ctx, key.as_ptr(), value.as_ptr()])?; check_call!(raw::setting_set(&mut ctx, key.as_ptr(), value.as_ptr()))?;
} }
Ok(()) Ok(())
} }
@ -21,7 +21,12 @@ pub fn get(key: &str) -> Result<String> {
let key = std::ffi::CString::new(key)?; let key = std::ffi::CString::new(key)?;
let mut r: Result<String> = result_string_init!(); let mut r: Result<String> = result_string_init!();
unsafe { unsafe {
check_call!(raw::setting_get[&mut ctx, key.as_ptr(), Some(callback_get_result_string), callback_get_result_string_data(&mut r)])?; check_call!(raw::setting_get(
&mut ctx,
key.as_ptr(),
Some(callback_get_result_string),
callback_get_result_string_data(&mut r)
))?;
} }
r r
} }
@ -36,7 +41,7 @@ mod tests {
fn setup() { fn setup() {
let mut ctx = context::Context::new(); let mut ctx = context::Context::new();
unsafe { unsafe {
check_call!(raw::libstore_init[&mut ctx]).unwrap(); check_call!(raw::libstore_init(&mut ctx)).unwrap();
} }
} }