01: package freemarker.ext.rhino;
02:
03: import java.util.List;
04:
05: import org.mozilla.javascript.Context;
06: import org.mozilla.javascript.Function;
07: import org.mozilla.javascript.Scriptable;
08: import org.mozilla.javascript.ScriptableObject;
09:
10: import freemarker.ext.beans.BeansWrapper;
11: import freemarker.template.TemplateMethodModelEx;
12: import freemarker.template.TemplateModel;
13: import freemarker.template.TemplateModelException;
14:
15: /**
16: * <p><font color="red">Experimental: no backward compatibility guarantees</font>;
17: * any feedback is highly welcome!</p>
18: *
19: * @author Attila Szegedi
20: * @version $Id: RhinoFunctionModel.java,v 1.2.2.1 2006/12/27 16:10:48 szegedia Exp $
21: */
22: public class RhinoFunctionModel extends RhinoScriptableModel implements
23: 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]);
39: }
40: return wrapper.wrap(((Function) getScriptable())
41: .call(cx, ScriptableObject.getTopLevelScope(fnThis),
42: fnThis, args));
43: }
44: }
|