-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickJSConnector.java
More file actions
199 lines (180 loc) · 7.55 KB
/
Copy pathQuickJSConnector.java
File metadata and controls
199 lines (180 loc) · 7.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package org.scriptable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.function.Predicate;
import java.lang.ref.WeakReference;
public class QuickJSConnector {
// NOTE: since this class must be loaded from tomcat's lib using its root class loader,
// static variables here will be shared by all apps
String filename;
String mainFunc;
String ctxKey;
private ArrayList<WeakReference<QJSRuntime>> allInstances;
// need to maintain per app/script list, since static is shared between apps
private static HashMap<String, ArrayList<WeakReference<QJSRuntime>>> allInstancesMap = new HashMap<>();
long timestamp;
static {
System.loadLibrary("quickjsc");
}
private native static byte[] nativeNewQJSRuntime(String filename, String mainFunc);
private native static void nativeFreeQJSRuntime(byte[] ctx);
private native int nativeCallQJS(byte[] ctx, Object[] argv);
private native Object[] nativeGetQJSException(byte[] ctx);
public QuickJSConnector(String filename, String mainFunc, long timestamp) {
this.filename = filename;
this.mainFunc = mainFunc;
this.ctxKey = makeCtxKey(filename, mainFunc);
this.timestamp = timestamp;
synchronized(QuickJSConnector.class) {
if (!allInstancesMap.containsKey(this.ctxKey))
allInstancesMap.put(this.ctxKey, new ArrayList<WeakReference<QJSRuntime>>());
this.allInstances = allInstancesMap.get(this.ctxKey);
}
}
public static String makeCtxKey(String filename, String mainFunc) {
return filename + "/" + mainFunc;
}
public Object[] callJava(Object[] argv) {
Object [] ret = new Object[] { "__error__", "sample error" };
return ret;
}
// QJS runtime must be used by the thread which created it
// Also, worker threads in tomcat are shared by all apps
static ThreadLocal<HashMap<String, QJSRuntime>> perThread = new ThreadLocal<>();
private static final class QJSRuntime {
byte[] ctx;
long timestamp;
String ctxKey;
@SuppressWarnings("unchecked")
private QJSRuntime(byte[] ctx, String ctxKey, long timestamp) {
this.ctx = ctx;
this.ctxKey = ctxKey;
this.timestamp = timestamp;
}
static QJSRuntime getInstance(QuickJSConnector c) {
HashMap<String, QJSRuntime> rtMap = perThread.get();
if (rtMap == null) {
rtMap = new HashMap<>();
perThread.set(rtMap);
}
QJSRuntime rt = rtMap.get(c.ctxKey);
if (rt != null && (rt.timestamp < c.timestamp)) {
rt.release(c.allInstances);
rt = null;
}
if (rt == null || rt.ctx == null || rt.ctx.length == 0) synchronized(QuickJSConnector.class) {
rt = new QJSRuntime(nativeNewQJSRuntime(c.filename, c.mainFunc), c.ctxKey, c.timestamp);
c.allInstances.add(new WeakReference(rt));
if (rt.ctx == null || rt.ctx.length == 0) {
rt.ctx = null;
throw new RuntimeException("Failed to create quickjs runtime!");
}
rtMap.put(c.ctxKey, rt);
String compileError = c.getErrorStackTrace(rt);
if (compileError != null) {
rt.release(c.allInstances);
throw new RuntimeException("Error while loading " + c.filename + "\n" + compileError);
}
}
return rt;
}
void release(ArrayList<WeakReference<QJSRuntime>> allInstances) {
if (ctx != null && ctx.length > 0) synchronized(QuickJSConnector.class) {
HashMap<String, QJSRuntime> rtMap = perThread.get();
if (rtMap != null)
rtMap.remove(ctxKey);
nativeFreeQJSRuntime(ctx);
ctx = null;
QJSRuntime rt = this;
allInstances.removeIf(new Predicate<WeakReference<QJSRuntime>>() {
@Override public boolean test(WeakReference<QJSRuntime> wr) {
return wr.get() == rt;
}
});
}
}
static void releaseAll(ArrayList<WeakReference<QJSRuntime>> allInstances) {
synchronized(QuickJSConnector.class) {
for (WeakReference<QJSRuntime> wr: allInstances) {
QJSRuntime rt = wr.get();
if (rt != null && rt.ctx != null && rt.ctx.length > 0) {
nativeFreeQJSRuntime(rt.ctx);
rt.ctx = null;
}
else
System.out.println("releaseAll: null ctx still in the allInstances array");
}
allInstances.clear();
}
}
@Override protected void finalize() { // normally these should be released by release/releaseAll
synchronized(QuickJSConnector.class) {
if (ctx != null && ctx.length > 0) {
nativeFreeQJSRuntime(ctx);
ctx = null;
QJSRuntime rt = this;
ArrayList<WeakReference<QJSRuntime>> allInstances = allInstancesMap.get(ctxKey);
if (allInstances != null) {
allInstances.removeIf(new Predicate<WeakReference<QJSRuntime>>() {
@Override public boolean test(WeakReference<QJSRuntime> wr) {
return wr.get() == rt;
}
});
}
}
}
}
}
public String getErrorStackTrace(QJSRuntime rt) {
String error = null;
Object[] st = nativeGetQJSException(rt.ctx);
if (st != null && st.length > 0) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < st.length; i++) {
sb.append(st[i] == null? "NULL" : st[i].toString());
sb.append("\n");
}
error = sb.toString();
}
return error;
}
/* return null if OK, or error stack trace otherwise */
public int callQJS(Object[] argv) throws Exception {
String error = null;
int ret = 0;
try {
QJSRuntime rt = QJSRuntime.getInstance(this);
ret = nativeCallQJS(rt.ctx, argv);
if (ret < 0) {
error = getErrorStackTrace(rt);
rt.release(allInstances);
}
} catch(Exception e) {
error = e.getMessage();
}
if (error != null)
throw new Exception(error);
return ret;
}
public void releaseAllRuntimes() {
releaseAllRuntimes(filename, mainFunc);
}
public static void releaseAllRuntimes(String filename, String mainFunc) {
synchronized(QuickJSConnector.class) {
ArrayList<WeakReference<QJSRuntime>> allInstances = allInstancesMap.get(makeCtxKey(filename, mainFunc));
if (allInstances != null)
QJSRuntime.releaseAll(allInstances);
}
}
public static void main(String[] args) {
QuickJSConnector c = new QuickJSConnector("./test.js", "handleRequest", 0);
for (int i = 0; i < 1000000; i++) {
try {
c.callQJS(new Object[] { "GET", "/test", "param1", "Саша" });
} catch(Exception e) {
System.err.print(e.getMessage());
}
}
c.releaseAllRuntimes();
}
}