使用Java實現JS指令碼引擎
專案地址
https://github.com/GitHub-Laz...
生成語法樹 並輸出格式化程式碼
示例程式碼
class Test{
public static void main(String[] args){
Top.init();
DocNode doc = Top.compile("let a=1+2,b=3,c=\"string\",d=a*(b+c/2),func=function(){};");
System.out.println(doc);
}
}
輸出
let a = 1 + 2, b = 3, c = "string", d = a * (b + c / 2), func = function () {
}
執行指令碼
以下示例為執行快排演算法(執行環境執行緒隔離)
js指令碼
function sort(arr, i, j) {
if (i >= j) {
return;
}
let p = i, q = j;
let temp = arr[p];
while (p < q) {
while (p < q && arr[q] >= temp) {
q-=1;
}
arr[p] = arr[q];
while (p < q && arr[p] <= temp) {
p+=1;
}
arr[q] = arr[p];
}
arr[q] = temp;
sort(arr, i, q - 1);
sort(arr, q + 1, j);
}
let arr = [234, 57, 12, 123, 346, 1234, 2];
sort(arr, 0, arr.length - 1);
示例程式碼
class Test{
public static void main(String[] args){
Top.init();
Top.eval("function sort(arr, i, j) {\n" +
" if (i >= j) {\n" +
" return;\n" +
" }\n" +
" let p = i, q = j;\n" +
" let temp = arr[p];\n" +
" while (p < q) {\n" +
" while (p < q && arr[q] >= temp) {\n" +
" q-=1;\n" +
" }\n" +
" arr[p] = arr[q];\n" +
" while (p < q && arr[p] <= temp) {\n" +
" p+=1;\n" +
" }\n" +
" arr[q] = arr[p];\n" +
" }\n" +
" arr[q] = temp;\n" +
" sort(arr, i, q - 1);\n" +
" sort(arr, q + 1, j);\n" +
"}\n" +
"\n" +
"let arr = [234, 57, 12, 123, 346, 1234, 2];\n" +
"\n" +
"sort(arr, 0, arr.length - 1);");
Top.loop();
System.out.println(Top.getThreadLocalTop().getMainContexts().getContexts().peek().toSimpleString());
}
}
輸出
arr: [2, 12, 57, 123, 234, 346, 1234]
sort: [object Object]