lua-users home
lua-l archive

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

Jim Mellander wrote:
I've been tinkering with ways to figure out a way to determine
endianness of a system dynamically in pure lua.  This is what I came up,
which is based on the bytecode format as documented with the below,
which depends on examining the bytecode:

function endian()
        local function f() end
        return string.byte(string.dump(f),7)


I'm not happy with checking the header - is there something a bit less

Looking at Perl and Python, if one does not use the available system configuration variable/info, we end up testing endianness using pack(). So, to get the same convenient feature, it's possible to add a custom 'system library' to Lua with the minimal of changes to the sources, in order to have our own 'sys.byteorder'. But

function endian()
  # retrieve endian flag from a binary chunk header
  return string.byte(string.dump(function() end),7)

doesn't look too bad if one looks at it as the implementation of a primitive function... :-) To be perfectly safe though, note that the above is only guaranteed for major version bumps, I think.

Kein-Hong Man (esq.)
Kuala Lumpur, Malaysia