◐ Shell
clean mode source ↗

objfunction.rs.html -- source

  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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
use super::objcode::PyCodeRef;
use super::objdict::PyDictRef;
use super::objstr::PyStringRef;
use super::objtuple::PyTupleRef;
use super::objtype::PyClassRef;
use crate::bytecode;
use crate::frame::Frame;
use crate::function::{OptionalArg, PyFuncArgs};
use crate::obj::objcoroutine::PyCoroutine;
use crate::obj::objgenerator::PyGenerator;
use crate::pyobject::{
    IdProtocol, ItemProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue,
    TypeProtocol,
};
use crate::scope::Scope;
use crate::slots::{SlotCall, SlotDescriptor};
use crate::vm::VirtualMachine;

pub type PyFunctionRef = PyRef<PyFunction>;

#[pyclass]
#[derive(Debug)]
pub struct PyFunction {
    code: PyCodeRef,
    scope: Scope,
    defaults: Option<PyTupleRef>,
    kw_only_defaults: Option<PyDictRef>,
}

impl SlotDescriptor for PyFunction {
    fn descr_get(
        vm: &VirtualMachine,
        zelf: PyObjectRef,
        obj: Option<PyObjectRef>,
        cls: OptionalArg<PyObjectRef>,
    ) -> PyResult {
        let (zelf, obj) = Self::_unwrap(zelf, obj, vm)?;
        if obj.is(&vm.get_none()) && !Self::_cls_is(&cls, &obj.class()) {
            Ok(zelf.into_object())
        } else {
            Ok(vm.ctx.new_bound_method(zelf.into_object(), obj))
        }
    }
}

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,
        }
    }

    pub fn scope(&self) -> &Scope {
        &self.scope
    }

    fn fill_locals_from_args(
        &self,
        code_object: &bytecode::CodeObject,
        locals: &PyDictRef,
        func_args: PyFuncArgs,
        vm: &VirtualMachine,
    ) -> PyResult<()> {
        let nargs = func_args.args.len();
        let nexpected_args = code_object.arg_names.len();

        
        
        
        
        

        let n = if nargs > nexpected_args {
            nexpected_args
        } else {
            nargs
        };

        
        for i in 0..n {
            let arg_name = &code_object.arg_names[i];
            let arg = &func_args.args[i];
            locals.set_item(arg_name, arg.clone(), vm)?;
        }

        
        match code_object.varargs {
            bytecode::Varargs::Named(ref vararg_name) => {
                let mut last_args = vec![];
                for i in n..nargs {
                    let arg = &func_args.args[i];
                    last_args.push(arg.clone());
                }
                let vararg_value = vm.ctx.new_tuple(last_args);

                locals.set_item(vararg_name, vararg_value, vm)?;
            }
            bytecode::Varargs::Unnamed | bytecode::Varargs::None => {
                
                if nargs > nexpected_args {
                    return Err(vm.new_type_error(format!(
                        "Expected {} arguments (got: {})",
                        nexpected_args, nargs
                    )));
                }
            }
        }

        
        let kwargs = match code_object.varkeywords {
            bytecode::Varargs::Named(ref kwargs_name) => {
                let d = vm.ctx.new_dict();
                locals.set_item(kwargs_name, d.as_object().clone(), vm)?;
                Some(d)
            }
            bytecode::Varargs::Unnamed => Some(vm.ctx.new_dict()),
            bytecode::Varargs::None => None,
        };

        
        for (name, value) in func_args.kwargs {
            
            if code_object.arg_names.contains(&name) || code_object.kwonlyarg_names.contains(&name)
            {
                if locals.contains_key(&name, vm) {
                    return Err(
                        vm.new_type_error(format!("Got multiple values for argument '{}'", name))
                    );
                }

                locals.set_item(&name, value, vm)?;
            } else if let Some(d) = &kwargs {
                d.set_item(&name, value, vm)?;
            } else {
                return Err(
                    vm.new_type_error(format!("Got an unexpected keyword argument '{}'", name))
                );
            }
        }

        
        
        if nargs < nexpected_args {
            let num_defaults_available = self.defaults.as_ref().map_or(0, |d| d.as_slice().len());

            
            
            let required_args = nexpected_args - num_defaults_available;
            let mut missing = vec![];
            for i in 0..required_args {
                let variable_name = &code_object.arg_names[i];
                if !locals.contains_key(variable_name, vm) {
                    missing.push(variable_name)
                }
            }
            if !missing.is_empty() {
                return Err(vm.new_type_error(format!(
                    "Missing {} required positional arguments: {:?}",
                    missing.len(),
                    missing
                )));
            }
            if let Some(defaults) = &self.defaults {
                let defaults = defaults.as_slice();
                
                
                for (default_index, i) in (required_args..nexpected_args).enumerate() {
                    let arg_name = &code_object.arg_names[i];
                    if !locals.contains_key(arg_name, vm) {
                        locals.set_item(arg_name, defaults[default_index].clone(), vm)?;
                    }
                }
            }
        };

        
        for arg_name in &code_object.kwonlyarg_names {
            if !locals.contains_key(arg_name, vm) {
                if let Some(kw_only_defaults) = &self.kw_only_defaults {
                    if let Some(default) = kw_only_defaults.get_item_option(arg_name, vm)? {
                        locals.set_item(arg_name, default, vm)?;
                        continue;
                    }
                }

                
                return Err(
                    vm.new_type_error(format!("Missing required kw only argument: '{}'", arg_name))
                );
            }
        }

        Ok(())
    }

    pub fn invoke_with_scope(
        &self,
        func_args: PyFuncArgs,
        scope: &Scope,
        vm: &VirtualMachine,
    ) -> PyResult {
        let code = &self.code;

        let scope = if self.code.flags.contains(bytecode::CodeFlags::NEW_LOCALS) {
            scope.new_child_scope(&vm.ctx)
        } else {
            scope.clone()
        };

        self.fill_locals_from_args(&code, &scope.get_locals(), func_args, vm)?;

        
        let frame = Frame::new(code.clone(), scope).into_ref(vm);

        
        if code.flags.contains(bytecode::CodeFlags::IS_GENERATOR) {
            Ok(PyGenerator::new(frame, vm).into_object())
        } else if code.flags.contains(bytecode::CodeFlags::IS_COROUTINE) {
            Ok(PyCoroutine::new(frame, vm).into_object())
        } else {
            vm.run_frame_full(frame)
        }
    }

    pub fn invoke(&self, func_args: PyFuncArgs, vm: &VirtualMachine) -> PyResult {
        self.invoke_with_scope(func_args, &self.scope, vm)
    }
}

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

#[pyimpl(with(SlotDescriptor))]
impl PyFunction {
    #[pyslot]
    #[pymethod(magic)]
    fn call(&self, args: PyFuncArgs, vm: &VirtualMachine) -> PyResult {
        self.invoke(args, vm)
    }

    #[pyproperty(magic)]
    fn code(&self) -> PyCodeRef {
        self.code.clone()
    }

    #[pyproperty(magic)]
    fn defaults(&self) -> Option<PyTupleRef> {
        self.defaults.clone()
    }

    #[pyproperty(magic)]
    fn kwdefaults(&self) -> Option<PyDictRef> {
        self.kw_only_defaults.clone()
    }
}

#[pyclass]
#[derive(Debug)]
pub struct PyBoundMethod {
    
    pub object: PyObjectRef,
    pub function: PyObjectRef,
}

impl SlotCall for PyBoundMethod {
    fn call(&self, args: PyFuncArgs, vm: &VirtualMachine) -> PyResult {
        let args = args.insert(self.object.clone());
        vm.invoke(&self.function, args)
    }
}

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

#[pyimpl(with(SlotCall))]
impl PyBoundMethod {
    #[pymethod(magic)]
    fn getattribute(&self, name: PyStringRef, vm: &VirtualMachine) -> PyResult {
        vm.get_attribute(self.function.clone(), name)
    }
}

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

pub fn init(context: &PyContext) {
    let function_type = &context.types.function_type;
    PyFunction::extend_class(context, function_type);

    let method_type = &context.types.bound_method_type;
    PyBoundMethod::extend_class(context, method_type);
}