use std::cmp::Ordering;
use crate::decimal_obj::Arith;
use crate::error::{VmError, VmResult, code};
use crate::object::Object;
use crate::value::{INVALID_PROP, ObjId, Value};
use crate::vm::{NativeCall, Return, Vm};
impl Vm {
pub(crate) fn nested<T>(&mut self, work: impl FnOnce(&mut Self) -> VmResult<T>) -> VmResult<T> {
const MAX_NESTING: u32 = 500;
if self.nesting >= MAX_NESTING {
return Err(VmError::new(code::STACK_OVERFLOW));
}
self.nesting += 1;
let result = work(self);
self.nesting -= 1;
result
}
pub(crate) fn equals(&mut self, a: Value, b: Value) -> VmResult<bool> {
self.nested(|vm| vm.equals_inner(a, b))
}
fn equals_inner(&mut self, a: Value, b: Value) -> VmResult<bool> {
if let (Value::Obj(id), false) = (b, matches!(a, Value::Obj(_))) {
return self.obj_equals(id, a);
}
Ok(match a {
Value::Nil | Value::True => a == b,
Value::Obj(id) => return self.obj_equals(id, b),
Value::Prop(_) | Value::Int(_) | Value::Enum(_) | Value::ObjX(_) => a == b,
Value::BifPtr { set, index } | Value::BifPtrX { set, index } => {
b == Value::BifPtr { set, index }
}
Value::SString(_) => match (self.as_str(a), self.as_str(b)) {
(Some(x), Some(y)) => *x == *y,
_ => false,
},
Value::List(_) => return self.list_equals(a, b),
Value::CodeOfs(_) | Value::FuncPtr(_) => a == b,
Value::Empty | Value::DString(_) | Value::NativeCode(_) => false,
})
}
fn text_or_buffer(&self, value: Value) -> Option<Vec<u8>> {
if let Some(text) = self.as_str(value) {
return Some(text.to_vec());
}
match value {
Value::Obj(id) => self.string_builder_text(id),
_ => None,
}
}
fn obj_equals(&mut self, id: ObjId, other: Value) -> VmResult<bool> {
match self.objects.get(id) {
Some(Object::String(text)) => {
let text = text.clone();
Ok(match self.text_or_buffer(other) {
Some(other) => *other == *text,
None => false,
})
}
Some(Object::Decimal(big)) => {
let equal = self.big_equals(big, other);
self.big_operand(other);
Ok(equal)
}
Some(Object::CharSet { .. }) => Ok(self.charset_equals(id, other)),
Some(Object::FileName(_)) => Ok(self.file_name_equals(id, other)),
Some(Object::Date { .. }) => Ok(self.as_date(Value::Obj(id)) == self.as_date(other)),
Some(Object::ByteArray(bytes)) => {
Ok(other == Value::Obj(id)
|| self.as_bytes(other).is_some_and(|other| other == bytes))
}
Some(Object::StringBuilder(_)) => {
let text = self.string_builder_text(id).unwrap_or_default();
match self.text_or_buffer(other) {
Some(other) => Ok(other == text),
None => Err(VmError::new(code::INVALID_COMPARISON)),
}
}
Some(Object::List(_)) => self.list_equals(Value::Obj(id), other),
Some(Object::Vector(items)) => {
if other == Value::Obj(id) {
return Ok(true);
}
let items = items.clone();
let Some(len) = self.list_like_len(other)? else {
return Ok(false);
};
if len != items.len() {
return Ok(false);
}
for (index, &item) in items.iter().enumerate() {
let theirs = self.list_like_get(other, index + 1)?;
if !self.equals(item, theirs)? {
return Ok(false);
}
}
Ok(true)
}
_ => Ok(other == Value::Obj(id)),
}
}
fn list_equals(&mut self, list: Value, other: Value) -> VmResult<bool> {
let Some(items) = self.as_list(list) else {
return Ok(false);
};
let Some(len) = self.list_like_len(other)? else {
return Ok(false);
};
if len != items.len() {
return Ok(false);
}
for index in 0..len {
let theirs = self.list_like_get(other, index + 1)?;
if !self.equals(items.get(index), theirs)? {
return Ok(false);
}
}
Ok(true)
}
pub(crate) fn compare(&mut self, a: Value, b: Value) -> VmResult<Ordering> {
match (a, b) {
(Value::Int(x), Value::Int(y)) => Ok(x.cmp(&y)),
(Value::Obj(id), _) => self.obj_compare(id, b),
(Value::SString(_), _) => match (self.as_str(a), self.text_or_buffer(b)) {
(Some(x), Some(y)) => Ok((*x).cmp(y.as_slice())),
_ => Err(VmError::new(code::INVALID_COMPARISON)),
},
(_, Value::Obj(id)) => Ok(self.obj_compare(id, a)?.reverse()),
_ => Err(VmError::new(code::INVALID_COMPARISON)),
}
}
fn obj_compare(&mut self, id: ObjId, other: Value) -> VmResult<Ordering> {
match self.objects.get(id) {
Some(Object::Decimal(big)) => {
let order = self.big_compare(big, other);
self.big_operand(other);
order
}
Some(Object::String(_) | Object::StringBuilder(_)) => {
let text = self.text_or_buffer(Value::Obj(id)).unwrap_or_default();
match self.text_or_buffer(other) {
Some(other) => Ok(text.cmp(&other)),
None => Err(VmError::new(code::INVALID_COMPARISON)),
}
}
Some(Object::Date { dayno, daytime }) => match self.as_date(other) {
Some(other) => Ok((*dayno, *daytime).cmp(&other)),
None => Err(VmError::new(code::INVALID_COMPARISON)),
},
_ => Err(VmError::new(code::INVALID_COMPARISON)),
}
}
pub(crate) fn add(&mut self, a: Value, b: Value) -> VmResult<Option<Value>> {
match a {
Value::SString(_) => self.concat(a, b).map(Some),
Value::List(_) => self.list_append(a, b).map(Some),
Value::Obj(id) => match self.objects.get(id) {
Some(Object::Decimal(big)) => {
let big = big.clone();
self.big_arith(Arith::Add, &big, b).map(Some)
}
Some(Object::String(_)) => self.concat(a, b).map(Some),
Some(Object::FileName(_)) => self.file_name_add(id, b).map(Some),
Some(Object::Date { .. }) => self.date_add(id, b).map(Some),
Some(Object::List(_)) => self.list_append(a, b).map(Some),
Some(Object::Vector(items)) => {
let mut items = items.clone();
match self.list_like_len(b)? {
Some(len) => {
for index in 1..=len {
items.push(self.list_like_get(b, index)?);
}
}
None => items.push(b),
}
Ok(Some(Value::Obj(self.alloc_obj(Object::Vector(items)))))
}
_ => Ok(None),
},
Value::Int(n) => self.int_with_big(Arith::Add, n, b).map(Some),
_ => Ok(None),
}
}
fn int_with_big(&mut self, op: Arith, a: i32, b: Value) -> VmResult<Value> {
if self.as_big(b).is_none() {
return Err(VmError::new(code::NUM_VAL_REQD));
}
let a = crate::decimal::Big::from_int(a.into(), 10);
self.new_big(a.clone());
self.big_arith(op, &a, b)
}
pub(crate) fn sub(&mut self, a: Value, b: Value) -> VmResult<Option<Value>> {
match a {
Value::List(_) => self.list_remove(a, b).map(Some),
Value::Obj(id) => match self.objects.get(id) {
Some(Object::Decimal(big)) => {
let big = big.clone();
self.big_arith(Arith::Sub, &big, b).map(Some)
}
Some(Object::Date { .. }) => self.date_sub(id, b).map(Some),
Some(Object::List(_)) => self.list_remove(a, b).map(Some),
Some(Object::Vector(items)) => {
let items = items.clone();
let kept = self.without(&items, b)?;
let new = self.alloc_obj(Object::Vector(kept));
self.objects.set_vector_alloc(new, items.len());
Ok(Some(Value::Obj(new)))
}
_ => Ok(None),
},
Value::Int(n) => self.int_with_big(Arith::Sub, n, b).map(Some),
_ => Ok(None),
}
}
pub(crate) fn mul_div(&mut self, op: Arith, a: Value, b: Value) -> VmResult<Option<Value>> {
match a {
Value::Int(n) => self.int_with_big(op, n, b).map(Some),
_ => match self.as_big(a).cloned() {
Some(big) => self.big_arith(op, &big, b).map(Some),
None => Ok(None),
},
}
}
fn concat(&mut self, string: Value, other: Value) -> VmResult<Value> {
let left = self.require_str(string)?;
let (_, right) = self.text_raw(other)?;
if right.is_empty() || other.is_nil() {
return Ok(string);
}
if left.is_empty() && self.as_str(other).is_some() {
return Ok(other);
}
let mut text = Vec::with_capacity(left.len() + right.len());
text.extend_from_slice(&left);
text.extend_from_slice(&right);
self.try_new_string(text)
}
fn list_append(&mut self, list: Value, other: Value) -> VmResult<Value> {
let mut items = match self.as_list(list) {
Some(items) => items.to_vec(),
None => return Err(VmError::new(code::LIST_VAL_REQD)),
};
match self.list_like_len(other)? {
Some(len) => {
for index in 1..=len {
items.push(self.list_like_get(other, index)?);
}
}
None => items.push(other),
}
Ok(self.new_list(items))
}
fn list_remove(&mut self, list: Value, other: Value) -> VmResult<Value> {
let items = match self.as_list(list) {
Some(items) => items.to_vec(),
None => return Err(VmError::new(code::LIST_VAL_REQD)),
};
let kept = self.without(&items, other)?;
Ok(self.new_list(kept))
}
fn without(&mut self, items: &[Value], other: Value) -> VmResult<Vec<Value>> {
let remove: Vec<Value> = match self.list_like_len(other)? {
Some(len) => {
let mut values = Vec::with_capacity(len);
for index in 1..=len {
values.push(self.list_like_get(other, index)?);
}
values
}
None => vec![other],
};
let mut kept = Vec::with_capacity(items.len());
for &item in items {
let mut keep = true;
for &value in &remove {
if self.equals(value, item)? {
keep = false;
break;
}
}
if keep {
kept.push(item);
}
}
Ok(kept)
}
fn index_arg(&self, index: Value) -> VmResult<i32> {
match index {
Value::Int(n) => Ok(n),
other => match self.as_big(other) {
Some(big) => Self::big_to_int(big),
None => Err(VmError::new(code::NUM_VAL_REQD)),
},
}
}
pub(crate) fn index(&mut self, container: Value, index: Value) -> VmResult<Option<Value>> {
if let Some(items) = self.as_list(container) {
let n = self.index_arg(index)?;
if n < 1 || n as usize > items.len() {
return Err(VmError::new(code::INDEX_OUT_OF_RANGE));
}
return Ok(Some(items.get(n as usize - 1)));
}
let Value::Obj(id) = container else {
return Ok(None);
};
match self.objects.get(id) {
Some(Object::Vector(items) | Object::AnonFn(items)) => {
let n = self.index_arg(index)?;
if n < 1 || n as usize > items.len() {
return Err(VmError::new(code::INDEX_OUT_OF_RANGE));
}
Ok(Some(items[n as usize - 1]))
}
Some(Object::Map(_)) => self.table_index(id, index).map(Some),
Some(Object::StringBuilder(_)) => self.string_builder_index(id, index).map(Some),
Some(Object::ByteArray(_)) => {
let n = self.index_arg(index)?;
self.bytes_index(id, n).map(Some)
}
Some(Object::FrameRef(_)) => self.frame_ref_index(id, index).map(Some),
Some(Object::FrameDesc(_)) => self.stack_frame_index(id, index).map(Some),
Some(Object::CompiledCode(func)) => match index {
Value::Int(1) => Ok(Some(func.method_ctx)),
_ => Err(VmError::new(code::INDEX_OUT_OF_RANGE)),
},
Some(Object::Opaque { .. }) => Err(self.unimplemented_object(id, "indexing")),
_ => Ok(None),
}
}
pub(crate) fn set_index(
&mut self,
container: Value,
index: Value,
value: Value,
) -> VmResult<Option<Value>> {
if let Some(items) = self.as_list(container) {
let n = self.index_arg(index)?;
if n < 1 || n as usize > items.len() {
return Err(VmError::new(code::INDEX_OUT_OF_RANGE));
}
let mut items = items.to_vec();
items[n as usize - 1] = value;
return Ok(Some(self.new_list(items)));
}
let Value::Obj(id) = container else {
return Ok(None);
};
match self.objects.get_mut(id) {
Some(Object::Vector(items) | Object::AnonFn(items)) => {
let n = match index {
Value::Int(n) => n,
_ => return Err(VmError::new(code::NUM_VAL_REQD)),
};
if n < 1 {
return Err(VmError::new(code::INDEX_OUT_OF_RANGE));
}
if n as usize > items.len() {
items.resize(n as usize, Value::Nil);
}
items[n as usize - 1] = value;
self.objects.note_vector(id);
Ok(Some(container))
}
Some(Object::Map(_)) => {
self.table_set(id, index, value)?;
Ok(Some(container))
}
Some(Object::StringBuilder(_)) => {
self.string_builder_set_index(id, index, value)?;
Ok(Some(container))
}
Some(Object::ByteArray(_)) => {
let n = self.index_arg(index)?;
let byte = self.index_arg(value)?;
self.bytes_set_index(id, n, byte)?;
Ok(Some(container))
}
Some(Object::FrameRef(_)) => self.frame_ref_set_index(id, index, value).map(Some),
Some(Object::FrameDesc(_)) => self.stack_frame_set_index(id, index, value).map(Some),
Some(Object::Opaque { .. }) => Err(self.unimplemented_object(id, "indexing")),
_ => Ok(None),
}
}
pub(crate) fn unimplemented_object(&self, id: ObjId, what: &str) -> VmError {
let class = self.kind_of(id).map_or("unknown", |kind| kind.name());
VmError::unimplemented(format!("{what} for built-in class {class}"))
}
pub(crate) fn list_like_len(&mut self, value: Value) -> VmResult<Option<usize>> {
if let Some(items) = self.as_list(value) {
return Ok(Some(items.len()));
}
let Value::Obj(id) = value else {
return Ok(None);
};
match self.objects.get(id) {
Some(Object::Vector(items)) => return Ok(Some(items.len())),
Some(Object::Program(_)) => {}
_ => return Ok(None),
}
let (operator_idx, length) = (self.predef.operator_idx, self.predef.length);
if operator_idx == INVALID_PROP || length == INVALID_PROP {
return Ok(None);
}
if self.obj_get_prop(id, operator_idx, None)?.is_none() {
return Ok(None);
}
match self.prop_interface(id, length)? {
Some((0, _, _)) => {}
_ => return Ok(None),
}
let native = NativeCall::named("Object.ll_length");
match self.call_prop(value, length, 0, Some(native))? {
Value::Int(n) if n >= 0 => Ok(Some(n as usize)),
_ => Ok(None),
}
}
pub(crate) fn list_like_get(&mut self, value: Value, index: usize) -> VmResult<Value> {
let index_value = Value::Int(index as i32);
if let Some(found) = self.index(value, index_value)? {
return Ok(found);
}
match value {
Value::Obj(_) => {
self.push(index_value);
self.op_overload(
0,
None,
value,
self.predef.operator_idx,
1,
code::CANNOT_INDEX_TYPE,
Return::Native,
)?;
Ok(self.r0)
}
_ => Ok(Value::Nil),
}
}
pub(crate) fn text_bytes(&mut self, value: Value) -> VmResult<std::rc::Rc<[u8]>> {
Ok(self.text_raw(value)?.1)
}
pub(crate) fn text_raw(
&mut self,
value: Value,
) -> VmResult<(Option<Value>, std::rc::Rc<[u8]>)> {
let text: &[u8] = match value {
Value::Int(n) => return Ok((None, n.to_string().into_bytes().into())),
Value::Nil => b"nil",
Value::True => b"true",
_ => {
let (string, text) = self.text_made(value)?;
return Ok((Some(string), text));
}
};
Ok((None, text.into()))
}
pub(crate) fn text_of(&mut self, value: Value) -> VmResult<(Value, std::rc::Rc<[u8]>)> {
match self.text_raw(value)? {
(Some(string), text) => Ok((string, text)),
(None, text) => Ok((self.try_new_string(text.clone())?, text)),
}
}
pub(crate) fn text_cast(&mut self, value: Value) -> VmResult<(Value, std::rc::Rc<[u8]>)> {
self.nested(|vm| vm.text_cast_inner(value))
}
fn text_cast_inner(&mut self, value: Value) -> VmResult<(Value, std::rc::Rc<[u8]>)> {
match value {
Value::Nil => {
let text: std::rc::Rc<[u8]> = Vec::new().into();
Ok((self.try_new_string(text.clone())?, text))
}
Value::Int(_) | Value::True => self.text_of(value),
_ if self.anon_fn_len(value).is_some() => self.join_text(value, b","),
_ if self.list_like_len(value)?.is_some() => self.join_text(value, b","),
Value::SString(_) | Value::Obj(_) | Value::List(_) => self.text_of(value),
_ => Err(VmError::new(code::NO_STR_CONV)),
}
}
fn anon_fn_len(&self, value: Value) -> Option<usize> {
match value {
Value::Obj(id) => match self.objects.get(id) {
Some(Object::AnonFn(items)) => Some(items.len()),
_ => None,
},
_ => None,
}
}
pub(crate) fn join_text(
&mut self,
list: Value,
separator: &[u8],
) -> VmResult<(Value, std::rc::Rc<[u8]>)> {
let len = match self.anon_fn_len(list) {
Some(len) => len,
None => self.list_like_len(list)?.unwrap_or(0),
};
let result = self.try_new_string(Vec::new())?;
self.push(result);
let mut text = Vec::new();
let mut failed = None;
for index in 1..=len {
if index > 1 {
text.extend_from_slice(separator);
}
let piece = match self.list_like_get(list, index) {
Ok(Value::Nil) => Ok(Vec::new().into()),
Ok(item @ Value::Int(_)) => self.text_bytes(item),
Ok(item) if self.as_str(item).is_some() => self.text_bytes(item),
Ok(item) => self.text_cast(item).map(|(_, text)| text),
Err(error) => Err(error),
};
match piece {
Ok(piece) => text.extend_from_slice(&piece),
Err(error) => {
failed = Some(error);
break;
}
}
}
self.discard(1);
if let Some(error) = failed {
return Err(error);
}
if text.len() > crate::vm::MAX_STRING_LEN {
return Err(VmError::new(code::STR_TOO_LONG));
}
let text: std::rc::Rc<[u8]> = text.into();
if let Some(Object::String(stored)) = self.objects.get_mut(result.obj_id()) {
*stored = text.clone();
}
Ok((result, text))
}
fn text_made(&mut self, value: Value) -> VmResult<(Value, std::rc::Rc<[u8]>)> {
match value {
Value::SString(_) => {
let text = self.require_str(value)?;
Ok((value, text.to_vec().into()))
}
Value::Obj(id) => self.obj_to_text(id),
Value::List(_) => self.list_to_text(value),
Value::Prop(prop) => self.reflect_to_text(value, format!("property#{prop}")),
Value::FuncPtr(func) => self.reflect_to_text(value, format!("function#{func:x}")),
Value::Enum(n) => self.reflect_to_text(value, format!("enum#{}", n as i32)),
Value::BifPtr { set, index } => {
self.reflect_to_text(value, format!("builtin#{set}.{index}"))
}
_ => Err(VmError::new(code::NO_STR_CONV)),
}
}
fn obj_to_text(&mut self, id: ObjId) -> VmResult<(Value, std::rc::Rc<[u8]>)> {
let value = Value::Obj(id);
match self.objects.get(id) {
Some(Object::String(text)) => return Ok((value, text.clone())),
Some(Object::List(_) | Object::Vector(_) | Object::AnonFn(_)) => {
return self.list_to_text(value);
}
Some(Object::Decimal(big)) => {
let text: std::rc::Rc<[u8]> = big.to_text().into_bytes().into();
return Ok((self.try_new_string(text.clone())?, text));
}
Some(Object::ByteArray(bytes)) => {
let text: std::rc::Rc<[u8]> =
crate::bytearray::latin1_text(bytes).into_bytes().into();
return Ok((self.try_new_string(text.clone())?, text));
}
Some(Object::StringBuilder(_)) => {
let text: std::rc::Rc<[u8]> =
self.string_builder_text(id).unwrap_or_default().into();
return Ok((self.try_new_string(text.clone())?, text));
}
Some(Object::Date { .. }) => {
let text: std::rc::Rc<[u8]> = self.date_text(id).into_bytes().into();
return Ok((self.try_new_string(text.clone())?, text));
}
Some(Object::TimeZone(_)) => {
let name = self.zone_name(id).unwrap_or_default();
let text: std::rc::Rc<[u8]> = name.into_bytes().into();
return Ok((self.try_new_string(text.clone())?, text));
}
Some(Object::FileName(name)) => {
let text: std::rc::Rc<[u8]> = name.path.clone().into_bytes().into();
return Ok((self.try_new_string(text.clone())?, text));
}
Some(Object::Pattern { source, .. }) => {
let source = *source;
return self.text_of(source);
}
Some(Object::Opaque { .. }) => {
return Err(self.unimplemented_object(id, "conversion to text"));
}
_ => {}
}
if self.predef.obj_to_string != INVALID_PROP {
let result = self.call_prop(value, self.predef.obj_to_string, 0, None)?;
if let Some(text) = self.as_str(result) {
return Ok((result, text.to_vec().into()));
}
}
self.reflect_to_text(value, format!("object#{}", id.0))
}
fn reflect_to_text(
&mut self,
value: Value,
fallback: String,
) -> VmResult<(Value, std::rc::Rc<[u8]>)> {
let services = self.predef.reflection_services;
let prop = self.predef.reflection_val_to_symbol;
if services.is_valid() && prop != INVALID_PROP {
self.push(value);
let result = self.call_prop(Value::Obj(services), prop, 1, None)?;
if let Some(text) = self.as_str(result) {
return Ok((result, text.to_vec().into()));
}
}
let mut bytes = fallback.into_bytes();
bytes.push(0);
let text: std::rc::Rc<[u8]> = bytes.into();
Ok((self.try_new_string(text.clone())?, text))
}
fn list_to_text(&mut self, list: Value) -> VmResult<(Value, std::rc::Rc<[u8]>)> {
let len = match self.anon_fn_len(list) {
Some(len) => len,
None => self.list_like_len(list)?.unwrap_or(0),
};
let converted = self.new_list(vec![Value::Nil; len]);
self.push(converted);
let mut items = Vec::with_capacity(len);
let mut failed = None;
for index in 1..=len {
let item = match self.list_like_get(list, index) {
Ok(item) => self.text_raw(item),
Err(error) => Err(error),
};
match item {
Ok((Some(string @ Value::Obj(_)), _)) => items.push(string),
Ok((_, text)) => {
let string = self.try_new_string(text)?;
items.push(string);
}
Err(error) => {
failed = Some(error);
break;
}
}
if let Some(Object::List(stored)) = self.objects.get_mut(converted.obj_id()) {
*stored = items.clone().into();
}
}
let joined = match failed {
Some(error) => Err(error),
None => self.join_text(converted, b","),
};
self.discard(1);
joined
}
}