在浏览器中使用Lua语言:Fengari


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 中的任何