ScriptEngine执行复杂js报数组越界

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

/**
 * @Author: SimonHu
 * @Date: 2019/2/12 11:13
 * @Description:
 */
public class Test {
    public static void main(String[] args) {
        ScriptEngineManager scriptEngineManager=new ScriptEngineManager();
        ScriptEngine scriptEngine=scriptEngineManager.getEngineByName("javascript");
        try {
            scriptEngine.eval("function getSwjg(fpdm, ckflag){\n" +
                    "\tvar flag = \"\";\n" +
                    "eval(function(p,a,c,k,e,d){e=function(c){return(c<a?\"\":e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)d[e(c)]=k[c]||e(c);k=[function(e){return d[e]}];e=function(){return'\\\\w+'};c=1;};while(c--)if(k[c])p=p.replace(new RegExp('\\\\b'+e(c)+'\\\\b','g'),k[c]);return p;}('19 18=[{\\'8\\':\\'1a\\',\\'6\\':\\'1c\\',\\'7\\':\\'0://L.D.2.1:4\\',\\'9\\':\\'0://L.D.2.1:4\\'},{\\'8\\':\\'1b\\',\\'6\\':\\'14\\',\\'7\\':\\'0://3.G.2.1:4\\',\\'9\\':\\'0://3.G.2.1:4\\'},{\\'8\\':\\'13\\',\\'6\\':\\'15\\',\\'7\\':\\'0://3.N-n-5.2.1:c\\',\\'9\\':\\'0://3.N-n-5.2.1:c\\'},{\\'8\\':\\'17\\',\\'6\\':\\'16\\',\\'7\\':\\'0://3.R-n-5.2.1:4\\',\\'9\\':\\'0://3.R-n-5.2.1:4\\'},{\\'8\\':\\'1d\\',\\'6\\':\\'1k\\',\\'7\\':\\'0://3.o-n-5.2.1:4\\',\\'9\\':\\'0://3.o-n-5.2.1:4\\'},{\\'8\\':\\'1j\\',\\'6\\':\\'1l\\',\\'7\\':\\'0://3.5.d.1:4\\',\\'9\\':\\'0://3.5.d.1:4\\'},{\\'8\\':\\'1n\\',\\'6\\':\\'1m\\',\\'7\\':\\'0://3.w.2.1:4\\',\\'9\\':\\'0://3.w.2.1:4\\'},{\\'8\\':\\'1f\\',\\'6\\':\\'1e\\',\\'7\\':\\'0://3.z-n-5.2.1:k\\',\\'9\\':\\'0://3.z-n-5.2.1:k\\'},{\\'8\\':\\'1g\\',\\'6\\':\\'1i\\',\\'7\\':\\'0://3.r-n-5.2.1:4\\',\\'9\\':\\'0://3.r-n-5.2.1:4\\'},{\\'8\\':\\'1h\\',\\'6\\':\\'12\\',\\'7\\':\\'0://a.5.t.2.1:q\\',\\'9\\':\\'0://a.5.t.2.1:q\\'},{\\'8\\':\\'10\\',\\'6\\':\\'W\\',\\'7\\':\\'0://e.m.2.1:b\\',\\'9\\':\\'0://e.m.2.1:b\\'},{\\'8\\':\\'V\\',\\'6\\':\\'Y\\',\\'7\\':\\'0://a.T.2.1:4\\',\\'9\\':\\'0://a.T.2.1:4\\'},{\\'8\\':\\'Z\\',\\'6\\':\\'11\\',\\'7\\':\\'0://3.K-n-5.2.1:4\\',\\'9\\':\\'0://3.K-n-5.2.1:4\\'},{\\'8\\':\\'X\\',\\'6\\':\\'1S\\',\\'7\\':\\'0://3.I-n-5.2.1:4\\',\\'9\\':\\'0://3.I-n-5.2.1:4\\'},{\\'8\\':\\'1R\\',\\'6\\':\\'1T\\',\\'7\\':\\'0://a.C-n-5.2.1:4\\',\\'9\\':\\'0://a.C-n-5.2.1:4\\'},{\\'8\\':\\'1V\\',\\'6\\':\\'1U\\',\\'7\\':\\'0://3.U-n-5.2.1\\',\\'9\\':\\'0://3.U-n-5.2.1\\'},{\\'8\\':\\'1Q\\',\\'6\\':\\'1M\\',\\'7\\':\\'0://3.B.2.1:c\\',\\'9\\':\\'0://3.B.2.1:c\\'},{\\'8\\':\\'1L\\',\\'6\\':\\'1N\\',\\'7\\':\\'0://3.h-n-5.2.1:4\\',\\'9\\':\\'0://3.h-n-5.2.1:4\\'},{\\'8\\':\\'1P\\',\\'6\\':\\'1O\\',\\'7\\':\\'0://3.A-n-5.2.1:4\\',\\'9\\':\\'0://3.A-n-5.2.1:4\\'},{\\'8\\':\\'23\\',\\'6\\':\\'22\\',\\'7\\':\\'0://3.j-n-5.2.1\\',\\'9\\':\\'0://3.j-n-5.2.1\\'},{\\'8\\':\\'24\\',\\'6\\':\\'26\\',\\'7\\':\\'0://3.p-n-5.2.1:4\\',\\'9\\':\\'0://3.p-n-5.2.1:4\\'},{\\'8\\':\\'25\\',\\'6\\':\\'21\\',\\'7\\':\\'0://3.u.2.1:s\\',\\'9\\':\\'0://3.u.2.1:s\\'},{\\'8\\':\\'1X\\',\\'6\\':\\'1W\\',\\'7\\':\\'0://3.v-n-5.2.1:4\\',\\'9\\':\\'0://3.v-n-5.2.1:4\\'},{\\'8\\':\\'1Y\\',\\'6\\':\\'20\\',\\'7\\':\\'0://3.y.2.1:4\\',\\'9\\':\\'0://3.y.2.1:4\\'},{\\'8\\':\\'1Z\\',\\'6\\':\\'1K\\',\\'7\\':\\'0://3.x.2.1:g\\',\\'9\\':\\'0://3.x.2.1:g\\'},{\\'8\\':\\'1v\\',\\'6\\':\\'1u\\',\\'7\\':\\'0://3.f.2.1:4\\',\\'9\\':\\'0://3.f.2.1:4\\'},{\\'8\\':\\'1w\\',\\'6\\':\\'1y\\',\\'7\\':\\'0://3.i.2.1:b\\',\\'9\\':\\'0://3.i.2.1:b\\'},{\\'8\\':\\'1x\\',\\'6\\':\\'1t\\',\\'7\\':\\'0://3.l-n-5.2.1:4\\',\\'9\\':\\'0://3.l-n-5.2.1:4\\'},{\\'8\\':\\'1p\\',\\'6\\':\\'1o\\',\\'7\\':\\'0://3.Q-n-5.2.1:b\\',\\'9\\':\\'0://3.Q-n-5.2.1:b\\'},{\\'8\\':\\'1q\\',\\'6\\':\\'1s\\',\\'7\\':\\'0://3.P.2.1:4\\',\\'9\\':\\'0://3.P.2.1:4\\'},{\\'8\\':\\'1r\\',\\'6\\':\\'1G\\',\\'7\\':\\'0://3.O.2.1:S\\',\\'9\\':\\'0://3.O.2.1:S\\'},{\\'8\\':\\'1F\\',\\'6\\':\\'1H\\',\\'7\\':\\'0://a.F-n-5.2.1:4\\',\\'9\\':\\'0://a.F-n-5.2.1:4\\'},{\\'8\\':\\'1J\\',\\'6\\':\\'1I\\',\\'7\\':\\'0://3.E-n-5.2.1:4\\',\\'9\\':\\'0://3.E-n-5.2.1:4\\'},{\\'8\\':\\'1E\\',\\'6\\':\\'1A\\',\\'7\\':\\'0://3.H-n-5.2.1:4\\',\\'9\\':\\'0://3.H-n-5.2.1:4\\'},{\\'8\\':\\'1z\\',\\'6\\':\\'1B\\',\\'7\\':\\'0://3.M.2.1:4\\',\\'9\\':\\'0://3.M.2.1:4\\'},{\\'8\\':\\'1D\\',\\'6\\':\\'1C\\',\\'7\\':\\'0://3.J-n-5.2.1:4\\',\\'9\\':\\'0://3.J-n-5.2.1:4\\'}];',62,131,'https|cn|gov|fpcy|443|tax|sfmc|Ip|code|address|fpcyweb|80|82|ln|fpdk|hitax|8200|sd|cqsw|ha|4432|sc|jsgs||nm|hb|1001|hl|8083|sh|hntax|gd|dlntax|gxgs|szgs|jl|qd|jxgs|fj|bjsat|gs|sn|tjsat|qh|ah|xj|nb|zjfpcyweb|nxgs|he|xztax|yngs|gz|sx|81|zjtax|xm|3300|江苏|3400|浙江|3302|3200|宁波|上海|1300|天津|河北|山西|1400|citys|var|1100|1200|北京|1500|吉林|2200|2300|3100|黑龙江|2100|内蒙古|辽宁|大连|2102|贵州|5200|5300|5400|云南|四川|海南|4600|5000|5100|重庆|6400|青海|宁夏|*|6500|6300|6100|*|陕西|甘肃|6200|广西|3700|江西|山东|青岛|3702|3600|3500|安徽|福建|厦门|3502|广东|4400|4403|4500|深圳|湖南|河南|4100|4200|4300|湖北'.split('|'),0,{}))\n" +
                    "\n" +
                    "var dqdm=null;var swjginfo=new Array();if(fpdm.length==12){dqdm=fpdm.substring(1,5)}else{dqdm=fpdm.substring(0,4)}if(dqdm!=\"2102\"&&dqdm!=\"3302\"&&dqdm!=\"3502\"&&dqdm!=\"3702\"&&dqdm!=\"4403\"){dqdm=dqdm.substring(0,2)+\"00\"}for(var i=0;i<citys.length;i++){if(dqdm==citys[i].code){swjginfo[0]=citys[i].sfmc;if(flag=='debug'){swjginfo[1]=\"\";swjginfo[2]=dqdm}else{swjginfo[1]=citys[i].Ip+\"/WebQuery\";swjginfo[2]=dqdm}break}}return swjginfo;\n" +
                    "}");

            Invocable invocable= (Invocable) scriptEngine;
        } catch (ScriptException e) {
            e.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
java.lang.ArrayIndexOutOfBoundsException: -1
    at jdk.nashorn.internal.codegen.CodeGeneratorLexicalContext.nextFreeSlot(CodeGeneratorLexicalContext.java:195)
    at jdk.nashorn.internal.codegen.CodeGenerator.initLocals(CodeGenerator.java:958)
    at jdk.nashorn.internal.codegen.CodeGenerator.enterBlock(CodeGenerator.java:568)
    at jdk.nashorn.internal.ir.Block.accept(Block.java:142)
    at jdk.nashorn.internal.ir.LexicalContextNode$Acceptor.accept(LexicalContextNode.java:57)
    at jdk.nashorn.internal.ir.Block.accept(Block.java:361)
    at jdk.nashorn.internal.codegen.CodeGenerator.enterIfNode(CodeGenerator.java:1154)
    at jdk.nashorn.internal.ir.IfNode.accept(IfNode.java:76)
    at jdk.nashorn.internal.ir.Node.accept(Node.java:291)
    at jdk.nashorn.internal.ir.Block.accept(Block.java:143)
    at jdk.nashorn.internal.ir.LexicalContextNode$Acceptor.accept(LexicalContextNode.java:57)
    at jdk.nashorn.internal.ir.Block.accept(Block.java:361)
    at jdk.nashorn.internal.ir.FunctionNode.accept(FunctionNode.java:309)
    at jdk.nashorn.internal.ir.LexicalContextNode$Acceptor.accept(LexicalContextNode.java:57)
    at jdk.nashorn.internal.ir.LexicalContextExpression.accept(LexicalContextExpression.java:46)
    at jdk.nashorn.internal.ir.FunctionNode.accept(FunctionNode.java:50)
    at jdk.nashorn.internal.codegen.CodeGenerator$1.enterFunctionNode(CodeGenerator.java:456)
    at jdk.nashorn.internal.ir.FunctionNode.accept(FunctionNode.java:308)
    at jdk.nashorn.internal.ir.LexicalContextNode$Acceptor.accept(LexicalContextNode.java:57)
    at jdk.nashorn.internal.ir.LexicalContextExpression.accept(LexicalContextExpression.java:46)
    at jdk.nashorn.internal.ir.FunctionNode.accept(FunctionNode.java:50)
    at jdk.nashorn.internal.codegen.CodeGenerator.load(CodeGenerator.java:424)
    at jdk.nashorn.internal.codegen.CodeGenerator.load(CodeGenerator.java:359)
    at jdk.nashorn.internal.codegen.CodeGenerator.enterVarNode(CodeGenerator.java:2175)
    at jdk.nashorn.internal.ir.VarNode.accept(VarNode.java:125)
    at jdk.nashorn.internal.ir.Node.accept(Node.java:291)
    at jdk.nashorn.internal.ir.Block.accept(Block.java:143)
    at jdk.nashorn.internal.ir.LexicalContextNode$Acceptor.accept(LexicalContextNode.java:57)
    at jdk.nashorn.internal.ir.Block.accept(Block.java:361)
    at jdk.nashorn.internal.ir.FunctionNode.accept(FunctionNode.java:309)
    at jdk.nashorn.internal.ir.LexicalContextNode$Acceptor.accept(LexicalContextNode.java:57)
    at jdk.nashorn.internal.ir.LexicalContextExpression.accept(LexicalContextExpression.java:46)
    at jdk.nashorn.internal.ir.FunctionNode.accept(FunctionNode.java:50)
    at jdk.nashorn.internal.codegen.CompilationPhase$8.transform(CompilationPhase.java:379)
    at jdk.nashorn.internal.codegen.CompilationPhase.apply(CompilationPhase.java:513)
    at jdk.nashorn.internal.codegen.Compiler.compile(Compiler.java:361)
    at jdk.nashorn.internal.runtime.Context.compile(Context.java:1071)
    at jdk.nashorn.internal.runtime.Context.compileScript(Context.java:1019)
    at jdk.nashorn.internal.runtime.Context.compileScript(Context.java:490)
    at jdk.nashorn.api.scripting.NashornScriptEngine.compileImpl(NashornScriptEngine.java:651)
    at jdk.nashorn.api.scripting.NashornScriptEngine.compileImpl(NashornScriptEngine.java:640)
    at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:521)
    at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:192)
    at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
    at com.zhx.util.irsutil.Tets.main(Tets.java:21)
Disconnected from the target VM, address: '127.0.0.1:49825', transport: 'socket'

Process finished with exit code 0
 jdk 内嵌的引擎有 bug,那个叫 nashorn 的东西,在群里问了大佬,说是jdk小版本的问题,我本地是1.8.0_25的,同事的是1.8.0_144,换成新版本的就可以了。
上一篇:乐蒙网-鸿蒙不再属于华为,工信部正式接手


下一篇:javascript – JDK 1.8.0_92 Nashorn JS引擎indexOf行为