On Apr 9, 11:58 am, Wernke <[email protected]> wrote:
> On Apr 7, 9:33 am, Hannes Wallnoefer <[email protected]> wrote:
>
>
>
> > On Apr 2, 2:39 pm, Wernke <[email protected]> wrote:
>
> > > Hi all,
>
> > > I have a Java class 'Procedure' derived from ScriptableObject that has
> > > a public enum Result {OK, ERROR}. How can I make the enum values
> > > available to a script?
>
> > > As an example, say there is a method Procedure.setResult( Result r ),
> > > which I want to call from the script with an OK value.
>
> > > I found that with a given Procedure object proc I can do:
>
> > > proc.setResult( Packages.my.namespace.Procedure.Result.OK );
>
> > > but I find this clumsy.
>
> > > What would I have to do to be able to call
>
> > > proc.setResult( OK ) or at least proc.setResult( Result.OK )
>
> > > without any further import or such in the script? What I am actually
> > > looking for is a Java method on ScriptableObject that makes the const
> > > enum values visible and usable in the script, just similar to
> > > defineProperty().
>
> > If you are using ScriptableObject.defineClass() to set up your
> > Procedure host object then the finishInit() method is your friend.
>
> >http://www.mozilla.org/rhino/apidocs/org/mozilla/javascript/Scriptabl...)
>
> > You can use it to set up all kinds of properties on the constructor,
> > global scope, or prototype. For example, to set up the enum values as
> > constants in the Procedure constructor:
>
> >     public static void finishInit(Scriptable scope, FunctionObject
> > constructor, Scriptable prototype) {
> >         int flags = DONTENUM | READONLY | PERMANENT;
> >         constructor.defineProperty("OK", Context.toObject(Result.OK,
> > scope), flags);
> >         constructor.defineProperty("ERROR", Context.toObject
> > (Result.ERROR, scope), flags);
> >     }
>
> > or as global constants:
>
> >     public static void finishInit(Scriptable scope, FunctionObject
> > constructor, Scriptable prototype) {
> >         int flags = DONTENUM | READONLY | PERMANENT;
> >         ScriptableObject.defineProperty(scope, "OK", Context.toObject
> > (Result.OK, scope), flags);
> >         ScriptableObject.defineProperty(scope, "ERROR",
> > Context.toObject(Result.ERROR, scope), flags);
> >     }
>
> > I also tried to setting up the enum class as constant in the global
> > scope, but that didn't work, probably because of how enums and their
> > constants are represented within the jvm. Note that finishInit must be
> > declared public in order to work.
>
> > hannes
>
> > > Thanks for any suggestions,
>
> > > Wernke
>
> Hi Hannes,
>
> thanks a lot for your hint. I had overlooked the defineClass() method,
> and actually I do not need it, because I do not want to create
> Procedure instances from scripts. I want to create Procedure objects
> from Java code and then run a script that can access the Procedure's
> methods and properties (and the enum, of course).
>
> I am now simply defining the properties OK and ERROR with the
> defineProperty() method, in conjunction with Context.toObject
> (enumValue, scope) directly after creating a Procedure instance. That
> works perfectly - I can use the objects OK and ERROR in scripts
> without string quotes, and that is what I was actually looking for.
> These objects even convert themselves to the strings 'OK' and 'ERROR'
> when used in a string context.
>
> Perhaps I can still find a way to define the entire enum class in one
> go, without defining each value separately. That would be more than
> perfect.
>
> Thank you again!
>
> Regards, Wernke


If anybody is still interested in a better  and type-safe solution,
here it is, taking the above example:

ScriptableObject.defineProperty(prc, "Result", new NativeJavaClass
(prc, Procedure.Result.class), flags);

This allows a script to use the values of the Result enum in a type-
safe manner, i.e.

Result.OK and Result.ERROR

Regards, Wernke
_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino

Reply via email to