From bb09cfddb3f49d48d7abfca709a5d7174870649e Mon Sep 17 00:00:00 2001 From: Aria Moradi Date: Sun, 28 Mar 2021 01:15:22 +0430 Subject: [PATCH] Migrate from Oracle Nashorn to Mozilla's Rhino --- AndroidCompat/build.gradle.kts | 6 +- .../java/com/squareup/duktape/Duktape.java | 68 ++++++++----------- .../com/squareup/duktape/DuktapeStub.java | 37 ++++++++++ server/build.gradle.kts | 6 +- .../ir/armor/tachidesk/impl/Extension.kt | 1 + .../impl/{ => util}/APKExtractor.java | 2 +- 6 files changed, 74 insertions(+), 46 deletions(-) create mode 100644 AndroidCompat/src/main/java/com/squareup/duktape/DuktapeStub.java rename server/src/main/kotlin/ir/armor/tachidesk/impl/{ => util}/APKExtractor.java (99%) diff --git a/AndroidCompat/build.gradle.kts b/AndroidCompat/build.gradle.kts index 4c86208..dfab985 100644 --- a/AndroidCompat/build.gradle.kts +++ b/AndroidCompat/build.gradle.kts @@ -53,7 +53,11 @@ dependencies { // AndroidX annotations compileOnly( "androidx.annotation:annotation:1.2.0-alpha01") -// compileOnly("io.reactivex:rxjava:1.3.8") + // substitute for duktape-android + // 'org.mozilla:rhino' includes some code that we don't need so use 'org.mozilla:rhino-runtime' instead + implementation("org.mozilla:rhino-runtime:1.7.13") + // 'org.mozilla:rhino-engine' provides the same interface as 'javax.script' a.k.a Nashorn + implementation("org.mozilla:rhino-engine:1.7.13") } //def fatJarTask = tasks.getByPath(':AndroidCompat:JVMPatch:fatJar') diff --git a/AndroidCompat/src/main/java/com/squareup/duktape/Duktape.java b/AndroidCompat/src/main/java/com/squareup/duktape/Duktape.java index 18f1a34..cb30a95 100644 --- a/AndroidCompat/src/main/java/com/squareup/duktape/Duktape.java +++ b/AndroidCompat/src/main/java/com/squareup/duktape/Duktape.java @@ -1,20 +1,12 @@ -/* - * Copyright (C) 2015 Square, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ package com.squareup.duktape; +/* + * Copyright (C) Contributors to the Suwayomi project + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ + import kotlin.NotImplementedError; import javax.script.ScriptEngine; @@ -22,11 +14,18 @@ import javax.script.ScriptEngineManager; import javax.script.ScriptException; import java.io.Closeable; -/** A simple EMCAScript (Javascript) interpreter. */ +/* Note (March 2021): + * The old implementation for duktape-android used the nashorn engine which is deprecated. + * This new implementation uses Mozilla's Rhino: https://github.com/mozilla/rhino + */ + +/** + * A simple EMCAScript (Javascript) interpreter. + */ public final class Duktape implements Closeable, AutoCloseable { private ScriptEngineManager factory = new ScriptEngineManager(); - private ScriptEngine engine = factory.getEngineByName("JavaScript"); + private ScriptEngine engine = factory.getEngineByName("rhino"); /** * Create a new interpreter instance. Calls to this method must matched with @@ -38,17 +37,6 @@ public final class Duktape implements Closeable, AutoCloseable { private Duktape() {} - /** - * Evaluate {@code script} and return a result. {@code fileName} will be used in error - * reporting. Note that the result must be one of the supported Java types or the call will - * return null. - * - * @throws DuktapeException if there is an error evaluating the script. - */ - public synchronized Object evaluate(String script, String fileName) { - throw new NotImplementedError("Not implemented!"); - } - /** * Evaluate {@code script} and return a result. Note that the result must be one of the * supported Java types or the call will return null. @@ -76,18 +64,18 @@ public final class Duktape implements Closeable, AutoCloseable { throw new NotImplementedError("Not implemented!"); } - /** - * Attaches to a global JavaScript object called {@code name} that implements {@code type}. - * {@code type} defines the interface implemented in JavaScript that will be accessible to Java. - * {@code type} must be an interface that does not extend any other interfaces, and cannot define - * any overloaded methods. - *

Methods of the interface may return {@code void} or any of the following supported argument - * types: {@code boolean}, {@link Boolean}, {@code int}, {@link Integer}, {@code double}, - * {@link Double}, {@link String}. - */ - public synchronized T get(final String name, final Class type) { - throw new NotImplementedError("Not implemented!"); - } +// /** +// * Attaches to a global JavaScript object called {@code name} that implements {@code type}. +// * {@code type} defines the interface implemented in JavaScript that will be accessible to Java. +// * {@code type} must be an interface that does not extend any other interfaces, and cannot define +// * any overloaded methods. +// *

Methods of the interface may return {@code void} or any of the following supported argument +// * types: {@code boolean}, {@link Boolean}, {@code int}, {@link Integer}, {@code double}, +// * {@link Double}, {@link String}. +// */ +// public synchronized T get(final String name, final Class type) { +// throw new NotImplementedError("Not implemented!"); +// } /** * Release the native resources associated with this object. You must call this diff --git a/AndroidCompat/src/main/java/com/squareup/duktape/DuktapeStub.java b/AndroidCompat/src/main/java/com/squareup/duktape/DuktapeStub.java new file mode 100644 index 0000000..91ab1ed --- /dev/null +++ b/AndroidCompat/src/main/java/com/squareup/duktape/DuktapeStub.java @@ -0,0 +1,37 @@ +package com.squareup.duktape; + +/* + * Copyright (C) Contributors to the Suwayomi project + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ +// part of tachiyomi-extensions which was originally licensed under Apache License Version 2.0 + + +import java.io.Closeable; +import java.io.IOException; + +/** This is the reference Duktape stub that tachiyomi's extensions depend on. + * Intended to be used as a reference. + */ +public class DuktapeStub implements Closeable { + + public static Duktape create() { + throw new RuntimeException("Stub!"); + } + + @Override + public synchronized void close() throws IOException { + throw new RuntimeException("Stub!"); + } + + public synchronized Object evaluate(String script) { + throw new RuntimeException("Stub!"); + } + + public synchronized void set(String name, Class type, T object) { + throw new RuntimeException("Stub!"); + } + +} \ No newline at end of file diff --git a/server/build.gradle.kts b/server/build.gradle.kts index 164917a..9848ec0 100644 --- a/server/build.gradle.kts +++ b/server/build.gradle.kts @@ -57,7 +57,6 @@ dependencies { implementation("org.jsoup:jsoup:1.13.1") implementation("com.github.salomonbrys.kotson:kotson:2.5.0") - implementation("com.squareup.duktape:duktape-android:1.3.0") val coroutinesVersion = "1.3.9" @@ -86,9 +85,8 @@ dependencies { implementation(project(":AndroidCompat")) implementation(project(":AndroidCompat:Config")) - -// testImplementation("org.jetbrains.kotlin:kotlin-test") -// testImplementation("org.jetbrains.kotlin:kotlin-test-junit") + // uncomment to test extensions directly +// implementation(fileTree("lib/")) } val name = "ir.armor.tachidesk.Main" diff --git a/server/src/main/kotlin/ir/armor/tachidesk/impl/Extension.kt b/server/src/main/kotlin/ir/armor/tachidesk/impl/Extension.kt index b8a98e4..ba1baae 100644 --- a/server/src/main/kotlin/ir/armor/tachidesk/impl/Extension.kt +++ b/server/src/main/kotlin/ir/armor/tachidesk/impl/Extension.kt @@ -17,6 +17,7 @@ import eu.kanade.tachiyomi.source.SourceFactory import eu.kanade.tachiyomi.source.online.HttpSource import ir.armor.tachidesk.database.table.ExtensionTable import ir.armor.tachidesk.database.table.SourceTable +import ir.armor.tachidesk.impl.util.APKExtractor import ir.armor.tachidesk.server.applicationDirs import kotlinx.coroutines.runBlocking import mu.KotlinLogging diff --git a/server/src/main/kotlin/ir/armor/tachidesk/impl/APKExtractor.java b/server/src/main/kotlin/ir/armor/tachidesk/impl/util/APKExtractor.java similarity index 99% rename from server/src/main/kotlin/ir/armor/tachidesk/impl/APKExtractor.java rename to server/src/main/kotlin/ir/armor/tachidesk/impl/util/APKExtractor.java index bc50a08..4210d04 100644 --- a/server/src/main/kotlin/ir/armor/tachidesk/impl/APKExtractor.java +++ b/server/src/main/kotlin/ir/armor/tachidesk/impl/util/APKExtractor.java @@ -1,4 +1,4 @@ -package ir.armor.tachidesk.impl; +package ir.armor.tachidesk.impl.util; /* * Copyright (C) Contributors to the Suwayomi project