loadlib.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. ** $Id: loadlib.c,v 1.7 2004/10/07 17:27:20 roberto Exp roberto $
  3. ** Dynamic library loader for Lua
  4. ** See Copyright Notice in lua.h
  5. *
  6. * This Lua library exports a single function, called loadlib, which
  7. * is called from Lua as loadlib(lib,init), where lib is the full
  8. * name of the library to be loaded (including the complete path) and
  9. * init is the name of a function to be called after the library is
  10. * loaded. Typically, this function will register other functions, thus
  11. * making the complete library available to Lua. The init function is
  12. * *not* automatically called by loadlib. Instead, loadlib returns the
  13. * init function as a Lua function that the client can call when it
  14. * thinks is appropriate. In the case of errors, loadlib returns nil and
  15. * two strings describing the error. The first string is supplied by
  16. * the operating system; it should be informative and useful for error
  17. * messages. The second string is "open", "init", or "absent" to identify
  18. * the error and is meant to be used for making decisions without having
  19. * to look into the first string (whose format is system-dependent).
  20. * This module contains an implementation of loadlib for Unix systems
  21. * that have dlfcn, an implementation for Darwin (Mac OS X), an
  22. * implementation for Windows, and a stub for other systems. See
  23. * the list at the end of this file for some links to available
  24. * implementations of dlfcn and interfaces to other native dynamic
  25. * loaders on top of which loadlib could be implemented.
  26. */
  27. #define loadlib_c
  28. #define LUA_LIB
  29. #include "lua.h"
  30. #include "lauxlib.h"
  31. #include "lualib.h"
  32. static void registerlib (lua_State *L, const void *lib);
  33. #if defined(USE_DLOPEN)
  34. /*
  35. * This is an implementation of loadlib based on the dlfcn interface.
  36. * The dlfcn interface is available in Linux, SunOS, Solaris, IRIX, FreeBSD,
  37. * NetBSD, AIX 4.2, HPUX 11, and probably most other Unix flavors, at least
  38. * as an emulation layer on top of native functions.
  39. */
  40. #include <dlfcn.h>
  41. #define freelib dlclose
  42. static int loadlib(lua_State *L)
  43. {
  44. const char *path=luaL_checkstring(L,1);
  45. const char *init=luaL_checkstring(L,2);
  46. void *lib=dlopen(path,RTLD_NOW);
  47. if (lib!=NULL)
  48. {
  49. lua_CFunction f=(lua_CFunction) dlsym(lib,init);
  50. if (f!=NULL)
  51. {
  52. registerlib(L, lib);
  53. lua_pushcfunction(L,f);
  54. return 1;
  55. }
  56. }
  57. /* else return appropriate error messages */
  58. lua_pushnil(L);
  59. lua_pushstring(L,dlerror());
  60. lua_pushstring(L,(lib!=NULL) ? "init" : "open");
  61. if (lib!=NULL) dlclose(lib);
  62. return 3;
  63. }
  64. #elif defined(USE_DLL)
  65. /*
  66. * This is an implementation of loadlib for Windows using native functions.
  67. */
  68. #include <windows.h>
  69. #define freelib(lib) FreeLibrary((HINSTANCE)lib)
  70. static void pusherror(lua_State *L)
  71. {
  72. int error=GetLastError();
  73. char buffer[128];
  74. if (FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,
  75. 0, error, 0, buffer, sizeof(buffer), 0))
  76. lua_pushstring(L,buffer);
  77. else
  78. lua_pushfstring(L,"system error %d\n",error);
  79. }
  80. static int loadlib(lua_State *L)
  81. {
  82. const char *path=luaL_checkstring(L,1);
  83. const char *init=luaL_checkstring(L,2);
  84. HINSTANCE lib=LoadLibrary(path);
  85. if (lib!=NULL)
  86. {
  87. lua_CFunction f=(lua_CFunction) GetProcAddress(lib,init);
  88. if (f!=NULL)
  89. {
  90. registerlib(L, lib);
  91. lua_pushcfunction(L,f);
  92. return 1;
  93. }
  94. }
  95. lua_pushnil(L);
  96. pusherror(L);
  97. lua_pushstring(L,(lib!=NULL) ? "init" : "open");
  98. if (lib!=NULL) FreeLibrary(lib);
  99. return 3;
  100. }
  101. /* Native Mac OS X / Darwin Implementation */
  102. #elif defined(USE_DYLD)
  103. #include <mach-o/dyld.h>
  104. /* Mach cannot unload images (?) */
  105. #define freelib(lib) ((void)lib)
  106. static void pusherror (lua_State *L)
  107. {
  108. const char *err_str;
  109. const char *err_file;
  110. NSLinkEditErrors err;
  111. int err_num;
  112. NSLinkEditError(&err, &err_num, &err_file, &err_str);
  113. lua_pushstring(L, err_str);
  114. }
  115. static int loadlib (lua_State *L) {
  116. const char *path=luaL_checkstring(L,1);
  117. const char *init=luaL_checkstring(L,2);
  118. const struct mach_header *lib;
  119. /* this would be a rare case, but prevents crashing if it happens */
  120. if(!_dyld_present()) {
  121. lua_pushnil(L);
  122. lua_pushstring(L,"dyld not present.");
  123. lua_pushstring(L,"absent");
  124. return 3;
  125. }
  126. lib = NSAddImage(path, NSADDIMAGE_OPTION_RETURN_ON_ERROR);
  127. if(lib != NULL) {
  128. NSSymbol init_sym = NSLookupSymbolInImage(lib, init,
  129. NSLOOKUPSYMBOLINIMAGE_OPTION_BIND |
  130. NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
  131. if(init_sym != NULL) {
  132. lua_CFunction f = (lua_CFunction)NSAddressOfSymbol(init_sym);
  133. registerlib(L, lib);
  134. lua_pushcfunction(L,f);
  135. return 1;
  136. }
  137. }
  138. /* else an error ocurred */
  139. lua_pushnil(L);
  140. pusherror(L);
  141. lua_pushstring(L, (lib != NULL) ? "init" : "open");
  142. /* Can't unload image */
  143. return 3;
  144. }
  145. #else
  146. /* Fallback for other systems */
  147. #define freelib(lib) ((void)lib)
  148. static int loadlib(lua_State *L)
  149. {
  150. registerlib(L, NULL); /* to avoid warnings */
  151. lua_pushnil(L);
  152. lua_pushliteral(L,"`loadlib' not supported");
  153. lua_pushliteral(L,"absent");
  154. return 3;
  155. }
  156. #endif
  157. /*
  158. ** common code for all implementations: put a library into the registry
  159. ** so that, when Lua closes its state, the library's __gc metamethod
  160. ** will be called to unload the library.
  161. */
  162. static void registerlib (lua_State *L, const void *lib)
  163. {
  164. const void **plib = (const void **)lua_newuserdata(L, sizeof(const void *));
  165. *plib = lib;
  166. luaL_getmetatable(L, "_LOADLIB");
  167. lua_setmetatable(L, -2);
  168. lua_pushboolean(L, 1);
  169. lua_settable(L, LUA_REGISTRYINDEX); /* registry[lib] = true */
  170. }
  171. /*
  172. ** __gc tag method: calls library's `freelib' function with the lib
  173. ** handle
  174. */
  175. static int gctm (lua_State *L)
  176. {
  177. void *lib = *(void **)luaL_checkudata(L, 1, "_LOADLIB");
  178. freelib(lib);
  179. return 0;
  180. }
  181. LUALIB_API int luaopen_loadlib (lua_State *L)
  182. {
  183. luaL_newmetatable(L, "_LOADLIB");
  184. lua_pushcfunction(L, gctm);
  185. lua_setfield(L, -2, "__gc");
  186. lua_register(L,"loadlib",loadlib);
  187. return 0;
  188. }
  189. /*
  190. * Here are some links to available implementations of dlfcn and
  191. * interfaces to other native dynamic loaders on top of which loadlib
  192. * could be implemented. Please send contributions and corrections to us.
  193. *
  194. * AIX
  195. * Starting with AIX 4.2, dlfcn is included in the base OS.
  196. * There is also an emulation package available.
  197. * http://www.faqs.org/faqs/aix-faq/part4/section-21.html
  198. *
  199. * HPUX
  200. * HPUX 11 has dlfcn. For HPUX 10 use shl_*.
  201. * http://www.geda.seul.org/mailinglist/geda-dev37/msg00094.html
  202. * http://www.stat.umn.edu/~luke/xls/projects/dlbasics/dlbasics.html
  203. *
  204. * Macintosh, Windows
  205. * http://www.stat.umn.edu/~luke/xls/projects/dlbasics/dlbasics.html
  206. *
  207. * GLIB has wrapper code for BeOS, OS2, Unix and Windows
  208. * http://cvs.gnome.org/lxr/source/glib/gmodule/
  209. *
  210. */