[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Determine endianness in pure lua
- From: Jim Mellander <jmellander@...>
- Date: Thu, 06 Nov 2008 09:10:40 -0800
KHMan wrote:
> 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)
>> end
>>
>> print(endian())
>>
>> I'm not happy with checking the header - is there something a bit less
>> magic?
>
> 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)
> end
>
> 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.
>
Thanks, I got the idea from looking at your No Frills Intro to the Lua
instruction set - I appreciate it.
I suppose the lua version could be checked, as well, to address
versioning issues.
--
Jim Mellander
Incident Response Manager
Computer Protection Program
Lawrence Berkeley National Laboratory
(510) 486-7204
The reason you are having computer problems is:
The MGs ran out of gas.