Skip to content

Commit 52404a2

Browse files
committed
use thread local vm
1 parent 67e7900 commit 52404a2

6 files changed

Lines changed: 46 additions & 31 deletions

File tree

‎Cargo.toml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ bz2 = ["stdlib", "rustpython-stdlib/bz2"]
2222
sqlite = ["rustpython-stdlib/sqlite"]
2323
ssl = ["rustpython-stdlib/ssl"]
2424
ssl-vendor = ["ssl", "rustpython-stdlib/ssl-vendor"]
25+
capi = ["rustpython-capi"]
2526

2627
[dependencies]
27-
rustpython-capi = { workspace = true }
28+
rustpython-capi = { workspace = true, optional = true }
2829
rustpython-compiler = { workspace = true }
2930
rustpython-pylib = { workspace = true, optional = true }
3031
rustpython-stdlib = { workspace = true, optional = true, features = ["compiler"] }

‎capi/src/error.rs‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use std::ffi;
2+
3+
use rustpython_vm::PyObjectRef;
4+
5+
#[unsafe(export_name = "PyErr_Clear")]
6+
pub unsafe extern "C" fn err_clear() {
7+
todo!()
8+
}
9+
10+
#[unsafe(export_name = "PyErr_NewException")]
11+
pub unsafe extern "C" fn err_new_exception(
12+
name: *const ffi::c_char,
13+
_base: *mut ffi::c_void,
14+
_dict: *mut ffi::c_void,
15+
) -> PyObjectRef {
16+
let vm = crate::get_vm();
17+
let name_str = unsafe { std::ffi::CStr::from_ptr(name).to_str().unwrap() };
18+
let name_split = name_str.split('.');
19+
let module = name_split.clone().next().unwrap();
20+
let name = name_split.last().unwrap();
21+
vm.ctx.new_exception_type(module, name, Some(vec![vm.ctx.exceptions.exception_type.to_owned()])).into()
22+
}

‎capi/src/lib.rs‎

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1-
use std::ffi;
1+
use std::{cell::RefCell, ffi, sync::Arc};
22

33
use rustpython_vm as vm;
44

5-
use malachite_bigint::BigInt;
5+
mod error;
6+
7+
thread_local ! {
8+
pub static VM: RefCell<Option<Arc<vm::VirtualMachine>>> = RefCell::new(None);
9+
}
10+
11+
fn get_vm() -> Arc<vm::VirtualMachine> {
12+
VM.with(|vm| vm.borrow().as_ref().unwrap().clone())
13+
}
614

715
#[repr(C)]
816
pub enum PyStatusType {
@@ -50,28 +58,3 @@ pub struct PyInterpreterConfig {
5058
gil: i32,
5159
}
5260

53-
macro_rules! pylong_from_num {
54-
($name:ident) => {
55-
{
56-
let big_int = BigInt::from($name);
57-
let pyi = vm::builtins::PyInt::from(big_int);
58-
let pyi = Box::new(pyi);
59-
Box::into_raw(pyi) as *mut ffi::c_void
60-
}
61-
};
62-
}
63-
64-
#[unsafe(export_name = "PyLong_FromLong")]
65-
pub extern "C" fn pylong_from_long(v: ffi::c_long) -> *mut ffi::c_void {
66-
pylong_from_num!(v)
67-
}
68-
69-
#[unsafe(export_name = "PyLong_FromUnsignedLong")]
70-
pub extern "C" fn pylong_from_unsigned_long(v: ffi::c_ulong) -> *mut ffi::c_void {
71-
pylong_from_num!(v)
72-
}
73-
74-
#[unsafe(export_name = "PyLong_FromUnsignedLongLong")]
75-
pub extern "C" fn pylong_from_unsigned_long_long(v: ffi::c_ulonglong) -> *mut ffi::c_void {
76-
pylong_from_num!(v)
77-
}

‎capi/src/object.rs‎

Whitespace-only changes.

‎src/lib.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ pub fn run(init: impl FnOnce(&mut VirtualMachine) + 'static) -> ExitCode {
9999
config = config.init_hook(Box::new(init));
100100

101101
let interp = config.interpreter();
102+
#[cfg(feature = "capi")]
103+
{
104+
rustpython_capi::VM.with(|vm| {
105+
*vm.borrow_mut() = Some(interp.vm.clone());
106+
});
107+
}
102108
let exitcode = interp.run(move |vm| run_rustpython(vm, run_mode));
103109

104110
ExitCode::from(exitcode)

‎vm/src/vm/interpreter.rs‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::{Context, VirtualMachine, setting::Settings, thread};
22
use crate::{PyResult, stdlib::atexit, vm::PyBaseExceptionRef};
3-
use std::sync::atomic::Ordering;
3+
use std::sync::{atomic::Ordering, Arc};
44

55
/// The general interface for the VM
66
///
@@ -21,7 +21,7 @@ use std::sync::atomic::Ordering;
2121
/// });
2222
/// ```
2323
pub struct Interpreter {
24-
vm: VirtualMachine,
24+
pub vm: Arc<VirtualMachine>,
2525
}
2626

2727
impl Interpreter {
@@ -53,7 +53,10 @@ impl Interpreter {
5353
let mut vm = VirtualMachine::new(settings, ctx.clone());
5454
init(&mut vm);
5555
vm.initialize();
56-
Self { vm }
56+
let vm = Arc::new(vm);
57+
Self {
58+
vm,
59+
}
5760
}
5861

5962
/// Run a function with the main virtual machine and return a PyResult of the result.

0 commit comments

Comments
 (0)