Skip to content

Commit 7b6ab8d

Browse files
committed
Replace vm.new_{int,str,bool} with vm.new_pyobj
1 parent df20a26 commit 7b6ab8d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+224
-218
lines changed

src/main.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ fn _run_string(vm: &VirtualMachine, scope: Scope, source: &str, source_path: Str
395395
// trace!("Code object: {:?}", code_obj.borrow());
396396
scope
397397
.globals
398-
.set_item("__file__", vm.new_str(source_path), vm)?;
398+
.set_item("__file__", vm.ctx.new_str(source_path), vm)?;
399399
vm.run_code_obj(code_obj, scope)
400400
}
401401

@@ -409,7 +409,7 @@ fn run_module(vm: &VirtualMachine, module: &str) -> PyResult<()> {
409409
debug!("Running module {}", module);
410410
let runpy = vm.import("runpy", &[], 0)?;
411411
let run_module_as_main = vm.get_attribute(runpy, "_run_module_as_main")?;
412-
vm.invoke(&run_module_as_main, vec![vm.new_str(module.to_owned())])?;
412+
vm.invoke(&run_module_as_main, vec![vm.ctx.new_str(module.to_owned())])?;
413413
Ok(())
414414
}
415415

@@ -440,7 +440,11 @@ fn run_script(vm: &VirtualMachine, scope: Scope, script_file: &str) -> PyResult<
440440

441441
let dir = file_path.parent().unwrap().to_str().unwrap().to_owned();
442442
let sys_path = vm.get_attribute(vm.sys_module.clone(), "path").unwrap();
443-
vm.call_method(&sys_path, "insert", vec![vm.new_int(0), vm.new_str(dir)])?;
443+
vm.call_method(
444+
&sys_path,
445+
"insert",
446+
vec![vm.ctx.new_int(0), vm.ctx.new_str(dir)],
447+
)?;
444448

445449
match util::read_file(&file_path) {
446450
Ok(source) => {

vm/src/builtins.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ mod decl {
345345
format!("0x{:x}", n)
346346
};
347347

348-
Ok(vm.new_str(s))
348+
Ok(vm.ctx.new_str(s))
349349
}
350350

351351
#[pyfunction]
@@ -551,7 +551,7 @@ mod decl {
551551
format!("0o{:o}", n)
552552
};
553553

554-
Ok(vm.new_str(s))
554+
Ok(vm.ctx.new_str(s))
555555
}
556556

557557
#[pyfunction]
@@ -621,14 +621,14 @@ mod decl {
621621
return Err(vm.new_value_error("pow() 3rd argument cannot be 0".to_owned()));
622622
}
623623
let x = objint::get_value(&x);
624-
Ok(vm.new_int(x.modpow(&y, &m)))
624+
Ok(vm.ctx.new_int(x.modpow(&y, &m)))
625625
}
626626
}
627627
}
628628

629629
#[pyfunction]
630630
pub fn exit(exit_code_arg: OptionalArg<PyObjectRef>, vm: &VirtualMachine) -> PyResult {
631-
let code = exit_code_arg.unwrap_or_else(|| vm.new_int(0));
631+
let code = exit_code_arg.unwrap_or_else(|| vm.ctx.new_int(0));
632632
Err(vm.new_exception(vm.ctx.exceptions.system_exit.clone(), vec![code]))
633633
}
634634

@@ -784,7 +784,7 @@ mod decl {
784784
.split('.')
785785
.next_back()
786786
.unwrap();
787-
let name_obj = vm.new_str(name.to_owned());
787+
let name_obj = vm.ctx.new_str(name.to_owned());
788788

789789
let mut metaclass = if let Some(metaclass) = kwargs.pop_kwarg("metaclass") {
790790
PyClassRef::try_from_object(vm, metaclass)?

vm/src/bytesinner.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ impl PyBytesInner {
313313
match needle {
314314
SequenceIndex::Int(int) => {
315315
if let Some(idx) = self.elements.get_pos(int) {
316-
Ok(vm.new_int(self.elements[idx]))
316+
Ok(vm.ctx.new_int(self.elements[idx]))
317317
} else {
318318
Err(vm.new_index_error("index out of range".to_owned()))
319319
}
@@ -338,7 +338,7 @@ impl PyBytesInner {
338338
});
339339
let value = result?;
340340
self.elements[idx] = value;
341-
Ok(vm.new_int(value))
341+
Ok(vm.ctx.new_int(value))
342342
} else {
343343
Err(vm.new_index_error("index out of range".to_owned()))
344344
}

vm/src/dictdatatype.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -491,13 +491,13 @@ mod tests {
491491
let dict = Dict::default();
492492
assert_eq!(0, dict.len());
493493

494-
let key1 = vm.new_bool(true);
495-
let value1 = vm.new_str("abc".to_owned());
494+
let key1 = vm.ctx.new_bool(true);
495+
let value1 = vm.ctx.new_str("abc".to_owned());
496496
dict.insert(&vm, key1.clone(), value1.clone()).unwrap();
497497
assert_eq!(1, dict.len());
498498

499-
let key2 = vm.new_str("x".to_owned());
500-
let value2 = vm.new_str("def".to_owned());
499+
let key2 = vm.ctx.new_str("x".to_owned());
500+
let value2 = vm.ctx.new_str("def".to_owned());
501501
dict.insert(&vm, key2.clone(), value2.clone()).unwrap();
502502
assert_eq!(2, dict.len());
503503

@@ -537,7 +537,7 @@ mod tests {
537537
fn check_hash_equivalence(text: &str) {
538538
let vm: VirtualMachine = Default::default();
539539
let value1 = text;
540-
let value2 = vm.new_str(value1.to_owned());
540+
let value2 = vm.ctx.new_str(value1.to_owned());
541541

542542
let hash1 = value1.key_hash(&vm).expect("Hash should not fail.");
543543
let hash2 = value2.key_hash(&vm).expect("Hash should not fail.");

vm/src/frame.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,7 +1291,7 @@ impl ExecutingFrame<'_> {
12911291
.split('.')
12921292
.next_back()
12931293
.unwrap();
1294-
vm.set_attr(&func_obj, "__name__", vm.new_str(name.to_owned()))?;
1294+
vm.set_attr(&func_obj, "__name__", vm.ctx.new_str(name.to_owned()))?;
12951295
vm.set_attr(&func_obj, "__qualname__", qualified_name)?;
12961296
let module = self
12971297
.scope
@@ -1416,12 +1416,12 @@ impl ExecutingFrame<'_> {
14161416
bytecode::ComparisonOperator::LessOrEqual => vm._le(a, b)?,
14171417
bytecode::ComparisonOperator::Greater => vm._gt(a, b)?,
14181418
bytecode::ComparisonOperator::GreaterOrEqual => vm._ge(a, b)?,
1419-
bytecode::ComparisonOperator::Is => vm.new_bool(self._is(a, b)),
1420-
bytecode::ComparisonOperator::IsNot => vm.new_bool(self._is_not(a, b)),
1421-
bytecode::ComparisonOperator::In => vm.new_bool(self._in(vm, a, b)?),
1422-
bytecode::ComparisonOperator::NotIn => vm.new_bool(self._not_in(vm, a, b)?),
1419+
bytecode::ComparisonOperator::Is => vm.ctx.new_bool(self._is(a, b)),
1420+
bytecode::ComparisonOperator::IsNot => vm.ctx.new_bool(self._is_not(a, b)),
1421+
bytecode::ComparisonOperator::In => vm.ctx.new_bool(self._in(vm, a, b)?),
1422+
bytecode::ComparisonOperator::NotIn => vm.ctx.new_bool(self._not_in(vm, a, b)?),
14231423
bytecode::ComparisonOperator::ExceptionMatch => {
1424-
vm.new_bool(builtin_isinstance(a, b, vm)?)
1424+
vm.ctx.new_bool(builtin_isinstance(a, b, vm)?)
14251425
}
14261426
};
14271427

@@ -1439,7 +1439,7 @@ impl ExecutingFrame<'_> {
14391439
fn store_attr(&mut self, vm: &VirtualMachine, attr_name: &str) -> FrameResult {
14401440
let parent = self.pop_value();
14411441
let value = self.pop_value();
1442-
vm.set_attr(&parent, vm.new_str(attr_name.to_owned()), value)?;
1442+
vm.set_attr(&parent, vm.ctx.new_str(attr_name.to_owned()), value)?;
14431443
Ok(None)
14441444
}
14451445

vm/src/import.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,13 @@ pub fn import_codeobj(
106106
set_file_attr: bool,
107107
) -> PyResult {
108108
let attrs = vm.ctx.new_dict();
109-
attrs.set_item("__name__", vm.new_str(module_name.to_owned()), vm)?;
109+
attrs.set_item("__name__", vm.ctx.new_str(module_name.to_owned()), vm)?;
110110
if set_file_attr {
111-
attrs.set_item("__file__", vm.new_str(code_obj.source_path.to_owned()), vm)?;
111+
attrs.set_item(
112+
"__file__",
113+
vm.ctx.new_str(code_obj.source_path.to_owned()),
114+
vm,
115+
)?;
112116
}
113117
let module = vm.new_module(module_name, attrs.clone());
114118

vm/src/obj/objbool.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ impl PyBool {
166166
OptionalArg::Present(val) => boolval(vm, val.clone())?,
167167
OptionalArg::Missing => false,
168168
};
169-
Ok(vm.new_bool(val))
169+
Ok(vm.ctx.new_bool(val))
170170
}
171171
}
172172

vm/src/obj/objcode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ impl PyCodeRef {
111111
let varnames = self
112112
.code
113113
.varnames()
114-
.map(|s| vm.new_str(s.to_owned()))
114+
.map(|s| vm.ctx.new_str(s.to_owned()))
115115
.collect();
116116
vm.ctx.new_tuple(varnames)
117117
}

vm/src/obj/objdict.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ impl PyDictRef {
100100
}
101101

102102
for (key, value) in kwargs.into_iter() {
103-
dict.insert(vm, vm.new_str(key), value)?;
103+
dict.insert(vm, vm.ctx.new_str(key), value)?;
104104
}
105105
Ok(())
106106
}
@@ -392,7 +392,7 @@ impl PyDictRef {
392392
if let Some((key, value)) = self.entries.pop_front() {
393393
Ok(vm.ctx.new_tuple(vec![key, value]))
394394
} else {
395-
let err_msg = vm.new_str("popitem(): dictionary is empty".to_owned());
395+
let err_msg = vm.ctx.new_str("popitem(): dictionary is empty".to_owned());
396396
Err(vm.new_key_error(err_msg))
397397
}
398398
}

vm/src/obj/objiter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ impl PySequenceIterator {
186186
let step: isize = if self.reversed { -1 } else { 1 };
187187
let pos = self.position.fetch_add(step);
188188
if pos >= 0 {
189-
match vm.call_method(&self.obj, "__getitem__", vec![vm.new_int(pos)]) {
189+
match vm.call_method(&self.obj, "__getitem__", vec![vm.ctx.new_int(pos)]) {
190190
Err(ref e) if objtype::isinstance(&e, &vm.ctx.exceptions.index_error) => {
191191
Err(new_stop_iteration(vm))
192192
}

0 commit comments

Comments
 (0)