lua-users home
lua-l archive

Thoughts wanted on automatically "wrapping" existing functions.

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


First a little background:
I often use Lua-GD because in many cases,  I find it easier to create an image by writing code than to try and draw it. Recently I came up with an idea to make the process a little more interactive, and to type less. I've tried it out with just a small handful of the functions and I like how it's shaping up.
Here's the essence of the idea:
gd = require 'gd'
local _img
local _file = os.tmpname()
function show()
   _img:png(_file)
   os.execute('open -a Preview ' .. _file)
end
function create(sx, sy)
   _img = gd.create(sx, sy)
   --
   -- some code to create a set of colors...
   ---
end
function setpixel(x, y, c)
   local result = _img:setPixel(x, y, c)
   show()
   return result
end
function rectangle(x1, y1, x2, y2, c)
   local result = _img:rectangle(x1, y1, x2, y2, c)
   show()
   return result
end
-- etc, etc
Now for my question:
I could write the "wrapper" functions by hand. There are roughly 130 functions and constants in Lua-GD, so it wouldn't take forever. But I feel like there's some clever way I'm not seeing to do it more automatically. Another possibility is to change the C code that implements the library. But I'd prefer to leave it intact.
Here are a couple of issues with doing it automatically:
- debug.getInfo tells me that all the functions are vararg so I can't build a parameter list - if I build up the wrapped function as a string and then pass it to load() to create, since _img is local, I get an error about it being nil when I try and invoke the function. So I could easily crank this out by hand and already be done. But I'd like to hear any suggestions on what's a more clever approach.

AltStyle によって変換されたページ (->オリジナル) /