Subj : Re: Rhino - general method in JS To : Yossi B From : Igor Bukanov Date : Tue Sep 16 2003 06:12 pm Yossi B wrote: > I'm looking for a way to call a method from within the JS BUT this > method must not be in the JS itself. > > the JS: > ..... > root = Packages.com.mercury.topaz.fm.execution.DataCollectorsAPI; > SS = root.getSiteScopeInstance(); > host = GetMapParam("Host"); > ...... > > where the GetMapParam() is not in the JS. > > is there a way to announce the method before evaluate the JS using the > Context or the Scriptable(scope) ? See API docs for the constructor of org.mozilla.javascript.FunctionObject or for the method defineFunctionProperties of ScriptableObject where the later is a convinience method to call the former when you need to define multiple functions. A possible usage would be to call the methods after you called Context.initStandardObjects: ScriptableObject scope = Context.initStandardObjects(null, false); // Export the following function to JS String[] names = { "print", "anotherFunction", "GetMapParam" }; try { scope.defineFunctionProperties(names, ClassThatDefinesFunctions.class, ScriptableObject.DONTENUM); } catch (PropertyException e) { throw RuntimeException("Unexpected"); } where the code supposes that there is ClassThatDefinesFunctions class that defines the functions: class ClassThatDefinesFunctions { public static int print(String arg) { // Rhino converts JS types into Java types automatically System.out.print(arg); return arg.length(); } public static Object anotherFunction(Context cx, Scriptable thisObj, Object[] args, Function funObj) { // Working with raw JS objects System.out.println("Number of suplied arguments: "+args.length); return thisObj; } public static String GetMapParam(String paramName) { ... } } See FunctionObject for details on method signatures. Regards, Igor .