开发者

Method of binding Java functions to Lua?

开发者 https://www.devze.com 2023-02-17 23:02 出处:网络
I\'m working on a Lua wrapper for my Android app, which will allow me to write Lua code to speed up development. I\'ve made a static class called lua with functions like newState and pushString. I man

I'm working on a Lua wrapper for my Android app, which will allow me to write Lua code to speed up development. I've made a static class called lua with functions like newState and pushString. I manage the Lua state by passing around a long with the pointer to the lua_State. As you can tell, I don't need any fancy stuff that makes interaction easier, like overloads to push variables.

Now, the problem is binding Java functions to Lua variables. I've thought of a few ways to do this, but they're all ugly.

  1. Instead of functions, pass around a table with a reference to the Java function as a userdatum and have a __call metamethod take care of calling the "function开发者_开发知识库".

  2. Alter Lua internals to include a Java reference with Lua C functions.

Is there any better way to go about this? Or should I go with the second method? (I realise the first method is ridiculous, but it manifested itself in my mind as a solution anyways.)


You can have a look at my simple project AndroLua. It contains Lua and LuaJava compiled using the Android NDK.

Because it uses LuaJava, it allows to bind Java functions to Lua, in a similar way like you said, using userdata. Here is an example of how I override the print function to output text into a TextView:

JavaFunction print = new JavaFunction(L) {
    @Override
    public int execute() throws LuaException {
        StringBuilder sb = new StringBuilder();
        for (int i = 2; i <= L.getTop(); i++) {
            int type = L.type(i);
            String val = L.toString(i);
            if (val == null)
                val = L.typeName(type);
            sb.append(val);
            sb.append("\t");
        }
        sb.append("\n");
        status.append(sb.toString());
        return 0;
    }
};
print.register("print");

The downside is that sometimes you cannot pass the print as a function parameter (because it is a userdata, even though it has a __call metamethod). Fortunately, it can be solved in Lua by creating a pure Lua function, like this:

do
    local oldprint = print
    function print(...) oldprint(...) end
end


I decided to use lua_pushcclosure, as it allows you to 'store' arbitrary values on functions that can be retrieved with the lua_upvalueindex macro.


There is also Kahlua vs LuaJava.

It is mentioned in this book: http://books.google.com/books?id=2v55tfq9rosC&lpg=PA166&ots=9RRVaz5JjP&dq=krka%20kahlua%20blog&pg=PA166#v=onepage&q&f=false

The development blog:

http://krkadev.blogspot.com/2010/05/getting-started-with-kahlua2.html

0

精彩评论

暂无评论...
验证码 换一张
取 消