-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
107 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 0 additions & 54 deletions
54
autojs/src/main/java/com/stardust/autojs/rhino/AndroidContextFactory.java
This file was deleted.
Oops, something went wrong.
82 changes: 82 additions & 0 deletions
82
autojs/src/main/java/com/stardust/autojs/rhino/AndroidContextFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} |
55 changes: 0 additions & 55 deletions
55
autojs/src/main/java/com/stardust/autojs/rhino/InterruptibleAndroidContextFactory.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters