imporve script execution of tasker plugin, fix sublime plugin large file issue
This commit is contained in:
31
autojs/src/main/assets/modules/__ui__.js
Normal file
31
autojs/src/main/assets/modules/__ui__.js
Normal file
@@ -0,0 +1,31 @@
|
||||
|
||||
|
||||
ui.layout = function(layout){
|
||||
layout = layout[0];
|
||||
view = ui.inflate(layout);
|
||||
ui.setView(view);
|
||||
}
|
||||
|
||||
ui.inflate = function(xml){
|
||||
var name = xml.name();
|
||||
var view = ui.createView(name);
|
||||
ui.putAttributes(view, xml.attributes());
|
||||
ui.addChildren(view, xml.children());
|
||||
return view;
|
||||
}
|
||||
|
||||
ui.putAttributes = function(view, attrs){
|
||||
var len = attrs.length();
|
||||
for(var i = 0; i < len; i++){
|
||||
var attr = attrs[i];
|
||||
view.putAttribute(attr.name(), attr.toString());
|
||||
}
|
||||
}
|
||||
|
||||
ui.addChildren = function(view, children){
|
||||
var len = children.length();
|
||||
for(var i = 0; i < len; i++){
|
||||
var child = children[i];
|
||||
view.addChild(ui.inflate(child));
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,10 @@
|
||||
package com.stardust.autojs.engine;
|
||||
|
||||
import com.iwebpp.SimpleDebug;
|
||||
import com.iwebpp.node.NodeContext;
|
||||
import com.iwebpp.node.http.IncomingMessage;
|
||||
import com.stardust.autojs.runtime.ScriptStopException;
|
||||
import com.stardust.autojs.script.ScriptSource;
|
||||
import com.stardust.pio.PFile;
|
||||
import com.stardust.pio.UncheckedIOException;
|
||||
|
||||
@@ -15,6 +19,10 @@ import java.io.IOException;
|
||||
|
||||
public class NodeJsJavaScriptEngine extends RhinoJavaScriptEngine {
|
||||
|
||||
static {
|
||||
SimpleDebug.setDebugLevel(SimpleDebug.DebugLevel.DEBUG);
|
||||
}
|
||||
|
||||
private static String initScript;
|
||||
private NodeContext mNodeContext = new NodeContext();
|
||||
|
||||
@@ -29,6 +37,17 @@ public class NodeJsJavaScriptEngine extends RhinoJavaScriptEngine {
|
||||
getContext().evaluateString(getScriptable(), getNodeJsInitScript(), "<node_js_init>", 1, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object execute(ScriptSource source) {
|
||||
Object result = super.execute(source);
|
||||
try {
|
||||
mNodeContext.execute();
|
||||
} catch (Throwable throwable) {
|
||||
throw new ScriptStopException(throwable);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private String getNodeJsInitScript() {
|
||||
if (initScript == null) {
|
||||
try {
|
||||
|
||||
@@ -141,12 +141,12 @@ public class RhinoJavaScriptEngine implements ScriptEngine {
|
||||
|
||||
protected Context createContext() {
|
||||
if (!ContextFactory.hasExplicitGlobal()) {
|
||||
ContextFactory.initGlobal(new InterruptibleAndroidContextFactory(new File(mEngineManager.getContext().getCacheDir(), "classes")));
|
||||
//ContextFactory.initGlobal(new InterruptibleAndroidContextFactory(new File(mEngineManager.getContext().getCacheDir(), "classes")));
|
||||
}
|
||||
Context context = new RhinoAndroidHelper(mEngineManager.getContext()).enterContext();
|
||||
Context context = Context.enter();//new RhinoAndroidHelper(mEngineManager.getContext()).enterContext();
|
||||
contextCount++;
|
||||
context.setOptimizationLevel(-1);
|
||||
context.setLanguageVersion(Context.VERSION_ES6);
|
||||
//context.setLanguageVersion(Context.VERSION_ES6);
|
||||
return context;
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import com.stardust.autojs.runtime.ScriptInterruptedException;
|
||||
|
||||
import org.mozilla.javascript.Context;
|
||||
import org.mozilla.javascript.ContextFactory;
|
||||
import org.mozilla.javascript.tools.shell.ShellContextFactory;
|
||||
import org.mozilla.javascript.xml.XMLLib;
|
||||
|
||||
import java.io.File;
|
||||
@@ -13,7 +14,7 @@ import java.io.File;
|
||||
* Created by Stardust on 2017/4/5.
|
||||
*/
|
||||
|
||||
public class AndroidContextFactory extends ContextFactory {
|
||||
public class AndroidContextFactory extends ShellContextFactory {
|
||||
private final File cacheDirectory;
|
||||
|
||||
/**
|
||||
@@ -51,10 +52,4 @@ public class AndroidContextFactory extends ContextFactory {
|
||||
return cx;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected XMLLib.Factory getE4xImplementationFactory() {
|
||||
return org.mozilla.javascript.xml.XMLLib.Factory.create(
|
||||
"org.mozilla.javascript.xmlimpl.XMLLibImpl"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.stardust.autojs.runtime.api.ui;
|
||||
|
||||
import android.view.View;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/5/13.
|
||||
*/
|
||||
|
||||
public interface AttributeSetter<V extends View> {
|
||||
|
||||
boolean putAttribute(V view, String value);
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.stardust.autojs.runtime.api.ui;
|
||||
|
||||
import android.view.View;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/5/13.
|
||||
*/
|
||||
|
||||
public class AttributeSetters<V extends View> {
|
||||
|
||||
private Map<String, AttributeSetter<V>> mAttributeSetters = new HashMap<>();
|
||||
|
||||
|
||||
public boolean putAttribute(V view, String name, String value) {
|
||||
AttributeSetter<V> setter = mAttributeSetters.get(name);
|
||||
return setter != null && setter.putAttribute(view, value);
|
||||
}
|
||||
|
||||
public AttributeSetters<V> registerAttributeSetter(String name, AttributeSetter<V> setter) {
|
||||
mAttributeSetters.put(name, setter);
|
||||
return this;
|
||||
}
|
||||
|
||||
public void removeAttributeSetter(String name) {
|
||||
mAttributeSetters.remove(name);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.stardust.autojs.runtime.api.ui;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Build;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.annotation.RequiresApi;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.View;
|
||||
import android.widget.LinearLayout;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/5/13.
|
||||
*/
|
||||
|
||||
public class JsLinearLayout extends LinearLayout {
|
||||
|
||||
private static final AttributeSetters<JsLinearLayout> LINEAR_LAYOUT_ATTRIBUTE_SETTERS = new AttributeSetters<JsLinearLayout>()
|
||||
.registerAttributeSetter("orientation", new AttributeSetter<JsLinearLayout>() {
|
||||
@Override
|
||||
public boolean putAttribute(JsLinearLayout layout, String value) {
|
||||
switch (value) {
|
||||
case "vertical":
|
||||
layout.setOrientation(VERTICAL);
|
||||
return true;
|
||||
case "horizontal":
|
||||
layout.setOrientation(HORIZONTAL);
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
public JsLinearLayout(Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
public JsLinearLayout(Context context, @Nullable AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
}
|
||||
|
||||
public JsLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
}
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
|
||||
public JsLinearLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
|
||||
super(context, attrs, defStyleAttr, defStyleRes);
|
||||
}
|
||||
|
||||
public void putAttribute(String name, String value) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package com.stardust.autojs.runtime.api.ui;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/5/14.
|
||||
*/
|
||||
|
||||
public class MethodMapper<O> {
|
||||
|
||||
public interface IMethod<O> {
|
||||
|
||||
Object call(O receiver, Object... params);
|
||||
}
|
||||
|
||||
private Class<O> mType;
|
||||
|
||||
public MethodMapper(Class<O> type) {
|
||||
mType = type;
|
||||
}
|
||||
|
||||
private Map<String, IMethod<O>> mMethodMap = new HashMap<>();
|
||||
|
||||
public void put(String methodName, String actualMethodName) {
|
||||
final Method method = getMethod(methodName);
|
||||
mMethodMap.put(methodName, new IMethod<O>() {
|
||||
@Override
|
||||
public Object call(O obj, Object... params) {
|
||||
try {
|
||||
return method.invoke(obj, params);
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private Method getMethod(String methodName) {
|
||||
for (Method method : mType.getMethods()) {
|
||||
if (method.getName().equals(methodName))
|
||||
return method;
|
||||
}
|
||||
throw new RuntimeException(new NoSuchMethodException(methodName));
|
||||
}
|
||||
|
||||
public void put(String methodName, IMethod<O> iMethod) {
|
||||
mMethodMap.put(methodName, iMethod);
|
||||
}
|
||||
|
||||
|
||||
public Object invoke(String methodName, O obj, Object... params) {
|
||||
IMethod<O> iMethod = mMethodMap.get(methodName);
|
||||
if (iMethod == null)
|
||||
throw new RuntimeException(new NoSuchMethodException(methodName));
|
||||
return iMethod.call(obj, params);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.stardust.autojs.runtime.api.ui;
|
||||
|
||||
import android.view.View;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/5/14.
|
||||
*/
|
||||
|
||||
public class ViewAttributeSetters extends AttributeSetters<View> {
|
||||
|
||||
private static ViewAttributeSetters setters;
|
||||
|
||||
public ViewAttributeSetters() {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.stardust.autojs.script;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
|
||||
import com.stardust.autojs.rhino_android.RhinoAndroidHelper;
|
||||
import com.stardust.pio.PFile;
|
||||
@@ -47,6 +48,8 @@ public class JsBeautifier {
|
||||
prepareIfNeeded();
|
||||
enterContext();
|
||||
Object beautifiedCode = mJsBeautifyFunction.call(mScriptContext, mScriptable, mScriptable, new Object[]{code});
|
||||
Object o = mScriptContext.evaluateString(mScriptable, " (<xml id=\"foo\"></xml>).attributes()[0].name()", "<e4x>", 1, null);
|
||||
Log.i("e4x", o + "");
|
||||
callback.onSuccess(beautifiedCode.toString());
|
||||
} catch (Exception e) {
|
||||
callback.onException(e);
|
||||
@@ -68,6 +71,7 @@ public class JsBeautifier {
|
||||
private void enterContext() {
|
||||
if (mScriptContext == null) {
|
||||
mScriptContext = org.mozilla.javascript.Context.enter();
|
||||
mScriptContext.setLanguageVersion(org.mozilla.javascript.Context.VERSION_1_8);
|
||||
mScriptContext.setOptimizationLevel(-1);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user