HsLua examples on the Haskell Wiki are broken (dostring and dofile are undefined). It looks like the API has changed since the examples were written.
However, I tried to modify the examples to fit the current API and not be very successful. Here is a program that should really work!
main = do l <- Lua.newstate Lua.openlibs l Lua.loadfile l "configfile.lua" [name,pwd] <- Lua.callfunc l "getuserpwd" "mail.google.com" putStrLn name Lua.close l
However, this does not even compile, since to me this is a strange error message -
No instance for (Lua.StackValue [String]) arising from a use of `Lua.callfunc' Possible fix: add an instance declaration for (Lua.StackValue [String]) In a stmt of a 'do' expression: [name, pwd] <- Lua.callfunc l "getuserpwd" "mail.google.com" In the expression: do { l <- Lua.newstate; Lua.openlibs l; Lua.loadfile l "configfile.lua"; [name, pwd] <- Lua.callfunc l "getuserpwd" "mail.google.com"; .... } In an equation for `main': main = do { l <- Lua.newstate; Lua.openlibs l; Lua.loadfile l "configfile.lua"; .... }
While the contents of the configfile.lua file probably doesn't matter (since the haskell code doesn't even compile), it's like below (same as on the wiki page) -
function getuserpwd (site) local cookies = { ["www.ibm.com"] = {"joe", "secret"} , ["www.sun.com"] = {"hoe", "another secret"} } if cookies[site] then return cookies[site] elseif site:match("[.]google[.]com$") then return {"boss", "boss"} else return { os.getenv("USER") or "God" , os.getenv("PWD") or "dontdisturb" } end end
Can someone please provide me with a working example of Haskell-> Lua and Lua-> Haskell?
Edit
Well, I changed the return type to String (from an earlier array of String), and this program compiles! However, now it does not work at runtime. Here's the modified program -
main = do l <- Lua.newstate Lua.openlibs l Lua.loadfile l "configfile.lua" Lua.callfunc l "getuserpwd" "mail.google.com" >>= putStrLn Lua.close l
And here is configfile.lua -
function getuserpwd (site) return "boss" end
And the runtime error message looks like this:
**** Exception: user error (attempt to call a nil value)