forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjfunction.rs
More file actions
174 lines (154 loc) · 4.62 KB
/
objfunction.rs
File metadata and controls
174 lines (154 loc) · 4.62 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use crate::frame::Scope;
use crate::pyobject::{
AttributeProtocol, IdProtocol, PyContext, PyFuncArgs, PyObjectRef, PyResult, PyValue,
TypeProtocol,
};
use crate::vm::VirtualMachine;
#[derive(Debug)]
pub struct PyFunction {
// TODO: these shouldn't be public
pub code: PyObjectRef,
pub scope: Scope,
pub defaults: PyObjectRef,
}
impl PyFunction {
pub fn new(code: PyObjectRef, scope: Scope, defaults: PyObjectRef) -> Self {
PyFunction {
code,
scope,
defaults,
}
}
}
impl PyValue for PyFunction {
fn required_type(ctx: &PyContext) -> PyObjectRef {
ctx.function_type()
}
}
#[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 required_type(ctx: &PyContext) -> PyObjectRef {
ctx.bound_method_type()
}
}
pub fn init(context: &PyContext) {
let function_type = &context.function_type;
context.set_attr(&function_type, "__get__", context.new_rustfunc(bind_method));
context.set_attr(
&function_type,
"__code__",
context.new_property(function_code),
);
let builtin_function_or_method_type = &context.builtin_function_or_method_type;
context.set_attr(
&builtin_function_or_method_type,
"__get__",
context.new_rustfunc(bind_method),
);
let classmethod_type = &context.classmethod_type;
context.set_attr(
&classmethod_type,
"__get__",
context.new_rustfunc(classmethod_get),
);
context.set_attr(
&classmethod_type,
"__new__",
context.new_rustfunc(classmethod_new),
);
let staticmethod_type = &context.staticmethod_type;
context.set_attr(
staticmethod_type,
"__get__",
context.new_rustfunc(staticmethod_get),
);
context.set_attr(
staticmethod_type,
"__new__",
context.new_rustfunc(staticmethod_new),
);
}
fn bind_method(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
required = [(function, None), (obj, None), (cls, None)]
);
if obj.is(&vm.get_none()) && !cls.is(&obj.typ()) {
Ok(function.clone())
} else {
Ok(vm.ctx.new_bound_method(function.clone(), obj.clone()))
}
}
fn function_code(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
match args.args[0].payload() {
Some(PyFunction { ref code, .. }) => Ok(code.clone()),
None => Err(vm.new_type_error("no code".to_string())),
}
}
// Classmethod type methods:
fn classmethod_get(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
trace!("classmethod.__get__ {:?}", args.args);
arg_check!(
vm,
args,
required = [
(cls, Some(vm.ctx.classmethod_type())),
(_inst, None),
(owner, None)
]
);
match cls.get_attr("function") {
Some(function) => {
let py_obj = owner.clone();
let py_method = vm.ctx.new_bound_method(function, py_obj);
Ok(py_method)
}
None => Err(vm.new_attribute_error(
"Attribute Error: classmethod must have 'function' attribute".to_string(),
)),
}
}
fn classmethod_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
trace!("classmethod.__new__ {:?}", args.args);
arg_check!(vm, args, required = [(cls, None), (callable, None)]);
let py_obj = vm.ctx.new_instance(cls.clone(), None);
vm.ctx.set_attr(&py_obj, "function", callable.clone());
Ok(py_obj)
}
// `staticmethod` methods.
fn staticmethod_get(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
trace!("staticmethod.__get__ {:?}", args.args);
arg_check!(
vm,
args,
required = [
(cls, Some(vm.ctx.staticmethod_type())),
(_inst, None),
(_owner, None)
]
);
match cls.get_attr("function") {
Some(function) => Ok(function),
None => Err(vm.new_attribute_error(
"Attribute Error: staticmethod must have 'function' attribute".to_string(),
)),
}
}
fn staticmethod_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
trace!("staticmethod.__new__ {:?}", args.args);
arg_check!(vm, args, required = [(cls, None), (callable, None)]);
let py_obj = vm.ctx.new_instance(cls.clone(), None);
vm.ctx.set_attr(&py_obj, "function", callable.clone());
Ok(py_obj)
}