handle js-framework rollup

This commit is contained in:
王劲鹏
2019-10-23 16:00:21 +08:00
committed by pengfei.zhou
parent 186b0dfb80
commit 9864b57b21
6 changed files with 303 additions and 307 deletions

View File

@@ -11,6 +11,8 @@ import org.jetbrains.annotations.NotNull;
import java.io.EOFException;
import java.net.ConnectException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;
@@ -25,6 +27,8 @@ public class RemoteJSExecutor {
private final WebSocket webSocket;
private final Gson gson = new Gson();
private final Map<String, JavaFunction> globalFunctions = new HashMap<>();
public RemoteJSExecutor() {
OkHttpClient okHttpClient = new OkHttpClient
.Builder()
@@ -55,36 +59,40 @@ public class RemoteJSExecutor {
@Override
public void onMessage(@NotNull WebSocket webSocket, @NotNull String text) {
JsonElement je = gson.fromJson(text, JsonElement.class);
System.out.println(je);
LockSupport.unpark(current);
if (je instanceof JsonObject) {
JsonObject jo = ((JsonObject) je);
String cmd = jo.get("cmd").getAsString();
switch (cmd) {
case "injectGlobalJSFunction":
String name = jo.get("name").getAsString();
JsonObject arguments = jo.getAsJsonObject("arguments");
for (String key : arguments.keySet()) {
System.out.println(key + " " + arguments.get(key));
}
break;
}
}
}
});
LockSupport.park(current);
}
public String loadJS(String script, String source) {
JsonObject jo = new JsonObject();
jo.addProperty("cmd", "loadJS");
jo.addProperty("script", script);
jo.addProperty("source", source);
webSocket.send(gson.toJson(jo));
LockSupport.park(Thread.currentThread());
return null;
}
public JSDecoder evaluateJS(String script, String source, boolean hashKey) {
JsonObject jo = new JsonObject();
jo.addProperty("cmd", "evaluateJS");
jo.addProperty("script", script);
jo.addProperty("source", source);
jo.addProperty("hashKey", hashKey);
webSocket.send(gson.toJson(jo));
return null;
}
public void injectGlobalJSFunction(String name, JavaFunction javaFunction) {
globalFunctions.put(name, javaFunction);
JsonObject jo = new JsonObject();
jo.addProperty("cmd", "injectGlobalJSFunction");
jo.addProperty("name", name);
webSocket.send(gson.toJson(jo));
}
public void injectGlobalJSObject(String name, JavaValue javaValue) {

View File

@@ -0,0 +1,91 @@
package pub.doric.engine.remote;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.ByteArrayOutputStream;
import java.util.Iterator;
/**
* Created by pengfei.zhou on 2018/4/17.
*/
public class ValueBuilder {
private final Object val;
private void writeBoolean(ByteArrayOutputStream output, boolean b) {
output.write((byte) (b ? 1 : 0));
}
private void writeInt(ByteArrayOutputStream output, int i) {
output.write((byte) (i >>> 24));
output.write((byte) (i >>> 16));
output.write((byte) (i >>> 8));
output.write((byte) i);
}
private void writeDouble(ByteArrayOutputStream output, double d) {
long l = Double.doubleToRawLongBits(d);
output.write((byte) (l >>> 56));
output.write((byte) (l >>> 48));
output.write((byte) (l >>> 40));
output.write((byte) (l >>> 32));
output.write((byte) (l >>> 24));
output.write((byte) (l >>> 16));
output.write((byte) (l >>> 8));
output.write((byte) l);
}
private void writeString(ByteArrayOutputStream output, String S) {
byte[] buf;
try {
buf = S.getBytes("UTF-8");
} catch (Exception e) {
buf = new byte[0];
}
int i = buf.length;
writeInt(output, i);
output.write(buf, 0, i);
}
private void write(ByteArrayOutputStream output, Object O) {
if (O instanceof Number) {
output.write((byte) 'D');
writeDouble(output, Double.valueOf(String.valueOf(O)));
} else if (O instanceof String) {
output.write((byte) 'S');
writeString(output, (String) O);
} else if (O instanceof Boolean) {
output.write((byte) 'B');
writeBoolean(output, (Boolean) O);
} else if (O instanceof JSONObject) {
output.write((byte) 'O');
//writeBoolean(output, (Boolean) O);
Iterator<String> iterator = ((JSONObject) O).keys();
while (iterator.hasNext()) {
String key = iterator.next();
writeInt(output, key.hashCode());
write(output, ((JSONObject) O).opt(key));
}
writeInt(output, 0);
} else if (O instanceof JSONArray) {
output.write((byte) 'A');
writeInt(output, ((JSONArray) O).length());
for (int i = 0; i < ((JSONArray) O).length(); i++) {
write(output, ((JSONArray) O).opt(i));
}
} else {
output.write((byte) 'N');
}
}
public ValueBuilder(Object o) {
this.val = o;
}
public byte[] build() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
write(outputStream, val);
return outputStream.toByteArray();
}
}