Skip to content

Commit

Permalink
bug修复
Browse files Browse the repository at this point in the history
  • Loading branch information
aiselp committed Sep 14, 2023
1 parent 29cbcbd commit 7f86d2b
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 140 deletions.
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,11 +7,11 @@ 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.Context
import org.mozilla.javascript.Script
Expand All @@ -20,7 +20,6 @@ import org.mozilla.javascript.ScriptableObject
import org.mozilla.javascript.commonjs.module.provider.SoftCachingModuleScriptProvider
import java.io.IOException
import java.io.InputStreamReader
import java.util.Locale
import java.util.concurrent.ConcurrentHashMap

/**
Expand Down Expand Up @@ -48,12 +47,17 @@ open class RhinoJavaScriptEngine(private val mAndroidContext: android.content.Co
val scriptable: Scriptable
get() = mScriptable

init {

}

override fun put(name: String, value: Any?) {
ScriptableObject.putProperty(mScriptable, name, Context.javaToJS(value, mScriptable))
}

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

Expand Down Expand Up @@ -135,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()
private 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 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)
}
}
}
}

This file was deleted.

20 changes: 12 additions & 8 deletions autojs/src/main/java/com/stardust/autojs/runtime/ScriptBridges.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.stardust.autojs.runtime

import com.stardust.autojs.engine.RhinoJavaScriptEngine
import com.stardust.automator.UiObjectCollection
import org.mozilla.javascript.BaseFunction
import org.mozilla.javascript.BoundFunction
Expand All @@ -13,14 +14,17 @@ import org.mozilla.javascript.annotations.JSFunction
* Created by Stardust on 2017/7/21.
*/
class ScriptBridges {
companion object {
fun <T> useJsContext(f: (context: Context) -> T): T {
val context = Context.getCurrentContext()
try {
return f(context ?: Context.enter())
} finally {
context ?: Context.exit()
}
var engine: RhinoJavaScriptEngine? = null
fun setup(engine: RhinoJavaScriptEngine) {
this.engine = engine
}

private fun <T> useJsContext(f: (context: Context) -> T): T {
val context = Context.getCurrentContext()
try {
return f(context ?: engine?.enterContext() ?: Context.enter())
} finally {
context ?: Context.exit()
}
}

Expand Down

0 comments on commit 7f86d2b

Please sign in to comment.