Oti Na Nai
Oti Na Nai

Reputation: 1407

Lua - make bytecode

So, I've been asked to make a bytecode for the simpliest code:

print("Hello, world!")

But I have no idea how and I can't seem to find any information on how to make one... Can someone please help? I use Lua for Windows as a compiler. Thanks a lot

Upvotes: 5

Views: 9993

Answers (2)

lhf
lhf

Reputation: 72432

You can do it from Lua without luac using string.dump. Try for instance

f=assert(io.open("luac.out","wb"))
assert(f:write(string.dump(assert(loadfile("foo.lua")))))
assert(f:close())

If the code to be compiled is in a string, use load(s).

You can also save the file below as luac.luaand run it from the command line:

-- 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())

Upvotes: 3

deltheil
deltheil

Reputation: 16121

You can use the Lua compiler (see luac manual):

# 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!

Upvotes: 5

Related Questions