パッケージ、リポジトリ、アプリカテゴリを検索

パッケージ詳細v0.0.5

lua_dardo

Dartで書かれたLua仮想マシンで、Lua5.3バージョンを実装しています。

AndroidiOSmacOSWindowsLinux
30日間ダウンロード
582
いいね数
47
Pub ポイント
115

パッケージをインストール

flutter pub add lua_dardo

パッケージ概要

公開情報に基づく選定ガイドです。参考としてご利用ください。

LuaDardo は Dart で実装された Lua 5.3 仮想マシンであり、Dart アプリケーション内での Lua スクリプトのシームレスな統合を可能にします。Flutter および他の Dart 環境で、複数のプラットフォーム上で Lua コードを直接実行できます。このパッケージは、アプリケーションロジックを動的に拡張するのに理想的な軽量で埋め込み可能なスクリプトソリューションを提供します。Android、iOS、macOS、Windows、Linux を完全サポートしており、クロスプラットフォームでの一貫性とパフォーマンスを提供します。Lua 5.3 標準との互換性を重視した設計により、Lua で書かれたスクリプトの信頼性ある動作が保証されます。

用途

  • FlutterアプリにLuaスクリプトを埋め込む
  • 動的ゲームロジックスクリプト
  • 拡張可能な構成システム
  • 実行時コード評価
  • プラグインベースのアーキテクチャ

主な機能

  • Lua 5.3互換性
  • クロスプラットフォーム対応
  • 軽量な仮想マシン
  • Dartネイティブ統合
  • 文字列またはファイルからのスクリプト実行

適している対象

  • スクリプティングが必要なFlutter開発者
  • 動的な挙動を備えたゲーム開発
  • プラグイン拡張性を必要とするアプリケーション
  • 埋め込みスクリプティングが必要なツール

注意事項

  • Lua構文の理解が必要です
  • Lua 5.3 機能に限定
  • パフォーマンスはスクリプトの複雑さに依存します
  • 組み込みのデバッグツールなし

元の README

関連するソースリポジトリの README 原文のスナップショットです。最新の内容はリポジトリをご覧ください。

LuaDardo

logo


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.


一些中文资料:

Flutter 热更新及动态UI生成

Lua 15分钟快速上手(上)

Lua 15分钟快速上手(下)

Lua与C语言的互相调用

LuaDardo中Dart与Lua的相互调用

カテゴリー

依存関係

依存関係一覧 1 件

ソースリポジトリ

このパッケージを使用しているプロジェクト

データはまだありません。