forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvm.rs
More file actions
631 lines (552 loc) · 21 KB
/
vm.rs
File metadata and controls
631 lines (552 loc) · 21 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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
//! Implement virtual machine to run instructions.
//!
//! See also:
//! https://github.com/ProgVal/pythonvm-rust/blob/master/src/processor/mod.rs
//!
extern crate rustpython_parser;
use std::collections::hash_map::HashMap;
use super::builtins;
use super::bytecode;
use super::frame::Frame;
use super::obj::objcode::copy_code;
use super::obj::objgenerator;
use super::obj::objiter;
use super::obj::objsequence;
use super::obj::objstr;
use super::obj::objtype;
use super::pyobject::{
AttributeProtocol, DictProtocol, PyContext, PyFuncArgs, PyObjectKind, PyObjectRef, PyResult,
TypeProtocol,
};
use super::stdlib;
use super::sysmodule;
// use objects::objects;
// Objects are live when they are on stack, or referenced by a name (for now)
/// Top level container of a python virtual machine. In theory you could
/// create more instances of this struct and have them operate fully isolated.
pub struct VirtualMachine {
pub builtins: PyObjectRef,
pub sys_module: PyObjectRef,
pub stdlib_inits: HashMap<String, stdlib::StdlibInitFunc>,
pub ctx: PyContext,
pub current_frame: Option<PyObjectRef>,
}
impl VirtualMachine {
/// Create a new `VirtualMachine` structure.
pub fn new() -> VirtualMachine {
let ctx = PyContext::new();
// Hard-core modules:
let builtins = builtins::make_module(&ctx);
let sysmod = sysmodule::mk_module(&ctx);
// Add builtins as builtins module:
let modules = sysmod.get_attr("modules").unwrap();
ctx.set_item(&modules, "builtins", builtins.clone());
let stdlib_inits = stdlib::get_module_inits();
VirtualMachine {
builtins: builtins,
sys_module: sysmod,
stdlib_inits,
ctx: ctx,
current_frame: None,
}
}
pub fn run_code_obj(&mut self, code: PyObjectRef, scope: PyObjectRef) -> PyResult {
let mut frame = Frame::new(code, scope);
frame.run_frame_full(self)
}
/// Create a new python string object.
pub fn new_str(&self, s: String) -> PyObjectRef {
self.ctx.new_str(s)
}
/// Create a new python bool object.
pub fn new_bool(&self, b: bool) -> PyObjectRef {
self.ctx.new_bool(b)
}
pub fn new_dict(&self) -> PyObjectRef {
self.ctx.new_dict()
}
pub fn new_exception(&mut self, exc_type: PyObjectRef, msg: String) -> PyObjectRef {
// TODO: maybe there is a clearer way to create an instance:
info!("New exception created: {}", msg);
let pymsg = self.new_str(msg);
let args: Vec<PyObjectRef> = vec![pymsg];
let args = PyFuncArgs {
args: args,
kwargs: vec![],
};
// Call function:
let exception = self.invoke(exc_type, args).unwrap();
exception
}
pub fn new_type_error(&mut self, msg: String) -> PyObjectRef {
let type_error = self.ctx.exceptions.type_error.clone();
self.new_exception(type_error, msg)
}
/// Create a new python ValueError object. Useful for raising errors from
/// python functions implemented in rust.
pub fn new_value_error(&mut self, msg: String) -> PyObjectRef {
let value_error = self.ctx.exceptions.value_error.clone();
self.new_exception(value_error, msg)
}
pub fn new_key_error(&mut self, msg: String) -> PyObjectRef {
let key_error = self.ctx.exceptions.key_error.clone();
self.new_exception(key_error, msg)
}
pub fn new_index_error(&mut self, msg: String) -> PyObjectRef {
let index_error = self.ctx.exceptions.index_error.clone();
self.new_exception(index_error, msg)
}
pub fn new_not_implemented_error(&mut self, msg: String) -> PyObjectRef {
let value_error = self.ctx.exceptions.not_implemented_error.clone();
self.new_exception(value_error, msg)
}
pub fn new_scope(&mut self, parent_scope: Option<PyObjectRef>) -> PyObjectRef {
// let parent_scope = self.current_frame_mut().locals.clone();
self.ctx.new_scope(parent_scope)
}
pub fn get_none(&self) -> PyObjectRef {
self.ctx.none()
}
pub fn get_type(&self) -> PyObjectRef {
self.ctx.type_type()
}
pub fn get_object(&self) -> PyObjectRef {
self.ctx.object()
}
pub fn get_locals(&self) -> PyObjectRef {
// let scope = &self.frames.last().unwrap().locals;
// scope.clone()
// TODO: fix this!
self.get_none()
/*
match (*scope).kind {
PyObjectKind::Scope { scope } => { scope.locals.clone() },
_ => { panic!("Should be scope") },
} // .clone()
*/
}
pub fn context(&self) -> &PyContext {
&self.ctx
}
pub fn get_builtin_scope(&mut self) -> PyObjectRef {
let a2 = &*self.builtins.borrow();
match a2.kind {
PyObjectKind::Module { name: _, ref dict } => dict.clone(),
_ => {
panic!("OMG");
}
}
}
// Container of the virtual machine state:
pub fn to_str(&mut self, obj: &PyObjectRef) -> PyResult {
self.call_method(&obj, "__str__", vec![])
}
pub fn to_pystr(&mut self, obj: &PyObjectRef) -> Result<String, PyObjectRef> {
let py_str_obj = self.to_str(obj)?;
Ok(objstr::get_value(&py_str_obj))
}
pub fn to_repr(&mut self, obj: &PyObjectRef) -> PyResult {
self.call_method(obj, "__repr__", vec![])
}
pub fn call_get_descriptor(&mut self, attr: PyObjectRef, obj: PyObjectRef) -> PyResult {
let attr_class = attr.typ();
if let Some(descriptor) = attr_class.get_attr("__get__") {
let cls = obj.typ();
self.invoke(
descriptor,
PyFuncArgs {
args: vec![attr, obj.clone(), cls],
kwargs: vec![],
},
)
} else {
Ok(attr)
}
}
pub fn call_method(
&mut self,
obj: &PyObjectRef,
method_name: &str,
args: Vec<PyObjectRef>,
) -> PyResult {
self.call_method_pyargs(
obj,
method_name,
PyFuncArgs {
args: args,
kwargs: vec![],
},
)
}
pub fn call_method_pyargs(
&mut self,
obj: &PyObjectRef,
method_name: &str,
args: PyFuncArgs,
) -> PyResult {
// This is only used in the vm for magic methods, which use a greatly simplified attribute lookup.
let cls = obj.typ();
match cls.get_attr(method_name) {
Some(func) => {
trace!(
"vm.call_method {:?} {:?} {:?} -> {:?}",
obj,
cls,
method_name,
func
);
let wrapped = self.call_get_descriptor(func, obj.clone())?;
self.invoke(wrapped, args)
}
None => Err(self.new_type_error(format!("Unsupported method: {}", method_name))),
}
}
pub fn invoke(&mut self, func_ref: PyObjectRef, args: PyFuncArgs) -> PyResult {
trace!("Invoke: {:?} {:?}", func_ref, args);
match func_ref.borrow().kind {
PyObjectKind::RustFunction { ref function } => function(self, args),
PyObjectKind::Function {
ref code,
ref scope,
ref defaults,
} => self.invoke_python_function(code, scope, defaults, args),
PyObjectKind::Class {
name: _,
dict: _,
mro: _,
} => self.call_method_pyargs(&func_ref, "__call__", args),
PyObjectKind::BoundMethod {
ref function,
ref object,
} => self.invoke(function.clone(), args.insert(object.clone())),
PyObjectKind::Instance { .. } => self.call_method_pyargs(&func_ref, "__call__", args),
ref kind => {
// TODO: is it safe to just invoke __call__ otherwise?
trace!("invoke __call__ for: {:?}", kind);
self.call_method_pyargs(&func_ref, "__call__", args)
}
}
}
fn invoke_python_function(
&mut self,
code: &PyObjectRef,
scope: &PyObjectRef,
defaults: &PyObjectRef,
args: PyFuncArgs,
) -> PyResult {
let code_object = copy_code(code);
let scope = self.ctx.new_scope(Some(scope.clone()));
self.fill_scope_from_args(&code_object, &scope, args, defaults)?;
// Construct frame:
let mut frame = Frame::new(code.clone(), scope);
// If we have a generator, create a new generator
if code_object.is_generator {
objgenerator::new_generator(self, frame)
} else {
frame.run_frame_full(self)
}
}
fn fill_scope_from_args(
&mut self,
code_object: &bytecode::CodeObject,
scope: &PyObjectRef,
args: PyFuncArgs,
defaults: &PyObjectRef,
) -> Result<(), PyObjectRef> {
let nargs = args.args.len();
let nexpected_args = code_object.arg_names.len();
// This parses the arguments from args and kwargs into
// the proper variables keeping into account default values
// and starargs and kwargs.
// See also: PyEval_EvalCodeWithName in cpython:
// https://github.com/python/cpython/blob/master/Python/ceval.c#L3681
let n = if nargs > nexpected_args {
nexpected_args
} else {
nargs
};
// Copy positional arguments into local variables
for i in 0..n {
let arg_name = &code_object.arg_names[i];
let arg = &args.args[i];
self.ctx.set_item(scope, arg_name, arg.clone());
}
// Pack other positional arguments in to *args:
if let Some(vararg) = &code_object.varargs {
let mut last_args = vec![];
for i in n..nargs {
let arg = &args.args[i];
last_args.push(arg.clone());
}
let vararg_value = self.ctx.new_tuple(last_args);
// If we have a name (not '*' only) then store it:
if let Some(vararg_name) = vararg {
self.ctx.set_item(scope, vararg_name, vararg_value);
}
} else {
// Check the number of positional arguments
if nargs > nexpected_args {
return Err(self.new_type_error(format!(
"Expected {} arguments (got: {})",
nexpected_args, nargs
)));
}
}
// Do we support `**kwargs` ?
let kwargs = if let Some(kwargs) = &code_object.varkeywords {
let d = self.new_dict();
// Store when we have a name:
if let Some(kwargs_name) = kwargs {
self.ctx.set_item(scope, &kwargs_name, d.clone());
}
Some(d)
} else {
None
};
// Handle keyword arguments
for (name, value) in args.kwargs {
// Check if we have a parameter with this name:
if code_object.arg_names.contains(&name) || code_object.kwonlyarg_names.contains(&name)
{
if scope.contains_key(&name) {
return Err(
self.new_type_error(format!("Got multiple values for argument '{}'", name))
);
}
self.ctx.set_item(scope, &name, value);
} else if let Some(d) = &kwargs {
self.ctx.set_item(d, &name, value);
} else {
return Err(
self.new_type_error(format!("Got an unexpected keyword argument '{}'", name))
);
}
}
// Add missing positional arguments, if we have fewer positional arguments than the
// function definition calls for
if nargs < nexpected_args {
let available_defaults = match defaults.borrow().kind {
PyObjectKind::Sequence { ref elements } => elements.clone(),
PyObjectKind::None => vec![],
_ => panic!("function defaults not tuple or None"),
};
// Given the number of defaults available, check all the arguments for which we
// _don't_ have defaults; if any are missing, raise an exception
let required_args = nexpected_args - available_defaults.len();
let mut missing = vec![];
for i in 0..required_args {
let variable_name = &code_object.arg_names[i];
if !scope.contains_key(variable_name) {
missing.push(variable_name)
}
}
if !missing.is_empty() {
return Err(self.new_type_error(format!(
"Missing {} required positional arguments: {:?}",
missing.len(),
missing
)));
}
// We have sufficient defaults, so iterate over the corresponding names and use
// the default if we don't already have a value
let mut default_index = 0;
for i in required_args..nexpected_args {
let arg_name = &code_object.arg_names[i];
if !scope.contains_key(arg_name) {
self.ctx
.set_item(scope, arg_name, available_defaults[default_index].clone());
}
default_index += 1;
}
};
// Check if kw only arguments are all present:
let kwdefs: HashMap<String, String> = HashMap::new();
for arg_name in &code_object.kwonlyarg_names {
if !scope.contains_key(arg_name) {
if kwdefs.contains_key(arg_name) {
// If not yet specified, take the default value
unimplemented!();
} else {
// No default value and not specified.
return Err(self.new_type_error(format!(
"Missing required kw only argument: '{}'",
arg_name
)));
}
}
}
Ok(())
}
pub fn extract_elements(
&mut self,
value: &PyObjectRef,
) -> Result<Vec<PyObjectRef>, PyObjectRef> {
// Extract elements from item, if possible:
let elements = if objtype::isinstance(value, &self.ctx.tuple_type()) {
objsequence::get_elements(value).to_vec()
} else if objtype::isinstance(value, &self.ctx.list_type()) {
objsequence::get_elements(value).to_vec()
} else {
let iter = objiter::get_iter(self, value)?;
objiter::get_all(self, &iter)?
};
Ok(elements)
}
// get_attribute should be used for full attribute access (usually from user code).
pub fn get_attribute(&mut self, obj: PyObjectRef, attr_name: PyObjectRef) -> PyResult {
trace!("vm.__getattribute__: {:?} {:?}", obj, attr_name);
self.call_method(&obj, "__getattribute__", vec![attr_name])
}
pub fn del_attr(&mut self, obj: &PyObjectRef, attr_name: PyObjectRef) -> PyResult {
self.call_method(&obj, "__delattr__", vec![attr_name])
}
// get_method should be used for internal access to magic methods (by-passing
// the full getattribute look-up.
pub fn get_method(&mut self, obj: PyObjectRef, method_name: &str) -> PyResult {
let cls = obj.typ();
match cls.get_attr(method_name) {
Some(method) => self.call_get_descriptor(method, obj.clone()),
None => Err(self.new_type_error(format!(
"{} has no method {:?}",
obj.borrow(),
method_name
))),
}
}
/// Calls default method, reverse method or exception
///
/// * `a` - First argument.
/// * `b` - Second argument.
/// * `d` - Default method to try and call (such as `__and__`).
/// * `r` - Reverse method to try and call (such as `__rand__`), in case first one fails.
/// * `op` - Operator for the exception text, for example `&`.
///
/// Given the above example, it will
/// 1. Try to call `__and__` with `a` and `b`
/// 2. If above fails try to call `__rand__` with `a` and `b`
/// 3. If above fails throw an exception:
/// `TypeError: Unsupported operand types for '&': 'float' and 'int'`
/// if `a` is of type float and `b` of type int
///
pub fn call_or_unsupported(
&mut self,
a: PyObjectRef,
b: PyObjectRef,
d: &str,
r: &str,
op: &str,
) -> PyResult {
// Try to call the first method
if let Ok(method) = self.get_method(a.clone(), d) {
match self.invoke(
method,
PyFuncArgs {
args: vec![b.clone()],
kwargs: vec![],
},
) {
Ok(value) => return Ok(value),
Err(err) => {
if !objtype::isinstance(&err, &self.ctx.exceptions.not_implemented_error) {
return Err(err);
}
}
}
}
// 2. Try to call reverse method
if let Ok(method) = self.get_method(b.clone(), r) {
match self.invoke(
method,
PyFuncArgs {
args: vec![a.clone()],
kwargs: vec![],
},
) {
Ok(value) => return Ok(value),
Err(err) => {
if !objtype::isinstance(&err, &self.ctx.exceptions.not_implemented_error) {
return Err(err);
}
}
}
}
// 3. Both failed, throw an exception
// TODO: Move this chunk somewhere else, it should be
// called in other methods as well (for example objint.rs)
let a_type_name = objtype::get_type_name(&a.typ());
let b_type_name = objtype::get_type_name(&b.typ());
Err(self.new_type_error(format!(
"Unsupported operand types for '{}': '{}' and '{}'",
op, a_type_name, b_type_name
)))
}
pub fn _sub(&mut self, a: PyObjectRef, b: PyObjectRef) -> PyResult {
self.call_or_unsupported(a, b, "__sub__", "__rsub__", "-")
}
pub fn _add(&mut self, a: PyObjectRef, b: PyObjectRef) -> PyResult {
self.call_method(&a, "__add__", vec![b])
}
pub fn _mul(&mut self, a: PyObjectRef, b: PyObjectRef) -> PyResult {
self.call_method(&a, "__mul__", vec![b])
}
pub fn _div(&mut self, a: PyObjectRef, b: PyObjectRef) -> PyResult {
self.call_method(&a, "__truediv__", vec![b])
}
pub fn _pow(&mut self, a: PyObjectRef, b: PyObjectRef) -> PyResult {
self.call_method(&a, "__pow__", vec![b])
}
pub fn _modulo(&mut self, a: PyObjectRef, b: PyObjectRef) -> PyResult {
self.call_method(&a, "__mod__", vec![b])
}
pub fn _xor(&mut self, a: PyObjectRef, b: PyObjectRef) -> PyResult {
self.call_method(&a, "__xor__", vec![b])
}
pub fn _or(&mut self, a: PyObjectRef, b: PyObjectRef) -> PyResult {
self.call_or_unsupported(a, b, "__or__", "__ror__", "|")
}
pub fn _and(&mut self, a: PyObjectRef, b: PyObjectRef) -> PyResult {
self.call_or_unsupported(a, b, "__and__", "__rand__", "&")
}
pub fn _eq(&mut self, a: &PyObjectRef, b: PyObjectRef) -> PyResult {
self.call_method(a, "__eq__", vec![b])
}
pub fn _ne(&mut self, a: &PyObjectRef, b: PyObjectRef) -> PyResult {
self.call_method(a, "__ne__", vec![b])
}
pub fn _lt(&mut self, a: &PyObjectRef, b: PyObjectRef) -> PyResult {
self.call_method(a, "__lt__", vec![b])
}
pub fn _le(&mut self, a: &PyObjectRef, b: PyObjectRef) -> PyResult {
self.call_method(a, "__le__", vec![b])
}
pub fn _gt(&mut self, a: &PyObjectRef, b: PyObjectRef) -> PyResult {
self.call_method(a, "__gt__", vec![b])
}
pub fn _ge(&mut self, a: &PyObjectRef, b: PyObjectRef) -> PyResult {
self.call_method(a, "__ge__", vec![b])
}
}
#[cfg(test)]
mod tests {
use super::super::obj::{objint, objstr};
use super::VirtualMachine;
use num_bigint::ToBigInt;
#[test]
fn test_add_py_integers() {
let mut vm = VirtualMachine::new();
let a = vm.ctx.new_int(33_i32.to_bigint().unwrap());
let b = vm.ctx.new_int(12_i32.to_bigint().unwrap());
let res = vm._add(a, b).unwrap();
let value = objint::get_value(&res);
assert_eq!(value, 45_i32.to_bigint().unwrap());
}
#[test]
fn test_multiply_str() {
let mut vm = VirtualMachine::new();
let a = vm.ctx.new_str(String::from("Hello "));
let b = vm.ctx.new_int(4_i32.to_bigint().unwrap());
let res = vm._mul(a, b).unwrap();
let value = objstr::get_value(&res);
assert_eq!(value, String::from("Hello Hello Hello Hello "))
}
}