Skip to content

Commit dbae0b0

Browse files
committed
Add builtin id function
1 parent 794313c commit dbae0b0

3 files changed

Lines changed: 31 additions & 4 deletions

File tree

vm/src/builtins.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::io::{self, Write};
44

55
use super::compile;
66
use super::pyobject::DictProtocol;
7-
use super::pyobject::{Executor, PyContext, PyObject, PyObjectKind, PyObjectRef, PyResult, Scope};
7+
use super::pyobject::{Executor, PyContext, PyObject, PyObjectKind, PyObjectRef, PyResult, Scope, IdProtocol};
88
use super::objbool;
99

1010

@@ -41,6 +41,14 @@ pub fn builtin_dir(rt: &mut Executor, args: Vec<PyObjectRef>) -> PyResult {
4141
}
4242
}
4343

44+
pub fn builtin_id(rt: &mut Executor, args: Vec<PyObjectRef>) -> PyResult {
45+
if args.len() != 1 {
46+
return Err(rt.new_exception("Expected only one argument".to_string()))
47+
}
48+
49+
Ok(rt.context().new_int(args[0].get_id() as i32))
50+
}
51+
4452
pub fn builtin_print(rt: &mut Executor, args: Vec<PyObjectRef>) -> PyResult {
4553
trace!("print called with {:?}", args);
4654
for a in args {
@@ -88,6 +96,7 @@ pub fn len(rt: &mut Executor, args: Vec<PyObjectRef>) -> PyResult {
8896
pub fn make_module(ctx: &PyContext) -> PyObjectRef {
8997
// scope[String::from("print")] = print;
9098
let mut dict = HashMap::new();
99+
dict.insert(String::from("id"), ctx.new_rustfunc(builtin_id));
91100
dict.insert(String::from("print"), ctx.new_rustfunc(builtin_print));
92101
dict.insert(String::from("type"), ctx.type_type.clone());
93102
dict.insert(String::from("int"), ctx.int_type.clone());

vm/src/pyobject.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,16 @@ impl Default for PyObject {
152152
}
153153
}
154154

155+
pub trait IdProtocol {
156+
fn get_id(&self) -> usize;
157+
}
158+
159+
impl IdProtocol for PyObjectRef {
160+
fn get_id(&self) -> usize {
161+
self.as_ptr() as usize
162+
}
163+
}
164+
155165
pub trait ParentProtocol {
156166
fn has_parent(&self) -> bool;
157167
fn get_parent(&self) -> PyObjectRef;

vm/src/vm.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use super::objlist;
1616
use super::objstr;
1717
use super::objtuple;
1818
use super::pyobject::{DictProtocol, Executor, PyContext, PyObject, PyObjectKind, PyObjectRef,
19-
PyResult, ParentProtocol, Scope};
19+
PyResult, ParentProtocol, Scope, IdProtocol};
2020

2121
// use objects::objects;
2222

@@ -409,16 +409,24 @@ impl VirtualMachine {
409409
Ok(result)
410410
}
411411

412+
fn _id(&mut self, a: PyObjectRef) -> usize {
413+
a.get_id()
414+
}
415+
412416
fn _is(&mut self, a: PyObjectRef, b: PyObjectRef) -> PyResult {
413417
// Pointer equal:
414-
let result_bool = true; // TODO: *a.as_ptr() == *b.as_ptr();
418+
let id_a = self._id(a);
419+
let id_b = self._id(b);
420+
let result_bool = id_a == id_b;
415421
let result = self.ctx.new_bool(result_bool);
416422
Ok(result)
417423
}
418424

419425
fn _is_not(&mut self, a: PyObjectRef, b: PyObjectRef) -> PyResult {
420426
// Pointer equal:
421-
let result_bool = true; // TODO: *a.as_ptr() != *b.as_ptr();
427+
let id_a = self._id(a);
428+
let id_b = self._id(b);
429+
let result_bool = id_a != id_b;
422430
let result = self.ctx.new_bool(result_bool);
423431
Ok(result)
424432
}

0 commit comments

Comments
 (0)