add: module threads; read and write binary file supports
This commit is contained in:
@@ -49,6 +49,7 @@ var __that__ = this;
|
||||
var Promise = require('promise.js');
|
||||
var JSON = require('__json2__.js');
|
||||
var util = require('__util__.js');
|
||||
var threads = __runtime__.threads;
|
||||
|
||||
var __asGlobal__ = function(obj, functions){
|
||||
var len = functions.length;
|
||||
|
||||
@@ -17,7 +17,7 @@ module.exports = function(__runtime__, scope){
|
||||
}
|
||||
|
||||
engines.myEngine = function(){
|
||||
return scope.__engine__;
|
||||
return rtEngines.myEngine();
|
||||
}
|
||||
|
||||
engines.stopAll = rtEngines.stopAll.bind(rtEngines);
|
||||
|
||||
@@ -32,6 +32,7 @@ public abstract class JavaScriptEngine extends ScriptEngine.AbstractScriptEngine
|
||||
throw new IllegalStateException("a runtime has been set");
|
||||
}
|
||||
mRuntime = runtime;
|
||||
mRuntime.engines.setCurrentEngine(this);
|
||||
put("__runtime__", runtime);
|
||||
}
|
||||
|
||||
|
||||
@@ -144,7 +144,7 @@ public class RhinoJavaScriptEngine extends JavaScriptEngine {
|
||||
return importerTopLevel;
|
||||
}
|
||||
|
||||
protected Context createContext() {
|
||||
public Context createContext() {
|
||||
if (!ContextFactory.hasExplicitGlobal()) {
|
||||
ContextFactory.initGlobal(new InterruptibleAndroidContextFactory(new File(mAndroidContext.getCacheDir(), "classes")));
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ import com.stardust.autojs.runtime.api.Console;
|
||||
import com.stardust.autojs.runtime.api.Engines;
|
||||
import com.stardust.autojs.runtime.api.Events;
|
||||
import com.stardust.autojs.runtime.api.Loopers;
|
||||
import com.stardust.autojs.runtime.api.Threads;
|
||||
import com.stardust.autojs.runtime.api.Timers;
|
||||
import com.stardust.autojs.core.accessibility.UiSelector;
|
||||
import com.stardust.autojs.runtime.api.Images;
|
||||
@@ -146,6 +147,9 @@ public class ScriptRuntime {
|
||||
@ScriptVariable
|
||||
public final Engines engines;
|
||||
|
||||
@ScriptVariable
|
||||
public final Threads threads;
|
||||
|
||||
private Images images;
|
||||
|
||||
private static WeakReference<Context> applicationContext;
|
||||
@@ -172,6 +176,7 @@ public class ScriptRuntime {
|
||||
}
|
||||
engines = new Engines(builder.mEngineService);
|
||||
dialogs = new Dialogs(app, mUiHandler, bridges);
|
||||
threads = new Threads(this);
|
||||
}
|
||||
|
||||
public void init() {
|
||||
@@ -303,6 +308,7 @@ public class ScriptRuntime {
|
||||
}
|
||||
|
||||
public void onExit() {
|
||||
threads.shutDownAll();
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
||||
images.releaseScreenCapturer();
|
||||
}
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
package com.stardust.autojs.runtime.api;
|
||||
|
||||
import com.stardust.autojs.ScriptEngineService;
|
||||
import com.stardust.autojs.engine.JavaScriptEngine;
|
||||
import com.stardust.autojs.engine.ScriptEngine;
|
||||
import com.stardust.autojs.execution.ExecutionConfig;
|
||||
import com.stardust.autojs.execution.ScriptExecution;
|
||||
import com.stardust.autojs.script.AutoFileSource;
|
||||
import com.stardust.autojs.script.JavaScriptFileSource;
|
||||
import com.stardust.autojs.script.JavaScriptSource;
|
||||
import com.stardust.autojs.script.StringScriptSource;
|
||||
|
||||
/**
|
||||
@@ -14,6 +17,7 @@ import com.stardust.autojs.script.StringScriptSource;
|
||||
public class Engines {
|
||||
|
||||
private ScriptEngineService mEngineService;
|
||||
private ScriptEngine<JavaScriptSource> mScriptEngine;
|
||||
|
||||
public Engines(ScriptEngineService engineService) {
|
||||
mEngineService = engineService;
|
||||
@@ -40,4 +44,11 @@ public class Engines {
|
||||
}
|
||||
|
||||
|
||||
public void setCurrentEngine(ScriptEngine<JavaScriptSource> engine) {
|
||||
mScriptEngine = engine;
|
||||
}
|
||||
|
||||
public ScriptEngine<JavaScriptSource> myEngine() {
|
||||
return mScriptEngine;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
package com.stardust.autojs.runtime.api;
|
||||
|
||||
import com.stardust.autojs.engine.RhinoJavaScriptEngine;
|
||||
import com.stardust.autojs.runtime.ScriptRuntime;
|
||||
import com.stardust.autojs.runtime.exception.ScriptInterruptedException;
|
||||
import com.stardust.concurrent.VolatileBox;
|
||||
import com.stardust.concurrent.VolatileDispose;
|
||||
import com.stardust.lang.ThreadCompat;
|
||||
import com.stardust.pio.PFile;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentSkipListSet;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/12/3.
|
||||
*/
|
||||
|
||||
public class Threads {
|
||||
|
||||
private List<ThreadCompat> mThreads = new ArrayList<>();
|
||||
private ScriptRuntime mScriptRuntime;
|
||||
|
||||
public Threads(ScriptRuntime scriptRuntime) {
|
||||
mScriptRuntime = scriptRuntime;
|
||||
}
|
||||
|
||||
public void start(Runnable runnable) {
|
||||
ThreadCompat threadCompat = new ThreadCompat(() -> {
|
||||
try {
|
||||
((RhinoJavaScriptEngine) mScriptRuntime.engines.myEngine()).createContext();
|
||||
runnable.run();
|
||||
} catch (Exception e) {
|
||||
if (!ScriptInterruptedException.causedByInterrupted(e)) {
|
||||
mScriptRuntime.console.error(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
mThreads.add(threadCompat);
|
||||
threadCompat.start();
|
||||
}
|
||||
|
||||
public VolatileBox variable() {
|
||||
return new VolatileBox();
|
||||
}
|
||||
|
||||
public VolatileDispose disposable() {
|
||||
return new VolatileDispose();
|
||||
}
|
||||
|
||||
public List list() {
|
||||
return Collections.synchronizedList(new ArrayList<>());
|
||||
}
|
||||
|
||||
public Set set() {
|
||||
return new ConcurrentSkipListSet();
|
||||
}
|
||||
|
||||
public Map map() {
|
||||
return new ConcurrentHashMap();
|
||||
}
|
||||
|
||||
public AtomicInteger atomicInt() {
|
||||
return new AtomicInteger();
|
||||
}
|
||||
|
||||
public void shutDownAll() {
|
||||
for (ThreadCompat threadCompat : mThreads) {
|
||||
threadCompat.interrupt();
|
||||
}
|
||||
mThreads.clear();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user