pub struct Interpreter { /* private fields */ }
Expand description

The general interface for the VM

Examples

Runs a simple embedded hello world program.

use rustpython_vm::Interpreter;
use rustpython_vm::compiler::Mode;
Interpreter::without_stdlib(Default::default()).enter(|vm| {
    let scope = vm.new_scope_with_builtins();
    let source = r#"print("Hello World!")"#;
    let code_obj = vm.compile(
            source,
            Mode::Exec,
            "<embedded>".to_owned(),
    ).map_err(|err| vm.new_syntax_error(&err, Some(source))).unwrap();
    vm.run_code_obj(code_obj, scope).unwrap();
});

Implementations§

source§

impl Interpreter

source

pub fn without_stdlib(settings: Settings) -> Self

This is a bare unit to build up an interpreter without the standard library. To create an interpreter with the standard library with the rustpython crate, use rustpython::InterpreterConfig. To create an interpreter without the rustpython crate, but only with rustpython-vm, try to build one from the source code of InterpreterConfig. It will not be a one-liner but it also will not be too hard.

source

pub fn with_init<F>(settings: Settings, init: F) -> Selfwhere F: FnOnce(&mut VirtualMachine),

Create with initialize function taking mutable vm reference.

use rustpython_vm::Interpreter;
Interpreter::with_init(Default::default(), |vm| {
    // put this line to add stdlib to the vm
    // vm.add_native_modules(rustpython_stdlib::get_module_inits());
}).enter(|vm| {
    vm.run_code_string(vm.new_scope_with_builtins(), "print(1)", "<...>".to_owned());
});
source

pub fn enter<F, R>(&self, f: F) -> Rwhere F: FnOnce(&VirtualMachine) -> R,

source

pub fn run<F, R>(self, f: F) -> u8where F: FnOnce(&VirtualMachine) -> PyResult<R>,

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T, U> ExactFrom<T> for Uwhere U: TryFrom<T>,

§

fn exact_from(value: T) -> U

§

impl<T, U> ExactInto<U> for Twhere U: ExactFrom<T>,

§

fn exact_into(self) -> U

source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T, U> OverflowingInto<U> for Twhere U: OverflowingFrom<T>,

§

fn overflowing_into(self) -> (U, bool)

§

impl<T, U> RoundingInto<U> for Twhere U: RoundingFrom<T>,

§

fn rounding_into(self, rm: RoundingMode) -> U

source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
§

impl<T, U> SaturatingInto<U> for Twhere U: SaturatingFrom<T>,

§

fn saturating_into(self) -> U

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T, U> WrappingInto<U> for Twhere U: WrappingFrom<T>,

§

fn wrapping_into(self) -> U

source§

impl<T> PyThreadingConstraint for T