From ff9aa0fc544916143442b2e419b8723d4ce7bb62 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Wed, 7 Aug 2024 06:51:19 +0900 Subject: [PATCH] Add example projects --- .github/workflows/ci.yaml | 6 ++++ Cargo.toml | 2 +- example_projects/.gitignore | 2 ++ example_projects/aheui-rust.md | 10 ++++++ example_projects/barebone/Cargo.toml | 9 +++++ example_projects/barebone/src/main.rs | 17 ++++++++++ example_projects/frozen_stdlib/Cargo.toml | 11 ++++++ example_projects/frozen_stdlib/src/main.rs | 39 ++++++++++++++++++++++ 8 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 example_projects/.gitignore create mode 100644 example_projects/aheui-rust.md create mode 100644 example_projects/barebone/Cargo.toml create mode 100644 example_projects/barebone/src/main.rs create mode 100644 example_projects/frozen_stdlib/Cargo.toml create mode 100644 example_projects/frozen_stdlib/src/main.rs diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 8cb71b067..bec7eafa3 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -148,6 +148,12 @@ jobs: - name: check compilation without threading run: cargo check ${{ env.CARGO_ARGS }} + - name: Test example projects + run: + cargo run --manifest-path example_projects/barebone/Cargo.toml + cargo run --manifest-path example_projects/frozen_stdlib/Cargo.toml + if: runner.os == 'Linux' + - name: prepare AppleSilicon build uses: dtolnay/rust-toolchain@stable with: diff --git a/Cargo.toml b/Cargo.toml index 2c407851a..b8e1c0c51 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -176,4 +176,4 @@ perf = "warn" style = "warn" complexity = "warn" suspicious = "warn" -correctness = "warn" \ No newline at end of file +correctness = "warn" diff --git a/example_projects/.gitignore b/example_projects/.gitignore new file mode 100644 index 000000000..661ad40fd --- /dev/null +++ b/example_projects/.gitignore @@ -0,0 +1,2 @@ +*/target +*/Cargo.lock diff --git a/example_projects/aheui-rust.md b/example_projects/aheui-rust.md new file mode 100644 index 000000000..d812ee244 --- /dev/null +++ b/example_projects/aheui-rust.md @@ -0,0 +1,10 @@ +# aheui-rust + +- Crate link: https://github.com/youknowone/aheui-rust/tree/main/rpaheui +- Creating a frozenlib: https://github.com/youknowone/aheui-rust/blob/main/rpaheui/src/lib.rs + +This crate shows you how to embed an entire Python project into bytecode and ship it. Follow the `FROZEN` constant and see how it's made and how you can use it. + +If you'd like to learn more about how to initialize the Standard Library with the `freeze-stdlib` feature, check out the example project at `example_projects/frozen_stdlib/src/main.rs`. + +Just a heads-up, it doesn't automatically resolve dependencies. If you have more dependencies than the standard library, Don't forget to also freeze them. diff --git a/example_projects/barebone/Cargo.toml b/example_projects/barebone/Cargo.toml new file mode 100644 index 000000000..ffae04e4b --- /dev/null +++ b/example_projects/barebone/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "example-barebone" +version = "0.1.0" +edition = "2021" + +[dependencies] +rustpython-vm = { path = "../../vm", default-features = false } + +[workspace] diff --git a/example_projects/barebone/src/main.rs b/example_projects/barebone/src/main.rs new file mode 100644 index 000000000..0d9ffbefa --- /dev/null +++ b/example_projects/barebone/src/main.rs @@ -0,0 +1,17 @@ +use rustpython_vm::Interpreter; + +pub fn main() { + let interp = Interpreter::without_stdlib(Default::default()); + let value = interp.enter(|vm| { + let max = vm.builtins.get_attr("max", vm)?; + let value = max.call((vm.ctx.new_int(5), vm.ctx.new_int(10)), vm)?; + vm.print((vm.ctx.new_str("python print"), value.clone()))?; + Ok(value) + }); + match value { + Ok(value) => println!("Rust repr: {:?}", value), + Err(err) => { + interp.finalize(err); + } + } +} diff --git a/example_projects/frozen_stdlib/Cargo.toml b/example_projects/frozen_stdlib/Cargo.toml new file mode 100644 index 000000000..12e9e5902 --- /dev/null +++ b/example_projects/frozen_stdlib/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "example_frozen_stdlib" +version = "0.1.0" +edition = "2021" + +[dependencies] +rustpython = { path = "../../", default-features = false, features = ["freeze-stdlib"] } +rustpython-vm = { path = "../../vm", default-features = false, features = ["freeze-stdlib"] } +rustpython-pylib = { path = "../../pylib", default-features = false, features = ["freeze-stdlib"] } + +[workspace] diff --git a/example_projects/frozen_stdlib/src/main.rs b/example_projects/frozen_stdlib/src/main.rs new file mode 100644 index 000000000..dd6cfc3b3 --- /dev/null +++ b/example_projects/frozen_stdlib/src/main.rs @@ -0,0 +1,39 @@ +/// Setting up a project with a frozen stdlib can be done *either* by using `rustpython::InterpreterConfig` or `rustpython_vm::Interpreter::with_init`. +/// See each function for example. +/// +/// See also: `aheui-rust.md` for freezing your own package. + +use rustpython_vm::{PyResult, VirtualMachine}; + +fn run(keyword: &str, vm: &VirtualMachine) -> PyResult<()> { + let json = vm.import("json", 0)?; + let json_loads = json.get_attr("loads", vm)?; + let template = r#"{"key": "value"}"#; + let json_string = template.replace("value", keyword); + let dict = json_loads.call((vm.ctx.new_str(json_string),), vm)?; + vm.print((dict,))?; + Ok(()) +} + +fn interpreter_with_config() { + let interpreter = rustpython::InterpreterConfig::new() + .init_stdlib() + .interpreter(); + // Use interpreter.enter to reuse the same interpreter later + interpreter.run(|vm| run("rustpython::InterpreterConfig", vm)); +} + +fn interpreter_with_vm() { + let interpreter = rustpython_vm::Interpreter::with_init(Default::default(), |vm| { + // This is unintuitive, but the stdlib is out of the vm crate. + // Any suggestion to improve this is welcome. + vm.add_frozen(rustpython_pylib::FROZEN_STDLIB); + }); + // Use interpreter.enter to reuse the same interpreter later + interpreter.run(|vm| run("rustpython_vm::Interpreter::with_init", vm)); +} + +fn main() { + interpreter_with_config(); + interpreter_with_vm(); +}