lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It was thus said that the Great Soni L. once stated:
> As of Lua 5.3 you can add a #! line to a precompiled bytecode file and 
> it'll parse and run.
> 
> Seeing as how insane that is, can we get to embed bytecode in source 
> code? As in:
> 
> load(string.format("f = %s f(print, 'hello world!')", 
> string.dump(function(pf, v) pf(v) end)))()
> 
> Or:
> 
> sourcecode_part = "f = %s f(print, 'hello world!')"
> bytecode_part = string.dump(function(pf, v) pf(v) end)
> combined_parts = string.format(sourcecode_part, bytecode_part)
> loaded_function = load(combined_parts)
> loaded_function() --> prints "hello world!"
> 
> (Note: Code shown here doesn't (yet) work, it's just an example)

  Just so I know if I understand the request.  You want to do:

	#!/blah/blah/lua
	function foo() end
	-- assume the rest is actual binary
	1B 4C 75 61 51 00 01 04 08 04 08 00 00 00 00 00
	00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 02
	03 00 00 00 24 00 00 00 07 00 00 00 1E 00 80 00
	01 00 00 00 04 04 00 00 00 00 00 00 00 62 61 72
	00 01 00 00 00 00 00 00 00 00 00 00 00 01 00 00
	00 01 00 00 00 00 00 00 02 01 00 00 00 1E 00 80
	00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
	00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
	00

or

	#!/blah/blah/lua -- assume hex is actually binary
	1B 4C 75 61 51 00 01 04 08 04 08 00 00 00 00 00
	00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 02
	03 00 00 00 24 00 00 00 07 00 00 00 1E 00 80 00
	01 00 00 00 04 04 00 00 00 00 00 00 00 62 61 72
	00 01 00 00 00 00 00 00 00 00 00 00 00 01 00 00
	00 01 00 00 00 00 00 00 02 01 00 00 00 1E 00 80
	00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
	00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
	00
	function foo() end

  I don't think you can mix binary and text, as the current parser isn't
expecting it.  Even assuming it worked, you still have to deal with endian
and size issues (say, going from 64-bit big endian to 32-bit little endian).

  -spc