{"id":15289273,"url":"https://github.com/arcticfox1919/luadardo","last_synced_at":"2025-04-09T18:19:15.030Z","repository":{"id":48804806,"uuid":"316697388","full_name":"arcticfox1919/LuaDardo","owner":"arcticfox1919","description":"A Lua virtual machine written in Dart","archived":false,"fork":false,"pushed_at":"2024-02-06T23:16:33.000Z","size":124,"stargazers_count":184,"open_issues_count":26,"forks_count":38,"subscribers_count":7,"default_branch":"main","last_synced_at":"2025-04-09T18:19:10.152Z","etag":null,"topics":["dart","flutter","lua","vm"],"latest_commit_sha":null,"homepage":"","language":"Dart","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/arcticfox1919.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-11-28T09:18:30.000Z","updated_at":"2025-04-01T03:02:30.000Z","dependencies_parsed_at":"2024-01-18T05:43:34.615Z","dependency_job_id":"e4fe9712-e490-431b-a870-bd07ae36fc49","html_url":"https://github.com/arcticfox1919/LuaDardo","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arcticfox1919%2FLuaDardo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arcticfox1919%2FLuaDardo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arcticfox1919%2FLuaDardo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arcticfox1919%2FLuaDardo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/arcticfox1919","download_url":"https://codeload.github.com/arcticfox1919/LuaDardo/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248085325,"owners_count":21045139,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dart","flutter","lua","vm"],"created_at":"2024-09-30T16:00:09.862Z","updated_at":"2025-04-09T18:19:15.011Z","avatar_url":"https://github.com/arcticfox1919.png","language":"Dart","funding_links":[],"categories":[],"sub_categories":[],"readme":"# LuaDardo\n\n![logo](https://github.com/arcticfox1919/ImageHosting/blob/master/language_logo.png?raw=true)\n\n------\n\nA Lua virtual machine written in [Dart](https://github.com/dart-lang/sdk), which implements [Lua5.3](http://www.lua.org/manual/5.3/) version.\n\n## Example:\n\n```yaml\ndependencies:\n  lua_dardo: ^0.0.3\n```\n\n```dart\nimport 'package:lua_dardo/lua.dart';\n\n\nvoid main(List\u003cString\u003e arguments) {\n  LuaState state = LuaState.newState();\n  state.openLibs();\n  state.loadString(r'''\na=10\nwhile( a \u003c 20 ) do\n   print(\"a value is\", a)\n   a = a+1\nend\n''');\n  state.call(0, 0);\n}\n```\n\n## Usage\nThe 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](https://www.lua.org/manual/5.3/manual.html#luaL_newstate).\n\nSome simple examples:\n\n```dart\nLuaState state = LuaState.newState();\n// Load the Lua standard library\nstate.openLibs();\nstate.loadString(\"print('hello')\");\nstate.call(0, 0);\n```\n\n### Dart calls Lua\n\nGet Lua variables:\n```lua\n-- test.lua\na = 100\nb = 120\n```\nDart code:\n```dart\nLuaState ls = LuaState.newState();\nls.openLibs();\nls.doFile(\"test.lua\");\n\n// a push into the stack\nls.getGlobal(\"a\");\nif(ls.isNumber(-1)){\n    var a = ls.toNumber(-1);\n    print(\"a=$a\");\n}\n// b push into the stack\nls.getGlobal(\"b\");\nif(ls.isNumber(-1)){\n    var b = ls.toNumber(-1);\n    print(\"b=$b\");\n}\n```\n\nGet lua global table:\n```lua\n-- test.lua\nmytable = {k1 = 1, k2 = 2.34, k3 = \"test\"}\n```\nDart:\n```dart\n    ls.getGlobal(\"mytable\");\n    ls.pushString(\"k1\");\n    // 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\n    ls.getTable(-2);\n\n    if(ls.isInteger(-1)){\n      // Get the value of key k1\n      var k1 = ls.toInteger(-1);\n    }\n\n    // Repeat\n    ls.pushString(\"k2\");\n    ls.getTable(-2);\n    \n    if(ls.isNumber(-1)){\n      var k2 = ls.toNumber(-1);\n    }\n```\n\nA simpler alternative: `getField`\n```dart\n    ls.getGlobal(\"mytable\");\n    ls.getField(-1, \"k1\");\n    if(ls.isInteger(-1)){\n      var k1 = ls.toInteger(-1);\n    }\n```\n\nCall lua function:\n\n```lua\n-- test.lua\n\nfunction myFunc()\n    print(\"myFunc run\")\nend\n```\n\nDart:\n```dart\nls.doFile(\"test.lua\");\n\nls.getGlobal(\"myFunc\");\nif(ls.isFunction(-1)){\n    ls.pCall(0, 0, 0);\n}\n```\nThe `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.\n\n### Lua calls Dart\n\n```dart\n// Push value onto stack\nls.pushString(\"Alex\");\n// Set variable name\nls.setGlobal(\"name\");\n```\nLua:\n```lua\n-- Get global variable name\nprint(name) -- Alex\n```\n\nDefine global table in Dart:\n```dart\n    // Create a table and push it onto the stack\n    ls.newTable();\n    // Push a key onto the stack\n    ls.pushString(\"name\");\n    // Push the value onto the stack. Note that at this time the index of the table in the stack becomes -3\n    ls.pushString(\"Alex\");\n    // Set the above key-value pair to the table, and pop up the key and value\n    ls.setTable(-3);\n    // Set the variable name to the table, and pop up the table\n    ls.setGlobal(\"students\");\n\n```\n\nLua:\n```lua\n-- Equivalent to a table：students = {name=\"Alex\"}\nprint(students.name)\n```\n\nCall Dart function:\n```dart\nimport 'package:lua_dardo/lua.dart';\nimport 'dart:math';\n\n//  wrapper function must use this signature：int Function(LuaState ls)\n//  the return is the number of returned values\nint randomInt(LuaState ls) {\n  int max = ls.checkInteger(1);\n  ls.pop(1);\n\n  var random = Random();\n  var randVal = random.nextInt(max);\n  ls.pushInteger(randVal);\n  return 1;\n}\n\nvoid main(List\u003cString\u003e arguments) {\n  LuaState state = LuaState.newState();\n  state.openLibs();\n\n  state.pushDartFunction(randomInt);\n  state.setGlobal('randomInt');\n\n  // execute the Lua script to test the randomInt function\n  state.loadString('''\nrand_val = randomInt(10)\nprint('random value is '..rand_val)\n''');\n  state.call(0, 0);\n}\n```\n\nSome people are curious about how to access Lua tables in Dart. Here is a simple example:\n\n```dart\n  state.loadString('''\nrand_val = randomInt(10,{ [\"hello\"] = \"World\", [\"hello22\"] = \"World132414\" })\nprint('random value is '..rand_val)\n''');\n```\n\n```dart\nint randomInt(LuaState ls) {\n  int? max = ls.checkInteger(1);\n  ls.getField(2, \"hello\");\n  // This is a debugging method that looks at the stack\n  ls.printStack();\n  var hello = ls.toStr(-1);\n  print(hello);\n  ls.pop(1);\n\n  ls.getField(2, \"hello22\");\n  var hello22 = ls.toStr(-1);\n  print(hello22);\n  ls.pop(1);\n\n  var random = Random();\n  var randVal = random.nextInt(max!);\n  ls.pushInteger(randVal);\n  return 1;\n}\n```\n\n## Try on Flutter\n\n![](https://picturehost.oss-cn-shenzhen.aliyuncs.com/img/GIF_2021-5-11_21-44-49.gif)\n\n```lua\nfunction getContent1()\n    return Row:new({\n        children={\n            GestureDetector:new({\n                onTap=function()\n                    flutter.debugPrint(\"--------------onTap--------------\")\n                end,\n\n                child=Text:new(\"click here\")}),\n            Text:new(\"label1\"),\n            Text:new(\"label2\"),\n            Text:new(\"label3\"),\n        },\n        mainAxisAlign=MainAxisAlign.spaceEvenly,\n    })\nend\n\nfunction getContent2()\n    return Column:new({\n        children={\n            Row:new({\n                children={Text:new(\"Hello\"),Text:new(\"Flutter\")},\n                mainAxisAlign=MainAxisAlign.spaceAround\n            }),\n            Image:network('https://gitee.com/arcticfox1919/ImageHosting/raw/master/img/flutter_lua_test.png'\n                ,{fit=BoxFit.cover})\n        },\n        mainAxisSize=MainAxisSize.min,\n        crossAxisAlign=CrossAxisAlign.center\n    })\nend\n```\n\n**For use in flutter, see [here](https://github.com/arcticfox1919/flutter_lua_dardo).**\n\n------\n一些中文资料：\n\n[Flutter 热更新及动态UI生成](https://arcticfox.blog.csdn.net/article/details/116681188)\n\n[Lua 15分钟快速上手（上）](https://arcticfox.blog.csdn.net/article/details/119516215)\n\n[Lua 15分钟快速上手（下）](https://arcticfox.blog.csdn.net/article/details/119535814)\n\n[Lua与C语言的互相调用](https://arcticfox.blog.csdn.net/article/details/119544987)\n\n[LuaDardo中Dart与Lua的相互调用](https://arcticfox.blog.csdn.net/article/details/119582403)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farcticfox1919%2Fluadardo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Farcticfox1919%2Fluadardo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farcticfox1919%2Fluadardo/lists"}