Skip to content

Commit 3d08cdb

Browse files
committed
add better handling of Proxy lifecycle
1 parent af91f79 commit 3d08cdb

File tree

9 files changed

+32
-11
lines changed

9 files changed

+32
-11
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,8 @@ __Example__
384384
385385
Creates a new java Proxy for the given interface. Functions passed in will run on the v8 main thread and not a new thread.
386386
387-
The returned object has two methods ref() and unref() which you can use to maintain references to prevent premature
388-
garbage collection. You must call these methods to ensure the proxy stays around.
387+
The returned object has a method unref() which you can use to free the object for
388+
garbage collection.
389389
390390
__Arguments__
391391

compile-java-code.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ javac ${JAVAC_OPTS} *.java
88

99
cd ../src-java/node
1010
javac ${JAVAC_OPTS} *.java
11+
12+
cd ../../
13+
javah -classpath src-java -d ./src node.NodeDynamicProxyClass
12 Bytes
Binary file not shown.

src-java/node/NodeDynamicProxyClass.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package node;
22

3-
public class NodeDynamicProxyClass implements java.lang.reflect.InvocationHandler
4-
{
3+
import java.util.HashSet;
4+
5+
public class NodeDynamicProxyClass implements java.lang.reflect.InvocationHandler {
56
private native Object callJs(long ptr, java.lang.reflect.Method m, Object[] args) throws Throwable;
7+
private native void unref(long ptr) throws Throwable;
68
public long ptr;
79

810
public NodeDynamicProxyClass(String path, long ptr) {
@@ -25,11 +27,7 @@ public Object invoke(Object proxy, java.lang.reflect.Method m, Object[] args) th
2527
return result;
2628
}
2729

28-
public void ref() {
29-
30-
}
31-
32-
public void unref() {
33-
30+
public void unref() throws Throwable {
31+
unref(this.ptr);
3432
}
3533
}

src/java.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ void Java::destroyJVM(JavaVM** jvm, JNIEnv** env) {
356356
if(result->IsNativeError()) {
357357
return ThrowException(result);
358358
}
359+
dynamicProxyData->jsObject = v8::Persistent<v8::Value>::New(result);
359360
return scope.Close(result);
360361
}
361362

@@ -989,3 +990,11 @@ JNIEXPORT jobject JNICALL Java_node_NodeDynamicProxyClass_callJs(JNIEnv *env, jo
989990
}
990991
return dynamicProxyData->result;
991992
}
993+
994+
JNIEXPORT void JNICALL Java_node_NodeDynamicProxyClass_unref(JNIEnv *env, jobject src, jlong ptr) {
995+
DynamicProxyData* dynamicProxyData = (DynamicProxyData*)ptr;
996+
if(!dynamicProxyDataVerify(dynamicProxyData)) {
997+
return;
998+
}
999+
dynamicProxyData->jsObject.Clear();
1000+
}

src/javaObject.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ JavaObject::~JavaObject() {
9797
jfieldID ptrField = env->GetFieldID(nodeDynamicProxyClass, "ptr", "J");
9898
DynamicProxyData* proxyData = (DynamicProxyData*)(long)env->GetLongField(m_obj, ptrField);
9999
if(dynamicProxyDataVerify(proxyData)) {
100+
proxyData->markerStart = 0;
101+
proxyData->markerEnd = 0;
100102
delete proxyData;
101103
}
102104
}

src/node_NodeDynamicProxyClass.h

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/utils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ int dynamicProxyDataVerify(DynamicProxyData* data) {
694694
return 1;
695695
}
696696

697-
printf("*** ERROR: Lost reference to the dynamic proxy. You must maintain a reference in javascript land using ref() and unref(). ***\n");
697+
printf("*** ERROR: Lost reference to the dynamic proxy. You must maintain a reference in javascript land using ref() and unref(). (%p) ***\n", data);
698698
return 0;
699699
}
700700

src/utils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ struct DynamicProxyData {
3535
Java* java;
3636
std::string interfaceName;
3737
v8::Persistent<v8::Object> functions;
38+
v8::Persistent<v8::Value> jsObject;
3839
std::string methodName;
3940
jobjectArray args;
4041
jobject result;

0 commit comments

Comments
 (0)