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
use crate::{machine::Machine, object::Object, opcodes::*, value::Value};
use std::any::Any;

#[derive(Debug)]
pub enum Function
{
    Virtual(VirtualFunction),
    Native(NativeFunction),
}

impl crate::object::ObjectAddon for Function {}

impl Object for Function
{
    fn as_any(&self) -> &dyn Any
    {
        self as &dyn Any
    }

    fn as_any_mut(&mut self) -> &mut dyn Any
    {
        self as &mut dyn Any
    }

    /// Get Object Id's(Used for GC) W.I.P
    fn get_children(&self) -> Vec<usize>
    {
        vec![]
    }

    fn load_at(&self, m: &mut Machine, _args: Vec<Value>, dest: usize)
    {
        let _this = _args[0];
        let val = if let Value::Object(id) = &_args[1] {
            m.pool.get(*id)
        } else {
            panic!("Exptected object")
        };

        let fname: &str = &val.to_string(m);

        match fname {
            "disassemble" => {
                let code = if let Function::Virtual(vf) = self {
                    vf.code.toString()
                } else {
                    "<native function>".to_string()
                };
                let obj = m.pool.allocate(Box::new(code));
                let code = vec![Instruction::LoadObject(1, obj), Instruction::Ret(1)];
                let func = Function::from(code);
                let obj = m.pool.allocate(Box::new(func));
                m.set(dest, Value::Object(obj));
            }
            f => panic!("Unknown field `{}`", f),
        }
    }

    /// Call object
    fn call(&self, m: &mut Machine, args: Vec<Value>, c_index: u8) -> Value
    {
        if c_index == 0 {
            let ret = match self {
                Function::Virtual(ref vf) => {
                    let func = vf.clone();

                    for i in 0..args.len() {
                        m.last_frame_mut().stack[i] = args[i];
                    }

                    m.run_code(func.code)
                }

                Function::Native(nv) => nv.invoke(m, args),
            };
            return ret;
        } else {
            panic!("Function expect CALL idnex,found `{}`", c_index);
        }
    }
}

#[derive(Clone, Debug)]
pub struct VirtualFunction
{
    pub code: Vec<Instruction>,
    pub argc: usize,
}

impl Function
{
    pub fn from_instructions(code: Vec<Instruction>, args: usize) -> Function
    {
        Function::Virtual(VirtualFunction {
            code: code,
            argc: args,
        })
    }

    pub fn from_native(f: NativeFunction) -> Function
    {
        Function::Native(f)
    }
}

impl From<Vec<Instruction>> for Function
{
    fn from(f: Vec<Instruction>) -> Function
    {
        Function::Virtual(VirtualFunction { code: f, argc: 0 })
    }
}

pub struct NativeFunction(Box<dyn Fn(&mut Machine, Vec<Value>) -> Value + Send>);

impl NativeFunction
{
    pub fn invoke(&self, m: &mut Machine, args: Vec<Value>) -> Value
    {
        self.0(m, args)
    }
}

use std::fmt;

impl fmt::Debug for NativeFunction
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
    {
        write!(f, "<native function>")
    }
}