Subj : [Fwd: Re: Rhino help] To : netscape.public.mozilla.jseng From : Igor Bukanov Date : Fri Jul 25 2003 08:14 pm Prasad Sethumadhavan wrote: > i have the following javascript > methods to be implemented > queue() and queue("text value"). > The java class implementing it looks this: > > public class PromptScriptableObject extends ScriptableObject { > //other required methods like getClassName() etc... > public void jsFunction_Queue() throws Exception { > LOGGER.debug(module + "Prompt.Queue()"); > } > public void jsFunction_Queue(String strText) throws Exception { > LOGGER.debug(module + "Prompt.Queue(" + strText + ")"); > } > } > However the rhino runtime doesn't call the correct functions (i am expecting > that it will do this automatically). However i saw you following message and > i guess you are saying that we need to handle this ourselves. > If you would need to support only a version of document.write with a > single argument, you would use jsFunction_write(String str), Rhino will > convert the corresponding JavaScript argument automatically when calling > it. But since you need to deal with JS calling document.write(arg1, > arg2, ...(, you should use something like: > > void jsFunction_write(ScriptableObject str) > public static Object jsFunction_getClass( > Context cx,Scriptable thisObj, Object[] args, Function funObj) > { > StringBuffer sb = new StringBuffer(); > for (int i = 0; i != args.length; ++i) { > sb.append(ScriptRuntme.toString(args[i])); > } > DocumentClass doc = (DocumentClass)thisObj; > doc.write_impl(sb.toString()); > return Undefined.instance; // void result > } > However i don't quite understand the code snippet totally. It would be great > if you could help me out in this. Rhino does not support overloading when you extend ScriptableObject to implement a custom host object. If you need this functionality, you have to declare a single static method jsFunction_ with signature public static Object jsFunction_( Context cx, Scriptable thisObj, Object[] args, Function funObj) { .... } and explicitly check for length of args array and convert arg elemnts to a Java type using ScriptRuntime.toSomething functions. In your case you will have something like: public static Object jsFunction_Object( Context cx, Scriptable thisObj, Object[] args, Function funObj) throws Exception { if (!(thisObj instanceof PromptScriptableObject)) throw Context.reportRuntimeError("Incompatible call"); PromptScriptableObject self = (PromptScriptableObject)thisObj; if (args.length == 0) { LOGGER.debug(self.module + "Prompt.Queue()"); } else { String strText = ScriptRuntime.toString(args[0]); LOGGER.debug(module + "Prompt.Queue(" + strText + ")"); } return Undefined.instance; } Regards, Igor .