View Single Post
Old 11-01-2009, 12:09 PM   #11
hansel
JSR FFD2
hansel can extract oil from cheesehansel can extract oil from cheesehansel can extract oil from cheesehansel can extract oil from cheesehansel can extract oil from cheesehansel can extract oil from cheesehansel can extract oil from cheesehansel can extract oil from cheese
 
hansel's Avatar
 
Posts: 305
Karma: 1045
Join Date: Aug 2008
Location: Rotterdam, Netherlands, Europe, Sol 3
Device: iliad
Quote:
Originally Posted by Iņigo View Post
In my experience, the Lua API is not exactly the same as the C API, sometimes you should expect the value you want in a var argument you pass to the function to be filled (like in C) but often you get it as the return value.
In Lgob 99% of the Lua API is generated automatically (iface.c in the src dir) from a description of the C API (xxx.def and xxx.ovr files in src). I you want to see what really happens, look at iface.c. The Lua API (specially what the function returns) depends on the types of the parameters defined in the def file. The magic happens in Lgob's code generator; more specifically in the file lgob-9.09/codegen/src/types.lua, which starts with
Code:
--[[
        Handles the conversions between C and Lua function calls.
--]]
What I like about Lua/Lgob is that you can add your own functions (in Lua) for functions that have an ugly auto-generated API. You can even replace them at run-time.

For example to delete the selected text in a text buffer you have to do:

Code:
  local b,e = gtk.TextIter:new(), gtk.TextIter:new()
  local sel = buffer:get_selection_bounds(b,e)
  if sel then buf:delete(b,e) end
If I add my own get_sel function to (the existing class defined in C by lgob) TextBuffer:

Code:
function gtk.TextBuffer:get_sel_bounds()
  local b,e = gtk.TextIter:new(), gtk.TextIter:new()
  local sel = self:get_selection_bounds(b,e)
  return sel, b, e
end
Then deleting the selection becomes the more elegant and more Lua:

Code:
 local sel, b, e = buf:get_sel_bounds()
  if sel then buf:delete(b,e) end

Last edited by hansel; 11-01-2009 at 12:29 PM.
hansel is offline   Reply With Quote