Basic problem with Kahlua

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Basic problem with Kahlua

Qu0ll

I am just having a look at Kahlua but I cannot get the basic things to work.

 

I am trying to call one of my Java methods from within Lua and my code looks like this:

 

            final LuaState state = new LuaState(System.out);

            UserdataArray.register(state);

            OsLib.register(state);

            LuaCompiler.register(state);

 

            final LuaTable john = new LuaTableImpl();

            state.getEnvironment().rawset("john", john);

            john.rawset("m1", this);

 

            john.rawset("__index", john);

            state.setUserdataMetatable(KahluaTest.class, john);

 

            try {

                  final LuaClosure closure =

                      LuaCompiler.loadstring("function doIt() john.m1(42) end", "doIt", state

                          .getEnvironment());

                  final Object runTest = state.getEnvironment().rawget("doIt");

                  BaseLib.luaAssert(runTest != null, "Could not find doIt");

                  final Object[] results = state.pcall(runTest, new Object[] {closure});

            } catch (final Exception e) {

                  KahluaTest.logger.error(e);

            }

 

Where “john” is the name of the function library and “m1” is the function I am trying to call.  Well, it never gets to even try to call it as the code above fails on the line where BaseLib.luaAssert()  (5 lines from the bottom) is being called to check for the existence of the function doIt.

 

No errors are returned from the Lua compiler so why can’t it find “doIt” in the environment?  As you can probably tell, I don’t know much about Lua as I have not used it before.

 

Thanks,

 

John

Reply | Threaded
Open this post in threaded view
|

Re: Basic problem with Kahlua

Peter Cawley
On Tue, Aug 25, 2009 at 2:41 PM, John C. Turnbull<[hidden email]> wrote:

>                   final LuaClosure closure =
>
>                       LuaCompiler.loadstring("function doIt() john.m1(42)
> end", "doIt", state
>
>                           .getEnvironment());
>
>                   final Object runTest =
> state.getEnvironment().rawget("doIt");
>
> ...
>
> No errors are returned from the Lua compiler so why can’t it find “doIt” in
> the environment?

Assuming that the Kahlua API is similar to the standard C API,
loadstring only creates a function object - it does not execute any
Lua code. Thus the variable "closure" will contain a function, which
when executed will assign `function() john.m1(42) end` to the global
"doIt". You need to (p)call closure to actually make this happen,
after which you will be able to find "doIt" in the environment.
Reply | Threaded
Open this post in threaded view
|

RE: Basic problem with Kahlua

Qu0ll
Thanks Peter - that solved the problem.

John

> -----Original Message-----
> From: [hidden email] [mailto:lua-
> [hidden email]] On Behalf Of Peter Cawley
> Sent: Tuesday, 25 August 2009 23:49
> To: Lua list
> Subject: Re: Basic problem with Kahlua
>
> On Tue, Aug 25, 2009 at 2:41 PM, John C.
> Turnbull<[hidden email]> wrote:
> >                   final LuaClosure closure =
> >
> >                       LuaCompiler.loadstring("function doIt()
> john.m1(42)
> > end", "doIt", state
> >
> >                           .getEnvironment());
> >
> >                   final Object runTest =
> > state.getEnvironment().rawget("doIt");
> >
> > ...
> >
> > No errors are returned from the Lua compiler so why can’t it find
> “doIt” in
> > the environment?
>
> Assuming that the Kahlua API is similar to the standard C API,
> loadstring only creates a function object - it does not execute any
> Lua code. Thus the variable "closure" will contain a function, which
> when executed will assign `function() john.m1(42) end` to the global
> "doIt". You need to (p)call closure to actually make this happen,
> after which you will be able to find "doIt" in the environment.