use crate::value::Value;
use super::MAX_CAPTURES;
#[allow(improper_ctypes_definitions)]
#[unsafe(no_mangle)]
pub extern "C" fn patch_seq_create_env(size: i32) -> *mut [Value] {
if size < 0 {
panic!("create_env: size cannot be negative: {}", size);
}
let size_usize = size as usize;
if size_usize > MAX_CAPTURES {
panic!(
"create_env: size {} exceeds MAX_CAPTURES ({})",
size_usize, MAX_CAPTURES
);
}
let mut vec: Vec<Value> = Vec::with_capacity(size_usize);
for _ in 0..size {
vec.push(Value::Int(0));
}
Box::into_raw(vec.into_boxed_slice())
}
#[allow(improper_ctypes_definitions)]
#[unsafe(no_mangle)]
pub unsafe extern "C" fn patch_seq_env_set(env: *mut [Value], index: i32, value: Value) {
if env.is_null() {
panic!("env_set: null environment pointer");
}
if index < 0 {
panic!("env_set: index cannot be negative: {}", index);
}
let env_slice = unsafe { &mut *env };
let idx = index as usize;
if idx >= env_slice.len() {
panic!(
"env_set: index {} out of bounds for environment of size {}",
index,
env_slice.len()
);
}
env_slice[idx] = value;
}
#[allow(improper_ctypes_definitions)]
#[unsafe(no_mangle)]
pub unsafe extern "C" fn patch_seq_env_get(
env_data: *const Value,
env_len: usize,
index: i32,
) -> Value {
if env_data.is_null() {
panic!("env_get: null environment pointer");
}
if index < 0 {
panic!("env_get: index cannot be negative: {}", index);
}
let idx = index as usize;
if idx >= env_len {
panic!(
"env_get: index {} out of bounds for environment of size {}",
index, env_len
);
}
unsafe { (*env_data.add(idx)).clone() }
}