tips/examples/docs for invoking Ringo CommonJS modules directly from java?

97 views
Skip to first unread message

Darren Cruse

unread,
May 12, 2011, 6:51:53 PM5/12/11
to RingoJS
We have a need to invoke CommonJS javascript module code we've written
not from the JSGI servlet or from the Ringo shell, but simply from an
existing java program.

I notice the web site says "RingoJS is geared to web applications, but
can be used for command line or even GUI applications as well."

I'm guessing the "or even GUI applications" part is similar to what
I'm asking.

Are there docs or examples explaining how to invoke Ringo from java?

FWIW I'd seen reference to a "RhinoEngine" class in the code, and I'd
mistakenly assumed that was a Mozilla Rhino class - i.e. that I could/
would invoke the Rhino engine directly from java using the Rhino apis
or even the generic java JSR223 scripting api.

But I now realize I'd been naive about the amount of java code
involved in Ringo over and above Rhino. I assume largely to support
the CommonJS standard...

So now I'm guessing those standard apis that work for invoking Rhino
from java are not what I can/should use for Ringo.

But I've yet to find any docs or examples explaining such an
"embedded" usage of Ringo...

At the moment I'm thinking that a modified form of the Rhingo
JsgiServlet class might be what I'm after... where I know it causes
the jsgi.js script to be invoked with a request and I'm thinking I
would just do something similar in a non-servlet class invoking my own
script instead...

Hannes Wallnoefer

unread,
May 13, 2011, 10:50:33 AM5/13/11
to rin...@googlegroups.com
I think the best examples of how to invoke Ringo from Java are in
org.ringojs.jsgi.JsgiServlet and org.ringojs.tools.RingoShell. Both
instantiate a RhinoEngine (the name is maybe a bit confusing - maybe
it should be "RingoEngine"?) and then invoke/execute code in it.

JsgiServlet uses RhinoEngine.invoke(moduleid, functionname, args...)
to load a module and invoke an exported function. RingoShell just
compiles the input and executes it on a scope it got from the engine,
but the printResult() and printError() methods in RingoShell git
master also uses invoke to render the result.

I hope this helps you get started, let me know if you need more help.

Hannes

2011/5/13 Darren Cruse <darren...@gmail.com>:

> --
> You received this message because you are subscribed to the Google Groups "RingoJS" group.
> To post to this group, send email to rin...@googlegroups.com.
> To unsubscribe from this group, send email to ringojs+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/ringojs?hl=en.
>
>

Darren Cruse

unread,
May 13, 2011, 10:57:18 AM5/13/11
to RingoJS
Related question: I've since stumbled on this link mentioning
CommonJS support in the newest/upcoming version on Rhino:
https://developer.mozilla.org/en/New_in_Rhino_1.7R3#CommonJS.c2.a0module_support

Does anybody know:
a. Would CommonJS module code written under Ringo, that doesn't use
RingoJS specifics i.e. Request/Response, etc., run under this Rhino
CommonJS support?

b. Is it likely for Ringo to switch over to the built-in Rhino
CommonJS support in the future?

c. Is either Ringo or Rhino's CommonJS support usable via the JSR223
scripting api?

Thanks again (hope somebody knows/responds - have a deadline dependent
on the answers :),

Darren

Darren Cruse

unread,
May 13, 2011, 10:59:35 AM5/13/11
to RingoJS
Thanks Hannes and my apologies I must have been typing my follow up
question as you were typing this answer to my first one!

I'd still be interested in your thoughts about the Rhino 1.7R3
CommonJS support though...

Hannes Wallnoefer

unread,
May 13, 2011, 11:06:26 AM5/13/11
to rin...@googlegroups.com
2011/5/13 Darren Cruse <darren...@gmail.com>:

> Related question:  I've since stumbled on this link mentioning
> CommonJS support in the newest/upcoming version on Rhino:
>    https://developer.mozilla.org/en/New_in_Rhino_1.7R3#CommonJS.c2.a0module_support
>
> Does anybody know:
> a.  Would CommonJS module code written under Ringo, that doesn't use
> RingoJS specifics i.e. Request/Response, etc., run under this Rhino
> CommonJS support?

Yes, implementations should be by and large compatible.

> b.  Is it likely for Ringo to switch over to the built-in Rhino
> CommonJS support in the future?

Not in the near future, as Ringo's module loader has a few features
the one in Rhino lacks (and vice versa).

> c.  Is either Ringo or Rhino's CommonJS support usable via the JSR223
> scripting api?

I'm not sure, but I'd guess no. Unfortunately we blew the deadline to
get Rhino 1.7R3 ready in time to be included in JDK 7 which is now
close to release. Even if it was, CommonJS module support has to be
activated by the embedding code or the Rhino shell, and I'm not sure
this is possible with JSR223 (except maybe from JS code).

Hannes

> Thanks again (hope somebody knows/responds - have a deadline dependent
> on the answers :),
>
> Darren
>

Darren Cruse

unread,
May 15, 2011, 7:36:34 AM5/15/11
to RingoJS
Thanks guys just a quick note that it did work out I created a
"RhinoModuleBridge" class from looking at the JsgiServlet class and
how it uses the RhinoEngine class. It pulls the configuration the
servlet gets from web.xml from spring instead, and provides
"invokeFunction" and "invokeMethod" function for java to call through
to the module's exported JavaScript functions.

I chose those function names to imitate the JSR223 Invocable interface
though I'm not using that for real of course.

Though I thought the "invokeMethod" might be a way to expose CommonJS
modules through the standard, as it does let you pass a java Object
representing an "instance of a class" you're invoking the method on.

I.e. With the standard being language agnostic, maybe treating a
CommonJS "module" as "object" would work?

Alternatively (or in addition?) I was thinking there's other parts of
the API where you simply pass a function name, and I was wondering if
by convention you could pass "module.function" e.g. "ringo/
jsgi.handleRequest" as function name to the standard Apis?

But I'm happy it's working standard or not. I was just thinking
support for the standard might help adoption with the broader java
community(?)

Reply all
Reply to author
Forward
0 new messages