lua-users home
lua-l archive

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


Am 19.06.2014 23:26 schröbte Aaron Faanes:
I'm actually surprised and sort-of sad that luaL_requiref doesn't check
package.loaded. I had assumed it did, but I wrote out your test and it
confirmed what you said.

The safest solution IMO would be to write your luaopen_* to be resilient
(or a no-op) in the face of redundant requires. If all they do are
assignments to the global environment, this level of safety is easy to
attain. I'd be curious to hear if this wasn't the case for you. :)

I started to throw an error in my C bindings if `luaL_newmetatable` detects that a metatable with a given name already exists. I could just register my metatables again, but then I won't notice if I accidentally overwrite another module's metatable ...


I guess I treat require's "load only once" ultimately as a very strong hint
rather than as a fundamental guarantee - it's just too easy for the
programmer to break. In my experience, the most common "require didn't
honor its contract" problems are when a loaded file overwrites one of its
own stateful globals (causing some weird fissure in what I assumed were
immutable globals), but this is really my own fault for not treating global
state with more suspicion.

-- Aaron

Philipp