Skip to content

Commit 6fb41a4

Browse files
authored
Merge pull request #97 from HarlonWang/feature/2.4.5
Feature/2.4.5
2 parents 47c1dd0 + 09f924d commit 6fb41a4

File tree

5 files changed

+56
-19
lines changed

5 files changed

+56
-19
lines changed

CHANGELOG.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
# Change Log
22

3-
## 2.4.4 *(2024-02-12)*
3+
## 2.4.5 *(2025-03-15)*
4+
- fix: not correctly released when an exception occurs during function execution
5+
6+
## 2.4.4 *(2025-02-12)*
47
- 优化 toMap 的循环引用处理逻辑
58
- quickjs 增加判空处理,解决 OOM 场景里的异常崩溃问题
69

7-
## 2.4.3 *(2024-01-21)*
10+
## 2.4.3 *(2025-01-21)*
811
- 添加了在将 JSObjects 转换为 Java Maps 时支持自定义映射创建。
912
- 添加了 setGCThreshold 方法,用于控制垃圾回收阈值。
1013

11-
## 2.4.2 *(2024-01-10)*
14+
## 2.4.2 *(2025-01-10)*
1215
- 修复:源码执行模式下的字符串泄漏问题
1316

1417
## 2.4.1 *(2024-12-04)*

native/cpp/quickjs_wrapper.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,7 @@ jobject QuickJSWrapper::call(JNIEnv *env, jobject thiz, jlong func, jlong this_o
551551

552552
JSValue ret = JS_Call(context, jsFunc, jsObj, arguments.size(), arguments.data());
553553
if (JS_IsException(ret)) {
554+
JS_FreeValue(context, ret);
554555
throwJSException(env, context);
555556
return nullptr;
556557
}

wrapper-android/src/androidTest/java/com/whl/quickjs/wrapper/QuickJSTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,4 +1331,26 @@ public void testArraySameRefToMap() {
13311331
}
13321332
}
13331333

1334+
@Test
1335+
public void testCallThrowError() {
1336+
QuickJSContext context = createContext();
1337+
JSFunction function = null;
1338+
try {
1339+
function = (JSFunction) context.evaluate("function testThrowError() {\n" +
1340+
"\tthrow new Error(\"test\");\n" +
1341+
"}\n" +
1342+
"\n" +
1343+
"testThrowError;");
1344+
function.call();
1345+
} catch (Exception e) {
1346+
if (function != null) {
1347+
// 测试
1348+
function.release();
1349+
}
1350+
int defaultLength = 1;
1351+
assertEquals(defaultLength, context.getObjectRecords().size());
1352+
context.destroy();
1353+
}
1354+
}
1355+
13341356
}

wrapper-java/src/main/java/com/whl/quickjs/wrapper/QuickJSContext.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.io.Closeable;
44
import java.io.File;
5-
import java.io.IOException;
65
import java.util.ArrayList;
76
import java.util.HashMap;
87
import java.util.Iterator;
@@ -135,24 +134,30 @@ public long getMemoryUsedSize() {
135134
}
136135

137136
public void dumpMemoryUsage(File target) {
138-
if (target == null || !target.exists()) {
139-
return;
137+
checkSameThread();
138+
checkDestroyed();
139+
String fileName = null;
140+
if (target != null && target.exists()) {
141+
fileName = target.getAbsolutePath();
140142
}
141143

142-
dumpMemoryUsage(runtime, target.getAbsolutePath());
144+
dumpMemoryUsage(runtime, fileName);
143145
}
144146

145147
// will use stdout to print.
146148
public void dumpMemoryUsage() {
147-
dumpMemoryUsage(runtime, null);
149+
dumpMemoryUsage(null);
148150
}
149151

150152
public void dumpObjects(File target) {
151-
if (target == null || !target.exists()) {
152-
return;
153+
checkSameThread();
154+
checkDestroyed();
155+
String fileName = null;
156+
if (target != null && target.exists()) {
157+
fileName = target.getAbsolutePath();
153158
}
154159

155-
dumpObjects(runtime, target.getAbsolutePath());
160+
dumpObjects(runtime, fileName);
156161
}
157162

158163
public JSObjectCreator getCreator() {
@@ -161,7 +166,7 @@ public JSObjectCreator getCreator() {
161166

162167
// will use stdout to print.
163168
public void dumpObjects() {
164-
dumpObjects(runtime, null);
169+
dumpObjects(null);
165170
}
166171

167172
private final long runtime;

wrapper-java/src/main/java/com/whl/quickjs/wrapper/QuickJSFunction.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,22 @@ public Object call(Object... args) {
4444
checkRefCountIsZero();
4545

4646
currentStatus = Status.CALLING;
47-
Object ret = getContext().call(this, thisPointer, thisPointerTag, args);
48-
currentStatus = Status.CALLED;
47+
Object ret;
48+
try {
49+
ret = getContext().call(this, thisPointer, thisPointerTag, args);
50+
} finally {
51+
// call 可能会抛出异常,需要保障以下代码被执行,不然因为状态不对,导致无法正常 release。
52+
currentStatus = Status.CALLED;
4953

50-
if (stashTimes > 0) {
51-
// 如果有暂存,这里需要恢复下 release 操作
52-
for (int i = 0; i < stashTimes; i++) {
53-
release();
54+
if (stashTimes > 0) {
55+
// 如果有暂存,这里需要恢复下 release 操作
56+
for (int i = 0; i < stashTimes; i++) {
57+
release();
58+
}
59+
stashTimes = 0;
5460
}
55-
stashTimes = 0;
5661
}
62+
5763
return ret;
5864
}
5965

0 commit comments

Comments
 (0)