Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug修复 #667

Merged
merged 4 commits into from
Sep 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions autojs/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<!-- termux -->
<uses-permission android:name="com.termux.permission.RUN_COMMAND"/>
<uses-permission
android:name="android.permission.WRITE_SETTINGS"
tools:ignore="ProtectedPermissions" />
Expand Down
2 changes: 1 addition & 1 deletion autojs/src/main/assets/modules/__images__.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = function (runtime, scope) {
if (Array.isArray(list)) {
this.matches = list;
} else {
this.matches = runtime.bridges.bridges.toArray(list);
this.matches = runtime.bridges.toArray(list);
}
this.__defineGetter__("points", () => {
if (typeof (this.__points__) == 'undefined') {
Expand Down
4 changes: 2 additions & 2 deletions autojs/src/main/java/com/stardust/autojs/AutoJs.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import com.stardust.autojs.engine.LoopBasedJavaScriptEngine;
import com.stardust.autojs.engine.RootAutomatorEngine;
import com.stardust.autojs.engine.ScriptEngineManager;
import com.stardust.autojs.rhino.InterruptibleAndroidContextFactory;
import com.stardust.autojs.rhino.AndroidContextFactory;
import com.stardust.autojs.runtime.ScriptRuntime;
import com.stardust.autojs.runtime.accessibility.AccessibilityConfig;
import com.stardust.autojs.runtime.api.AppUtils;
Expand Down Expand Up @@ -133,7 +133,7 @@ protected void initScriptEngineManager() {
}

protected void initContextFactory() {
ContextFactory.initGlobal(new InterruptibleAndroidContextFactory(new File(mContext.getCacheDir(), "classes")));
ContextFactory.initGlobal(new AndroidContextFactory(new File(mContext.getCacheDir(), "classes")));
}

protected ScriptRuntime createRuntime() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package com.stardust.autojs.core.ui.nativeview;

import android.graphics.PorterDuff;
import android.view.View;
import android.widget.Button;

import com.stardust.autojs.core.ui.JsViewHelper;
import com.stardust.autojs.core.ui.ViewExtras;
import com.stardust.autojs.core.ui.attribute.ViewAttributes;
import com.stardust.autojs.rhino.NativeJavaObjectWithPrototype;
import com.stardust.autojs.runtime.ScriptRuntime;

import org.mozilla.javascript.NativeJavaObject;
import org.mozilla.javascript.NativeObject;
import org.mozilla.javascript.ScriptRuntime;
import org.mozilla.javascript.Scriptable;

public class NativeView extends NativeJavaObjectWithPrototype {
Expand Down Expand Up @@ -45,7 +42,7 @@ public LongClickEvent(View view) {
private final View mView;
private final ViewPrototype mViewPrototype;

public NativeView(Scriptable scope, View view, Class<?> staticType, com.stardust.autojs.runtime.ScriptRuntime runtime) {
public NativeView(Scriptable scope, View view, Class<?> staticType, ScriptRuntime runtime) {
super(scope, view, staticType);
mViewAttributes = ViewExtras.getViewAttributes(view, runtime.ui.getResourceParser());
mView = view;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ import com.stardust.autojs.engine.module.AssetAndUrlModuleSourceProvider
import com.stardust.autojs.engine.module.ScopeRequire
import com.stardust.autojs.execution.ExecutionConfig
import com.stardust.autojs.project.ScriptConfig
import com.stardust.autojs.rhino.AndroidContextFactory
import com.stardust.autojs.rhino.RhinoAndroidHelper
import com.stardust.autojs.rhino.TopLevelScope
import com.stardust.autojs.runtime.ScriptRuntime
import com.stardust.autojs.script.JavaScriptSource
import com.stardust.automator.UiObjectCollection
import com.stardust.pio.UncheckedIOException
import org.mozilla.javascript.*
import org.mozilla.javascript.Context
import org.mozilla.javascript.Script
import org.mozilla.javascript.Scriptable
import org.mozilla.javascript.ScriptableObject
import org.mozilla.javascript.commonjs.module.provider.SoftCachingModuleScriptProvider
import java.io.IOException
import java.io.InputStreamReader
import java.io.Reader
import java.util.*
import java.util.concurrent.ConcurrentHashMap

/**
Expand All @@ -28,29 +29,26 @@ import java.util.concurrent.ConcurrentHashMap
open class RhinoJavaScriptEngine(private val mAndroidContext: android.content.Context) :
JavaScriptEngine() {

val context: Context
private val mScriptable: TopLevelScope
val context: Context = enterContext()
private val mScriptable: TopLevelScope = createScope(this.context)
lateinit var thread: Thread
private set

private val initScript: Script
get() {
return sInitScript ?: try {
val reader = InputStreamReader(mAndroidContext.assets.open("init.js"))
val script = context.compileReader(reader, SOURCE_NAME_INIT, 1, null)
sInitScript = script
script
} catch (e: IOException) {
throw UncheckedIOException(e)
}
private val initScript: Script by lazy<Script> {
try {
val reader = InputStreamReader(mAndroidContext.assets.open("init.js"))
val script = context.compileReader(reader, SOURCE_NAME_INIT, 1, null)
script
} catch (e: IOException) {
throw UncheckedIOException(e)
}
}

val scriptable: Scriptable
get() = mScriptable

init {
this.context = enterContext()
mScriptable = createScope(this.context)

}

override fun put(name: String, value: Any?) {
Expand All @@ -59,13 +57,13 @@ open class RhinoJavaScriptEngine(private val mAndroidContext: android.content.Co

override fun setRuntime(runtime: ScriptRuntime) {
super.setRuntime(runtime)
runtime.bridges.setup(this)
runtime.topLevelScope = mScriptable
}

public override fun doExecution(source: JavaScriptSource): Any? {
var reader = source.nonNullScriptReader
val reader = source.nonNullScriptReader
try {
reader = preprocess(reader)
val script = context.compileReader(reader, source.toString(), 1, null)
return if (hasFeature(ScriptConfig.FEATURE_CONTINUATION)) {
context.executeScriptWithContinuations(script, mScriptable)
Expand All @@ -83,10 +81,6 @@ open class RhinoJavaScriptEngine(private val mAndroidContext: android.content.Co
return config != null && config.scriptConfig.hasFeature(feature)
}

@Throws(IOException::class)
protected fun preprocess(script: Reader): Reader {
return script
}

override fun forceStop() {
Log.d(LOG_TAG, "forceStop: interrupt Thread: $thread")
Expand Down Expand Up @@ -117,7 +111,7 @@ open class RhinoJavaScriptEngine(private val mAndroidContext: android.content.Co
}
}

internal fun initRequireBuilder(context: Context, scope: Scriptable) {
private fun initRequireBuilder(context: Context, scope: Scriptable) {
val provider = AssetAndUrlModuleSourceProvider(
mAndroidContext,
listOf(
Expand All @@ -132,7 +126,7 @@ open class RhinoJavaScriptEngine(private val mAndroidContext: android.content.Co
require.install(scope)
}

protected fun createScope(context: Context): TopLevelScope {
private fun createScope(context: Context): TopLevelScope {
val topLevelScope = TopLevelScope()
topLevelScope.initStandardObjects(context, false)
return topLevelScope
Expand All @@ -145,23 +139,12 @@ open class RhinoJavaScriptEngine(private val mAndroidContext: android.content.Co
return context
}

protected fun setupContext(context: Context) {
context.optimizationLevel = -1
context.languageVersion = Context.VERSION_ES6
context.locale = Locale.getDefault()
fun setupContext(context: Context) {
context.wrapFactory = WrapFactory()
}

private inner class WrapFactory : org.mozilla.javascript.WrapFactory() {

override fun wrap(cx: Context, scope: Scriptable, obj: Any?, staticType: Class<*>?): Any? {
return when {
obj is String -> runtime.bridges.toString(obj.toString())
staticType == UiObjectCollection::class.java -> runtime.bridges.asArray(obj as UiObjectCollection)
else -> super.wrap(cx, scope, obj, staticType)
}
}

private inner class WrapFactory : AndroidContextFactory.WrapFactory() {
override fun wrapAsJavaObject(
cx: Context?,
scope: Scriptable,
Expand All @@ -179,16 +162,10 @@ open class RhinoJavaScriptEngine(private val mAndroidContext: android.content.Co
}

companion object {
const val SOURCE_NAME_INIT = "<init>"
private const val LOG_TAG = "RhinoJavaScriptEngine"

val SOURCE_NAME_INIT = "<init>"

private val LOG_TAG = "RhinoJavaScriptEngine"

private val MODULES_PATH = "modules"
private var sInitScript: Script? = null
private val sContextEngineMap = ConcurrentHashMap<Context, RhinoJavaScriptEngine>()


fun getEngineOfContext(context: Context): RhinoJavaScriptEngine? {
return sContextEngineMap[context]
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.stardust.autojs.rhino

import android.os.Looper
import android.util.Log
import com.stardust.autojs.runtime.ScriptBridges
import com.stardust.autojs.runtime.exception.ScriptInterruptedException
import com.stardust.automator.UiObjectCollection
import org.mozilla.javascript.Context
import org.mozilla.javascript.ContextFactory
import org.mozilla.javascript.Scriptable
import java.io.File
import java.util.Locale
import java.util.concurrent.atomic.AtomicInteger

/**
* Created by Stardust on 2017/4/5.
*/
open class AndroidContextFactory(private val cacheDirectory: File) : ContextFactory() {
companion object {
const val LOG_TAG = "ContextFactory"
val bridges = ScriptBridges()
}

private val mContextCount = AtomicInteger()
private val wrapFactory = WrapFactory()

init {
initApplicationClassLoader(createClassLoader(AndroidContextFactory::class.java.classLoader!!))
}

/**
* Create a ClassLoader which is able to deal with bytecode
*
* @param parent the parent of the create classloader
* @return a new ClassLoader
*/
final override fun createClassLoader(parent: ClassLoader): AndroidClassLoader {
return AndroidClassLoader(parent, cacheDirectory)
}

override fun observeInstructionCount(cx: Context, instructionCount: Int) {
if (Thread.currentThread().isInterrupted && Looper.myLooper() != Looper.getMainLooper()) {
throw ScriptInterruptedException()
}
}

override fun makeContext(): Context {
val cx: Context = AutoJsContext(this)
setupContext(cx)
return cx
}

private fun setupContext(context: Context) {
context.instructionObserverThreshold = 10000
context.optimizationLevel = -1
context.languageVersion = Context.VERSION_ES6
context.locale = Locale.getDefault()
context.wrapFactory = wrapFactory
}

override fun onContextCreated(cx: Context) {
super.onContextCreated(cx)
val i = mContextCount.incrementAndGet()
Log.d(LOG_TAG, "onContextCreated: count = $i")
}

override fun onContextReleased(cx: Context) {
super.onContextReleased(cx)
val i = mContextCount.decrementAndGet()
Log.d(LOG_TAG, "onContextReleased: count = $i")
}

open class WrapFactory : org.mozilla.javascript.WrapFactory() {
override fun wrap(cx: Context, scope: Scriptable, obj: Any?, staticType: Class<*>?): Any? {
return when {
obj is String -> bridges.toString(obj.toString())
staticType == UiObjectCollection::class.java -> bridges.asArray(obj as UiObjectCollection)
else -> super.wrap(cx, scope, obj, staticType)
}
}
}
}
Loading
Loading