1 package freemarker.ext.rhino; 2 3 import java.util.List ; 4 5 import org.mozilla.javascript.Context; 6 import org.mozilla.javascript.Function; 7 import org.mozilla.javascript.Scriptable; 8 import org.mozilla.javascript.ScriptableObject; 9 10 import freemarker.ext.beans.BeansWrapper; 11 import freemarker.template.TemplateMethodModelEx; 12 import freemarker.template.TemplateModel; 13 import freemarker.template.TemplateModelException; 14 15 22 public class RhinoFunctionModel extends RhinoScriptableModel 23 implements TemplateMethodModelEx { 24 25 private final Scriptable fnThis; 26 27 public RhinoFunctionModel(Function function, Scriptable fnThis, 28 BeansWrapper wrapper) { 29 super(function, wrapper); 30 this.fnThis = fnThis; 31 } 32 33 public Object exec(List arguments) throws TemplateModelException { 34 Context cx = Context.getCurrentContext(); 35 Object [] args = arguments.toArray(); 36 BeansWrapper wrapper = getWrapper(); 37 for (int i = 0; i < args.length; i++) { 38 args[i] = wrapper.unwrap((TemplateModel)args[i], Scriptable.class); 39 } 40 return wrapper.wrap(((Function)getScriptable()).call(cx, 41 ScriptableObject.getTopLevelScope(fnThis), fnThis, args)); 42 } 43 } 44 | Popular Tags |