mirror of
https://github.com/Suwayomi/Tachidesk.git
synced 2025-12-22 20:42:37 +01:00
replace quickjs with Mozilla Rhino (#415)
* replace quickjs with jdk 8 default js engine * replace quickjs with rhino engine and translate type for read comic online extension * move quick js to AndroidCompat * fix commicabc long type cast exception
This commit is contained in:
69
AndroidCompat/src/main/java/app/cash/quickjs/QuickJs.java
Normal file
69
AndroidCompat/src/main/java/app/cash/quickjs/QuickJs.java
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
package app.cash.quickjs;
|
||||||
|
|
||||||
|
import org.mozilla.javascript.ConsString;
|
||||||
|
import org.mozilla.javascript.NativeArray;
|
||||||
|
|
||||||
|
import javax.script.ScriptEngine;
|
||||||
|
import javax.script.ScriptEngineManager;
|
||||||
|
import java.io.Closeable;
|
||||||
|
|
||||||
|
public final class QuickJs implements Closeable {
|
||||||
|
private ScriptEngine engine;
|
||||||
|
|
||||||
|
public static QuickJs create() {
|
||||||
|
return new QuickJs(new ScriptEngineManager());
|
||||||
|
}
|
||||||
|
|
||||||
|
public QuickJs(ScriptEngineManager manager) {
|
||||||
|
this.engine = manager.getEngineByName("rhino");
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object evaluate(String script, String fileName) {
|
||||||
|
return this.evaluate(script);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object evaluate(String script) {
|
||||||
|
try {
|
||||||
|
Object value = engine.eval(script);
|
||||||
|
return translateType(value);
|
||||||
|
} catch (Exception exception) {
|
||||||
|
throw new QuickJsException(exception.getMessage(), exception);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Object translateType(Object obj) {
|
||||||
|
if (obj instanceof NativeArray) {
|
||||||
|
NativeArray array = (NativeArray) obj;
|
||||||
|
long length = array.getLength();
|
||||||
|
Object[] objects = new Object[(int) length];
|
||||||
|
for (int i = 0; i < (int) length; i++) {
|
||||||
|
objects[i] = translateType(array.get(i));
|
||||||
|
}
|
||||||
|
return objects;
|
||||||
|
}
|
||||||
|
if (obj instanceof ConsString) {
|
||||||
|
ConsString consString = (ConsString) obj;
|
||||||
|
return consString.toString();
|
||||||
|
}
|
||||||
|
if (obj instanceof Long) {
|
||||||
|
Long value = (Long) obj;
|
||||||
|
return value.intValue();
|
||||||
|
}
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] compile(String sourceCode, String fileName) {
|
||||||
|
return sourceCode.getBytes();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public Object execute(byte[] bytecode) {
|
||||||
|
return this.evaluate(new String(bytecode));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() {
|
||||||
|
this.engine = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package app.cash.quickjs;
|
||||||
|
|
||||||
|
public final class QuickJsException extends RuntimeException {
|
||||||
|
public QuickJsException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -44,7 +44,6 @@ dependencies {
|
|||||||
implementation("com.squareup.okhttp3:okhttp:$okhttpVersion")
|
implementation("com.squareup.okhttp3:okhttp:$okhttpVersion")
|
||||||
implementation("io.reactivex:rxjava:1.3.8")
|
implementation("io.reactivex:rxjava:1.3.8")
|
||||||
implementation("org.jsoup:jsoup:1.14.3")
|
implementation("org.jsoup:jsoup:1.14.3")
|
||||||
implementation("app.cash.quickjs:quickjs-jvm:0.9.2")
|
|
||||||
|
|
||||||
// Sort
|
// Sort
|
||||||
implementation("com.github.gpanther:java-nat-sort:natural-comparator-1.1")
|
implementation("com.github.gpanther:java-nat-sort:natural-comparator-1.1")
|
||||||
|
|||||||
Reference in New Issue
Block a user