lua-users home
lua-l archive

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


On 02.10.2012 18:36, David Collier wrote:
> I'm thinking about code size.
> 
> If I write
> 
>  if CheckCarefully and fred > 3
>  then
>      print("help fred is >3 which is a very bad idea in our case")
>      return
>  else
>      <whatever using fred>
>  end
>  
> Can I set up the value of CheckCarefully so that the compiler can omit
> the print and omit storing the error string.

What compiler are you writing about? Lua has an interpreter and no
compiler. LuaJIT has just-in-time compiler, but it "compiles" code as it
runs (so it still needs the full original source/bytecode).

> Or will it always create the strings in the bytecode?

Yes. Check for yourself:
$ luac -l example.lua

main <example.lua:0,0> (15 instructions, 60 bytes at 0x985bd18)
0+ params, 2 slots, 0 upvalues, 0 locals, 6 constants, 0 functions
	1	[2]	GETGLOBAL	0 -1	; CheckCarefully
	2	[2]	TEST     	0 0 0
	3	[2]	JMP      	8	; to 12
	4	[2]	GETGLOBAL	0 -2	; fred
	5	[2]	LT       	0 -3 0	; 3 -
	6	[2]	JMP      	5	; to 12
	7	[3]	GETGLOBAL	0 -4	; print
	8	[3]	LOADK    	1 -5	; "help fred is >3 which is a very bad idea in our
case"
	9	[3]	CALL     	0 2 1
	10	[4]	RETURN   	0 1
	11	[4]	JMP      	3	; to 15
	12	[6]	GETGLOBAL	0 -4	; print
	13	[6]	LOADK    	1 -6	; "<whatever using fred>"
	14	[6]	CALL     	0 2 1
	15	[7]	RETURN   	0 1

> If this won't work, anyone got a good way to have code and strings in
> during testing phase and omitted in deployed code?

You are looking for any kind of preprocessor, echeck out metalua:
http://metalua.luaforge.net/

Regards,
miko