Skip to content

Instantly share code, notes, and snippets.

@fkaa
Created June 16, 2017 15:32
Show Gist options
  • Save fkaa/5fef46fead46c54f568dcfe5ef46f0ec to your computer and use it in GitHub Desktop.
Save fkaa/5fef46fead46c54f568dcfe5ef46f0ec to your computer and use it in GitHub Desktop.
fn example() -> Result<()> {
let mut state = State::new();
fn add(stack: FunctionStack) -> Result<i32> {
let sz = stack.check_size(1..)?;
let mut sum = 0f64;
for i in 1..(sz + 1) {
sum += stack.arg::<f64>(i)?;
}
stack.push(sum);
Ok(1)
}
state.set("test", Table::new(|cxt| {
cxt.set("add", add);
}));
state.eval(r#"
print(test.add(5)) -- 5
print(test.add(5, 10)) -- 15
print(test.add(5, 10, 100, 200)) -- 315
"#)?;
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment