Lua到JS的转译器,不需要运行时

经过长时间的搜索,我似乎找不到一个npm包或浏览器脚本,只能将Lua翻译成Javascript。我发现所有我找到的包都强制我执行已 transpile 的代码,但这不是我想要的,我只想将它从Lua翻译成Javascript并获取返回的源代码。以下为示例。

我想要更像

输入:

local str = "Hello World!"
print(str)

输出:

var str = "Hello World"
console.log(str)

我尝试使用[lua-in-js](https://www.npmjs.com/package/lua-in-js)并更改一些代码,但它从一开始就不会返回正常的源代码。如果你们中有任何人知道我如何只转换而不被强制执行代码,那将是非常好的。

这是我首先尝试的[lua-in-js]:

const fs = require("fs")
const path = require("path")
const luainjs = require("lua-in-js")
const rootPath = './lua'
const luaEnv = luainjs.createEnv({
  fileExists: p => fs.existsSync(path.join(rootPath, p)),
  loadFile: p => fs.readFileSync(path.join(rootPath, p), { encoding: 'utf8' }),
  osExit: code => (exitCode += code)
})
const luaScript = luaEnv.parseFile('somefile.lua')
console.log(luaScript)

在这里,我试图看看它返回了什么,但它只返回了一个exec()函数

如果这有所帮助,这是luainjs环境的配置。

const luaEnv = luainjs.createEnv({
    LUA_PATH,   // default value of package.path
    fileExists, // function that takes in a path and returns a boolean
    loadFile,   // function that takes in a path and returns the content of a file
    stdin,      // string representing the standard input
    stdout,     // function representing the standard output
    osExit      // function called by os.exit
})

const luaScript = luaEnv.parseFile('somefile.lua')
const returnValue = luaScript.exec()

原文链接 https://stackoverflow.com/questions/71159127

点赞