flutter_lua_bridge 0.2.0
flutter_lua_bridge: ^0.2.0 copied to clipboard
Typed Dart / Lua interoperability with annotation-based code generation.
flutter_lua_bridge #
Run Lua in Flutter and Dart apps with synchronous calls in both directions.
Lua is compiled from source for the target platform by a build hook; nothing
needs to be installed. LuaRuntime is the single entry point, used directly
or through typed APIs generated by
flutter_lua_bridge_codegen.
flutter pub add flutter_lua_bridge
import 'package:flutter_lua_bridge/flutter_lua_bridge.dart';
final lua = LuaRuntime.create(); // LuaLibrary.safe by default
try {
lua['greet'] = lua.function((args) => 'hello ${args.first}');
print(lua.eval('return greet("lua")')); // hello lua
final config = lua.eval('return { size = 3, tags = { "a", "b" } }') as LuaTable;
print(config.toDart()); // {size: 3, tags: [a, b]}
} on LuaBridgeException catch (e) {
print('${e.message}\n${e.traceback}');
} finally {
lua.close();
}
- Values cross the boundary as
null,bool,int,double,String,Uint8List, or a handle (LuaTable,LuaFunction,LuaThread,LuaUserdata); DartMapandListarguments are copied into new tables. - Errors never crash the process: every operation runs protected, and a
failure is a
LuaBridgeExceptionwithstatus,messageandtraceback. - Sandboxing: the default
LuaLibrary.safeleaves outio,os,debugand native module loading; otherLuaLibraryflags add them. Memory limits and interrupts are available on the runtime. - Threading: calls are synchronous and confined to the isolate that created the runtime.
Typed contracts #
Describe the API in Dart and let the generator write both sides:
@LuaApi(module: 'battle')
abstract class BattleApi {
int calculateDamage(Fighter attacker, Fighter defender);
}
This library re-exports the annotations, so a contract imports only
package:flutter_lua_bridge/flutter_lua_bridge.dart. See
flutter_lua_bridge_codegen
and the step-by-step
Getting started.
Lua from a server #
The Lua source is plain text loaded at runtime, so it can be delivered without an app release. It must implement the contract the app was built with, as source text (bytecode is rejected) within the sandbox you open. See Loading Lua at runtime.
Lua version #
Lua 5.3, 5.4 (default) and 5.5 are bundled. Pick one in the workspace root
pubspec.yaml:
hooks:
user_defines:
flutter_lua_bridge:
lua_version: "5.5"
A local source tree or a downloaded archive works too; see Choosing the Lua source.