From: David Flanagan Date: 2007-10-13T08:26:29+09:00 Subject: Re: Wrapped loads and Module::nesting Has anyone had a chance to take a look at this patch and my rationale (below) for it? I think it would be nice for wrapped loads to be able to export the anonymous modules they are loaded into, but the ability to do this has broken in 1.9... David David Flanagan wrote: > The patch below fixes Module.nesting so that it behaves in 1.9 like it > does in 1.8. Note, however, that I don't really understand what I'm > doing in eval.c, but I guessed and it seems to work. Of course, there > may be some good reason for this change in Module.nesting that I'm > unaware of... > > David > > $ diff -u eval.c~ eval.c > --- eval.c~ 2007-08-29 22:59:14.000000000 -0700 > +++ eval.c 2007-09-13 15:01:41.000000000 -0700 > @@ -361,6 +361,13 @@ > } > cref = cref->nd_next; > } > + > + if (RARRAY(ary)->len == 0) { > + rb_thread_t *th = GET_THREAD(); > + if (th->top_wrapper) > + rb_ary_push(ary, th->top_wrapper); > + } > + > return ary; > } > > > David Flanagan wrote: >> When I call load with a second argument of true, the file is loaded >> into an anonymous module. In Ruby 1.8 I can use Module::nesting[0] to >> obtain this anonymous module. It no longer works in Ruby 1.9. I >> think Ruby 1.9 ought to work like 1.8 because then a wrapped module >> can pass itself back to the loader with code like this: >> >> # This code is in the file to be loaded wrapped >> if Module.nesting.size > 0 # If we're loaded into a wrapper module >> $wrapper = Module.nesting[0] # Pass the module back to the loader >> end >> >> module Foo >> class Bar >> def b >> "Foo::Bar.b" >> end >> end >> end >> >> # This code is the file that does the wrapped load: >> # Use include to safely allow the wrapped file to define new modules >> # without modifying existing ones >> load "wrapmod.rb", true >> include $wrapper if $wrapper >> puts Foo::Bar.new.b >> >> Doing something like this is the only way that using wrapped loads >> makes sense to me. If we can't do this in 1.9 then wrapped loads no >> longer seem useful. >> >> Are there security implications I'm missing here? >> >> David >> > >