首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

Rhino,一个不错的Javascript发动机

2012-11-03 
Rhino,一个不错的Javascript引擎package co.testimport java.io.FileReaderimport java.io.LineNumberRe

Rhino,一个不错的Javascript引擎

package co.test;    import java.io.FileReader;  import java.io.LineNumberReader;    import org.mozilla.javascript.Context;  import org.mozilla.javascript.Function;  import org.mozilla.javascript.Scriptable;    public class JSExploration  {       private Context cx;         private Scriptable scope;         public JSExploration()       {           this.cx = Context.enter();           this.scope = cx.initStandardObjects();       }         public Object runJavaScript(String filename)       {           String jsContent = this.getJsContent(filename);           Object result = cx.evaluateString(scope, jsContent, filename, 1, null);           return result;       }         private String getJsContent(String filename)       {           LineNumberReader reader;           try          {               reader = new LineNumberReader(new FileReader(filename));               String s = null;               StringBuffer sb = new StringBuffer();               while ((s = reader.readLine()) != null)               {                   sb.append(s).append("\n");               }               return sb.toString();           }           catch (Exception e)           {               // TODO Auto-generated catch block               e.printStackTrace();               return null;           }       }           public Scriptable getScope()       {           return scope;       }         public static void main(String[] args)       {           String filename = System.getProperty("user.dir") + "/jsmap.js";           JSExploration jsExploration = new JSExploration();           Object result = jsExploration.runJavaScript(filename);           Scriptable scope = jsExploration.getScope();           Function sum = (Function) scope.get("sum", scope);          Function isPrime = (Function)sum.call(Context.getCurrentContext(), scope, sum, new Object[] {2,8});          Object ss = isPrime.call(Context.getCurrentContext(), sum, isPrime, new Object[] {2,8});          System.out.println(Context.toString(ss));      }  } 

?

?

function  sum(x, y) { // this.formulaeObject = null; // this.formulaeObject["vager"] = function (c, d) { // return (c + d)/2; // }; var vager = 1000; return function (x,y){return x + y + vager;} ; }
?

?

热点排行