lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Fri, Aug 08, 2014 at 05:13:39PM -0700, William Ahern wrote:
> On Fri, Aug 08, 2014 at 05:29:18PM -0500, Mason Mackaman wrote:
> > So why isn?t everything in the standard libraries made local by default if
> > it?s always faster that way?
> 
> Other than the implementation for "print" (which gets the global
> "tostring"), the standard library is written entirely in C and doesn't do
> global lookups.

I think maybe I misunderstood the question. If the question was why doesn't
the compiler turn all references to standard library symbols into fast local
access, it's because the names are _not_ intended to be magical.

For example, maybe you want to sandbox code by hiding rawset. If the
compiler didn't do a run-time symbol lookup, then it would be much more
difficult to hide that symbol, and it would require additional, complex APIs
to do so.

Also, the following reason still applies as well.

> But one reason not to copy globals into locals is because then other code
> can't interpose that function.
> 
> For example, in my cqueues project I provide a yieldable implementation of
> tostring. Applications can choose to make this yieldable implementation
> global by doing
> 
> 	_G.tostring = require"cqueues.auxlib".tostring
> 
> However, any module loaded before that line executes and which copies the
> tostring function to a local will never see or use the yieldable version.
>