From b3090040f0795cb2afdae00a4ec709aaadac0405 Mon Sep 17 00:00:00 2001 From: Noah <33094578+coolreader18@users.noreply.github.com> Date: Tue, 1 Dec 2020 15:04:16 -0600 Subject: [PATCH 1/3] Move main.rs to lib.rs --- src/{main.rs => lib.rs} | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) rename src/{main.rs => lib.rs} (98%) diff --git a/src/main.rs b/src/lib.rs similarity index 98% rename from src/main.rs rename to src/lib.rs index 064b44567..56ab26413 100644 --- a/src/main.rs +++ b/src/lib.rs @@ -23,7 +23,12 @@ use std::str::FromStr; mod shell; -fn main() { +pub use rustpython_vm; + +pub fn run(init: F) -> ! +where + F: FnOnce(&mut VirtualMachine), +{ #[cfg(feature = "flame-it")] let main_guard = flame::start_guard("RustPython main"); env_logger::init(); @@ -45,13 +50,16 @@ fn main() { } // We only include the standard library bytecode in WASI when initializing - let init = if cfg!(target_os = "wasi") { + let init_param = if cfg!(target_os = "wasi") { InitParameter::Internal } else { InitParameter::External }; - let interp = Interpreter::new(settings, init); + let interp = Interpreter::new_with_init(settings, |vm| { + init(vm); + init_param + }); let exitcode = interp.enter(move |vm| { let res = run_rustpython(vm, &matches); @@ -110,7 +118,7 @@ fn main() { exitcode }); - process::exit(exitcode); + process::exit(exitcode) } fn flush_std(vm: &VirtualMachine) { From 95c1392d65c8cf232c78f0248a766e00bf547af3 Mon Sep 17 00:00:00 2001 From: Noah <33094578+coolreader18@users.noreply.github.com> Date: Tue, 1 Dec 2020 15:06:02 -0600 Subject: [PATCH 2/3] Re-add main.rs using the rustpython lib --- src/main.rs | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 src/main.rs diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 000000000..fdc9f7037 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,3 @@ +pub fn main() { + rustpython::run(|_vm| {}) +} From 6dc93c5e5a4f6e664401b182dd9d5489ea8e953f Mon Sep 17 00:00:00 2001 From: Noah <33094578+coolreader18@users.noreply.github.com> Date: Tue, 1 Dec 2020 15:20:02 -0600 Subject: [PATCH 3/3] Add doc comments --- src/lib.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 56ab26413..2ef1def6b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,43 @@ +//! This is the `rustpython` binary. If you're looking to embed RustPython into your application, +//! you're likely looking for the [`rustpython-vm`](https://docs.rs/rustpython-vm) crate. +//! +//! You can install `rustpython` with `cargo install rustpython`, or if you'd like to inject your +//! own native modules you can make a binary crate that depends on the `rustpython` crate (and +//! probably `rustpython-vm`, too), and make a `main.rs` that looks like: +//! +//! ```no_run +//! use rustpython_vm::{pymodule, py_freeze}; +//! fn main() { +//! rustpython::run(|vm| { +//! vm.add_native_module("mymod".to_owned(), Box::new(mymod::make_module)); +//! vm.add_frozen(py_freeze!(source = "def foo(): pass", module_name = "otherthing")); +//! }); +//! } +//! +//! #[pymodule] +//! mod mymod { +//! use rustpython_vm::builtins::PyStrRef; +//TODO: use rustpython_vm::prelude::*; +//! use rustpython_vm::common::borrow::BorrowValue; +//! +//! #[pyfunction] +//! fn do_thing(x: i32) -> i32 { +//! x + 1 +//! } +//! +//! #[pyfunction] +//! fn other_thing(s: PyStrRef) -> (String, usize) { +//! let new_string = format!("hello from rust, {}!", s); +//! let prev_len = s.borrow_value().len(); +//! (new_string, prev_len) +//! } +//! } +//! ``` +//! +//! The binary will have all the standard arguments of a python interpreter (including a REPL!) but +//! it will have your modules loaded into the vm. +#![allow(clippy::needless_doctest_main)] + #[macro_use] extern crate clap; extern crate env_logger; @@ -25,6 +65,8 @@ mod shell; pub use rustpython_vm; +/// The main cli of the `rustpython` interpreter. This function will exit with `process::exit()` +/// based on the return code of the python code ran through the cli. pub fn run(init: F) -> ! where F: FnOnce(&mut VirtualMachine),