luavm 0.2.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 64

Flutter Lua VM Plugin #

Pub package Dartdoc reference

A Flutter plugin provides Lua virtual machine

This plugin is inspired by flutter_lua, a Go based Lua implementation for Flutter.

Getting Started #

Features

  • Supports the latest stable vanilla Lua 5.4.0
  • Support multiple Lua instances (don't be too much. <=100 instances)
  • Lua script runs in platform thread
  • Use Java/ObjC to avoid the annoying Swift version compatibility problem

Limitations

  • Lua library "os" is NOT supported yet, due to unsupported functions in iOS: system and tmpnam
  • All returned values will be converted to string

Usage #

Open a new VM

VM instances are named to distinguish each other.

import 'package:luavm/luavm.dart';

...

await Luavm.open("vm-name");

Run Lua Code

When VM is opened, run Lua code with 'eval' function:

  • To load a Lua function:

await Luavm.eval("name","function luafn(a,b) return a+b end" );
  • To simply run Lua code:
final res = await Luavm.eval("name","return _VERSION")

res should be returned as:

["Lua 5.4"]
  • To call a Lua function:
final res = await Luavm.eval("name","return luafn(1,2)");

Luavm.eval returns a list of String, contains each value returned from Lua function.

final res = await Luavm.eval("name","return 1,2,'hello'");

should return a Dart list:

["1","2","hello"]

Close Lua VM

await Luavm.close("name");

Error Handling

Errors will be thrown as LuaError which contains error message as a string.

How to contribute #

Welcome to create issue about bug, feature request, etc.

0.2.0 #

  • Upgrade to the newest Lua 5.4.0

0.1.1 #

  • Fix document badges

0.1.0 #

  • Fix document and format for pub.dev requirements.

0.0.1 #

  • Vanilla Lua 5.3.5 without loslib, iOS and Android should work both

example/lib/main.dart

import 'package:flutter/material.dart';
import 'dart:async';
import 'package:luavm/luavm.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final _nameCtrl = TextEditingController(text: 'base');
  final _codeCtrl = TextEditingController(text: 'return hi(3),_VERSION,3*4');

  String _luaRes = 'Unknown';
  bool _luaError = false;

  Future<void> runCode() async {
    String luaRes;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      final reslist = await Luavm.eval(_nameCtrl.text, _codeCtrl.text);
      luaRes = reslist.join("\n");

      _luaError = false;
    } on LuaError catch (e) {
      setState(() {
        _luaError = true;
        _luaRes = e.toString();
      });
    }

    if (luaRes != null && luaRes.length > 0) {
      setState(() {
        _luaRes = luaRes;
      });
    }
    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> opLua(String name) async {
    if (Luavm.opened(name)) {
      await Luavm.close(name);
    } else {
      await Luavm.open(name);
      await Luavm.eval(name, "function hi(n) return 'hi-$name-'..n end");
    }
    setState(() {});
  }

  @override
  void dispose() {
    _codeCtrl.dispose();
    _nameCtrl.dispose();
    super.dispose();
  }

  Widget nameLine() {
    String btnTitle = '--';
    if (_nameCtrl.text != '' && _nameCtrl.text != null) {
      if (Luavm.opened(_nameCtrl.text)) {
        btnTitle = 'Close';
      } else {
        btnTitle = 'Open';
      }
    }
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      children: [
        Expanded(
            child: TextField(
          controller: _nameCtrl,
          autocorrect: false,
          onChanged: (_) {
            setState(() {});
          },
        )),
        MaterialButton(
            onPressed: () async {
              await opLua(_nameCtrl.text);
            },
            child: Text(btnTitle))
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    var style = TextStyle(color: Colors.black);
    if (_luaError) {
      style = TextStyle(color: Colors.red);
    }

    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Example of LuaVM'),
        ),
        body: Center(
            child: Column(children: [
          nameLine(),
          TextField(controller: _codeCtrl, autocorrect: false),
          MaterialButton(
              onPressed: () async {
                await runCode();
              },
              child: Text('Run')),
          Text(_luaRes, style: style),
        ], mainAxisAlignment: MainAxisAlignment.spaceEvenly)),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  luavm: ^0.2.0

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:luavm/luavm.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
29
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
64
Learn more about scoring.

We analyzed this package on Jul 9, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.14
  • Flutter: 1.17.5

Analysis suggestions

Package does not support Flutter platform linux

Because:

  • package:luavm/luavm.dart that declares support for platforms: android, ios

Package does not support Flutter platform macos

Because:

  • package:luavm/luavm.dart that declares support for platforms: android, ios

Package does not support Flutter platform web

Because:

  • package:luavm/luavm.dart that declares support for platforms: android, ios

Package does not support Flutter platform windows

Because:

  • package:luavm/luavm.dart that declares support for platforms: android, ios

Package not compatible with SDK dart

Because:

  • luavm that is a package requiring null.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.12 1.14.13
meta 1.1.8 1.2.2
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8 2.1.0-nullsafety
Dev dependencies
flutter_test