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 10:07 am 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). 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; } 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? Regards, Fabrice -----Original Message----- From: Igor Bukanov [mailto:igor@fastmail.fm] Sent: jeudi 11 decembre 2003 18:30 To: Fabrice DELHOSTE [GEMPLUS] Subject: Re: Help: Defining "this" as a Java object (not inheriting from ScriptableObject) Fabrice DELHOSTE wrote: > Ok. > > Having such a class: > > public class Me { > public int getAge() { return age; } > public void setAge(int anAge) { age = anAge; } > public String getSex() { return "male"; } > private int age; > } > > My script could simply be: > > this.age = 33; > myage = this.getAge(); > > So my problem is: > how could I invoke a script and fix "this" to be a Java instance of my class > "Me" without inheriting from ScriptableObject? > I tried to register "this" as a variable but obviously, it does not work. Well, you can use either pure JS solution for that, that is put your script inside function f and then uss f.call to pass arbitrary this to the function: var me = new Packages.Me(); // Or new Packages.com.foo.bar.Me() if Me is in package com.foo.bar function f() { this.age = 33; myage = this.getAge(); } f.call(me); Or you can use in Java Context.compileReader/compileString to compile your script into Script object, http://www.mozilla.org/rhino/apidocs/org/mozilla/javascript/Script.html , and then instead of calling Script.exec, cast the Script object to Function, and then use Function.call which would allow to pass arbitrary this to the script instead of initializing it to the scope value as Scope.exec would do. .