forked from Rust-related/RustPython
Add example projects
This commit is contained in:
committed by
Jeong, YunWon
parent
0bd1a3efb2
commit
ff9aa0fc54
6
.github/workflows/ci.yaml
vendored
6
.github/workflows/ci.yaml
vendored
@@ -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:
|
||||
|
||||
@@ -176,4 +176,4 @@ perf = "warn"
|
||||
style = "warn"
|
||||
complexity = "warn"
|
||||
suspicious = "warn"
|
||||
correctness = "warn"
|
||||
correctness = "warn"
|
||||
|
||||
2
example_projects/.gitignore
vendored
Normal file
2
example_projects/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
*/target
|
||||
*/Cargo.lock
|
||||
10
example_projects/aheui-rust.md
Normal file
10
example_projects/aheui-rust.md
Normal file
@@ -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.
|
||||
9
example_projects/barebone/Cargo.toml
Normal file
9
example_projects/barebone/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "example-barebone"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
rustpython-vm = { path = "../../vm", default-features = false }
|
||||
|
||||
[workspace]
|
||||
17
example_projects/barebone/src/main.rs
Normal file
17
example_projects/barebone/src/main.rs
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
11
example_projects/frozen_stdlib/Cargo.toml
Normal file
11
example_projects/frozen_stdlib/Cargo.toml
Normal file
@@ -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]
|
||||
39
example_projects/frozen_stdlib/src/main.rs
Normal file
39
example_projects/frozen_stdlib/src/main.rs
Normal file
@@ -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();
|
||||
}
|
||||
Reference in New Issue
Block a user