Fengari(希腊文中的Moon)是用 JavaScript 编写的 Lua VM。它使用 JavaScript 的垃圾收集器,因此与 DOM 的互操作性是无泄漏的。
在浏览器中使用 Lua 意味着您可以使用协程编写漂亮的异步代码:
local js = require "js" local window = js.global
local function sleep(delay) local co = assert(coroutine.running(), "Should be run in a coroutine")
window:setTimeout(function() assert(coroutine.resume(co)) end, delay*1000)
coroutine.yield() end
coroutine.wrap(function() print "Going to sleep now..."
sleep(3)
print "Sleep well?" end)()
|
步骤:
在浏览器中开始使用 Fengari 的最简单方法是使用fengari-web。该模块将监视 DOM 中的任何<script type="application/lua" src="...">并使用 Fengari 运行它们。
$ curl -L -O https://github.com/fengari-lua/fengari-web/releases/download/v0.1.4/fengari-web.js
|
<script src="fengari-web.js" type="text/javascript"></script>
|
- 现在任何application/lua类型的脚本都将由 fengari 运行:
<script type="application/lua"> print("hello world!") </script>
<script src="/my-script.lua" type="application/lua" async></script>
|
点击标题更多