/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ht.data.script;

import javax.script.ScriptContext;
import javax.script.ScriptException;
import javax.script.SimpleBindings;
import javax.script.SimpleScriptContext;

/**
 *
 * @author Administrator
 */
public class ScriptsTest {

	/**
	 * @param args the command line arguments
	 */
	public static void main(String[] args) {
		// TODO code application logic here
		try {
			Object ret = ScriptUtil.INSTANCE.invokeFunction("function exp(){return v2}", "exp", new Object[]{8, 23, 24});
			System.out.println(ret);
			ret = ScriptUtil.INSTANCE.invokeFunction("function exp(){return true}", "exp", new Object[]{8, true});
			System.out.println(ret instanceof Boolean);
			System.out.println(ret);

			ret = ScriptUtil.INSTANCE.invokeFunction("function exp(){v8 = 888; return v3}", "exp", new Object[]{8, true});
			System.out.println(ret);

			ret = ScriptUtil.INSTANCE.invokeFunction("function exp(){return v8}", "exp", new Object[]{8, true});
			System.out.println(ret);

			System.out.println("==================");
			SimpleBindings binder = new SimpleBindings();
			ScriptUtil.INSTANCE.getEngine().setBindings(binder, ScriptContext.GLOBAL_SCOPE);
			//binder.put("s1", "s1value");
			SimpleScriptContext context = new SimpleScriptContext();
			context.setBindings(binder, ScriptContext.GLOBAL_SCOPE);
			
			binder.put("s1", "s1value");
			ScriptUtil.INSTANCE.getEngine().eval("s2 = s1", context);
			context.setAttribute("g1", "g1_value", ScriptContext.GLOBAL_SCOPE);
			//Object obj = ScriptUtil.INSTANCE.getEngine().eval("s2 = s1");
			Object obj = ScriptUtil.INSTANCE.getEngine().eval("s2", context);
			System.out.println(obj);
			
			context = new SimpleScriptContext();
			context.setBindings(binder, ScriptContext.GLOBAL_SCOPE);
			obj = ScriptUtil.INSTANCE.getEngine().eval("g1", context);
			System.out.println(obj);
			obj = ScriptUtil.INSTANCE.getEngine().eval("s2", context);
			System.out.println(obj);
			//Object obj = ScriptUtil.INSTANCE.getEngine().eval("s2 = s1");

//			ScriptUtil.INSTANCE.getEngine().getBindings(ScriptContext.GLOBAL_SCOPE).put("s1", "中国");
//			ret = ScriptUtil.INSTANCE.getEngine().eval("s1 == \"中国\"");
//			System.out.println(ret);
//			ScriptUtil.INSTANCE.getEngine().eval("s1 = \"你好\"");
//			System.out.println("获取:" + ScriptUtil.INSTANCE.getEngine().getBindings(ScriptContext.GLOBAL_SCOPE).get("s1"));
//
//			ScriptUtil.INSTANCE.getEngine().getBindings(ScriptContext.GLOBAL_SCOPE).put("中文_", 2);
//			ret = ScriptUtil.INSTANCE.getEngine().eval("中文_=2");
//			System.out.println(ret instanceof Double);


		} catch (ScriptException ex) {
			ex.printStackTrace();
		} catch (NoSuchMethodException ex) {
			ex.printStackTrace();
		}
	}
}
