arcticfox1919/LuaDardo
A Lua virtual machine written in Dart
go to github downloadLuaDardo
A Lua virtual machine written in Dart, which implements Lua5.3 version.
Example:
dependencies:
lua_dardo: ^0.0.3
import 'package:lua_dardo/lua.dart';
void main(List<String> arguments) {
LuaState state = LuaState.newState();
state.openLibs();
state.loadString(r'''
a=10
while( a < 20 ) do
print("a value is", a)
a = a+1
end
''');
state.call(0, 0);
}
Usage
The LuaDardo library is compatible with most Lua C APIs. For the mutual call between Dart and Lua, please refer to the Lua C API guide.
Some simple examples:
LuaState state = LuaState.newState();
// Load the Lua standard library
state.openLibs();
state.loadString("print('hello')");
state.call(0, 0);
Dart calls Lua
Get Lua variables:
-- test.lua
a = 100
b = 120
Dart code:
LuaState ls = LuaState.newState();
ls.openLibs();
ls.doFile("test.lua");
// a push into the stack
ls.getGlobal("a");
if(ls.isNumber(-1)){
var a = ls.toNumber(-1);
print("a=$a");
}
// b push into the stack
ls.getGlobal("b");
if(ls.isNumber(-1)){
var b = ls.toNumber(-1);
print("b=$b");
}
Get lua global table:
-- test.lua
mytable = {k1 = 1, k2 = 2.34, k3 = "test"}
Dart:
ls.getGlobal("mytable");
ls.pushString("k1");
// Pop the key at the top of the stack, get the value of the key, and push the result onto the top of the stack
ls.getTable(-2);
if(ls.isInteger(-1)){
// Get the value of key k1
var k1 = ls.toInteger(-1);
}
// Repeat
ls.pushString("k2");
ls.getTable(-2);
if(ls.isNumber(-1)){
var k2 = ls.toNumber(-1);
}
A simpler alternative: getField
ls.getGlobal("mytable");
ls.getField(-1, "k1");
if(ls.isInteger(-1)){
var k1 = ls.toInteger(-1);
}
Call lua function:
-- test.lua
function myFunc()
print("myFunc run")
end
Dart:
ls.doFile("test.lua");
ls.getGlobal("myFunc");
if(ls.isFunction(-1)){
ls.pCall(0, 0, 0);
}
The pCall
method has three parameters. The first parameter indicates the number of parameters of the called Lua function, and the second parameter indicates the number of return values of the called Lua function.
Lua calls Dart
// Push value onto stack
ls.pushString("Alex");
// Set variable name
ls.setGlobal("name");
Lua:
-- Get global variable name
print(name) -- Alex
Define global table in Dart:
// Create a table and push it onto the stack
ls.newTable();
// Push a key onto the stack
ls.pushString("name");
// Push the value onto the stack. Note that at this time the index of the table in the stack becomes -3
ls.pushString("Alex");
// Set the above key-value pair to the table, and pop up the key and value
ls.setTable(-3);
// Set the variable name to the table, and pop up the table
ls.setGlobal("students");
Lua:
-- Equivalent to a table:students = {name="Alex"}
print(students.name)
Call Dart function:
import 'package:lua_dardo/lua.dart';
import 'dart:math';
// wrapper function must use this signature:int Function(LuaState ls)
// the return is the number of returned values
int randomInt(LuaState ls) {
int max = ls.checkInteger(1);
ls.pop(1);
var random = Random();
var randVal = random.nextInt(max);
ls.pushInteger(randVal);
return 1;
}
void main(List<String> arguments) {
LuaState state = LuaState.newState();
state.openLibs();
state.pushDartFunction(randomInt);
state.setGlobal('randomInt');
// execute the Lua script to test the randomInt function
state.loadString('''
rand_val = randomInt(10)
print('random value is '..rand_val)
''');
state.call(0, 0);
}
Some people are curious about how to access Lua tables in Dart. Here is a simple example:
state.loadString('''
rand_val = randomInt(10,{ ["hello"] = "World", ["hello22"] = "World132414" })
print('random value is '..rand_val)
''');
int randomInt(LuaState ls) {
int? max = ls.checkInteger(1);
ls.getField(2, "hello");
// This is a debugging method that looks at the stack
ls.printStack();
var hello = ls.toStr(-1);
print(hello);
ls.pop(1);
ls.getField(2, "hello22");
var hello22 = ls.toStr(-1);
print(hello22);
ls.pop(1);
var random = Random();
var randVal = random.nextInt(max!);
ls.pushInteger(randVal);
return 1;
}
Try on Flutter
function getContent1()
return Row:new({
children={
GestureDetector:new({
onTap=function()
flutter.debugPrint("--------------onTap--------------")
end,
child=Text:new("click here")}),
Text:new("label1"),
Text:new("label2"),
Text:new("label3"),
},
mainAxisAlign=MainAxisAlign.spaceEvenly,
})
end
function getContent2()
return Column:new({
children={
Row:new({
children={Text:new("Hello"),Text:new("Flutter")},
mainAxisAlign=MainAxisAlign.spaceAround
}),
Image:network('https://gitee.com/arcticfox1919/ImageHosting/raw/master/img/flutter_lua_test.png'
,{fit=BoxFit.cover})
},
mainAxisSize=MainAxisSize.min,
crossAxisAlign=CrossAxisAlign.center
})
end
For use in flutter, see here.
一些中文资料:
![](/images/banner-woo.jpg)
excellent projects related to arcticfox1919/LuaDardo recommend downloading
AppFlowy
47859
AppFlowy is an open-source alternative to Notion. You are in charge of your data and customizations. Built with Flutter and Rust.
localsend
33797
An open-source cross-platform alternative to AirDrop
spotube
23772
🎧 Open source Spotify client that doesn't require Premium nor uses Electron! Available for both desktop & mobile!
revanced-manager
15035
💊 Application to use ReVanced on Android
gsy_github_app_flutter
14559
Flutter 超完整的开源项目,功能丰富,适合学习和日常使用。GSYGithubApp系列的优势:我们目前已经拥有Flutter、Weex、ReactNative、kotlin 四个版本。 功能齐全,项目框架内技术涉及面广,完成度高,持续维护,配套文章,适合全面学习,对比参考。跨平台的开源Github客户端App,更好的体验,更丰富的功能,旨在更好的日常管理和维护个人Github,提供更好更方便的驾车体验Σ( ̄。 ̄ノ)ノ。同款Weex版本 : https://github.com/CarGuo/GSYGithubAppWeex 、同款React Native版本 : https://github.com/CarGuo/GSYGithubApp 、原生 kotlin 版本 https://github.com/CarGuo/GSYGithubAppKotlin
dio
12249
A powerful HTTP client for Dart and Flutter, which supports global settings, Interceptors, FormData, aborting and canceling a request, files uploading and downloading, requests timeout, custom adapters, etc.
gopeed
11832
A modern download manager that supports all platforms. Built with Golang and Flutter.
bloc
11441
A predictable state management library that helps implement the BLoC design pattern
getx
9899
Open screens/snackbars/dialogs/bottomSheets without context, manage states and inject dependencies easily with Get.
flame
8846
A Flutter based game engine.
flutter_deer
7595
🦌 Flutter 练习项目(包括集成测试、可访问性测试)。内含完整UI设计图,更贴近真实项目的练习。Flutter practice project (including integration testing and accessibility testing). Contains complete UI design drawings for a more realistic practice project.
fish-redux
7343
An assembled flutter application framework.
fl_chart
6451
FL Chart is a highly customizable Flutter chart library that supports Line Chart, Bar Chart, Pie Chart, Scatter Chart, and Radar Chart.
pixez-flutter
6360
一个支持免代理直连及查看动图的第三方Pixiv flutter客户端
Flutter-Responsive-Admin-Panel-or-Dashboard
6355
Responsive Admin Panel or Dashboard using Flutter
aidea
6021
AIdea 是一款支持 GPT 以及国产大语言模型通义千问、文心一言等,支持 Stable Diffusion 文生图、图生图、 SDXL1.0、超分辨率、图片上色的全能型 APP。
riverpod
5828
A reactive caching and data-binding framework. Riverpod makes working with asynchronous code a breeze.
pikapika
5463
美观易用且无广告的漫画和游戏客户端,同时支持MacOS,Windows,Android,iOS。
provider
5029
InheritedWidgets, but simple
getwidget
4416
Most popular and easy to use open source UI library with 1000+ Widgets to build flutter app.