Subj : RE: Help: Defining "this" as a Java object (not inheriting from ScriptableObject) To : netscape.public.mozilla.jseng From : "Fabrice DELHOSTE" Date : Fri Dec 12 2003 11:17 am That works well now! Thanks again. Fabrice -----Original Message----- From: Igor Bukanov [mailto:igor@fastmail.fm] Sent: vendredi 12 decembre 2003 11:06 To: Fabrice DELHOSTE [GEMPLUS] Subject: Re: Help: Defining "this" as a Java object (not inheriting from ScriptableObject) Fabrice DELHOSTE wrote: > Thanks for your help. > > I have tried the second solution (calling Function.call) but it still does > not work. > The third parameter (thisObj) must implement Scriptable (as if I pass it to > Context.initStandardObjects). Use Context.toObject(yourJavaObject, scope) to wrap your Java object as Scriptable and then pass the resulting wrapper as thisObj. The wrapper object uses Java reflection to find all public fields and methods and setup access to them from scripts using Java beans rules. > So I modified my class to extend ScriptableObject (even if I wouldn't want > to because I consider it must be a regular Java class) and defining > getClassName() method: > > public class Me extends ScriptableObject { > public int getAge() { return age; } > public void setAge(int anAge) { age = anAge; } > public String getSex() { return "male"; } > > public String getClassName() { return "Me"; } > > private int age; > } > That would not work as Rhino runtime would not expose any method of ScriptableObject to scripts unless they follow a special convention specified in the documentation for ScriptableObject.defineClass and then you have to use ScriptableObject.defineClass to define object instances to export your specially named methods. > > When the script is executed, it returns the following error: > > org.mozilla.javascript.PropertyException: Constructor for "TypeError" not > found. > java.lang.RuntimeException: org.mozilla.javascript.PropertyException: > Constructor for "TypeError" not found. > at > org.mozilla.javascript.NativeGlobal.constructError(NativeGlobal.java:601) > at > org.mozilla.javascript.NativeGlobal.constructError(NativeGlobal.java:557) > at org.mozilla.javascript.NativeGlobal.typeError1(NativeGlobal.java:567) > at > org.mozilla.javascript.ScriptableObject.getDefaultValue(ScriptableObject.jav > a:627) > at org.mozilla.javascript.ScriptRuntime.add(ScriptRuntime.java:1332) > at org.mozilla.javascript.gen.c1.call(MyScript:2) > > > Any idea? > Does it come from the class I want to use for "this" cannot be managed as a > regular Java class through reflection? It comes from the fact that this.age was not found and this object did not have proper parent scope initialized so TypeError object from that scope can not be found. If you try Rhino tip from CVS, you will have much better error reporting. Regards, Igor .