Skip to content

Put RefCell around frames member of VirtualMachine. #706

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions vm/src/sysmodule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn argv(ctx: &PyContext) -> PyObjectRef {
fn frame_idx(vm: &mut VirtualMachine, offset: Option<&PyObjectRef>) -> Result<usize, PyObjectRef> {
if let Some(int) = offset {
if let Some(offset) = objint::get_value(&int).to_usize() {
if offset > vm.frames.len() - 1 {
if offset > vm.frames.borrow().len() - 1 {
return Err(vm.new_value_error("call stack is not deep enough".to_string()));
}
return Ok(offset);
Expand All @@ -39,8 +39,8 @@ fn getframe(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
);

let idx = frame_idx(vm, offset)?;
let idx = vm.frames.len() - idx - 1;
let frame = &vm.frames[idx];
let idx = vm.frames.borrow().len() - idx - 1;
let frame = &vm.frames.borrow()[idx];
Ok(frame.clone())
}

Expand Down
22 changes: 13 additions & 9 deletions vm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

extern crate rustpython_parser;

use std::cell::{Ref, RefCell};
use std::collections::hash_map::HashMap;
use std::collections::hash_set::HashSet;
use std::rc::Rc;
Expand Down Expand Up @@ -46,7 +47,7 @@ pub struct VirtualMachine {
pub sys_module: PyObjectRef,
pub stdlib_inits: HashMap<String, stdlib::StdlibInitFunc>,
pub ctx: PyContext,
pub frames: Vec<PyObjectRef>,
pub frames: RefCell<Vec<PyObjectRef>>,
pub wasm_id: Option<String>,
}

Expand All @@ -69,7 +70,7 @@ impl VirtualMachine {
sys_module: sysmod,
stdlib_inits,
ctx,
frames: vec![],
frames: RefCell::new(vec![]),
wasm_id: None,
}
}
Expand All @@ -87,21 +88,24 @@ impl VirtualMachine {
}

pub fn run_frame(&mut self, frame: PyObjectRef) -> PyResult<ExecutionResult> {
self.frames.push(frame.clone());
self.frames.borrow_mut().push(frame.clone());
let frame = objframe::get_value(&frame);
let result = frame.run(self);
self.frames.pop();
self.frames.borrow_mut().pop();
result
}

pub fn current_frame(&self) -> &Frame {
let current_frame = &self.frames[self.frames.len() - 1];
objframe::get_value(current_frame)
pub fn current_frame(&self) -> Ref<Frame> {
Ref::map(self.frames.borrow(), |frames| {
let index = frames.len() - 1;
let current_frame = &frames[index];
objframe::get_value(current_frame)
})
}

pub fn current_scope(&self) -> &Scope {
pub fn current_scope(&self) -> Ref<Scope> {
let frame = self.current_frame();
&frame.scope
Ref::map(frame, |f| &f.scope)
}

pub fn class(&mut self, module: &str, class: &str) -> PyObjectRef {
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy