On Jan 31, 7:16 pm, [EMAIL PROTECTED] wrote:
> Hello,
>
> I don't understand... I would like to use the class (methods) from Java
> not from Javascript. Ideally, the compiler would create a class that
> would contain all or some of the functions exposed as methods. For example:
>
> Javascript:
> function sum1(a, b) {
> return a+b;
> }
>
> function times(a, b) {
> return a*b;
> }
>
> jsc generates "simple.class"
>
> Java:
>
> ...
> private void junk(int a, int b) {
> return simple.sum(a, b);
> }
> ...
>
> Can this be done with JavaAdapter?
>
> -Edwin S. Ramirez-Norris Boyd wrote:
> > On Jan 31, 11:12 am, [EMAIL PROTECTED] wrote:
> >> Hello,
>
> >> Is it possible to expose a script's functions as methods of the class
> >> when compiling Javascript to a Java class? If yes, how?
>
> >> Reason:
>
> >> I would like to use Javascript functions as SQL functions within Oracle
> >> triggers, avoiding recoding these in PL/SQL. Oracle allows for public
> >> methods within Java classes to be used for this purpose.
>
> >> I am able to compile the Javascript using jsc but the functions are not
> >> directly (as far as I can tell) available from Java.
>
> >> Thanks,
> >> -Edwin S. Ramirez-
>
> > If you create a Java interface with the methods you want to expose,
> > then you can create a JavaAdapter in Rhino. The JavaAdapter can then
> > be used to have JavaScript objects implement the interface you
> > defined, and calls to that interface's methods will be resolved to
> > functions in your object.
>
> > --N
Okay, sounds like what you want is to use -extends option with jsc:
java -cp js.jar org.mozilla.javascript.tools.jsc.Main -extends
java.lang.Object simple.js
Here's a simple example:
[rhino] cat test.js
function f() { return 3; }
function g() { return 4; }
[rhino] java -cp build/rhino1_7R1/js.jar
org.mozilla.javascript.tools.jsc.Main -extends java.lang.Object
test.js
[rhino] javap -classpath . test
Compiled from "<adapter>"
public class test extends java.lang.Object{
public final org.mozilla.javascript.ContextFactory factory;
public final org.mozilla.javascript.Scriptable delegee;
public final org.mozilla.javascript.Scriptable self;
public test(org.mozilla.javascript.ContextFactory,
org.mozilla.javascript.Scriptable);
public test(org.mozilla.javascript.ContextFactory,
org.mozilla.javascript.Scriptable, org.mozilla.javascript.Scriptable);
public test();
public java.lang.Object f();
public java.lang.Object g();
}
[rhino] java -classpath 'build/rhino1_7R1/js.jar;.'
org.mozilla.javascript.tools.shell.Main
Rhino 1.7 release 1 2008 01 23
js> t = new Packages.test
[EMAIL PROTECTED]
js> t.f()
3.0
js> t.g()
4.0
Note that we call the class from the Rhino shell at the end, but
calling it from other Java programs should work fine as well as long
as js.jar is in the classpath.
--N
_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino