package com.smtaiserver.smtaiserver.javaai.jsonflow.core; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.mozilla.javascript.Context; import org.mozilla.javascript.ContextAction; import org.mozilla.javascript.ContextFactory; import org.mozilla.javascript.Function; import com.smtaiserver.smtaiserver.javaai.jsonflow.script.SMTJsonFlowScriptScope; public class SMTJsonFlowScriptJet { protected ContextFactory _scriptFactory; protected SMTJsonFlowScriptScope _globalScope; protected SMTJsonFlowManager _poolManager; public SMTJsonFlowScriptJet() throws Exception { // 创建脚本引擎 Exception[] exScript = new Exception[1]; _globalScope = new SMTJsonFlowScriptScope(); _scriptFactory = new ContextFactory(); _scriptFactory.call(new ContextAction(){ @Override public Object run(Context cx) { try { _globalScope.__init__(cx); } catch (Exception e) { exScript[0] = e; } return null; } }); if(exScript[0] != null) throw exScript[0]; } public void setPoolManager(SMTJsonFlowManager poolManager) { _poolManager = poolManager; } public void close() { _poolManager.freeScripteJet(this); } public Context entryContext() { return _scriptFactory.enterContext(); } public Object callFunction(Context cx, Function func, Object[] args) { return func.call(cx, func, null, args); } public Function compileFunction(Context cx, String name, String code) { Function func = cx.compileFunction(_globalScope, code, name, 1, null); return func; } public void executeScript(Context cx, String code) { cx.evaluateString(_globalScope, code, "code", 1, null); } }