luavm 0.2.1 luavm: ^0.2.1 copied to clipboard
Provide simple Lua VMs to Flutter (now supports vanilla Lua 5.4.0)
Flutter Lua VM Plugin #
A Flutter plugin provides Lua virtual machine
This plugin is inspired by flutter_lua, a Go based Lua implementation for Flutter.
Getting Started #
Features
- Supports the latest stable vanilla Lua 5.4.0
- Supports multiple Lua instances (don't be too much. <=100 instances)
- Lua "print" function outputs to Flutter console & Logging
- Lua script runs in platform thread
- Use Java/ObjC to avoid the annoying Swift version compatibility problem
Lua Modules
Modules are loaded when a Lua VM starts. Can be used in Lua code directly.
Name | Global Name | Version |
---|---|---|
LuaFileSystem | lfs | 1.8.0 |
lua-cjson | cjson | 2.1.0 |
cjson_safe | 2.1.0 |
Limitations
- Lua library "os" is NOT supported yet, due to unsupported functions in iOS: system and tmpnam
- All returned values will be converted to string
Usage #
Open a new VM
VM instances are named to distinguish each other.
import 'package:luavm/luavm.dart';
...
await Luavm.open("vm-name");
Run Lua Code
When VM is opened, run Lua code with 'eval' function:
- To load a Lua function:
await Luavm.eval("name","function luafn(a,b) return a+b end" );
- To simply run Lua code:
final res = await Luavm.eval("name","return _VERSION")
res should be returned as:
["Lua 5.4"]
- To call a Lua function:
final res = await Luavm.eval("name","return luafn(1,2)");
Luavm.eval returns a list of String, contains each value returned from Lua function.
final res = await Luavm.eval("name","return 1,2,'hello'");
should return a Dart list:
["1","2","hello"]
Close Lua VM
await Luavm.close("name");
Error Handling
Errors will be thrown as LuaError which contains error message as a string.
Lua Module Usage #
cjson
local cj = cjson.new()
local tbl = {a=1,b=2,c={'a','b','c'}}
local txt = cj.encode(tbl)
print(str)
local tres = cj.decode(txt)
print(tres.c[1])
Besides cjson, cjson_safe is also available to use.
lfs
for file in lfs.dir(spath) do
print ('-',file)
end
How to contribute #
Welcome to create issue about bug, feature request, etc.