Tuesday, June 14, 2011

Haiku, Meet Lua. Lua, Haiku

I've been pretty quiet since I got out of school last week. It's not because I've been too busy to do anything. Quite the contrary. I've had my head down in the trenches, hacking away. About six months ago, I posted a screenshot of CHaikuRun, a test application which interacted with the Haiku API from the C language. I have a new one:



On the outside, not all that different from the other one. Under the hood, though, the two test apps are worlds apart. Here is the source code:


function LuaAppSetupFunction()
    local win = MakeObject("PWindow");
    win.SetProperty("Frame", PRect(100,100,500,400))
    win.SetProperty("Flags", BQuitOnWindowClose)
    win.SetProperty("Title", "LuaGUI")

    local view = MakeObject("PView")
    view.SetProperty("Frame", PRect(0,0,400,300))
    view.SetProperty("BackColor", PColor(224,224,224));
    win.RunMethod("AddChild", {view.id})
   
    local label = MakeObject("PLabel");
    label.SetProperty("Frame", PRect(10, 10, 350, 60))
    label.SetProperty("Text", "Haiku, meet Lua!");
    view.RunMethod("AddChild", {label.id})
end

RunApp("application/x-vnd.dw-LuaGUI", "LuaAppSetupFunction");


That's pretty darn short, if you ask me. A graphical HelloWorld app can be written in 6 lines of code:

function LuaAppSetupFunction()
    local win = MakeObject("PWindow");
    win.SetProperty("Flags", BQuitOnWindowClose)
    win.SetProperty("Title", "Hello Haiku!")
end

RunApp("application/x-vnd.dw-LuaGUI", "LuaAppSetupFunction");


And I thought writing in C++ was easy. Yeesh. That beats everything I've seen under Haiku/BeOS except yab and quite competitive to something like wxPython. Then again, I could argue that this is a lot clearer than yab, but I digress.

The code snippets I've posted above are why I manually wrote Lua bindings instead of using SWIG. I'm not sure that I could have gotten SWIG to generate them in a way which has at least the same ease-of-use as the C++ API. I've still got a *lot* more testing to do and some more objects to implement before I can do a release, but this is definitely some progress and some good news, too. It's officially a good day now. :)

2 comments:

  1. So how do you run this code ? Have you built something into Paladin ? Or do I need to install Lua and then run some command to compile it ?

    ReplyDelete
  2. ATM it's not very simple to do because I don't have it in a state which is very easy for others to reproduce. Doing so requires compiling libcharlemagne in with the Lua bindings. Libcharlemagne just isn't ready for public release yet because there are a lot of classes to wrap still. With the new definition format for autogenerating the wrapper code, it should go quite a bit faster. Call the above code a proof of concept. :)

    ReplyDelete