Great that is exactly what I am looking for.  I even created an 
interface to specify the function signatures.  However:

Java Program:
public class trigger {

    public static void main(String[] args) {
        test e = new test();
        System.out.println("Sum: " + e.sum(10, 20));
        System.out.println("Times: " + e.times(10, 20));
     }
}

when executing it:

java trigger

results in:

Exception in thread "main" java.lang.RuntimeException: No Context 
associated with current Thread
         at org.mozilla.javascript.Context.getContext(Context.java:2277)
         at org.mozilla.javascript.JavaMembers.<init>(JavaMembers.java:68)
         at 
org.mozilla.javascript.JavaMembers.lookupClass(JavaMembers.java:759)
         at 
org.mozilla.javascript.NativeJavaObject.initMembers(NativeJavaObject.java:90)
         at 
org.mozilla.javascript.NativeJavaObject.<init>(NativeJavaObject.java:80)
         at 
org.mozilla.javascript.JavaAdapter.createAdapterWrapper(JavaAdapter.java:145)
         at test.<init>(<adapter>)
         at trigger.main(trigger.java:4)


Interface:
public interface sample {
     public int sum(int a, int b);
     public int times(int a, int b);
}

Javascript:
function sum(a, b) {
   return a+b;
}

function times(a, b) {
   return a*b;
}

JSC:
java org.mozilla.javascript.tools.jsc.Main -extends java.lang.Object 
-implements sample test.js

javap test
Compiled from "<adapter>"
public class test extends java.lang.Object implements sample{
     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 int sum(int, int);
     public int times(int, int);
}

Thanks,
-Edwin S. Ramirez-

Norris Boyd wrote:
> 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

Reply via email to