add samples, fix require()
This commit is contained in:
@@ -15,11 +15,9 @@ if(__engine_name__ == "rhino"){
|
||||
|
||||
|
||||
var __asGlobal__ = function(obj, functions){
|
||||
__runtime__.console.log(functions);
|
||||
var len = functions.length;
|
||||
for(var i = 0; i < len; i++) {
|
||||
var funcName = functions[i];
|
||||
__runtime__.console.log(funcName);
|
||||
this[funcName] = obj[funcName].bind(obj);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,10 @@ module.exports = function(__runtime__, scope){
|
||||
console.assertTrue(value, message);
|
||||
}
|
||||
|
||||
console.input = function(data, param){
|
||||
return eval(console.rawInput.call(console, [].slice(arguments)) + "");
|
||||
}
|
||||
|
||||
scope.print = console.log.bind(console);
|
||||
|
||||
scope.log = scope.print;
|
||||
|
||||
@@ -142,6 +142,10 @@ public class ScriptEngineService {
|
||||
return execute(new ScriptExecutionTask(source, listener, config));
|
||||
}
|
||||
|
||||
public ScriptExecution execute(ScriptSource source, ExecutionConfig config) {
|
||||
return execute(new ScriptExecutionTask(source, null, config));
|
||||
}
|
||||
|
||||
public ScriptExecution execute(ScriptSource source, ScriptExecutionListener listener) {
|
||||
return execute(source, listener, ExecutionConfig.getDefault());
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.stardust.autojs.engine;
|
||||
|
||||
import org.mozilla.javascript.commonjs.module.provider.ModuleSource;
|
||||
import org.mozilla.javascript.commonjs.module.provider.UrlModuleSourceProvider;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/5/9.
|
||||
*/
|
||||
|
||||
public class AssetAndUrlModuleSourceProvider extends UrlModuleSourceProvider {
|
||||
|
||||
private static final String MODULES_PATH = "modules";
|
||||
private android.content.Context mContext;
|
||||
private List<String> mModules;
|
||||
private final URI mBaseURI = URI.create("file:///android_asset/modules");
|
||||
|
||||
public AssetAndUrlModuleSourceProvider(android.content.Context context, List<URI> list) {
|
||||
super(list, null);
|
||||
mContext = context;
|
||||
try {
|
||||
mModules = Arrays.asList(mContext.getAssets().list(MODULES_PATH));
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected ModuleSource loadFromPrivilegedLocations(String moduleId, Object validator) throws IOException, URISyntaxException {
|
||||
String moduleIdWithExtension = moduleId;
|
||||
if (!moduleIdWithExtension.endsWith(".js")) {
|
||||
moduleIdWithExtension += ".js";
|
||||
}
|
||||
if (mModules.contains(moduleIdWithExtension)) {
|
||||
return new ModuleSource(new InputStreamReader(mContext.getAssets().open(MODULES_PATH + "/" + moduleIdWithExtension)), null,
|
||||
URI.create(moduleIdWithExtension), mBaseURI, validator);
|
||||
}
|
||||
return super.loadFromPrivilegedLocations(moduleId, validator);
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,6 @@ public class NodeJsJavaScriptEngineManager extends RhinoJavaScriptEngineManager
|
||||
@Override
|
||||
protected NodeJsJavaScriptEngine createEngineInner() {
|
||||
NodeJsJavaScriptEngine engine = new NodeJsJavaScriptEngine(this);
|
||||
initRequireBuilder(engine.getContext(), engine.getScriptable());
|
||||
return engine;
|
||||
}
|
||||
|
||||
|
||||
@@ -13,10 +13,15 @@ import org.mozilla.javascript.ContextFactory;
|
||||
import org.mozilla.javascript.ImporterTopLevel;
|
||||
import org.mozilla.javascript.Scriptable;
|
||||
import org.mozilla.javascript.ScriptableObject;
|
||||
import org.mozilla.javascript.commonjs.module.RequireBuilder;
|
||||
import org.mozilla.javascript.commonjs.module.provider.SoftCachingModuleScriptProvider;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@@ -29,6 +34,7 @@ public class RhinoJavaScriptEngine implements ScriptEngine {
|
||||
private static final String LOG_TAG = "RhinoJavaScriptEngine";
|
||||
|
||||
private static int contextCount = 0;
|
||||
private String[] mRequirePath = new String[0];
|
||||
|
||||
private Context mContext;
|
||||
private Scriptable mScriptable;
|
||||
@@ -94,9 +100,28 @@ public class RhinoJavaScriptEngine implements ScriptEngine {
|
||||
public void init() {
|
||||
ScriptableObject.putProperty(mScriptable, "__engine_name__", "rhino");
|
||||
ScriptableObject.putProperty(mScriptable, "__engine__", this);
|
||||
mRequirePath = (String[]) getTag("__require_path__");
|
||||
initRequireBuilder(mContext, mScriptable);
|
||||
mContext.evaluateString(mScriptable, mEngineManager.getInitScript().getScript(), "<init>", 1, null);
|
||||
}
|
||||
|
||||
public void setRequirePath(String... requirePath) {
|
||||
setTag("__require_path__", requirePath);
|
||||
}
|
||||
|
||||
void initRequireBuilder(Context context, Scriptable scope) {
|
||||
List<URI> list = new ArrayList<>();
|
||||
for (String path : mRequirePath) {
|
||||
list.add(new File(path).toURI());
|
||||
}
|
||||
AssetAndUrlModuleSourceProvider provider = new AssetAndUrlModuleSourceProvider(getEngineManager().getContext(), list);
|
||||
new RequireBuilder()
|
||||
.setModuleScriptProvider(new SoftCachingModuleScriptProvider(provider))
|
||||
.setSandboxed(false)
|
||||
.createRequire(context, scope)
|
||||
.install(scope);
|
||||
}
|
||||
|
||||
public Context getContext() {
|
||||
return mContext;
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
@@ -31,7 +32,6 @@ public class RhinoJavaScriptEngineManager extends AbstractScriptEngineManager {
|
||||
|
||||
private String[] mFunctions;
|
||||
|
||||
private String mRequirePath = "";
|
||||
private ScriptSource mCustomInitScript;
|
||||
private ScriptSource mInitScript;
|
||||
|
||||
@@ -41,7 +41,6 @@ public class RhinoJavaScriptEngineManager extends AbstractScriptEngineManager {
|
||||
|
||||
protected RhinoJavaScriptEngine createEngineInner() {
|
||||
RhinoJavaScriptEngine engine = new RhinoJavaScriptEngine(this);
|
||||
initRequireBuilder(engine.getContext(), engine.getScriptable());
|
||||
return engine;
|
||||
}
|
||||
|
||||
@@ -77,20 +76,6 @@ public class RhinoJavaScriptEngineManager extends AbstractScriptEngineManager {
|
||||
return mInitScript;
|
||||
}
|
||||
|
||||
public void setRequirePath(String requirePath) {
|
||||
mRequirePath = requirePath;
|
||||
}
|
||||
|
||||
void initRequireBuilder(Context context, Scriptable scope) {
|
||||
List<URI> list = Collections.singletonList(new File(mRequirePath).toURI());
|
||||
AssetAndUrlModuleSourceProvider provider = new AssetAndUrlModuleSourceProvider(getContext(), list);
|
||||
new RequireBuilder()
|
||||
.setModuleScriptProvider(new SoftCachingModuleScriptProvider(provider))
|
||||
.setSandboxed(false)
|
||||
.createRequire(context, scope)
|
||||
.install(scope);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getGlobalFunctions() {
|
||||
if (mFunctions == null)
|
||||
@@ -114,36 +99,5 @@ public class RhinoJavaScriptEngineManager extends AbstractScriptEngineManager {
|
||||
}
|
||||
|
||||
|
||||
private static class AssetAndUrlModuleSourceProvider extends UrlModuleSourceProvider {
|
||||
|
||||
private static final String MODULES_PATH = "modules";
|
||||
private android.content.Context mContext;
|
||||
private List<String> mModules;
|
||||
private final URI mBaseURI = URI.create("file:///android_asset/modules");
|
||||
|
||||
public AssetAndUrlModuleSourceProvider(android.content.Context context, List<URI> list) {
|
||||
super(list, null);
|
||||
mContext = context;
|
||||
try {
|
||||
mModules = Arrays.asList(mContext.getAssets().list(MODULES_PATH));
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected ModuleSource loadFromPrivilegedLocations(String moduleId, Object validator) throws IOException, URISyntaxException {
|
||||
String moduleIdWithExtension = moduleId;
|
||||
if (!moduleIdWithExtension.endsWith(".js")) {
|
||||
moduleIdWithExtension += ".js";
|
||||
}
|
||||
if (mModules.contains(moduleIdWithExtension)) {
|
||||
return new ModuleSource(new InputStreamReader(mContext.getAssets().open(MODULES_PATH + "/" + moduleIdWithExtension)), null,
|
||||
URI.create(moduleIdWithExtension), mBaseURI, validator);
|
||||
}
|
||||
return super.loadFromPrivilegedLocations(moduleId, validator);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import java.io.Serializable;
|
||||
*/
|
||||
public class ExecutionConfig implements Serializable {
|
||||
|
||||
private String[] mRequirePath = new String[0];
|
||||
private static final ExecutionConfig DEFAULT = new ExecutionConfig();
|
||||
|
||||
public static ExecutionConfig getDefault() {
|
||||
@@ -20,4 +21,12 @@ public class ExecutionConfig implements Serializable {
|
||||
return this;
|
||||
}
|
||||
|
||||
public ExecutionConfig requirePath(String... requirePath) {
|
||||
mRequirePath = requirePath;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String[] getRequirePath() {
|
||||
return mRequirePath;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.stardust.autojs.execution;
|
||||
import android.util.Log;
|
||||
|
||||
import com.stardust.autojs.ScriptEngineService;
|
||||
import com.stardust.autojs.engine.RhinoJavaScriptEngine;
|
||||
import com.stardust.autojs.engine.ScriptEngine;
|
||||
import com.stardust.autojs.runtime.ScriptRuntime;
|
||||
import com.stardust.autojs.script.ScriptSource;
|
||||
@@ -53,6 +54,7 @@ public class RunnableScriptExecution extends ScriptExecution.AbstractScriptExecu
|
||||
runtime.ensureAccessibilityServiceEnabled();
|
||||
}
|
||||
engine.put("__runtime__", runtime);
|
||||
engine.setTag("__require_path__", getConfig().getRequirePath());
|
||||
engine.init();
|
||||
}
|
||||
|
||||
|
||||
@@ -61,6 +61,7 @@ public class ScriptExecuteActivity extends Activity {
|
||||
private void prepare() {
|
||||
mScriptEngine.put("activity", this);
|
||||
mScriptEngine.put("__runtime__", execution.getRuntime());
|
||||
mScriptEngine.setTag("__require_path__", execution.getConfig().getRequirePath());
|
||||
mScriptEngine.init();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user