use glyph_runtime::{Capability, Instruction, VMConfig, Value, VM};
fn main() {
println!("=== Glyph VM Capability Demo ===\n");
println!("Test 1: Attempting network access without capability");
let config = VMConfig::default();
let mut vm = VM::new(config);
vm.load_bytecode(vec![vec![
Instruction::Push(Value::Str("Checking network access...".to_string())),
Instruction::TraceValue("attempt".to_string()),
Instruction::RequireCapability("network.fetch".to_string()),
Instruction::Push(Value::Str("Network access granted!".to_string())),
Instruction::Halt,
]]);
match vm.execute() {
Ok(result) => println!("Result: {result}"),
Err(e) => println!("Expected error: {e}"),
}
println!("\nTest 2: Attempting audio with granted capability");
let mut config = VMConfig::default();
config.capabilities.grant(Capability::AudioSpeak);
let mut vm = VM::new(config);
vm.load_bytecode(vec![vec![
Instruction::CheckCapability("audio.speak".to_string()),
Instruction::JumpIfNot(4), Instruction::Push(Value::Str("Hello from Glyph!".to_string())),
Instruction::TraceValue("audio_message".to_string()),
Instruction::Push(Value::Str("Audio capability granted".to_string())),
Instruction::Halt,
]]);
match vm.execute() {
Ok(result) => {
println!("Result: {result}");
for (event, value) in vm.telemetry() {
println!(" Telemetry - {event}: {value}");
}
}
Err(e) => println!("Error: {e}"),
}
println!("\nTest 3: Memory-limited execution");
let mut config = VMConfig::default();
config.capabilities.grant(Capability::MemoryLimited(1024)); let mut vm = VM::new(config);
let mut program = vec![
Instruction::Push(Value::Int(0)), Instruction::BindLocal("counter".to_string()),
];
let loop_start = 2;
program.extend(vec![
Instruction::LoadLocal("counter".to_string()),
Instruction::Push(Value::Int(1)),
Instruction::Add,
Instruction::Dup,
Instruction::BindLocal("counter".to_string()),
Instruction::Push(Value::Int(100)),
Instruction::Ge,
Instruction::JumpIf(20), Instruction::Push(Value::Str("Item".to_string())),
Instruction::LoadLocal("counter".to_string()),
Instruction::MakeList(2),
Instruction::TraceValue("created_list".to_string()),
Instruction::Jump(loop_start),
Instruction::Push(Value::Str("Completed successfully".to_string())),
Instruction::Halt,
]);
vm.load_bytecode(vec![program]);
match vm.execute() {
Ok(result) => {
println!("Result: {result}");
println!(
"Memory usage: {} bytes ({:.2}%)",
vm.memory_usage(),
vm.memory_usage_percentage()
);
let telemetry = vm.telemetry();
println!("Created {} lists", telemetry.len());
}
Err(e) => println!("Error: {e}"),
}
println!("\nTest 4: Pattern-based capability matching");
let mut config = VMConfig::default();
config.capabilities.grant(Capability::NetworkFetch(
"https://api.example.com/*".to_string(),
));
let mut vm = VM::new(config);
vm.load_bytecode(vec![vec![
Instruction::Push(Value::Str("https://api.example.com/data".to_string())),
Instruction::TraceValue("checking_url".to_string()),
Instruction::CheckCapability("network.fetch".to_string()),
Instruction::TraceValue("capability_check_result".to_string()),
Instruction::Push(Value::Str("Pattern capability check complete".to_string())),
Instruction::Halt,
]]);
match vm.execute() {
Ok(result) => {
println!("Result: {result}");
for (event, value) in vm.telemetry() {
println!(" {event}: {value}");
}
}
Err(e) => println!("Error: {e}"),
}
}