lua-users home
lua-l archive

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

Dustin Juliano wrote:
1) Why invoke the interpreter when you can directly call these function pointers to initialize the library, at will? 2) Why are the luaopen_* (luaopen_base, luaopen_package, ..., luaopen_debug) all Lua C Library functions? That is, why do they not just perform their intended purpose without having to expose themselves to Lua at all?

I'd say the main reason is is that they can push arbitrary number of results on to the stack. Ie base pushes 2, and the rest 1. Future packages may push 0 or 30. Did you notice that your modified function messes the stack? :). And should Lua ever have more then 20 packages, or a package which pushes lots of results, it'll cause a Lua stack overlua. The call is just proper stack handling - you specify how many results you want, you know
where the stack is after the call.

There may be another reason, but that's the first that comes to mind.

Anyway, I'm curious why you're worried about the extra function calls? :)

If via some scenario I can't imagine it's become a bottleneck, you could replace the pushcfunction, pushstring, and call with a regular c call and a lua_settop(L, 0), at the
cost of some more ambiguity and less consistency with Lua :).

- Alex