Skip to content

Commit 42c7c79

Browse files
committed
Rename Context::deault to genesis to clarify it has root
1 parent 2a1c02b commit 42c7c79

File tree

5 files changed

+18
-19
lines changed

5 files changed

+18
-19
lines changed

vm/src/builtins/type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -964,7 +964,7 @@ mod tests {
964964

965965
#[test]
966966
fn test_linearise() {
967-
let context = Context::default();
967+
let context = Context::genesis();
968968
let object = &context.types.object_type;
969969
let type_type = &context.types.type_type;
970970

vm/src/object/core.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1196,7 +1196,7 @@ mod tests {
11961196

11971197
#[test]
11981198
fn miri_test_drop() {
1199-
let ctx = crate::Context::default();
1199+
let ctx = crate::Context::genesis();
12001200
let obj = ctx.new_bytes(b"dfghjkl".to_vec());
12011201
drop(obj);
12021202
}

vm/src/vm/context.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use crate::{
1111
PyTupleRef, PyType, PyTypeRef,
1212
},
1313
class::{PyClassImpl, StaticType},
14+
common::rc::PyRc,
1415
exceptions,
1516
function::IntoPyNativeFunc,
1617
intern::{Internable, MaybeInterned, StringPool},
@@ -21,7 +22,7 @@ use num_bigint::BigInt;
2122
use num_complex::Complex64;
2223
use num_traits::ToPrimitive;
2324

24-
#[derive(Debug, Clone)]
25+
#[derive(Debug)]
2526
pub struct Context {
2627
pub true_value: PyIntRef,
2728
pub false_value: PyIntRef,
@@ -43,7 +44,7 @@ pub struct Context {
4344

4445
macro_rules! declare_const_name {
4546
($($name:ident,)*) => {
46-
#[derive(Debug, Clone)]
47+
#[derive(Debug, Clone, Copy)]
4748
#[allow(non_snake_case)]
4849
pub struct ConstName {
4950
$(pub $name: &'static PyStrInterned,)*
@@ -214,7 +215,14 @@ impl Context {
214215
pub const INT_CACHE_POOL_MIN: i32 = -5;
215216
pub const INT_CACHE_POOL_MAX: i32 = 256;
216217

217-
fn init() -> Self {
218+
pub fn genesis() -> &'static PyRc<Self> {
219+
rustpython_common::static_cell! {
220+
static CONTEXT: PyRc<Context>;
221+
}
222+
CONTEXT.get_or_init(|| PyRc::new(Self::init_genesis()))
223+
}
224+
225+
fn init_genesis() -> Self {
218226
flame_guard!("init Context");
219227
let types = TypeZoo::init();
220228
let exceptions = exceptions::ExceptionZoo::init();
@@ -494,15 +502,6 @@ impl Context {
494502
}
495503
}
496504

497-
impl Default for Context {
498-
fn default() -> Self {
499-
rustpython_common::static_cell! {
500-
static CONTEXT: Context;
501-
}
502-
CONTEXT.get_or_init(Self::init).clone()
503-
}
504-
}
505-
506505
impl AsRef<Context> for Context {
507506
fn as_ref(&self) -> &Self {
508507
self

vm/src/vm/interpreter.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{setting::Settings, thread, VirtualMachine};
1+
use super::{setting::Settings, thread, Context, VirtualMachine};
22
use crate::{
33
stdlib::{atexit, sys},
44
PyResult,
@@ -44,7 +44,8 @@ impl Interpreter {
4444
where
4545
F: FnOnce(&mut VirtualMachine),
4646
{
47-
let mut vm = VirtualMachine::new(settings);
47+
let ctx = Context::genesis();
48+
let mut vm = VirtualMachine::new(settings, ctx.clone());
4849
init(&mut vm);
4950
vm.initialize();
5051
Self { vm }

vm/src/vm/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,8 @@ pub struct PyGlobalState {
9090

9191
impl VirtualMachine {
9292
/// Create a new `VirtualMachine` structure.
93-
fn new(settings: Settings) -> VirtualMachine {
93+
fn new(settings: Settings, ctx: PyRc<Context>) -> VirtualMachine {
9494
flame_guard!("new VirtualMachine");
95-
let ctx = Context::default();
9695

9796
// make a new module without access to the vm; doesn't
9897
// set __spec__, __loader__, etc. attributes
@@ -131,7 +130,7 @@ impl VirtualMachine {
131130
let mut vm = VirtualMachine {
132131
builtins,
133132
sys_module,
134-
ctx: PyRc::new(ctx),
133+
ctx,
135134
frames: RefCell::new(vec![]),
136135
wasm_id: None,
137136
exceptions: RefCell::default(),

0 commit comments

Comments
 (0)