forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuiltins.rs
More file actions
434 lines (380 loc) · 12.5 KB
/
builtins.rs
File metadata and controls
434 lines (380 loc) · 12.5 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
// use std::ops::Deref;
use std::char;
use std::collections::HashMap;
use std::io::{self, Write};
use super::compile;
use super::obj::objstr;
use super::obj::objtype;
use super::objbool;
use super::pyobject::{
AttributeProtocol, DictProtocol, IdProtocol, PyContext, PyFuncArgs, PyObject, PyObjectKind,
PyObjectRef, PyResult, Scope, TypeProtocol,
};
use super::vm::VirtualMachine;
fn get_locals(vm: &mut VirtualMachine) -> PyObjectRef {
let d = vm.new_dict();
// TODO: implement dict_iter_items?
let locals = vm.get_locals();
match locals.borrow().kind {
PyObjectKind::Dict { ref elements } => {
for l in elements {
d.set_item(l.0, l.1.clone());
}
}
_ => {}
};
d
}
fn dir_locals(vm: &mut VirtualMachine) -> PyObjectRef {
get_locals(vm)
}
fn dir_object(vm: &mut VirtualMachine, obj: &PyObjectRef) -> PyObjectRef {
// Gather all members here:
let attributes = objtype::get_attributes(obj);
let mut members: Vec<String> = attributes.into_iter().map(|(n, _o)| n).collect();
// Sort members:
members.sort();
let members_pystr = members.into_iter().map(|m| vm.ctx.new_str(m)).collect();
vm.ctx.new_list(members_pystr)
}
// builtin_abs
fn builtin_all(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
for item in args.args {
let result = objbool::boolval(vm, item)?;
if !result {
return Ok(vm.new_bool(false));
}
}
Ok(vm.new_bool(true))
}
fn builtin_any(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
for item in args.args {
let result = objbool::boolval(vm, item)?;
if result {
return Ok(vm.new_bool(true));
}
}
Ok(vm.new_bool(false))
}
// builtin_ascii
// builtin_bin
// builtin_bool
// builtin_breakpoint
// builtin_bytearray
// builtin_bytes
// builtin_callable
fn builtin_chr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(i, Some(vm.ctx.int_type()))]);
let code_point_obj = i.borrow();
let code_point = *match code_point_obj.kind {
PyObjectKind::Integer { ref value } => value,
ref kind => panic!(
"argument checking failure: chr not supported for {:?}",
kind
),
} as u32;
let txt = match char::from_u32(code_point) {
Some(value) => value.to_string(),
None => '_'.to_string(),
};
Ok(vm.new_str(txt))
}
// builtin_classmethod
fn builtin_compile(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(source, None)]);
// TODO:
let mode = compile::Mode::Eval;
let source = source.borrow().str();
match compile::compile(vm, &source, mode, None) {
Ok(value) => Ok(value),
Err(msg) => Err(vm.new_type_error(msg)),
}
}
// builtin_complex
// builtin_delattr
fn builtin_dir(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
if args.args.is_empty() {
Ok(dir_locals(vm))
} else {
let obj = args.args.into_iter().next().unwrap();
Ok(dir_object(vm, &obj))
}
}
// builtin_divmod
// builtin_enumerate
fn builtin_eval(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
required = [
(source, None), // TODO: Use more specific type
(_globals, Some(vm.ctx.dict_type())),
(locals, Some(vm.ctx.dict_type()))
]
);
// TODO: handle optional global and locals
let code_obj = source; // if source.borrow().kind
// Construct new scope:
let scope_inner = Scope {
locals: locals.clone(),
parent: None,
};
let scope = PyObject {
kind: PyObjectKind::Scope { scope: scope_inner },
typ: None,
}.into_ref();
// Run the source:
vm.run_code_obj(code_obj.clone(), scope)
}
// builtin_exec
// builtin_filter
// builtin_float
// builtin_format
// builtin_frozenset
fn builtin_getattr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
required = [(obj, None), (attr, Some(vm.ctx.str_type()))]
);
if let PyObjectKind::String { ref value } = attr.borrow().kind {
vm.get_attribute(obj.clone(), value)
} else {
panic!("argument checking failure: attr not string")
}
}
// builtin_globals
fn builtin_hasattr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
required = [(obj, None), (attr, Some(vm.ctx.str_type()))]
);
if let PyObjectKind::String { ref value } = attr.borrow().kind {
let has_attr = match vm.get_attribute(obj.clone(), value) {
Ok(..) => true,
Err(..) => false,
};
Ok(vm.context().new_bool(has_attr))
} else {
panic!("argument checking failure: attr not string")
}
}
// builtin_hash
// builtin_help
// builtin_hex
fn builtin_id(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(obj, None)]);
Ok(vm.context().new_int(obj.get_id() as i32))
}
// builtin_input
// builtin_int
fn builtin_isinstance(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(obj, None), (typ, None)]);
let isinstance = objtype::isinstance(obj, typ.clone());
Ok(vm.context().new_bool(isinstance))
}
fn builtin_issubclass(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
if args.args.len() != 2 {
panic!("issubclass expects exactly two parameters");
}
let cls1 = &args.args[0];
let cls2 = args.args[1].clone();
Ok(vm.context().new_bool(objtype::issubclass(cls1, cls2)))
}
// builtin_iter
fn builtin_len(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(obj, None)]);
match obj.borrow().kind {
PyObjectKind::Dict { ref elements } => Ok(vm.context().new_int(elements.len() as i32)),
PyObjectKind::Tuple { ref elements } => Ok(vm.context().new_int(elements.len() as i32)),
_ => {
let len_method_name = "__len__".to_string();
match vm.get_attribute(obj.clone(), &len_method_name) {
Ok(value) => vm.invoke(value, PyFuncArgs::default()),
Err(..) => Err(vm.context().new_str(
format!(
"TypeError: object of this {:?} type has no method {:?}",
obj, len_method_name
).to_string(),
)),
}
}
}
}
// builtin_list
fn builtin_locals(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args);
Ok(vm.get_locals())
}
// builtin_map
// builtin_max
// builtin_memoryview
// builtin_min
// builtin_next
// builtin_object
// builtin_oct
// builtin_open
// builtin_ord
// builtin_pow
pub fn builtin_print(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
trace!("print called with {:?}", args);
for a in args.args {
let s = match vm.to_str(a) {
Ok(v) => objstr::get_value(&v),
Err(err) => return Err(err),
};
print!("{} ", s);
}
println!();
io::stdout().flush().unwrap();
Ok(vm.get_none())
}
// builtin_property
fn builtin_range(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(range, Some(vm.ctx.int_type()))]);
match range.borrow().kind {
PyObjectKind::Integer { ref value } => {
let range_elements: Vec<PyObjectRef> =
(0..*value).map(|num| vm.context().new_int(num)).collect();
Ok(vm.context().new_list(range_elements))
}
_ => panic!("argument checking failure: first argument to range must be an integer"),
}
}
// builtin_repr
fn builtin_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(obj, None)]);
vm.to_repr(obj.clone())
}
// builtin_reversed
// builtin_round
// builtin_set
fn builtin_setattr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
required = [(obj, None), (attr, Some(vm.ctx.str_type())), (value, None)]
);
if let PyObjectKind::String { value: ref name } = attr.borrow().kind {
obj.clone().set_attr(name, value.clone());
Ok(vm.get_none())
} else {
panic!("argument checking failure: attr not string")
}
}
// builtin_slice
// builtin_sorted
// builtin_staticmethod
// builtin_sum
// builtin_super
// builtin_vars
// builtin_zip
// builtin___import__
pub fn make_module(ctx: &PyContext) -> PyObjectRef {
// scope[String::from("print")] = print;
let mut dict = HashMap::new();
dict.insert(String::from("all"), ctx.new_rustfunc(builtin_all));
dict.insert(String::from("any"), ctx.new_rustfunc(builtin_any));
dict.insert(String::from("bool"), ctx.bool_type());
dict.insert(String::from("bytes"), ctx.bytes_type());
dict.insert(String::from("chr"), ctx.new_rustfunc(builtin_chr));
dict.insert(String::from("compile"), ctx.new_rustfunc(builtin_compile));
dict.insert(String::from("dict"), ctx.dict_type());
dict.insert(String::from("dir"), ctx.new_rustfunc(builtin_dir));
dict.insert(String::from("eval"), ctx.new_rustfunc(builtin_eval));
dict.insert(String::from("float"), ctx.float_type());
dict.insert(String::from("getattr"), ctx.new_rustfunc(builtin_getattr));
dict.insert(String::from("hasattr"), ctx.new_rustfunc(builtin_hasattr));
dict.insert(String::from("id"), ctx.new_rustfunc(builtin_id));
dict.insert(String::from("int"), ctx.int_type());
dict.insert(
String::from("isinstance"),
ctx.new_rustfunc(builtin_isinstance),
);
dict.insert(
String::from("issubclass"),
ctx.new_rustfunc(builtin_issubclass),
);
dict.insert(String::from("len"), ctx.new_rustfunc(builtin_len));
dict.insert(String::from("list"), ctx.list_type());
dict.insert(String::from("locals"), ctx.new_rustfunc(builtin_locals));
dict.insert(String::from("print"), ctx.new_rustfunc(builtin_print));
dict.insert(String::from("range"), ctx.new_rustfunc(builtin_range));
dict.insert(String::from("repr"), ctx.new_rustfunc(builtin_repr));
dict.insert(String::from("setattr"), ctx.new_rustfunc(builtin_setattr));
dict.insert(String::from("str"), ctx.str_type()); // new_rustfunc(builtin_str));
dict.insert(String::from("tuple"), ctx.tuple_type());
dict.insert(String::from("type"), ctx.type_type());
dict.insert(String::from("object"), ctx.object());
// Exceptions:
dict.insert(
String::from("BaseException"),
ctx.exceptions.base_exception_type.clone(),
);
dict.insert(
String::from("Exception"),
ctx.exceptions.exception_type.clone(),
);
dict.insert(
String::from("AssertionError"),
ctx.exceptions.assertion_error.clone(),
);
dict.insert(
String::from("AttributeError"),
ctx.exceptions.attribute_error.clone(),
);
dict.insert(String::from("NameError"), ctx.exceptions.name_error.clone());
dict.insert(
String::from("RuntimeError"),
ctx.exceptions.runtime_error.clone(),
);
dict.insert(
String::from("NotImplementedError"),
ctx.exceptions.not_implemented_error.clone(),
);
dict.insert(String::from("TypeError"), ctx.exceptions.type_error.clone());
dict.insert(
String::from("ValueError"),
ctx.exceptions.value_error.clone(),
);
let d2 = PyObject::new(PyObjectKind::Dict { elements: dict }, ctx.type_type());
let scope = PyObject::new(
PyObjectKind::Scope {
scope: Scope {
locals: d2,
parent: None,
},
},
ctx.type_type(),
);
let obj = PyObject::new(
PyObjectKind::Module {
name: "__builtins__".to_string(),
dict: scope,
},
ctx.type_type(),
);
obj
}
pub fn builtin_build_class_(vm: &mut VirtualMachine, mut args: PyFuncArgs) -> PyResult {
let function = args.shift();
let name_arg = args.shift();
let name_arg_ref = name_arg.borrow();
let name = match name_arg_ref.kind {
PyObjectKind::String { ref value } => value,
_ => panic!("Class name must by a string!"),
};
let mut bases = args.args.clone();
bases.push(vm.context().object());
let metaclass = vm.get_type();
let namespace = vm.new_dict();
&vm.invoke(
function,
PyFuncArgs {
args: vec![namespace.clone()],
kwargs: vec![],
},
);
objtype::new(metaclass, name, bases, namespace)
}