Lua - 制作字节码
Lua - make bytecode
所以,我被要求为最简单的代码制作一个字节码:
print("Hello, world!")
但我不知道怎么做,而且我似乎找不到任何关于如何制作的信息...有人可以帮忙吗?我使用 Lua for Windows 作为编译器。非常感谢
您可以使用 Lua compiler (see luac
手册):
# the default output is "luac.out"
echo 'print("Hello, world!")' | luac -
# you can execute this bytecode with the Lua interpreter
lua luac.out
# -> Hello, world!
您可以从 Lua 开始,而无需 luac
使用 string.dump
。例如尝试
f=assert(io.open("luac.out","wb"))
assert(f:write(string.dump(assert(loadfile("foo.lua")))))
assert(f:close())
如果要编译的代码是字符串,使用load(s)
。
您还可以从命令行将下面的文件另存为 luac.lua
和 运行:
-- bare-bones luac in Lua
-- usage: lua luac.lua file.lua
assert(arg[1]~=nil and arg[2]==nil,"usage: lua luac.lua file.lua")
f=assert(io.open("luac.out","wb"))
assert(f:write(string.dump(assert(loadfile(arg[1])))))
assert(f:close())
所以,我被要求为最简单的代码制作一个字节码:
print("Hello, world!")
但我不知道怎么做,而且我似乎找不到任何关于如何制作的信息...有人可以帮忙吗?我使用 Lua for Windows 作为编译器。非常感谢
您可以使用 Lua compiler (see luac
手册):
# the default output is "luac.out"
echo 'print("Hello, world!")' | luac -
# you can execute this bytecode with the Lua interpreter
lua luac.out
# -> Hello, world!
您可以从 Lua 开始,而无需 luac
使用 string.dump
。例如尝试
f=assert(io.open("luac.out","wb"))
assert(f:write(string.dump(assert(loadfile("foo.lua")))))
assert(f:close())
如果要编译的代码是字符串,使用load(s)
。
您还可以从命令行将下面的文件另存为 luac.lua
和 运行:
-- bare-bones luac in Lua
-- usage: lua luac.lua file.lua
assert(arg[1]~=nil and arg[2]==nil,"usage: lua luac.lua file.lua")
f=assert(io.open("luac.out","wb"))
assert(f:write(string.dump(assert(loadfile(arg[1])))))
assert(f:close())