jazz_vm/
object.rs

1use std::any::Any;
2use std::cmp::Ordering;
3use errors;
4use executor::{ExecutorImpl};
5use object_pool::ObjectPool;
6use value::{Value, ValueContext};
7
8/// Выделеный аллокатором объект.
9///
10/// Это основная абстракция в системе типов виртуальной машины.
11/// После создания объект должен быть привязан к пулу объектов
12/// для использования в виртуальной среде.
13///
14/// Если требуется инициализация до того, как объект закреплен
15/// для пула объектов, это должно быть сделано в методе `initialize`,
16///, который принимает изменяемую ссылку на пул объектов и делает
17/// возможным создавать внутренние поля и т.д.
18pub trait Object: Send {
19    fn finalize(&self, _pool: &mut ObjectPool) {}
20
21    // before allocating on the object pool...
22    fn initialize(&mut self, _pool: &mut ObjectPool) {}
23
24    fn call(&self, _executor: &mut ExecutorImpl) -> Value {
25        panic!(errors::VMError::from(errors::RuntimeError::new("Not callable")));
26    }
27    fn call_field(&self, field_name: &str, executor: &mut ExecutorImpl) -> Value {
28        let field = self.must_get_field(executor.get_object_pool(), field_name);
29        let obj = ValueContext::new(&field, executor.get_object_pool()).as_object();
30        obj.call(executor)
31    }
32    fn get_field(&self, _pool: &ObjectPool, _name: &str) -> Option<Value> {
33        None
34    }
35    fn set_field(&self, _name: &str, _value_ref: Value) {
36        panic!(errors::VMError::from(errors::RuntimeError::new("Cannot set field")));
37    }
38    fn must_get_field(&self, pool: &ObjectPool, name: &str) -> Value {
39        match self.get_field(pool, name) {
40            Some(v) => v,
41            None => panic!(errors::VMError::from(errors::FieldNotFoundError::from_field_name(name)))
42        }
43    }
44    fn has_const_field(&self, _pool: &ObjectPool, _name: &str) -> bool {
45        false
46    }
47    fn compare(&self, _other: &ValueContext) -> Option<Ordering> {
48        None
49    }
50    fn test_eq(&self, _other: &ValueContext) -> bool {
51        false
52    }
53    fn typename(&self) -> &str {
54        "object"
55    }
56    fn to_i64(&self) -> i64 {
57        panic!(errors::VMError::from(errors::RuntimeError::new("Cannot cast to i64")));
58    }
59    fn to_f64(&self) -> f64 {
60        panic!(errors::VMError::from(errors::RuntimeError::new("Cannot cast to f64")));
61    }
62    fn to_str(&self) -> &str {
63        panic!(errors::VMError::from(errors::RuntimeError::new("Cannot cast to str")));
64    }
65    fn to_string(&self) -> String {
66        self.to_str().to_string()
67    }
68    fn to_bool(&self) -> bool {
69        panic!(errors::VMError::from(errors::RuntimeError::new("Cannot cast to bool")));
70    }
71    fn get_children(&self) -> Vec<usize>;
72    fn as_any(&self) -> &Any;
73    fn as_any_mut(&mut self) -> &mut Any;
74}