Sign in to follow this  
Followers 0
JRowe

Embedding lua

3 posts in this topic

#1 ·  Posted (edited)

I'm working on embedding Lua, and I've been able to trigger lua functions from AutoIt, but I've hit a wall in passing parameters and results.

The Lua script is easy:

add.lua

function add (x, y)
    return (x + y)
end

The AutoIt script uses "lua51.dll", which I've attached.

$h_LuaDLL = DllOpen("lua51.dll")

    ;Create a new state for the Lua interpreter.
    Global $h_Lua_NewState = DllCall($h_LuaDLL, "ptr:cdecl", "luaL_newstate")

    ;Load all of the Lua libs
    DllCall($h_LuaDLL, "none:cdecl", "luaL_openlibs", "ptr", $h_Lua_NewState[0])

    ;Load the example script "add.lua" into the Lua state
    $h_luaFoo = DllCall($h_LuaDLL, "int:cdecl", "luaL_loadfile", "ptr", $h_Lua_NewState[0], "str", "add.lua")
    ConsoleWrite("Result is: " & $h_luaFoo[0] & @CRLF)
    $i_lua_pCall = DllCall($h_LuaDLL, "int:cdecl", "lua_pcall", "ptr", $h_Lua_NewState[0], "int", 0, "int", -1, "int", 0)
    ConsoleWrite("Result of loadfile pcall is: " & $i_lua_pCall[0] & @CRLF)

    ;Execute a Lua function, passing parameters 5,10
    ;Load the "add" function
        DllCall($h_LuaDLL, "none:cdecl", "lua_getglobal", "ptr", $h_Lua_NewState[0], "str", "add")
    ;Push the first parameter onto the stack
        DllCall($h_LuaDLL, "none:cdecl", "lua_pushnumber", "ptr", $h_Lua_NewState[0], "double", 41)
    ;Push the second parameter onto the stack
        DllCall($h_LuaDLL, "none:cdecl", "lua_pushnumber", "ptr", $h_Lua_NewState[0], "double", 22)
    ;Call (pcall) the function with 2 arguments and 1 result
        $i_lua_pCall = DllCall($h_LuaDLL, "int:cdecl", "lua_call", "ptr", $h_Lua_NewState[0], "int", 2, "int", 1)
            ConsoleWrite("Error Result is: " & $i_lua_pCall[0] & @CRLF)
    ;Get the results

    $sum = DllCall($h_LuaDLL, "double:cdecl", "lua_tonumber", "ptr", $h_Lua_NewState[0], "int", -1)
    $z = $sum[0]
    DllCall($h_LuaDLL, "none:cdecl", "lua_pop", "ptr", $h_Lua_NewState[0], "int", 1)

    ConsoleWrite("z is:" & $z & @CRLF)

    ;close the Lua state to clean up after ourselves.
    DllCall($h_LuaDLL, "none:cdecl", "lua_close", "ptr", $h_Lua_NewState[0])

I'm basing my attempt off this tutorial: http://www.debian-administration.org/articles/264 , the add.c example towards the end.

Even though it's geared towards linux, it's very basic and should translate almost exactly.

So, I'm after the result of $z = 63, or adding of the two parameters I push using "lua_pushnumber", as the function is supposed to do.

Lua_Number is a double type. lua_tonumber returns a double.

Right now, I'm getting

Thanks!

Edited by JRowe

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Well I mucked around with it for a bit...First off, you'll also need lua5.1.dll which is called by lua51.dll ( I think it's a wrapper or something). For some reason lua_call doesn't seem to be setting the stack with the return value. I actually compiled a custom version of lua5.1.dll with a little message box confirmation when the call was made... Which it is...but I'm not sure why the code you posted isn't working. If we use this code:

$h_LuaDLL = DllOpen("lua51.dll")
Global $h_Lua_NewState = DllCall($h_LuaDLL, "ptr:cdecl", "luaL_newstate")
DllCall($h_LuaDLL, "none:cdecl", "luaL_openlibs", "ptr", $h_Lua_NewState[0])
$h_luaFoo = DllCall($h_LuaDLL, "int:cdecl", "luaL_loadfile", "ptr", $h_Lua_NewState[0], "str", "add.lua")
DllCall($h_LuaDLL, "none:cdecl", "lua_getglobal", "ptr", $h_Lua_NewState[0], "str", "add")
DllCall($h_LuaDLL, "none:cdecl", "lua_pushinteger", "ptr", $h_Lua_NewState[0], "int", 41)
DllCall($h_LuaDLL, "none:cdecl", "lua_pushinteger", "ptr", $h_Lua_NewState[0], "int", 22)
$i_lua_pCall = DllCall($h_LuaDLL, "int:cdecl", "lua_call", "ptr", $h_Lua_NewState[0], "int", 2, "int", 1)
;DllCall($h_LuaDLL, "none:cdecl", "lua_settop", "ptr", $h_Lua_NewState[0], "int", 1)
$sum = DllCall($h_LuaDLL, "double:cdecl", "lua_tonumber", "ptr", $h_Lua_NewState[0], "int", 0)
$z = $sum[0]
ConsoleWrite("z is:" & $z & @CRLF)
DllCall($h_LuaDLL, "none:cdecl", "lua_close", "ptr", $h_Lua_NewState[0])

We see that the stack has 41, the first parameter of the call, on the top...to my understanding, lua_call should have pushed the return value...the other parameter seems lost (can't find it in the stack...)

Anyways, I've attached the custom lua5.1.dll if anyone else wants to play with it (or you can get the source yourself and do w/e).

lua5.1.zip

Edited by evilertoaster

Share this post


Link to post
Share on other sites

Smoke_N took the time to fix it, and set it up fairly robustly. I had a non interactive function working, but he took it to the next level.

I'll put these funcs together into something easily used, and then start a project thread. Embedded Lua should make for a very powerful tool in our arsenal.

Thanks for taking a look, and 3 cheers for Smoke_N!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0