Add: commonjs support, Fix: editor auto line break
This commit is contained in:
@@ -2,15 +2,23 @@ package com.stardust.scriptdroid;
|
||||
|
||||
import com.stardust.scriptdroid.droid.script.JavaScriptEngine;
|
||||
import com.stardust.scriptdroid.droid.script.RhinoJavaScriptEngine;
|
||||
import com.stardust.scriptdroid.droid.script.file.ScriptFile;
|
||||
import com.stardust.scriptdroid.record.inputevent.InputEventToJsConverter;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.mozilla.javascript.Context;
|
||||
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 org.mozilla.javascript.commonjs.module.provider.UrlModuleSourceProvider;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.reflect.Array;
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
@@ -27,80 +35,63 @@ import static org.junit.Assert.assertEquals;
|
||||
*/
|
||||
public class ExampleUnitTest {
|
||||
// TODO: 2017/3/3 自定义函数
|
||||
// TODO: 2017/3/19
|
||||
// TODO: 2017/3/19 exist函数
|
||||
// TODO: 2017/3/23 tasker插件
|
||||
// TODO: 2017/3/23 任务管理与控制台
|
||||
// TODO: 2017/3/23 悬浮窗加入控制台
|
||||
// TODO: 2017/3/24 文件读写api
|
||||
// TODO: 2017/3/24 网络读写api
|
||||
// TODO: 2017/3/24 常驻后台api
|
||||
// TODO: 2017/3/24 ui。E4x
|
||||
// TODO: 2017/3/24 编辑界面文档和自动补全
|
||||
// TODO: 2017/3/24 驻留模式
|
||||
|
||||
|
||||
// FIXME: 2017/3/23 死机重启问题
|
||||
// FIXME: 2017/3/23 卡顿问题
|
||||
|
||||
Counter counter = new Counter(5);
|
||||
private List<Integer> mIntegers = new ArrayList<>();
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
ExecutorService executor = Executors.newFixedThreadPool(10);
|
||||
executor.execute(new Task(1, 100));
|
||||
executor.execute(new Task(2, 1000));
|
||||
System.out.println(0);
|
||||
executor.execute(new Task(3, 100));
|
||||
executor.execute(new Task(4, 100));
|
||||
executor.execute(new Task(5, 100));
|
||||
System.out.println(100);
|
||||
counter.lock();
|
||||
|
||||
List<URI> paths = Collections.singletonList(new File("D:/js/").toURI());
|
||||
Context ctx = Context.enter();
|
||||
ctx.setOptimizationLevel(-1);
|
||||
ctx.setLanguageVersion(Context.VERSION_1_7);
|
||||
ctx.setInstructionObserverThreshold(10000);
|
||||
ImporterTopLevel scope = new ImporterTopLevel();
|
||||
scope.initStandardObjects(ctx, false);
|
||||
|
||||
new RequireBuilder()
|
||||
.setModuleScriptProvider(new SoftCachingModuleScriptProvider(
|
||||
new UrlModuleSourceProvider(paths, null)))
|
||||
.setSandboxed(true)
|
||||
.createRequire(ctx, scope)
|
||||
.install(scope);
|
||||
|
||||
|
||||
ctx.evaluateString(scope, "require('test.js')()", "<test>", 1, null);
|
||||
|
||||
Context.exit();
|
||||
}
|
||||
|
||||
private class Task implements Runnable {
|
||||
|
||||
public Task(int i, int delay) {
|
||||
this.i = i;
|
||||
this.delay = delay;
|
||||
}
|
||||
|
||||
private final int i, delay;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (delay > 0)
|
||||
try {
|
||||
Thread.sleep(delay);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
System.out.println(i);
|
||||
synchronized (mIntegers) {
|
||||
mIntegers.add(i);
|
||||
for (Integer integer : mIntegers) {
|
||||
System.out.println(i + ":" + integer);
|
||||
}
|
||||
}
|
||||
counter.minus();
|
||||
}
|
||||
}
|
||||
|
||||
private static class Counter {
|
||||
|
||||
private final Object lock = new Object();
|
||||
|
||||
public Counter(int i) {
|
||||
this.i = i;
|
||||
}
|
||||
|
||||
private volatile int i;
|
||||
|
||||
void minus() {
|
||||
i--;
|
||||
if (i == 0) {
|
||||
synchronized (lock) {
|
||||
lock.notify();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void lock() {
|
||||
synchronized (lock) {
|
||||
try {
|
||||
lock.wait();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@Test
|
||||
public void test2(){
|
||||
Context context = Context.enter();
|
||||
context.setOptimizationLevel(-1);
|
||||
context.setLanguageVersion(Context.VERSION_1_7);
|
||||
context.setInstructionObserverThreshold(10000);
|
||||
ImporterTopLevel scope = new ImporterTopLevel();
|
||||
scope.initStandardObjects(context, false);
|
||||
List<URI> paths = Collections.singletonList(new File("D:/js/").toURI());
|
||||
new RequireBuilder()
|
||||
.setModuleScriptProvider(new SoftCachingModuleScriptProvider(
|
||||
new UrlModuleSourceProvider(paths, null)))
|
||||
.setSandboxed(true)
|
||||
.createRequire(context, scope)
|
||||
.install(scope);
|
||||
context.evaluateString(scope, "require('test.js')()", "<test>", 1, null);
|
||||
|
||||
Context.exit();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user