mirror of
https://github.com/TonyJiangWJ/Auto.js.git
synced 2026-06-21 21:01:43 +08:00
Add: NodeJs Support
This commit is contained in:
parent
3f7dacad2a
commit
121949f9c0
@ -8,8 +8,8 @@ android {
|
||||
applicationId "com.stardust.scriptdroid"
|
||||
minSdkVersion 19
|
||||
targetSdkVersion 23
|
||||
versionCode 109
|
||||
versionName "2.0.6 Alpha"
|
||||
versionCode 110
|
||||
versionName "2.0.6 Alpha2"
|
||||
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
|
||||
multiDexEnabled true
|
||||
|
||||
@ -72,6 +72,7 @@ dependencies {
|
||||
compile 'com.bignerdranch.android:expandablerecyclerview:3.0.0-RC1'
|
||||
compile 'com.github.0xFireball:Enlightened:v31'
|
||||
compile 'com.ashokvarma.android:bottom-navigation-bar:1.4.1'
|
||||
compile 'com.github.hyb1996:node-android-lib:1.0.13'
|
||||
compile(name: 'libtermexec-release', ext: 'aar')
|
||||
compile(name: 'emulatorview-release', ext: 'aar')
|
||||
compile(name: 'term-debug', ext: 'aar')
|
||||
|
||||
Binary file not shown.
@ -6,6 +6,9 @@
|
||||
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
|
||||
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
|
||||
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/>
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
|
||||
|
||||
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
|
||||
|
||||
<application
|
||||
|
||||
46
app/src/main/assets/nodejs_engine_init.js
Normal file
46
app/src/main/assets/nodejs_engine_init.js
Normal file
@ -0,0 +1,46 @@
|
||||
var __requireOld__ = require;
|
||||
var __nodejs_modules__ = {
|
||||
'websocket' : com.iwebpp.wspp.WebSocket,
|
||||
'websocketserver': com.iwebpp.wspp.WebSocketServer,
|
||||
'net': com.iwebpp.node.net.TCP,
|
||||
'tcp': com.iwebpp.node.net.TCP,
|
||||
'udt': com.iwebpp.node.net.UDT,
|
||||
'readable': com.iwebpp.node.stream.Readable2,
|
||||
'writable': com.iwebpp.node.stream.Writable2,
|
||||
'duplex': com.iwebpp.node.stream.Duplex,
|
||||
'transform': com.iwebpp.node.stream.Transform,
|
||||
'passthrough': com.iwebpp.node.stream.PassThrough,
|
||||
'dns': com.iwebpp.node.Dns,
|
||||
'url': com.iwebpp.node.Url,
|
||||
}
|
||||
|
||||
var require = function(module){
|
||||
if (module === 'http'){
|
||||
return {
|
||||
get: function(url, listener){
|
||||
return com.iwebpp.node.http.http.get(NodeCurrentContext, url, listener);
|
||||
},
|
||||
request: function(url, listener) {
|
||||
return com.iwebpp.node.http.http.request(NodeCurrentContext, url, listener);
|
||||
},
|
||||
createServer: function(listener) {
|
||||
return com.iwebpp.node.http.http.createServer(NodeCurrentContext, listener);
|
||||
},
|
||||
};
|
||||
}
|
||||
if (module === 'httpp'){
|
||||
return {
|
||||
get: function(url, listener){
|
||||
return com.iwebpp.node.http.httpp.get(NodeCurrentContext, url, listener);
|
||||
},
|
||||
request: function(url, listener) {
|
||||
return com.iwebpp.node.http.httpp.request(NodeCurrentContext, url, listener);
|
||||
},
|
||||
createServer: function(listener) {
|
||||
return com.iwebpp.node.http.httpp.createServer(NodeCurrentContext, listener);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
return __requireOld__(module);
|
||||
};
|
||||
@ -1,3 +0,0 @@
|
||||
module.exports = function() {
|
||||
java.lang.System.out.println('The CommonJS require function works!')
|
||||
}
|
||||
@ -9,6 +9,7 @@ import com.stardust.automator.AccessibilityEventCommandHost;
|
||||
import com.stardust.scriptdroid.accessibility.AccessibilityInfoProvider;
|
||||
import com.stardust.scriptdroid.droid.runtime.DroidRuntime;
|
||||
import com.stardust.scriptdroid.droid.script.JavaScriptEngine;
|
||||
import com.stardust.scriptdroid.droid.script.NodeJsJavaScriptEngine;
|
||||
import com.stardust.scriptdroid.droid.script.RhinoJavaScriptEngine;
|
||||
import com.stardust.scriptdroid.droid.script.file.ScriptFileList;
|
||||
import com.stardust.scriptdroid.droid.script.file.SharedPrefScriptFileList;
|
||||
@ -71,7 +72,7 @@ public class App extends Application {
|
||||
|
||||
private void configApp() {
|
||||
ScriptFileList.setImpl(SharedPrefScriptFileList.getInstance());
|
||||
JavaScriptEngine.setDefault(new RhinoJavaScriptEngine(DroidRuntime.getRuntime()));
|
||||
JavaScriptEngine.setDefault(new NodeJsJavaScriptEngine(DroidRuntime.getRuntime()));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -0,0 +1,71 @@
|
||||
package com.stardust.scriptdroid.droid.script;
|
||||
|
||||
import com.iwebpp.node.Dns;
|
||||
import com.iwebpp.node.NodeContext;
|
||||
import com.iwebpp.node.Url;
|
||||
import com.iwebpp.node.http.http;
|
||||
import com.iwebpp.node.js.rhino.Host;
|
||||
import com.iwebpp.node.stream.Duplex;
|
||||
import com.iwebpp.node.stream.PassThrough;
|
||||
import com.iwebpp.node.stream.Readable2;
|
||||
import com.iwebpp.node.stream.Transform;
|
||||
import com.iwebpp.node.stream.Writable2;
|
||||
import com.iwebpp.nodeandroid.MainActivity;
|
||||
import com.iwebpp.nodeandroid.Toaster;
|
||||
import com.iwebpp.wspp.WebSocketServer;
|
||||
import com.stardust.scriptdroid.App;
|
||||
import com.stardust.scriptdroid.droid.Droid;
|
||||
import com.stardust.scriptdroid.droid.runtime.DroidRuntime;
|
||||
import com.stardust.scriptdroid.tool.FileUtils;
|
||||
|
||||
import org.mozilla.javascript.Context;
|
||||
import org.mozilla.javascript.Scriptable;
|
||||
import org.mozilla.javascript.ScriptableObject;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/3/25.
|
||||
*/
|
||||
|
||||
public class NodeJsJavaScriptEngine extends RhinoJavaScriptEngine {
|
||||
|
||||
public NodeJsJavaScriptEngine(DroidRuntime runtime) {
|
||||
super(runtime);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Object execute(String script) {
|
||||
NodeContext nodectx = new NodeContext();
|
||||
Context context = createContext();
|
||||
Scriptable scope = createScope(context);
|
||||
ScriptableObject.putProperty(scope, "NodeCurrentContext", Context.javaToJS(nodectx, scope));
|
||||
init(context, scope);
|
||||
Object result = context.evaluateString(scope, script, "<script>", 1, null);
|
||||
try {
|
||||
nodectx.execute();
|
||||
} catch (Throwable throwable) {
|
||||
throw new RuntimeException(throwable);
|
||||
}
|
||||
if (!script.startsWith(Droid.UI) && !script.startsWith(Droid.STAY))
|
||||
removeAndDestroy();
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void init(Context context, Scriptable scope) {
|
||||
super.init(context, scope);
|
||||
try {
|
||||
context.evaluateString(scope, FileUtils.readString(App.getApp().getAssets().open("nodejs_engine_init.js")), "<nodejs_init>", 1, null);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void putProperties(Context context, Scriptable scope) {
|
||||
super.putProperties(context, scope);
|
||||
ScriptableObject.putProperty(scope, "NodeHostEnv", Context.javaToJS(this, scope));
|
||||
}
|
||||
}
|
||||
@ -2,6 +2,9 @@ package com.stardust.scriptdroid.droid.script;
|
||||
|
||||
import android.net.Uri;
|
||||
|
||||
import com.iwebpp.node.NodeContext;
|
||||
import com.iwebpp.node.js.rhino.Host;
|
||||
import com.iwebpp.nodeandroid.Toaster;
|
||||
import com.stardust.scriptdroid.droid.Droid;
|
||||
import com.stardust.scriptdroid.App;
|
||||
import com.stardust.scriptdroid.droid.runtime.DroidRuntime;
|
||||
@ -55,13 +58,13 @@ public class RhinoJavaScriptEngine extends JavaScriptEngine {
|
||||
return result;
|
||||
}
|
||||
|
||||
private Scriptable createScope(Context context) {
|
||||
protected Scriptable createScope(Context context) {
|
||||
ImporterTopLevel importerTopLevel = new ImporterTopLevel();
|
||||
importerTopLevel.initStandardObjects(context, false);
|
||||
return importerTopLevel;
|
||||
}
|
||||
|
||||
private Context createContext() {
|
||||
protected Context createContext() {
|
||||
Context context = Context.enter();
|
||||
context.setOptimizationLevel(-1);
|
||||
context.setLanguageVersion(Context.VERSION_1_7);
|
||||
@ -69,18 +72,22 @@ public class RhinoJavaScriptEngine extends JavaScriptEngine {
|
||||
return context;
|
||||
}
|
||||
|
||||
private void init(Context context, Scriptable scope) {
|
||||
protected void init(Context context, Scriptable scope) {
|
||||
putProperties(context, scope);
|
||||
initRequireBuilder(context, scope);
|
||||
context.evaluateString(scope, Init.getInitScript(), "<init>", 1, null);
|
||||
mThreads.add(Thread.currentThread());
|
||||
}
|
||||
|
||||
protected void putProperties(Context context, Scriptable scope) {
|
||||
ScriptableObject.putProperty(scope, "context", App.getApp());
|
||||
ScriptableObject.putProperty(scope, "__engine__", "rhino");
|
||||
for (Map.Entry<String, Object> variable : mVariableMap.entrySet()) {
|
||||
ScriptableObject.putProperty(scope, variable.getKey(), variable.getValue());
|
||||
}
|
||||
initRequireBuilder(context, scope);
|
||||
context.evaluateString(scope, Init.getInitScript(), "<init>", 1, null);
|
||||
mThreads.add(Thread.currentThread());
|
||||
|
||||
}
|
||||
|
||||
|
||||
private void initRequireBuilder(Context context, Scriptable scope) {
|
||||
List<URI> paths = Collections.singletonList(new File(ScriptFile.DEFAULT_FOLDER).toURI());
|
||||
new RequireBuilder()
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@ -70,7 +70,6 @@ public class ExampleUnitTest {
|
||||
.install(scope);
|
||||
|
||||
|
||||
ctx.evaluateString(scope, "require('test.js')()", "<test>", 1, null);
|
||||
|
||||
Context.exit();
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user