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); Dart Map and List arguments are copied into new tables.
  • Errors never crash the process: every operation runs protected, and a failure is a LuaBridgeException with status, message and traceback.
  • Sandboxing: the default LuaLibrary.safe leaves out io, os, debug and native module loading; other LuaLibrary flags 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.

Libraries

flutter_lua_bridge
Dart / Lua interoperability on Lua 5.3, 5.4 or 5.5.