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
use crate::function::{Args, KwArgs};
use crate::obj::objcode::PyCodeRef;
use crate::obj::objdict::PyDictRef;
use crate::obj::objtuple::PyTupleRef;
use crate::obj::objtype::PyClassRef;
use crate::pyobject::{IdProtocol, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol};
use crate::scope::Scope;
use crate::vm::VirtualMachine;

pub type PyFunctionRef = PyRef<PyFunction>;

#[derive(Debug)]
pub struct PyFunction {
    // TODO: these shouldn't be public
    pub code: PyCodeRef,
    pub scope: Scope,
    pub defaults: Option<PyTupleRef>,
    pub kw_only_defaults: Option<PyDictRef>,
}

impl PyFunction {
    pub fn new(
        code: PyCodeRef,
        scope: Scope,
        defaults: Option<PyTupleRef>,
        kw_only_defaults: Option<PyDictRef>,
    ) -> Self {
        PyFunction {
            code,
            scope,
            defaults,
            kw_only_defaults,
        }
    }
}

impl PyValue for PyFunction {
    fn class(vm: &VirtualMachine) -> PyClassRef {
        vm.ctx.function_type()
    }
}

impl PyFunctionRef {
    fn call(self, args: Args, kwargs: KwArgs, vm: &VirtualMachine) -> PyResult {
        vm.invoke(&self.into_object(), (&args, &kwargs))
    }

    fn code(self, _vm: &VirtualMachine) -> PyCodeRef {
        self.code.clone()
    }

    fn defaults(self, _vm: &VirtualMachine) -> Option<PyTupleRef> {
        self.defaults.clone()
    }

    fn kwdefaults(self, _vm: &VirtualMachine) -> Option<PyDictRef> {
        self.kw_only_defaults.clone()
    }
}

#[derive(Debug)]
pub struct PyMethod {
    // TODO: these shouldn't be public
    pub object: PyObjectRef,
    pub function: PyObjectRef,
}

impl PyMethod {
    pub fn new(object: PyObjectRef, function: PyObjectRef) -> Self {
        PyMethod { object, function }
    }
}

impl PyValue for PyMethod {
    fn class(vm: &VirtualMachine) -> PyClassRef {
        vm.ctx.bound_method_type()
    }
}

pub fn init(context: &PyContext) {
    let function_type = &context.types.function_type;
    extend_class!(context, function_type, {
        "__get__" => context.new_rustfunc(bind_method),
        "__call__" => context.new_rustfunc(PyFunctionRef::call),
        "__code__" => context.new_property(PyFunctionRef::code),
        "__defaults__" => context.new_property(PyFunctionRef::defaults),
        "__kwdefaults__" => context.new_property(PyFunctionRef::kwdefaults),
    });

    let builtin_function_or_method_type = &context.types.builtin_function_or_method_type;
    extend_class!(context, builtin_function_or_method_type, {
        "__get__" => context.new_rustfunc(bind_method)
    });
}

fn bind_method(
    function: PyObjectRef,
    obj: PyObjectRef,
    cls: PyObjectRef,
    vm: &VirtualMachine,
) -> PyResult {
    if obj.is(&vm.get_none()) && !cls.is(&obj.class()) {
        Ok(function)
    } else {
        Ok(vm.ctx.new_bound_method(function, obj))
    }
}