Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
CommonJS Modules in Rhino
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  Messages 26 - 33 of 33 - Collapse all  -  Translate all to Translated (View all originals) < Older 
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Martin Blom  
View profile  
 More options Feb 10 2010, 4:28 pm
Newsgroups: mozilla.dev.tech.js-engine.rhino
From: Martin Blom <mar...@blom.org>
Date: Wed, 10 Feb 2010 22:28:34 +0100
Local: Wed, Feb 10 2010 4:28 pm
Subject: Re: CommonJS Modules in Rhino

On 31/01/2010 20:15, Attila Szegedi wrote:

> Hi all,

> I just put out the second implementation patch at<https://bugzilla.mozilla.org/show_bug.cgi?id=540724>, against current CVS HEAD. Check it out if you're interested. I have worked on this for I most of my free time I can have for coding in the last 12 days, and have arrived at a much better design than what was in the first attempt. I attached a comment to the Bugzilla issue describing the design decisions I took. It all feels round to me at the moment. I took care to document all classes and interfaces in great detail. I'll proceed with writing tests for it, but if you don't mind reviewing and trying bleeding-edge stuff, go for it.

Hi Attila and list,

I've started playing with the patch in the context of my ESXX project
and while I haven't had time to finish yet I have a couple of questions:

1) I already have a path array in ESXX, which also happens to be a JS
array that is searched from the front. However, having two path objects
is weired, so I'm wondering if we could either perhaps replace the
'secure' argument with a 'paths' argument (Scriptable) so I can inject
mine, or add a method that returns Require's paths object so I can use
that one instead?

2) I have a special property named 'esxx.location' that contains the URI
of the currently executing file. For this to work I need to know when
the Script is executed. It could be solved by making
Require.executeModuleScript protected, or better, adding an exec method
to ModuleScript.

Comments?

--
---- Martin Blom --------------------------- mar...@blom.org ----
Eccl 1:18                                 http://martin.blom.org/


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Martin Blom  
View profile  
 More options Feb 10 2010, 4:45 pm
Newsgroups: mozilla.dev.tech.js-engine.rhino
From: Martin Blom <mar...@blom.org>
Date: Wed, 10 Feb 2010 22:45:27 +0100
Local: Wed, Feb 10 2010 4:45 pm
Subject: Re: CommonJS Modules in Rhino

Martin Blom skrev 2010-02-10 22.28:

> 1) I already have a path array in ESXX, which also happens to be a JS
> array that is searched from the front. However, having two path objects
> is weired, so I'm wondering if we could either perhaps replace the
> 'secure' argument with a 'paths' argument (Scriptable) so I can inject
> mine, or add a method that returns Require's paths object so I can use
> that one instead?

(I just realized that my array is not an array of string but URI
objects, so I think I need to provide my own Scriptable that implements
the correct view of the path array.)

--
---- Martin Blom --------------------------- mar...@blom.org ----
Eccl 1:18                                 http://martin.blom.org/


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Attila Szegedi  
View profile  
 More options Feb 10 2010, 6:12 pm
Newsgroups: mozilla.dev.tech.js-engine.rhino
From: Attila Szegedi <szege...@gmail.com>
Date: Thu, 11 Feb 2010 00:12:14 +0100
Local: Wed, Feb 10 2010 6:12 pm
Subject: Re: CommonJS Modules in Rhino
Yeah, returning null in ScriptProviders and ScriptSourceProviders allows them to be composed and fall back on each other without exceptions being thrown and caught. It's only when it'd surface as nonexistent from require() that the error is thrown.

Attila.

On 2010.02.10., at 20:12, Jarosław Pałka wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Attila Szegedi  
View profile  
 More options Feb 10 2010, 6:18 pm
Newsgroups: mozilla.dev.tech.js-engine.rhino
From: Attila Szegedi <szege...@gmail.com>
Date: Thu, 11 Feb 2010 00:18:44 +0100
Local: Wed, Feb 10 2010 6:18 pm
Subject: Re: CommonJS Modules in Rhino

On 2010.02.10., at 19:38, Jarosław Pałka wrote:

> Attila,

> I applied third version of your patch. I have two comments:
> - I don't see where you execute interoperablejs tests, I see them in source code but it looks like they are not run as part of junit-all target Maybe I missed something :(

No, I think it's me who missed something - in testsrc/build.xml, just under:

    <copy todir="${coverage.classes.dir}">
      <fileset dir="src" excludes="**/*.java"/>
    </copy>

we need to add this too:

    <copy todir="${coverage.classes.dir}">
      <fileset dir="testsrc" excludes="**/*.java"/>
    </copy>

I have to admit that I run my unit tests from Eclipse IDE, so didn't run into this problem...

Attila.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Attila Szegedi  
View profile  
 More options Feb 12 2010, 5:02 pm
Newsgroups: mozilla.dev.tech.js-engine.rhino
From: Attila Szegedi <szege...@gmail.com>
Date: Fri, 12 Feb 2010 23:02:03 +0100
Local: Fri, Feb 12 2010 5:02 pm
Subject: Re: CommonJS Modules in Rhino
On 2010.02.10., at 22:28, Martin Blom wrote:

> On 31/01/2010 20:15, Attila Szegedi wrote:
>> Hi all,

>> I just put out the second implementation patch at<https://bugzilla.mozilla.org/show_bug.cgi?id=540724>, against current CVS HEAD. Check it out if you're interested. I have worked on this for I most of my free time I can have for coding in the last 12 days, and have arrived at a much better design than what was in the first attempt. I attached a comment to the Bugzilla issue describing the design decisions I took. It all feels round to me at the moment. I took care to document all classes and interfaces in great detail. I'll proceed with writing tests for it, but if you don't mind reviewing and trying bleeding-edge stuff, go for it.

> Hi Attila and list,

> I've started playing with the patch in the context of my ESXX project and while I haven't had time to finish yet I have a couple of questions:

> 1) I already have a path array in ESXX, which also happens to be a JS array that is searched from the front. However, having two path objects is weired, so I'm wondering if we could either perhaps replace the 'secure' argument with a 'paths' argument (Scriptable) so I can inject mine, or add a method that returns Require's paths object so I can use that one instead?

You can easily obtain the paths of a require instance. Remember, a require() function is just a JS object, so you can do:

Require require = ...;
NativeArray paths = ScriptableObject.getTypedProperty(require, "paths", NativeArray.class);

> 2) I have a special property named 'esxx.location' that contains the URI of the currently executing file. For this to work I need to know when the Script is executed. It could be solved by making Require.executeModuleScript protected, or better, adding an exec method to ModuleScript.

I see - you need a post-exec hook. Okay, that's a reasonable thing. I actually at one point in time had ModuleScript.exec(), but I scrapped it. The reason is in the way SoftCachingModuleScriptProvider is implemented; if you look into it, ModuleScript instances in it are not even preserved, but they are deconstructed/reconstructed into/from the ScriptReference as needed. This is done so that there's no strong reference Script objects being held, so unused scripts can indeed get unloaded. (functions have pointers to their parent scripts, so as long as there's any function from the script reachable in any of the currently running JS program instance, they won't get GCed, otherwise they can be). For the same reason, you shouldn't create such post-exec hook by creating a wrapper around Script that itself implements Script with pre/post processing - you'd screw up soft caching as you can't sneak in a strong reference to your wrapper Script object into the Script object that Rhino compiles for you, which'd be required to ensure soft cache doesn't load multiple reachable copies of scripts (& functions).

So, an explicit way to register a post-exec hook is the cleanest solution - I'll look into where would this fit best.

Thank you very much for taking time to look into this and make suggestions.

Attila.

--
twitter: http://twitter.com/szegedi
weblog: http://constc.blogspot.com
home: http://www.szegedi.org


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Attila Szegedi  
View profile  
 More options Feb 15 2010, 2:45 pm
Newsgroups: mozilla.dev.tech.js-engine.rhino
From: Attila Szegedi <szege...@gmail.com>
Date: Mon, 15 Feb 2010 20:45:55 +0100
Local: Mon, Feb 15 2010 2:45 pm
Subject: Re: CommonJS Modules in Rhino
Folks,

Given that the current implementation state seems sufficiently stable, I committed it to CVS HEAD and will continue development there.

The committed version features following changes compared to last patch:

- Require constructor can now accept a pre-exec and a post-exec Script; these are executed before and after any module script, respectively. This is in response to Martin Blom's requirement for a post-exec hook. Martin, can you confirm this is something that suits your needs?

- RequireBuilder has been moved to the base package, seeing how Require constructor now takes quite a lot of arguments (which, for all apparent unwieldiness, is how I like it, as I love immutable objects when possible). I expect people will prefer to use RequireBuilder instead of invoking the Require constructor directly, although of course, you can still do that just as well.

- Renamed the org.mozilla.javascript.commonjs.module.support package to *.provider, as all the classes in it actually deal with module provider implementation (now that RequireBuilder has been moved out of it).

- added a MultiModuleScriptProvider, for easy composition of multiple module source providers into a single one. This is meant to open the door for modular implementation of advanced module provision methods, above and beyond loading them from .js source files. I.e. Jarosław's idea of annotations-based Java-defined modules can be implemented as a separate provider, and composed with a more conventional provider into a single composite using the MultiModuleScriptProvider. Jarosław, care to provide the appropriate ModuleScriptProvider implementation?

Next on the roadmap is using the stuff available so far to add require() to Rhino shell. For compatibility, it'll probably require a new command-line switch and be off by default. I'll outline the further ideas soon.

Attila.

--
twitter: http://twitter.com/szegedi
weblog: http://constc.blogspot.com
home: http://www.szegedi.org

On 2010.02.10., at 12:43, Attila Szegedi wrote:

...

read more »


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Hannes Wallnoefer  
View profile  
 More options Feb 19 2010, 5:56 am
Newsgroups: mozilla.dev.tech.js-engine.rhino
From: Hannes Wallnoefer <hann...@gmail.com>
Date: Fri, 19 Feb 2010 02:56:34 -0800 (PST)
Local: Fri, Feb 19 2010 5:56 am
Subject: Re: CommonJS Modules in Rhino
Sorry Attila, I forgot I still owe you an answer...

On Feb 3, 6:39 pm, Attila Szegedi <szege...@gmail.com> wrote:

> On 2010.02.03., at 17:52, Hannes Wallnoefer wrote:

> > Other than that, I really like your implementation. Helma NG does a
> > few things differently, so it's not likely we'll replace our
> > implementation anytime soon, but I think I'll take some inspiration
> > from your code for things it does better than ours.

> :-)

> Is there a way to reconcile the differences? I tried to follow Modules/1.1 to the letter... Also, maybe your implementation is doing something better than mine does, in which case it wouldn't be bad if I'd get some inspiration the other way :-)

I put a lot of work and thought into the design of Helma NG (which
we're currently relaunching as RingoJS). As a result, it is a bit
farther away from the "classical" JS shell, but there are some very
tangible benefits.

For example, module scopes are top level scopes in HNG/RingoJS. This
gives as complete module isolation even with a shared "classical"
global scope. Also, the require() function in Helma NG  is shared
among multiple contexts and threads, and we're using a threadlocal to
keep maps of loaded modules per cx/thread.

This gives us a lot of flexibility. For example, while modules are
instantiated per context/thread by default, modules can opt to have
one instance shared among all threads simply by setting a flag in
their module meta-object, giving us optionally multithreaded modules.
There's also completely transparent reloading of modules that takes
care of module dependencies for shared modules.

To sum it up, HNG/RingoJS aims a bit farther away from the typical JS
shell/runtime. That's why I think it should remain a dedicated project
(I've also thought about proposing our module loader for inclusion in
Rhino at certain times). But I also think your implementation makes
perfect sense to be included in Rhino for more "conservative" CommonJS
runtimes.

Hannes


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Martin Blom  
View profile  
 More options Mar 6 2010, 9:04 am
Newsgroups: mozilla.dev.tech.js-engine.rhino
From: Martin Blom <mar...@blom.org>
Date: Sat, 06 Mar 2010 15:04:56 +0100
Local: Sat, Mar 6 2010 9:04 am
Subject: Re: CommonJS Modules in Rhino

On 12/02/2010 23:02, Attila Szegedi wrote:

Hi again,

Sorry for taking so long.

>> I've started playing with the patch in the context of my ESXX project and while I haven't had time to finish yet I have a couple of questions:

>> 1) I already have a path array in ESXX, which also happens to be a JS array that is searched from the front. However, having two path objects is weired, so I'm wondering if we could either perhaps replace the 'secure' argument with a 'paths' argument (Scriptable) so I can inject mine, or add a method that returns Require's paths object so I can use that one instead?

> You can easily obtain the paths of a require instance. Remember, a require() function is just a JS object, so you can do:

> Require require = ...;
> NativeArray paths = ScriptableObject.getTypedProperty(require, "paths", NativeArray.class);

Right. The problem is that it turned out that my existing path array is
not an array of Strings but an array of (my custom) URI objects, so I
would need to provide a custom JS object that automatically presents a
string "view" but at the same time allow modifications. However, even if
I was to putProperty() that object onto Require(), the Require code will
still use its internal reference.

Anyway, I ended up simply require.delete("paths") for now. From what I
see, the paths property is optional according to CommonJS.

>> 2) I have a special property named 'esxx.location' that contains the URI of the currently executing file. For this to work I need to know when the Script is executed. It could be solved by making Require.executeModuleScript protected, or better, adding an exec method to ModuleScript.

> I see - you need a post-exec hook. Okay, that's a reasonable thing. I actually at one point in time had ModuleScript.exec(), but I scrapped it. The reason is in the way SoftCachingModuleScriptProvider is implemented; if you look into it, ModuleScript instances in it are not even preserved, but they are deconstructed/reconstructed into/from the ScriptReference as needed. This is done so that there's no strong reference Script objects being held, so unused scripts can indeed get unloaded. (functions have pointers to their parent scripts, so as long as there's any function from the script reachable in any of the currently running JS program instance, they won't get GCed, otherwise they can be). For the same reason, you shouldn't create such post-exec hook by creating a wrapper around Script that itself implements Script with pre/post processing - you'd screw up soft caching as you can't sneak in a strong reference to your wrapper Script object into the Script object that Rhino compiles for you, which'd be required to ensure soft cache doesn't load multiple reachable copies of scripts (&  functions).

> So, an explicit way to register a post-exec hook is the cleanest solution - I'll look into where would this fit best.

Unfortunately, that didn't really help much, since

   1. The code I'm interested in running is Java, not JS
   2. No reference to the ModuleScript in question is passed to the
      pre/post hooks
   3. I would need hold state using a custom stack in order to restore
      the location in the post-hook

I ended up with the following (somewhat hackish) ModuleScript
implementation instead:

   public class ESXXScript
     extends ModuleScript
     implements Script {

     public ESXXScript(Script script, URI uri) {
       super(script, uri.toString());
       this.uri = uri;
     }

     @Override public Script getScript() {
       return this;
     }

     @Override public Object exec(Context cx, Scriptable scope) {
       URI old_location = setCurrentLocation(uri);

       try {
         return super.getScript().exec(cx, scope);
       }
       finally {
         setCurrentLocation(old_location);
       }
     }

     private URI uri;
   }

However, I still think it would be better if ModuleScript extended
Script and used public Object exec(Context cx, Scriptable scope) instead
of public Script getScript(). I can't see how that would prevent
soft-references from working as intended, but anyway, ESXXScript works
for me and I'm happy with it as it is.

More importantly, however, is the installation of the main module. I'm
setting up the application scope using custom code and not loading it as
a module, so Require.requireMain() is not applicable  here.
Require.install() only installs the require() function, it does not set
up a main module.

I modified Require() to include an installMain() method. Patch attached.
Would you consider to include it? (The patch is against your 3rd
Bugzilla patch; I'm on the Rhino 7r2 branch.)

--
---- Martin Blom --------------------------- mar...@blom.org ----
Eccl 1:18                                 http://martin.blom.org/

  commonjs-lcs.patch
3K Download

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages < Older 
« Back to Discussions « Newer topic     Older topic »