Skip to content

Commit

Permalink
Moved LuaReference into its own class, added ways for finalization
Browse files Browse the repository at this point in the history
  • Loading branch information
dafrito committed Jul 20, 2010
1 parent bdc0ac4 commit 41f8269
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 38 deletions.
5 changes: 5 additions & 0 deletions .classpath
Expand Up @@ -5,5 +5,10 @@
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="lib" path="lua.jar"/>
<classpathentry kind="lib" path="/home/dafrito/mirrors/guava-libraries/target/guava-r07-SNAPSHOT.jar" sourcepath="/home/dafrito/mirrors/guava-libraries/src">
<attributes>
<attribute name="javadoc_location" value="file:/home/dafrito/mirrors/guava-libraries/javadoc/"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="bin"/>
</classpath>
73 changes: 73 additions & 0 deletions src/com/dafrito/lua/script/LuaReference.java
@@ -0,0 +1,73 @@
package com.dafrito.lua.script;

import java.util.concurrent.atomic.AtomicInteger;

import com.google.common.base.FinalizablePhantomReference;
import com.google.common.base.FinalizableReferenceQueue;

import lua.LuaLibrary;
import lua.LuaLibrary.lua_State;

public class LuaReference {
private static final FinalizableReferenceQueue queue = new FinalizableReferenceQueue();
private static final LuaLibrary lua = LuaLibrary.INSTANCE;

private final LuaBindings b;
private AtomicInteger ref;
private final LuaPhantomReference finalizer;

public LuaReference(LuaBindings b) {
this.b = b;
this.ref = new AtomicInteger(lua.luaL_ref(b.getState(), LuaLibrary.LUA_REGISTRYINDEX));
this.finalizer = new LuaPhantomReference(this, queue);
check();
}

public LuaBindings getBindings() {
return this.b;
}

public void get() {
check();
lua.lua_rawgeti(b.getState(), LuaLibrary.LUA_REGISTRYINDEX, this.ref.get());
}

private void check() {
if (this.isClosed()) {
throw new RuntimeException();
}
}

public boolean isClosed() {
return this.ref.get() == LuaLibrary.LUA_REFNIL;
}

public void close() {
this.finalizer.close();
}

private static class LuaPhantomReference extends FinalizablePhantomReference<LuaReference> {

private AtomicInteger ref;
private lua_State state;

protected LuaPhantomReference(LuaReference referent, FinalizableReferenceQueue queue) {
super(referent, queue);
this.state = referent.getBindings().getState();
this.ref = referent.ref;
}

public void close() {
int v = this.ref.getAndSet(LuaLibrary.LUA_REFNIL);
if(v != LuaLibrary.LUA_REFNIL) {
lua.luaL_unref(state, LuaLibrary.LUA_REGISTRYINDEX, v);
}
}

@Override
public void finalizeReferent() {
this.close();
}
}

}
38 changes: 0 additions & 38 deletions test/com/dafrito/lua/script/LuaTableTest.java
Expand Up @@ -54,42 +54,4 @@ public void set(Object k, Object v) {
lua.lua_settop(s, -2);
}
}

class LuaReference {
private final LuaBindings b;
private int ref;

public LuaReference(LuaBindings b) {
this.b = b;
this.ref = lua.luaL_ref(b.getState(), LuaLibrary.LUA_REGISTRYINDEX);
check();
}

public LuaBindings getBindings() {
return this.b;
}

public void get() {
check();
lua.lua_rawgeti(b.getState(), LuaLibrary.LUA_REGISTRYINDEX, this.ref);
}

private void check() {
if (this.isClosed()) {
throw new RuntimeException();
}
}

public boolean isClosed() {
return this.ref == LuaLibrary.LUA_REFNIL;
}

public void close() {
if (this.isClosed()) {
return;
}
lua.luaL_unref(b.getState(), LuaLibrary.LUA_REGISTRYINDEX, this.ref);
this.ref = LuaLibrary.LUA_REFNIL;
}
}
}

0 comments on commit 41f8269

Please sign in to comment.