Subj : Re: Help: Defining "this" as a Java object (not inheriting from ScriptableObject) To : "Fabrice DELHOSTE [GEMPLUS]" From : Igor Bukanov Date : Thu Dec 11 2003 06:29 pm 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. .