Re: example of C types as a substitute for standard Lua types, without using any C function

  • From: Mike Pall <mike-1208@xxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Tue, 7 Aug 2012 13:42:40 +0200

Robert G. Jakabosky wrote:
> local ffi=require"ffi"
> -- define a nice constructor
> local int = ffi.typeof("int32_t")
> local i = int(5) -- make a 'int32_t' C type
> print(i)
> while i<999999999 do i=i+1 end
> print(i)

That's a 64 bit integer loop, due to the coercion semantics of
numeric cdata types. You trivially get a 32 bit integer loop with

  for i=5,999999998 do end

And it enables other optimizations, too. E.g. array bounds check


Other related posts: