Example usage of require function in Rhino 1.7R3

1,461 views
Skip to first unread message

agrohe21

unread,
Jun 21, 2011, 9:18:29 AM6/21/11
to mozilla-rhino
I see that commonjs modules are now supported. I was expecting to see
some more information here.
http://www.mozilla.org/rhino/apidocs/

or an example here.
http://www.mozilla.org/rhino/examples.html

Is there somewhere else to find more information about how to invoke
this functionality?

felipe valdez

unread,
Jun 21, 2011, 12:15:08 PM6/21/11
to mozill...@googlegroups.com
I also vote for own documentation on commonjs but I guess maybe the commonjs site itself would be the reference?

2011/6/21 agrohe21 <agro...@gmail.com>

Attila Szegedi

unread,
Jun 21, 2011, 1:49:27 PM6/21/11
to mozill...@googlegroups.com
Hm… those docs seem to be outdated; I thought they'll be updated with the release - hey, Hannes and Norris, what'd be the way to update them?

In the meantime, you can download Rhino 1.7R3, and open javadoc/index.html in the distribution itself, then look around the org.mozilla.javascript.commonjs.module package.

In the meantime, this can hopefully get you started:

import java.net.*;
import java.util.*;
import org.mozilla.javascript.*;
import org.mozilla.javascript.commonjs.module.*;
import org.mozilla.javascript.commonjs.module.provider.*;

...
    // Do this once:
    List<URI> paths = Arrays.asList(
        new URI("file://path/to/modules"),
        new URI("file://another/path/to/modules")
        );
    ModuleSourceProvider sourceProvider = new UrlModuleSourceProvider(paths, null);
    ModuleScriptProvider scriptProvider = new SoftCachingModuleScriptProvider(sourceProvider);
    RequireBuilder builder = new RequireBuilder();
    builder.setModuleScriptProvider(scriptProvider);

    // Then, for every script you want to execute you'll do something like this:
    Context cx = Context.enter();
    try {
      Scriptable topLevelScope = cx.initStandardObjects();
      Require require = builder.createRequire(cx, topLevelScope);

      // Finally, if you want to execute a top-level module, do this:
      require.requireMain(cx, "mainModuleId");

      // Or, if you just want require() in your top level scope, but want to run
      // some script manually, then just do:
      require.install(topLevelScope);
      Script script = getMyScriptFromSomewhere();
      script.exec(cx, topLevelScope);
    } finally {
      Context.exit();
    }

Attila.

Tim Schaub

unread,
Jun 21, 2011, 12:18:44 PM6/21/11
to mozill...@googlegroups.com
I'm not sure if you are interested in accessing exports/running
modules from Java or just running them using the js.jar. If the
latter, I put up a simple example here:
http://tschaub.net/blog/2011/05/17/commonjs-modules-with-rhino.html

I'd be happy to contribute examples of either type. Perhaps the wiki
would be easier to keep up to date
(https://developer.mozilla.org/en/Rhino). I agree it would be nice to
have the apidocs updated online.

Tim

jg

unread,
Jun 27, 2011, 11:09:11 AM6/27/11
to mozilla-rhino
Hi,

I've tried to apply the code Attila sent, but I am still unable to run
my own (very basic) commonjs scripts. Could you show a complete
example (I mean with a fully implemented getMyScriptFromSomewhere();
method as I don't understand the Script interface in fact). Could
someone apply it to Tim's example, say, call "add" method from Java:

main.js
my_modules/
math.js
math/
add.js
subtract.js

First, here’s the add.js module:

exports.add = function(a, b) {
return a + b;
};

Thanks,
Best regards,
jg

Tim Schaub

unread,
Jun 27, 2011, 6:03:49 PM6/27/11
to mozill...@googlegroups.com
On 6/27/11 9:09 AM, jg wrote:
> Hi,
>
> I've tried to apply the code Attila sent, but I am still unable to run
> my own (very basic) commonjs scripts. Could you show a complete
> example (I mean with a fully implemented getMyScriptFromSomewhere();
> method as I don't understand the Script interface in fact). Could
> someone apply it to Tim's example, say, call "add" method from Java:
>
> main.js
> my_modules/
> math.js
> math/
> add.js
> subtract.js
>

See if this helps:
https://gist.github.com/1049963 (untested example)

Attila will have to comment on whether there is anything wrong with that
approach.

Tim

> First, here�s the add.js module:

jg

unread,
Jun 28, 2011, 3:58:12 AM6/28/11
to mozilla-rhino
Hi Tim,

thanks for responding and providing the gist. But, I'm puzzled, sorry!
Your simpler approach using Global is different from Attila's who
introduces newly created commonjs objects. What are their purpose so?

But let's first complete your solution. I'm missing the way to init
standards objects, like print that I had put in js scripts. Why isn't
global.initStandardObjects(cx, true) enough?

Then could you provide an example using the Script interface as used
by Attila?

Then then would it possible to put this first working example into
rhino example folder, beside to E4X folder?
Thanks for your help guys!
jg


On 28 juin, 00:03, Tim Schaub <tim.sch...@gmail.com> wrote:
> On 6/27/11 9:09 AM, jg wrote:
>
> > Hi,
>
> > I've tried to apply the code Attila sent, but I am still unable to run
> > my own (very basic) commonjs scripts. Could you show a complete
> > example (I mean with a fully implemented getMyScriptFromSomewhere();
> > method as I don't understand the Script interface in fact).  Could
> > someone apply it to Tim's example, say, call "add" method from Java:
>
> >      main.js
> >      my_modules/
> >          math.js
> >          math/
> >              add.js
> >              subtract.js
>
> See if this helps:https://gist.github.com/1049963(untested example)
>
> Attila will have to comment on whether there is anything wrong with that
> approach.
>
> Tim
>
>
>
>
>
>
>
> >      First, here s the add.js module:
Reply all
Reply to author
Forward
0 new messages