Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复编辑器格式化功能 #650

Merged
merged 3 commits into from
Sep 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions autojs/src/main/assets/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,33 +48,33 @@ runtime.init();
}
};

// 初始化基础模块
global.timers = require('__timers__.js')(runtime, global);
// 初始化基础模块
global.timers = require('__timers__.js')(runtime, global);

//初始化不依赖环境的模块
global.JSON = require('__json2__.js');
global.util = global.$util = require('__util__.js');
global.device = runtime.device;
//初始化不依赖环境的模块
global.JSON = require('__json2__.js');
global.util = global.$util = require('__util__.js');
global.device = runtime.device;

global.process = require('process')
global.Promise = require('bluebird.js');
global.process = require('process')
global.Promise = require('bluebird');

//设置JavaScriptBridges用于与Java层的交互和数据转换
runtime.bridges.setBridges(require('__bridges__.js'));
//设置JavaScriptBridges用于与Java层的交互和数据转换
runtime.bridges.setBridges(require('__bridges__.js'));

//初始化全局函数
require("__globals__")(runtime, global);
//初始化一般模块
(function (scope) {
var modules = ['app', 'automator', 'console', 'dialogs', 'files', 'io', 'selector', 'shell', 'web', 'ui',
"images", "threads", "events", "engines", "RootAutomator", "http", "storages", "floaty",
"sensors", "media", "plugins", "continuation", "$zip", "$base64", "$crypto","paddle"];
"sensors", "media", "plugins", "continuation", "$zip", "$base64", "$crypto", "paddle"];
var len = modules.length;
for (var i = 0; i < len; i++) {
var m = modules[i];
let module = require('__' + m + '__')(scope.runtime, scope);
scope[m] = module;
if(!m.startsWith('$')) {
if (!m.startsWith('$')) {
scope['$' + m] = module;
}
}
Expand Down
50 changes: 27 additions & 23 deletions autojs/src/main/assets/modules/axios/browser-libs/XMLHttpRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,32 +126,36 @@
setReadonlyAttribute(xhr, '_call', call, false);
call.enqueue({
onFailure(call, e) {
xhr._setReadyState(4);
setReadonlyAttribute(xhr, 'statusText', e.message)
if (e instanceof InterruptedIOException) {
xhr.dispatchEvent(new Event('timeout'));
}
xhr.dispatchEvent(new Event('error'))
xhr.dispatchEvent(new Event('loadend'))
atl.removeTask()
},
onResponse(call, res) {
setReadonlyAttribute(xhr, 'status', res.code());
setReadonlyAttribute(xhr, 'statusText', res.message());
setReadonlyAttribute(xhr, 'responseURL', res.request().url().toString())
setReadonlyAttribute(xhr, '_resHeaders', res.headers(), false)
xhr._setReadyState(2);
try {
XMLHttpRequest._parserResBody
.parser(xhr.responseType, xhr, res.body());

} catch (e) {
res.close();
setImmediate(() => {
xhr._setReadyState(4);
setReadonlyAttribute(xhr, 'statusText', e.message)
if (e instanceof InterruptedIOException) {
xhr.dispatchEvent(new Event('timeout'));
}
xhr.dispatchEvent(new Event('error'))
xhr.dispatchEvent(new Event('loadend'))
}
atl.removeTask();
atl.removeTask()
})
},
onResponse(call, res) {
setImmediate(() => {
setReadonlyAttribute(xhr, 'status', res.code());
setReadonlyAttribute(xhr, 'statusText', res.message());
setReadonlyAttribute(xhr, 'responseURL', res.request().url().toString())
setReadonlyAttribute(xhr, '_resHeaders', res.headers(), false)
xhr._setReadyState(2);
try {
XMLHttpRequest._parserResBody
.parser(xhr.responseType, xhr, res.body());

} catch (e) {
res.close();
xhr._setReadyState(4);
xhr.dispatchEvent(new Event('error'))
xhr.dispatchEvent(new Event('loadend'))
}
atl.removeTask();
})
}
})
}
Expand Down
4 changes: 3 additions & 1 deletion autojs/src/main/assets/modules/npm/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ function addTask(th, fn, callback) {
})
}
function createThread() {
return threads.start(() => {
let th = threads.start(() => {
setInterval(() => { }, 1000)
})
th.waitFor()
return th
}

stream.fromInputStream = function (inp, options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,18 @@ import com.stardust.automator.UiObjectCollection
import com.stardust.pio.UncheckedIOException
import org.mozilla.javascript.*
import org.mozilla.javascript.commonjs.module.provider.SoftCachingModuleScriptProvider
import java.io.File
import java.io.IOException
import java.io.InputStreamReader
import java.io.Reader
import java.net.URI
import java.util.*
import java.util.concurrent.ConcurrentHashMap

/**
* Created by Stardust on 2017/4/2.
*/

open class RhinoJavaScriptEngine(private val mAndroidContext: android.content.Context) : JavaScriptEngine() {
open class RhinoJavaScriptEngine(private val mAndroidContext: android.content.Context) :
JavaScriptEngine() {

val context: Context
private val mScriptable: TopLevelScope
Expand Down Expand Up @@ -119,9 +118,17 @@ open class RhinoJavaScriptEngine(private val mAndroidContext: android.content.Co
}

internal fun initRequireBuilder(context: Context, scope: Scriptable) {
val provider = AssetAndUrlModuleSourceProvider(mAndroidContext, MODULES_PATH,
listOf<URI>(File("/").toURI()))
val require = ScopeRequire(context,scope,SoftCachingModuleScriptProvider(provider),null,null,false)
val provider = AssetAndUrlModuleSourceProvider(
mAndroidContext,
listOf(
AssetAndUrlModuleSourceProvider.MODULE_DIR,
AssetAndUrlModuleSourceProvider.NPM_MODULE_DIR
)
)
val require = ScopeRequire(
context, scope, SoftCachingModuleScriptProvider(provider),
null, null, false
)
require.install(scope)
}

Expand Down Expand Up @@ -155,7 +162,12 @@ open class RhinoJavaScriptEngine(private val mAndroidContext: android.content.Co
}
}

override fun wrapAsJavaObject(cx: Context?, scope: Scriptable, javaObject: Any?, staticType: Class<*>?): Scriptable? {
override fun wrapAsJavaObject(
cx: Context?,
scope: Scriptable,
javaObject: Any?,
staticType: Class<*>?
): Scriptable? {
//Log.d(LOG_TAG, "wrapAsJavaObject: java = " + javaObject + ", result = " + result + ", scope = " + scope);
return if (javaObject is View) {
ViewExtras.getNativeView(scope, javaObject, staticType, runtime)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,20 @@ import java.net.URI

class AssetAndUrlModuleSourceProvider(
context: Context,
assetDirPath: String,
list: List<URI>? = null
list: List<URI> = emptyList()
) : ModuleSourceProviderBase() {
val mContext = context
private val okHttpClient = OkHttpClient.Builder().followRedirects(true).build()
private val contentResolver: ContentResolver = context.contentResolver
private val moduleSources: ArrayList<URI> = arrayListOf(mBaseURI, npmModuleSource)
private val moduleSources: List<URI> = list

companion object {
val mBaseURI: URI = URI.create("file:/android_asset/modules")
val npmModuleSource: URI = URI.create("file:/android_asset/modules/npm")
private const val assetRoot: String = "file:/android_asset"
val MODULE_DIR: URI = URI.create("file:/android_asset/modules")
val NPM_MODULE_DIR: URI = URI.create("file:/android_asset/modules/npm")
fun toAssetUri(assetDirPath: String): URI {
return URI.create("$assetRoot/$assetDirPath")
}
}

//初始化脚本以及启动文件只会从此方法加载模块,子模块加载没有以"./"或"../"开头的模块也会从此方法加载
Expand Down Expand Up @@ -85,8 +88,10 @@ class AssetAndUrlModuleSourceProvider(
} catch (e: Exception) {
null
}
val main: URI = mainFile ?: File(uri.path, "index.js").toURI()
return loadAt(main, uri, validator)
mainFile?.let {
//若package.json中main入口读取成功则重新执行模块加载
return loadFromUri(it,uri,validator)
}?: return loadAt(File(uri.path, "index.js").toURI(),uri,validator)
}

private fun loadAt(uri: URI, base: URI?, validator: Any?): ModuleSource? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.content.Context;
import android.graphics.drawable.Drawable;
import androidx.annotation.Nullable;

import com.stardust.autojs.core.graphics.ScriptCanvasView;
import com.stardust.autojs.core.ui.inflater.DynamicLayoutInflater;
Expand Down
19 changes: 10 additions & 9 deletions autojs/src/main/java/com/stardust/autojs/script/JsBeautifier.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ package com.stardust.autojs.script
import android.content.Context
import android.view.View
import com.stardust.autojs.engine.module.AssetAndUrlModuleSourceProvider
import com.stardust.autojs.engine.module.ScopeRequire
import com.stardust.pio.PFiles.join
import com.stardust.pio.PFiles.read
import com.stardust.pio.UncheckedIOException
import org.mozilla.javascript.Function
import org.mozilla.javascript.ImporterTopLevel
import org.mozilla.javascript.Scriptable
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.util.concurrent.Executors

Expand Down Expand Up @@ -71,13 +70,15 @@ class JsBeautifier(view: View, beautifyJsDirPath: String) {
importerTopLevel.initStandardObjects(mScriptContext, false)
mScriptable = importerTopLevel
}
val provider =
AssetAndUrlModuleSourceProvider(mContext, mBeautifyJsDir, listOf(File("/").toURI()))
RequireBuilder()
.setModuleScriptProvider(SoftCachingModuleScriptProvider(provider))
.setSandboxed(false)
.createRequire(mScriptContext, mScriptable)
.install(mScriptable)
val provider = AssetAndUrlModuleSourceProvider(
mContext,
listOf(AssetAndUrlModuleSourceProvider.toAssetUri(mBeautifyJsDir))
)
ScopeRequire(
mScriptContext!!, mScriptable!!,
SoftCachingModuleScriptProvider(provider),
null, null, false
).install(mScriptable!!)
}

fun prepare() {
Expand Down
Loading