//! Set array element value
pub fn array_set_value(&mut self, array_ref: u32, index: i32, value: Value) -> Result<(), RuntimeError> {
self.memory.heap.array_set(array_ref, index as usize, value)?;
Ok(())
}
/// Set integer array element value (convenience method)
pub fn array_set_int(&mut self, array_ref: u32, index: i32, value: i32) -> Result<(), RuntimeError> {
self.memory.heap.array_set(array_ref, index as usize, Value::Int(value))?;
Ok(())
}
/// Get array element value
pub fn array_get_value(&mut self, array_ref: u32, index: i32) -> Result<Value, RuntimeError> {
self.memory.heap.array_get(array_ref, index as usize)
}
/// Get integer array element value (convenience method)
pub fn array_get_int(&mut self, array_ref: u32, index: i32) -> Result<i32, RuntimeError> {
match self.memory.heap.array_get(array_ref, index as usize)? {
Value::Int(val) => Ok(val),
_ => Err(RuntimeError::InvalidTypeConversion("array element".to_string(), "int".to_string())),
}
}
/// Create a new object array
pub fn new_array_object(&mut self, component_type: &str, length: i32) -> Result<u32, RuntimeError> {
if length < 0 {
return Err(RuntimeError::NegativeArraySizeException(length));
}
let arr = crate::memory::HeapArray::ReferenceArray(component_type.to_string(), vec![0; length as usize]);
Ok(self.memory.heap.allocate_array(arr))
}
/// Create a new boolean array
pub fn new_array_bool(&mut self, length: i32) -> Result<u32, RuntimeError> {
if length < 0 {
return Err(RuntimeError::NegativeArraySizeException(length));
}
let arr = crate::memory::HeapArray::BooleanArray(vec![false; length as usize]);
Ok(self.memory.heap.allocate_array(arr))
}
/// Create a new byte array
pub fn new_array_byte(&mut self, length: i32) -> Result<u32, RuntimeError> {
if length < 0 {
return Err(RuntimeError::NegativeArraySizeException(length));
}
let arr = crate::memory::HeapArray::ByteArray(vec![0; length as usize]);
Ok(self.memory.heap.allocate_array(arr))
}
/// Create a new char array
pub fn new_array_char(&mut self, length: i32) -> Result<u32, RuntimeError> {
if length < 0 {
return Err(RuntimeError::NegativeArraySizeException(length));
}
let arr = crate::memory::HeapArray::CharArray(vec![0u16; length as usize]);
Ok(self.memory.heap.allocate_array(arr))
}
/// Create a new short array
pub fn new_array_short(&mut self, length: i32) -> Result<u32, RuntimeError> {
if length < 0 {
return Err(RuntimeError::NegativeArraySizeException(length));
}
let arr = crate::memory::HeapArray::ShortArray(vec![0; length as usize]);
Ok(self.memory.heap.allocate_array(arr))
}
/// Create a new long array
pub fn new_array_long(&mut self, length: i32) -> Result<u32, RuntimeError> {
if length < 0 {
return Err(RuntimeError::NegativeArraySizeException(length));
}
let arr = crate::memory::HeapArray::LongArray(vec![0; length as usize]);
Ok(self.memory.heap.allocate_array(arr))
}
/// Create a new float array
pub fn new_array_float(&mut self, length: i32) -> Result<u32, RuntimeError> {
if length < 0 {
return Err(RuntimeError::NegativeArraySizeException(length));
}
let arr = crate::memory::HeapArray::FloatArray(vec![0.0; length as usize]);
Ok(self.memory.heap.allocate_array(arr))
}
/// Create a new double array
pub fn new_array_double(&mut self, length: i32) -> Result<u32, RuntimeError> {
if length < 0 {
return Err(RuntimeError::NegativeArraySizeException(length));
}
let arr = crate::memory::HeapArray::DoubleArray(vec![0.0; length as usize]);
Ok(self.memory.heap.allocate_array(arr))
}
/// Create a new integer array (convenience method)
pub fn new_array_int(&mut self, length: i32) -> Result<u32, RuntimeError> {
self.new_array_object("I", length)
}
}
impl Default for Interpreter {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod test_invokedynamic;
#[cfg(test)]
mod tests;