From 4c7d40c34948f2e4eb83d57c40001479c8138a89 Mon Sep 17 00:00:00 2001 From: Tim Mensch Date: Fri, 8 Feb 2013 12:16:44 -0700 Subject: [PATCH 1/4] Ignore two warnings. --- jni/luajava/luajava.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/jni/luajava/luajava.c b/jni/luajava/luajava.c index 08d86f3..6437153 100755 --- a/jni/luajava/luajava.c +++ b/jni/luajava/luajava.c @@ -33,6 +33,9 @@ * *****************************************************************************/ +// Ignore two warnings that, in this file, are false positives +#pragma GCC diagnostic ignored "-Wpointer-to-int-cast" +#pragma GCC diagnostic ignored "-Wint-to-pointer-cast" #include #include From b2c5d3f217e4300768d12bb4f8a5049808dc0561 Mon Sep 17 00:00:00 2001 From: Tim Mensch Date: Fri, 8 Feb 2013 12:17:45 -0700 Subject: [PATCH 2/4] Add __len method to Java arrays. --- jni/luajava/luajava.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/jni/luajava/luajava.c b/jni/luajava/luajava.c index 6437153..f5b3b1d 100755 --- a/jni/luajava/luajava.c +++ b/jni/luajava/luajava.c @@ -54,6 +54,8 @@ #define LUAJAVASTATEINDEX "LuaJavaStateIndex" /* Index metamethod name */ #define LUAINDEXMETAMETHODTAG "__index" +/* Length metamethod name */ +#define LUALENMETAMETHODTAG "__len" /* New index metamethod name */ #define LUANEWINDEXMETAMETHODTAG "__newindex" /* Garbage collector metamethod name */ @@ -744,6 +746,27 @@ int classIndex( lua_State * L ) return ret; } +/* Function: arrayLen */ +int arrayLen( lua_State * L ) +{ + lua_Number stateIndex; + JNIEnv * javaEnv; + jobject obj; + + javaEnv = getEnvFromState( L ); + stateIndex = getLuaStateIndex( L ); + + if ( !isJavaObject( L , 1 ) ) + { + lua_pushstring( L , "Not a valid Java Object." ); + lua_error( L ); + } + + obj = lua_jobject( L , 1 ); + + lua_pushnumber(L,(*javaEnv)->GetArrayLength(javaEnv, obj)); + return 1; +} /*************************************************************************** * @@ -1244,6 +1267,11 @@ int pushJavaArray( lua_State * L , jobject javaObject, JNIEnv * javaEnv ) lua_pushcfunction( L , &arrayIndex ); lua_rawset( L , -3 ); + /* pushes the __len metamethod */ + lua_pushstring( L , LUALENMETAMETHODTAG ); + lua_pushcfunction( L , &arrayLen ); + lua_rawset( L , -3 ); + /* pushes the __fallback metamethod (used for non-integer keys*/ lua_pushstring( L , "__fallback" ); lua_pushlightuserdata( L , javaEnv); From a364aee5559691f3c58c1a6e2536a02d6e2dba9c Mon Sep 17 00:00:00 2001 From: Tim Mensch Date: Fri, 8 Feb 2013 13:00:33 -0700 Subject: [PATCH 3/4] Defer finalization of Lua objects until someone in the Lua thread is creating a new object. This way a long-running Lua thread won't cause the garbage collector to time out blocking on synchronized(L). --- src/org/keplerproject/luajava/LuaObject.java | 33 +++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/src/org/keplerproject/luajava/LuaObject.java b/src/org/keplerproject/luajava/LuaObject.java index 36d4b62..eca9cf0 100755 --- a/src/org/keplerproject/luajava/LuaObject.java +++ b/src/org/keplerproject/luajava/LuaObject.java @@ -27,6 +27,7 @@ import java.lang.reflect.InvocationHandler; import java.lang.reflect.Proxy; import java.util.StringTokenizer; +import java.util.Vector; /** * This class represents a Lua object of any type. A LuaObject is constructed by a {@link LuaState} object using one of @@ -51,11 +52,24 @@ * @author Rizzato * @author Thiago Ponte */ +@SuppressWarnings("rawtypes") public class LuaObject { protected Integer ref; + protected LuaState L; - static int REGISTRYINDEX = LuaState.LUA_REGISTRYINDEX.intValue(); + + protected static Object m_deleteListLock = new Object(); + class RegisterPair { + public RegisterPair(int a_, int b_) + { + a=a_; + b=b_; + } + int a; + int b; + }; + protected static Vector m_toDelete = new Vector(); /** * Creates a reference to an object in the variable globalName @@ -71,6 +85,18 @@ protected LuaObject(LuaState L, String globalName) L.getGlobal(globalName); registerValue(-1); L.pop(1); + + synchronized (m_deleteListLock) + { + if (L.getCPtrPeer() != 0) + { + for (RegisterPair l : m_toDelete) + { + L.LunRef(l.a,l.b); + } + m_toDelete.clear(); + } + } } } @@ -209,10 +235,9 @@ protected void finalize() { try { - synchronized (L) + synchronized (m_deleteListLock) { - if (L.getCPtrPeer() != 0) - L.LunRef(REGISTRYINDEX, ref.intValue()); + m_toDelete.add(new RegisterPair(LuaState.LUA_REGISTRYINDEX, ref)); } } catch (Exception e) From 5fa65cf0ea45908a3697e4bd4ad4ecf6ffd4da5d Mon Sep 17 00:00:00 2001 From: Tim Mensch Date: Fri, 8 Feb 2013 13:09:46 -0700 Subject: [PATCH 4/4] Clean up and simplify implemenation of delete cache. --- src/org/keplerproject/luajava/LuaObject.java | 35 ++++++++------------ 1 file changed, 13 insertions(+), 22 deletions(-) diff --git a/src/org/keplerproject/luajava/LuaObject.java b/src/org/keplerproject/luajava/LuaObject.java index eca9cf0..86d4c0d 100755 --- a/src/org/keplerproject/luajava/LuaObject.java +++ b/src/org/keplerproject/luajava/LuaObject.java @@ -58,18 +58,9 @@ public class LuaObject protected Integer ref; protected LuaState L; + static int REGISTRYINDEX = LuaState.LUA_REGISTRYINDEX.intValue(); - protected static Object m_deleteListLock = new Object(); - class RegisterPair { - public RegisterPair(int a_, int b_) - { - a=a_; - b=b_; - } - int a; - int b; - }; - protected static Vector m_toDelete = new Vector(); + protected static Vector m_toDelete = new Vector(); /** * Creates a reference to an object in the variable globalName @@ -86,13 +77,13 @@ protected LuaObject(LuaState L, String globalName) registerValue(-1); L.pop(1); - synchronized (m_deleteListLock) + synchronized (m_toDelete) { if (L.getCPtrPeer() != 0) { - for (RegisterPair l : m_toDelete) + for (int i : m_toDelete) { - L.LunRef(l.a,l.b); + L.LunRef(REGISTRYINDEX,i); } m_toDelete.clear(); } @@ -115,7 +106,7 @@ protected LuaObject(LuaObject parent, String name) throws LuaException this.L = parent.getLuaState(); if (!parent.isTable() && !parent.isUserdata()) - { + { LuaJavaAPI.throwLuaException(parent.L,"Object parent should be a table or userdata ."); } @@ -225,7 +216,7 @@ private void registerValue(int index) ref = new Integer(key); } } - + public int getRef() { return (int)ref; @@ -235,9 +226,9 @@ protected void finalize() { try { - synchronized (m_deleteListLock) + synchronized (m_toDelete) { - m_toDelete.add(new RegisterPair(LuaState.LUA_REGISTRYINDEX, ref)); + m_toDelete.add(ref); } } catch (Exception e) @@ -245,7 +236,7 @@ protected void finalize() System.err.println("Unable to release object " + ref); } } - + public static LuaObject fromReference(LuaState L, int ref) { L.rawGetI(REGISTRYINDEX, ref); LuaObject obj = new LuaObject(L,-1); @@ -434,11 +425,11 @@ public LuaObject getField(String field) throws LuaException * @return Object[] - Returned Objects * @throws LuaException */ - public Object[] call(Object[] args, int nres) throws LuaException - { + public Object[] call(Object[] args, int nres) throws LuaException + { return call(args,nres,false); } - + public Object[] call(Object[] args, int nres, boolean trace) throws LuaException { synchronized (L)