问题描述:

I want to know how to pass Java Object reference to Lua

Is there lua_newuserdata equivalent in Lua Java Binding ? I am using jnlua library.

After creating newuserdata how do i pass it to lua using openlib in java ?

网友答案:

I think luaState.pushJavaObject(Object) and luaState.pushJaveObjectRaw(Object) are methods You're looking for :) Sadly, can't see this documented and it's not that clear from sources, at least to me.

EDIT: Yup, it is. Look at this code piece from ./main/c/jnlua.c (jnlua sources):

/* ---- Java objects and functions ---- */
/* Pushes a Java object on the stack. */

static void pushjavaobject (lua_State *L, jobject object) {
    jobject *user_data;

    user_data = (jobject *) lua_newuserdata(L, sizeof(jobject));
    luaL_getmetatable(L, JNLUA_OBJECT);
    *user_data = (*thread_env)->NewGlobalRef(thread_env, object);
    if (!*user_data) {
            lua_pushliteral(L, "JNI error: NewGlobalRef() failed pushing Java object");
            lua_error(L);
    }
    lua_setmetatable(L, -2);
}
相关阅读:
Top