Struct bpf_script::Compiler
source · pub struct Compiler<'a> { /* private fields */ }
Implementations
sourceimpl<'a> Compiler<'a>
impl<'a> Compiler<'a>
sourcepub fn create(types: &'a TypeDatabase) -> Self
pub fn create(types: &'a TypeDatabase) -> Self
sourcepub fn capture(&mut self, name: &str, value: i64)
pub fn capture(&mut self, name: &str, value: i64)
Used to capture variables from the outer scope into the BPF program being compiled. This is mostly used to capture map identifers to pass to BPF helpers and for other integer values that need to be captured. In the future, this will be extended to capture arbitrary types making sharing between Rust and BPF more seamless.
Arguments
name
- The name of the variable when referenced from the script.
value
- The value of the variable.
Example
use bpf_script::{Compiler, TypeDatabase};
let mut database = TypeDatabase::default();
let mut compiler = Compiler::create(&database);
compiler.capture("outer", 0xdeadbeef);
compiler.compile(r#"
fn()
return outer
"#).expect("Failed to compile.");
sourcepub fn compile(&mut self, script_text: &str) -> InternalResult<()>
pub fn compile(&mut self, script_text: &str) -> InternalResult<()>
Compile a given script.
Arguments
script_text
- The script to compile, as a string.
Example
use bpf_script::{Compiler, TypeDatabase};
let mut database = TypeDatabase::default();
database.add_integer(Some("u32"), 4, false);
let mut compiler = Compiler::create(&database);
compiler.compile(r#"
fn(a: u32)
return a
"#).expect("Failed to compile.");
sourcepub fn get_instructions(&self) -> &[Instruction]
pub fn get_instructions(&self) -> &[Instruction]
Returns the internally held instructions after compile
has been called.
Example
use bpf_script::{Compiler, TypeDatabase};
let mut database = TypeDatabase::default();
database.add_integer(Some("u32"), 4, false);
let mut compiler = Compiler::create(&database);
compiler.compile(r#"
fn(a: u32)
return a
"#).expect("Failed to compile.");
for ins in compiler.get_instructions() {
println!("{}", ins);
}
sourcepub fn get_bytecode(&self) -> Vec<u64>
pub fn get_bytecode(&self) -> Vec<u64>
Returns the bytecode of a program after compile
has been called. These
are the raw instructions that make up a BPF program that can be passed
directly to the kernel.
Example
use bpf_script::{Compiler, TypeDatabase};
let mut database = TypeDatabase::default();
database.add_integer(Some("u32"), 4, false);
let mut compiler = Compiler::create(&database);
compiler.compile(r#"
fn(a: u32)
return a
"#).expect("Failed to compile.");
for ins in compiler.get_bytecode() {
println!("{}", ins);
}
Auto Trait Implementations
impl<'a> RefUnwindSafe for Compiler<'a>
impl<'a> Send for Compiler<'a>
impl<'a> Sync for Compiler<'a>
impl<'a> Unpin for Compiler<'a>
impl<'a> UnwindSafe for Compiler<'a>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more