lua-users home
lua-l archive

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


Well, here I am again, to ask for some guidance regarding lua extensions.

This is the Way of Things, as I understand it:

- When you write a lua extension (ie. shared library dynamically
loaded in lua) you can do various things and return values.

- However, you when you do this you need to be careful. If you use the
functions inside lauxlib.h, you need to make sure the INSTANCE of the
lua runtime that you link against with the shared library is the SAME
INSTANCE as the one running the host application.

- You can achieve this by either:

a) Exposing the lua symbols from the host application binary and
linking your shared library specifically against that binary.
Downside: doesn't seem to be possible under windows, links shared
library against specific binary.

b) Linking against the same lua shared library / DLL that the host
application is linked against.
Downside: host application needs to load lua as shared library.
virtually nothing (not even the default lua binary) does this.

- That said, if you have multiple bound copies of the lua runtime it
will only mysteriously crash with obscure meaningless error messages
if you attempt to modify the lua state; it's totally possible to do
something like:

a -> liba + luaruntime
b -> libb + luaruntime
host: loads a, loads b, runs a
a: runs b:call() where call() is c function from b that does not
modify any lua state (although potentially reads lua state).

- ...but, doing this is really dodgy, and its only luck that it works
fine on pretty much all platforms.

So basically, a cross platform extension which transforms the lua
state in ANY WAY seems virtually impossible to write.

It can return values; but not change anything.

The only situation (it seems) that this is possible is in the
astonishingly rare case of lua being loaded as a dynamic library.

So, questions:

1) Am I correct? Or is there some other way of doing this?

2) Why is this so hard to do? Is it really something that people don't
tend to do? Every single extension I've written so far for lua I've
hit the wall when I realize I want to do this, and end up having to
write a lua wrapper around my c library to handle updating the lua
state before passing back from library calls.

Cheers,
Doug.