Giter Club home page Giter Club logo

flutter-luavm's Introduction

Flutter Lua VM Plugin

Pub package Dartdoc reference

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.1
  • Supports multiple Lua instances (don't be too much. <=100 instances)
  • Each Lua instance runs in a separate thread
  • 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
vmplugin vmplugin Plugin Specific

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

about require

To use internal Lua modules, no require is needed.

require is now used to import Lua code only, please set package.path properly before require.

This may import local lua file:

package.path = vmplugin.doc_dir.."/?.lua"

local add = require('lib-add')

vmplugin

It is a plugin specific module that provides platform support.

local doc_dir = vmplugin.doc_dir	-- Absolute directory for Application Document 
local platform = vmplugin.platform  -- "ios" or "android"
local temp_dir = vmplugin.temp_dir  -- Absolute directory for Temporary files, corresponding to Temporary Directory of iOS and CacheDir of Android

local res = vmplugin.invoke_method("method-name","method-args")	-- this will invoke a Method Channel call, can be handled by Dart/Other Flutter plugins, currently only support pure string arguments

The invoke_method function can be very useful.

For instance, http get can be archieved this way (together with plugin dio) :

  • In Lua
local jres = vmplugin.invoke_method('httpGet','https://api.myip.com');
  • In Dart
Future<String> httpGet(String url) async {
  final res = await Dio().get<String>(url);
  return res.data;
}

Luavm.setMethodHandler('httpGet', httpGet);

Please try http-test.lua in example project to see more information.

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.