Crates.io | silt-lua |
lib.rs | silt-lua |
version | 0.1.1 |
source | src |
created_at | 2023-09-09 16:36:14.680265 |
updated_at | 2023-09-14 05:39:35.687241 |
description | A pure rust Lua interpreter and virtual machine |
homepage | |
repository | https://github.com/auxnon/silt-lua |
max_upload_size | |
id | 968243 |
size | 312,812 |
This project was originally created to answer a problem with the current rust landscape in lacking a complete lua interpreter solution written in 100% rust. That may not necessarily be true anymore. Even still, the existing implementations at the time were missing crucial features and optimizations to satisfy the requirements of my closed source project Petrichor64, so here we are.
Core focus of the library is a basic interpreter with minor overhead and relative speed comparison to standard lua. The goal is for a perfect wasm32-unknown-unknown target build. No emscripten necessary! UserData will mirror traits used by the mlua and rlua crates to allow easy drop in.
Secondary goals are CLI, LSP, and as much standard library compliance as possible. There's also a concern for safety, as a number of unsafe code is present in the VM. In the future a safe and unsafe version of the VM will be hidden under a feature flag, on the assumption the unsafe version will operate marginally faster. Exact benchmarks will have to be determined.
There's also desire to add some custom non-lua syntax pulling syntactic sugar from other languages, such as allowing for !=
, typing, and maybe even arrow functions. This superset of lua will fall under a feature flag and by default be disabled as who really wants my opinionated concept of a programming language? This superset satisfies personal requirements but I'm open to requests if an interest is shown. Even if this superset is enabled it will not conflict with standard lua.
This library has been written from the ground up with observations of the lua language and documentation. Source code has not been referenced so naturally the VM will always have some noticeable differences that will hopefully be ironed out eventually. This includes the byte code, as lua now operates under wordcode to work with it's register based VM. Feel free to submit an issue for anything particularly glaring. This project is a learning exercise so there is a good deal of naive approaches I'm taking to make this a reality.
#
will give the actual hashmap length, not the "consecutive length" that lua does.print
and clock
function for testing. Feel free to utilize register_native_function
on the VM instance to fill any gaps for nowA simple wasm module example can be compiled via wasm-pack. Print calls a global jprintln function if one exists. A live example can be viewed at MakeAvoy.com
Keep in mind these may be polarizing and an LSP will flag them as an error
"bang"
Bang usage ! for not or not equal (~=) can be used if you're hard pressed to not use them like I am. They do not replace not or ~=, only act as builtin aliases"under-number"
Numbers can include underscores which are ignored characters used for readability, borrowed right from rust"short-declare"
Stolen right from Go you can now declare a local variable with :=
such as a := 2
"implicit-return"
Blocks and statements will implicitly return the last value on the stack unless ending in a ;
func_name =param -> param+1
in addition to this arrow functions have implicit returns. The last value on the stack is always returned. Regular functions without a return
keyword will return nil as before. +=
-=
*=
/=
let source_in = r#"
do
local d=5
function sum()
local a=1
local b=2
local c=3
return a+b+c+d+8
end
return sum()
end
"#;
let mut vm = Lua::new();
vm.load_standard_library();
match vm.run(source_in) {
Ok(value) => {
println!(">> {}", value);
assert_eq!(value, Value:Integer(19));
}
Err(e) => {
e.iter().for_each(|e| println!("!!Err: {}", e));
}
}