use gc::Gc;
use js::object::{ObjectData, Property};
use js::value::{to_value, ResultValue, Value, ValueData};
use std::collections::HashMap;
use syntax::ast::expr::Expr;
pub type NativeFunctionData = fn(Value, Value, Vec<Value>) -> ResultValue;
#[derive(Trace, Finalize, Debug, Clone)]
pub enum Function {
NativeFunc(NativeFunction),
RegularFunc(RegularFunction),
}
#[derive(Trace, Finalize, Debug, Clone)]
pub struct RegularFunction {
pub object: ObjectData,
pub expr: Expr,
pub args: Vec<String>,
}
impl RegularFunction {
pub fn new(expr: Expr, args: Vec<String>) -> RegularFunction {
let mut obj = HashMap::new();
obj.insert(
"arguments".to_string(),
Property::new(Gc::new(ValueData::Integer(args.len() as i32))),
);
RegularFunction {
object: obj,
expr: expr,
args: args,
}
}
}
#[derive(Trace, Finalize, Debug, Clone)]
pub struct NativeFunction {
pub object: ObjectData,
pub data: NativeFunctionData,
}
impl NativeFunction {
pub fn new(data: NativeFunctionData) -> NativeFunction {
let obj = HashMap::new();
NativeFunction {
object: obj,
data: data,
}
}
}
pub fn _create() -> Value {
let function: ObjectData = HashMap::new();
to_value(function)
}
pub fn init(global: Value) {
let global_ptr = global;
global_ptr.set_field_slice("Function", _create());
}