Lua Power Patches Archive

lua-users home
wiki

This page contains LuaPowerPatches for old versions of Lua.

Lua 5.0

Bitwise operators and hexadecimal support

This patch adds the following features to Lua 5.0:

NOTE: This patch adds opcodes to the Lua virtual machine. The two major consequences of this are that (1) the result cannot be called 'Lua', and (2) compiled code will not run on interpreters that do not include this patch.

After applying the patch, try running the following:


> hex=function(n) print("0x"..string.format("%X",n)) end

> hex(0x54&0x55)

0x54

> hex(0x54|0x66)

0x76

> hex(0x54#0x66)

0x32

> hex(#0x45)    

0xFFFFFFBA

> print("Hel\x6c\x6f world\x21")

Hello world!

> 

BUG-REPORT/NOTE: This patch uses the C function strtol(), which on most systems has a maximum input value of 0x7FFFFFFF, so the command hex(0x97F2DA31) will output 0x7FFFFFFF. [ This is easy enough to fix on machines that support 64-bit integers (i.e. long long) by using strtoll() (Posix) or __strtoi64() (Win32) ].

Improved Coroutines

This patch removes the restrictions on yielding coroutines from within metamethods or C functions to largest degree possible without introducing operating system-dependence or reliance on OS facilities such as threading libraries or C stack allocations. See ImprovedCoroutinesPatch.

__usedindex metatable patch

__newindex catches creation/assignment to new table indexes, but not to pre-existing ones. What if the old value had a back-reference to its parent? (__gc is not enough.) What if the changed value should be mirrored in a C data structure?

__usedindex will act the same as __newindex, except that table[key] will exist.

Example:

function used(t,k,v)

   local o

   o = rawget(t,k)

   print("__usedindex",t,k,v,o)

   rawset(t,k,v)

end

function new(t,k,v)

   print("__newindex",t,k,v)

   rawset(t,k,v)

end

Make superfluous 'do' and 'then' tokens optional

Lua requires 'then' following an if statement, and 'do' following while and for statements. This patch makes them optional.

Lua 4.x

Unix/Win32 Makefiles

Changes the lua-4.1-work4 Makefiles so that it's easy to build the Lua libraries and executables under Win32 with MSVC, using GNU Make, by editing lua/config. Does not hurt Unix compatibility. The main difficulty is that Windows uses different file extensions and a few slightly different archiver/linker/compiler options. By using macros to stand for the file extensions, the config file can configure for either Unix or Win32, without having to edit the actual Makefiles.

Block Comments

Adds block comments to Lua: --[[...]]. Uses the long string parser ([[...]]) so block comments may be nested. Recommended usage:

--[[---------

Comment...

--]]---------
That has the nice property that a block comment may be disabled by inserting a single space just before the first [ (or by adding a single - before --[[).

Lua autoconf patch

Autoconfiscates the Lua distribution (4.1work4).

Lua setconstant patch

Add a Lua function setconstant( table ), It will mark a table of strings or numbers as constant, so that the content of this table will never be garbage collected. Note that the table can be nested, and anything other than number and string will not be suited to this function.

yield()

Changes the Lua VM so that calls from Lua to Lua functions are "stackless"; i.e. no execution state is stored on the C stack. Adds a yield() function to the Lua base library, which can be used to implement latent functions/cooperative multitasking. When a script calls yield(), the function call to lua_dostring/lua_dofile/lua_dobuffer from the host program returns with the code LUA_YIELD. The script's execution state is preserved in the lua_State structure, and a new API function, lua_resume(L), can be used to continue the script's execution at some later time.

As a side benefit, this patch makes Lua's tailcalls (ones that use the OP_TAILCALL opcode) properly non-recursive.

(was "sleep patch"; "yield" was deemed a better name for this functionality, so I changed the name. -ThatcherUlrich)

Enhanced table constructors

The record part of table constructors (field=value) is modified so that function statements are allowed and the comma is optional now. So this becomes valid:

x = { 

  function foo() end

  function bar() end

  c=1

  d=2

} 
and is the same as:

x = { 

  foo=function() end,

  bar=function() end,

  c=1,

  d=2,

} 

The comma is still required in front of [val]=val records and in list initializers ({ expr, expr }).

Local functions

New syntactic sugar for the local-statement:

local foo(...) ... end
is the same as

local foo foo=function(...) ... end
Note that foo is visible within the function. In Lua 4.0 that doesn't help a lot (in fact, an error is raised when %foo is accessed).

I'm not used to Lua 4.1's code generator so that (more useful) version of this patch has to wait a little bit.

lua_getcclosure()

Adds missing function to the API that retrieves closure values of a C function. (See lua-l message "pushcclosure / tocfunction issue" by John Belmonte, 2000-Oct-7.)

Nested Function Names

Changes parser to allow definition function names to be nested in more than one level of tables. (See lua-l message "Re: Son of Lua - Sol" by John Belmonte, 2001-Feb-2.)

The syntax becomes: NAME {'.' NAME} [':' NAME]

Weak References

Adds weak references to Lua. See http://www.lua.org/notes/ltn006.html.

List Iteration For Construct

Adds another form of the for construct that iterates lists. Semantically equivalent to the standard library function foreachi (See lua-l message "list iteration for statement" by John Belmonte, 2001-Apr-26.)

lua_dolines patch

Patch to Lua-4.0.1. Add

lua_dolines(lua_State *L, char *fname, FILE *f, int *lineno)

to the Lua API which executes Lua code from an already open file until '$' is encountered.

lua_gc_long patch

Patch to Lua-4.0.1.

This patch changes the way scaled integers are compared to longs. It actually is a bug fix, as otherwise, on typical 64 bit boxes, lua_setgcthreshold(L,0) does not work.


RecentChanges · preferences
edit · history
Last edited January 4, 2012 1:36 pm GMT (diff)