File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -22,9 +22,10 @@ bz2 = ["stdlib", "rustpython-stdlib/bz2"]
2222sqlite = [" rustpython-stdlib/sqlite" ]
2323ssl = [" rustpython-stdlib/ssl" ]
2424ssl-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 }
2829rustpython-compiler = { workspace = true }
2930rustpython-pylib = { workspace = true , optional = true }
3031rustpython-stdlib = { workspace = true , optional = true , features = [" compiler" ] }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1- use std:: ffi;
1+ use std:: { cell :: RefCell , ffi, sync :: Arc } ;
22
33use 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 ) ]
816pub 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- }
Original file line number Diff line number Diff 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)
Original file line number Diff line number Diff line change 11use super :: { Context , VirtualMachine , setting:: Settings , thread} ;
22use 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/// ```
2323pub struct Interpreter {
24- vm : VirtualMachine ,
24+ pub vm : Arc < VirtualMachine > ,
2525}
2626
2727impl 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.
You can’t perform that action at this time.
0 commit comments