Skip to content

Commit

Permalink
db_call (doesn't work yet)
Browse files Browse the repository at this point in the history
  • Loading branch information
voltrevo committed Nov 2, 2023
1 parent 86339f3 commit e9b55b2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
9 changes: 4 additions & 5 deletions valuescript_vm/src/vs_storage_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,16 @@ impl VsStoragePtr {
}

pub fn get(&self) -> Val {
#[allow(unused_mut)] // Used in commented code
let mut cache = self.cache.borrow_mut();

if let Some(val) = &*cache {
return val.clone();
}

todo!()
// let val = /* TODO */;
// *cache = Some(val.clone());
// val
let val = self.resolver.resolve();
*cache = Some(val.clone());

val
}
}

Expand Down
47 changes: 46 additions & 1 deletion vstc/src/db_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub fn db_command(args: &[String]) {

match args.get(3).map(|s| s.as_str()) {
Some("new") => db_new(&path, args.get(4..).unwrap_or_default()),
Some("call") => println!("TODO: on database {}, call {:?}", path, args.get(4)),
Some("call") => db_call(&path, args.get(4..).unwrap_or_default()),
Some("-i") => println!("TODO: use database {} interactively", path),
arg => {
if let Some(arg) = arg {
Expand Down Expand Up @@ -126,3 +126,48 @@ fn db_new(path: &str, args: &[String]) {

println!("Created database at {}", path);
}

fn db_call(path: &str, args: &[String]) {
let fn_file = match args.get(0) {
Some(fn_file) => fn_file,
None => {
println!("ERROR: Missing function file\n");
show_help();
exit(1);
}
};

let fn_ = Rc::new(to_bytecode(format_from_path(fn_file), fn_file))
.decoder(0)
.decode_val(&mut vec![]);

let args = args
.get(1..)
.unwrap_or_default()
.iter()
.map(|s| s.clone().to_val())
.collect::<Vec<_>>();

let mut storage = Storage::new(SledBackend::open(path).unwrap());

let mut vm = VirtualMachine::default();

let mut instance = storage
.get_head(storage_head_ptr(b"state"))
.unwrap()
.unwrap();

match vm.run(None, &mut instance, fn_, args) {
Ok(res) => {
println!("{}", res.pretty());
}
Err(err) => {
println!("Uncaught exception: {}", err.pretty());
exit(1);
}
}

storage
.set_head(storage_head_ptr(b"state"), &instance)
.unwrap();
}

0 comments on commit e9b55b2

Please sign in to comment.