Skip to main content

flutter_lua_bridge_codegen 0.2.0 copy "flutter_lua_bridge_codegen: ^0.2.0" to clipboard
flutter_lua_bridge_codegen: ^0.2.0 copied to clipboard

build_runner builders that generate typed Dart/Lua bindings and Teal declarations for flutter_lua_bridge, and compile Teal to Lua.

example/example.md

Example #

A contract with one @LuaApi interface, implemented in Teal and called from Dart. The full Flutter app, with a card battle game, host APIs and every safety level, is in examples/flutter_lua_bridge_example.

dart run flutter_lua_bridge_codegen:init

lib/bridge/basic_api.dart, the contract:

import 'package:flutter_lua_bridge/flutter_lua_bridge.dart';

part 'basic_api.g.dart';

class RandomResult {
  const RandomResult({required this.luaVersion, required this.a, required this.b});
  final int luaVersion;
  final int a;
  final int b;
}

@LuaApi(module: 'basic')
abstract class BasicApi {
  RandomResult run();
}

lua/src/basic.tl, the implementation, type checked against the generated lua/types/basic_api_bridge.d.tl:

local bridge = require("basic_api_bridge")

local implementation: bridge.BasicApi = {
  run = function(): bridge.RandomResult
    local major, minor = _VERSION:match("^Lua (%d+)%.(%d+)")
    return {
      luaVersion = math.tointeger(tonumber(major) * 100 + tonumber(minor)),
      a = math.random(1, 1024),
      b = math.random(1, 1024),
    }
  end,
}

return bridge.bindBasicApi(implementation)
dart run build_runner build

This writes basic_api.g.dart and compiles the Teal to assets/lua/basic.lua (list assets/lua/ under flutter: assets:). Calling it:

final lua = LuaRuntime.create();
try {
  final source = await rootBundle.loadString('assets/lua/basic.lua');
  final result = BasicApiClient.load(runtime: lua, source: source).run();
  switch (result) {
    case LuaSuccess(:final value):
      print('Lua ${value.luaVersion}: ${value.a}, ${value.b}');
    case LuaFailure(:final kind, :final message):
      print('$kind: $message');
  }
} finally {
  lua.close();
}

At the default safety level O1 the call returns LuaCallResult<T>; with @LuaApi(module: 'basic', optimization: LuaOptimization.o3) it returns RandomResult directly and throws LuaBridgeException.

The Lua side also runs without the app:

dart run flutter_lua_bridge_codegen:run    # lua/test/main.tl and *_test.tl
0
likes
160
points
134
downloads

Documentation

API reference

Publisher

verified publishereplab.top

Weekly Downloads

build_runner builders that generate typed Dart/Lua bindings and Teal declarations for flutter_lua_bridge, and compile Teal to Lua.

Repository (GitHub)
View/report issues

Topics

#lua #code-generation

License

MIT (license)

Dependencies

analyzer, build, flutter_lua_bridge, flutter_lua_bridge_codegen_annotation, glob, path, source_gen

More

Packages that depend on flutter_lua_bridge_codegen