lua-users home
lua-l archive

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


Thanks both.

I did appreciate that unless there were language features I hadn't
spotted yet the code would have to get included.

I guess a preprocessor is the only option without a language extension.

I'm feeling my way towards a coding standard for Lua - I find the dynamic
typing makes me want to say we should check the type and range of
arguments to a 'company library routine' on entry, but I also want to be
able to write code for eLua, where the installed code should probably run
without the checks. But I only want one copy of the source to maintain.

I suppose I could write a function "checkargs", and pass it the arguments
and 1-byte statements of what they should be at the head of each routine.
Then I can replace that routine with 'ret'

Or I can dig around for a Lua preprocessor, and try to work out how to
tell eclipse to run it for me each time :-)

D.

> *From:* "Michal Kolodziejczyk" <miko@wp.pl>
> *To:* <lua-l@lists.lua.org>
> *Date:* Wed, 03 Oct 2012 09:30:00 +0200
> 
> 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
> 
> 
>