2013-07-08 61 views
0

因此,我有一些代碼可以成功加載和編譯腳本。這很好。接下來,雖然,我想能夠在編譯腳本中調用一個函數。不幸的是,我看不出有什麼辦法可以編譯腳本。在Java腳本API中的腳本中調用Javascript函數

Compilable compEngine = (Compilable)engine; 
compiledScripts.put(filename, compEngine.compile(new InputStreamReader(in))); 
compiledScripts.get(filename).eval(); 
//All works until this point. The compiled script does not seem to be invokable. 
Invocable inv = (Invocable) compiledScripts.get(filename); 
inv.invokeFunction("onLoad"); 

有沒有辦法做到這一點?如果是這樣,怎麼樣?如果不是,那麼在不編譯腳本時通常會有多大的性能下降?

+0

你在這裏使用什麼庫?犀牛? –

+0

Rhino是Java中的默認JavaScript引擎,所以是的。 – Katherine1

回答

1

我找到了我的問題的答案。這實際上是一個非常簡單的改變。

這條線:

Invocable inv = (Invocable) compiledScripts.get(filename); 

需要改變到:

Invocable inv = (Invocable) compiledScripts.get(filename).getEngine(); 

這將返回編譯腳本是越來越運行在發動機,從而讓我們從編譯腳本中調用函數。