1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
pub mod array; pub mod dynamic_object; pub mod typed_array; use std::any::Any; use object::Object; use function::Function; use value::{Value, ValueContext}; use executor::ExecutorImpl; use errors::{VMError, FieldNotFoundError}; use generic_arithmetic; use self::typed_array::TypedArray; use self::typed_array::TypedArrayElement; pub struct BuiltinObject { } impl BuiltinObject { pub fn new() -> BuiltinObject { BuiltinObject {} } } impl Object for BuiltinObject { fn get_children(&self) -> Vec<usize> { Vec::new() } fn as_any(&self) -> &Any { self as &Any } fn as_any_mut(&mut self) -> &mut Any { self as &mut Any } fn call_field(&self, name: &str, executor: &mut ExecutorImpl) -> Value { match name { "new_array" => { let array_obj: Box<Object> = Box::new(array::Array::new()); Value::Object( executor.get_object_pool_mut().allocate(array_obj) ) }, "new_dynamic" => { let prototype = match executor.get_current_frame().must_get_argument(0) { Value::Object(id) => Some(id), Value::Null => None, _ => panic!(VMError::from("Invalid prototype object")) }; Value::Object(executor.get_object_pool_mut().allocate( Box::new(dynamic_object::DynamicObject::new(prototype)) )) }, "freeze_dynamic" => { let target_id = match executor.get_current_frame().must_get_argument(0) { Value::Object(id) => id, _ => panic!(VMError::from("Invalid target object")) }; let target: &dynamic_object::DynamicObject = executor.get_object_pool().must_get_direct_typed(target_id); target.freeze(); Value::Null }, "optimize" => { let target_id = match executor.get_current_frame().must_get_argument(0) { Value::Object(id) => id, _ => panic!(VMError::from("Invalid target object")) }; let target = executor.get_object_pool().must_get_typed::<Function>(target_id); target.dynamic_optimize(executor.get_object_pool_mut()); Value::Null }, "new_typed_array" => { let type_name = ValueContext::new( &executor.get_current_frame().must_get_argument(0), executor.get_object_pool() ).to_str().to_string(); let size = ValueContext::new( &executor.get_current_frame().must_get_argument(1), executor.get_object_pool() ).to_i64() as usize; let default_value = executor.get_current_frame().must_get_argument(2); let obj_id = executor.get_object_pool_mut().allocate(match type_name.as_str() { "i8" => Box::new(TypedArray::new( i8::must_from_value(default_value), size )), "u8" => Box::new(TypedArray::new( u8::must_from_value(default_value), size )), "i16" => Box::new(TypedArray::new( i16::must_from_value(default_value), size )), "u16" => Box::new(TypedArray::new( u16::must_from_value(default_value), size )), "i32" => Box::new(TypedArray::new( i32::must_from_value(default_value), size )), "u32" => Box::new(TypedArray::new( u32::must_from_value(default_value), size )), "i64" => Box::new(TypedArray::new( i64::must_from_value(default_value), size )), "u64" => Box::new(TypedArray::new( u64::must_from_value(default_value), size )), _ => panic!(VMError::from("Unknown type")) }); Value::Object(obj_id) }, "add" => { let (left, right) = (executor.get_current_frame().must_get_argument(0), executor.get_current_frame().must_get_argument(1)); generic_arithmetic::exec_add(executor, left, right) }, "sub" => { let (left, right) = (executor.get_current_frame().must_get_argument(0), executor.get_current_frame().must_get_argument(1)); generic_arithmetic::exec_sub(executor, left, right) }, "mul" => { let (left, right) = (executor.get_current_frame().must_get_argument(0), executor.get_current_frame().must_get_argument(1)); generic_arithmetic::exec_mul(executor, left, right) }, "div" => { let (left, right) = (executor.get_current_frame().must_get_argument(0), executor.get_current_frame().must_get_argument(1)); generic_arithmetic::exec_div(executor, left, right) }, "mod" => { let (left, right) = (executor.get_current_frame().must_get_argument(0), executor.get_current_frame().must_get_argument(1)); generic_arithmetic::exec_mod(executor, left, right) }, "pow" => { let (left, right) = (executor.get_current_frame().must_get_argument(0), executor.get_current_frame().must_get_argument(1)); generic_arithmetic::exec_pow(executor, left, right) }, _ => panic!(VMError::from(FieldNotFoundError::from_field_name(name))) } } }