From:  "dg" <>
Date:  31 May 2006 01:12:20 Hong Kong Time

Do I need a FunctionObject for each method I want exposed on my scriptable Java object?


I have a scriptable object MyFoo whose methods I want to be callable
from JavaScript.  I've figured out that the way to make it available to
JavaScript is through the following:

    Context cx = Context.enter();
    Scriptable scope = cx.initStandardObjects(null);
    ScriptableObject.defineClass(scope, MyFoo.class);
    Scriptable fooObject = cx.newObject(scope, "MyFoo");
    scope.put("foo", scope, fooObject);

So, now that "foo" is a variable available for JavaScript, it looks
like I can't simply invoke MyFoo methods on "foo."  I get an "is not a
function" error when executing the script unless I explicitly handle
each method in the "get" method on MyFoo:

  public Object get(String name, Scriptable start) {
    FunctionObject func = null;
    if ("getThing".equals(name)) {
      Class[] params = new Class[0];
      try {
        func = new FunctionObject(name, this.getClass().getMethod(name,
params), getParentScope());
      } catch (NoSuchMethodException ex) {
    return func;

Is this right?  Is there any way no avoid doing this extra work in

- Dmitry