Skip to content

wwtssu/flutter-luavm

 
 

Repository files navigation

Flutter Lua VM Plugin With HTTP & JSON

Pub package Dartdoc reference

Fork from tgarm/flutter-luavm

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.3.5
  • Support multiple Lua instances (don't be too much. <=100 instances)
  • Lua script runs in platform thread
  • Use Java/ObjC to avoid the annoying Swift version compatibility problem

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.3"]
  • 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.

Use http and json

  • http: luasocket
  • json: dkjson
local socket = _G["socket"]
local json = _G["json"]

local response_body = {}
local res, code, response_headers = socket.http.request{
    url = myUrl,
    method = "GET",
    sink = ltn12.sink.table(response_body)
}
local jsonData = table.concat(response_body)
local obj, pos, err = json.decode (jsonData, 1, nil)
if not err then
   -- dosomething
end
response_body = nil

Use Network Async

Run this code when app start

Luavm.Init();

And replace

Luavm.eval("name","code");

to

Luavm.evalAsync("name","code");

About

Vanilla Lua vm for Flutter

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C 85.0%
  • C++ 4.9%
  • Lua 4.6%
  • CMake 4.2%
  • Dart 0.5%
  • Objective-C 0.3%
  • Other 0.5%