annotate lua/luadyncall/src/ldynload.c @ 54:918dab7a6606

- added callback support (comes with some bigger refactoring) - allow CPython's Py{CObject,Capsule} to be used as 'p'ointers
author Tassilo Philipp
date Tue, 02 Feb 2021 20:42:02 +0100
parents 0cfcc391201f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
1 #include "lua.h"
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
2 #include "lauxlib.h"
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
3 #include "dynload.h"
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
4
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
5 int lua_dlLoadLibrary(lua_State *L)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
6 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
7 const char* libpath;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
8 if ( lua_isnoneornil(L, 1) )
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
9 libpath = NULL;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
10 else
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
11 libpath = lua_tostring(L, 1);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
12 DLLib* pLib;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
13 pLib = dlLoadLibrary(libpath);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
14 if (pLib == NULL) return 0;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
15 lua_pushlightuserdata(L, pLib);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
16 return 1;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
17 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
18
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
19 int lua_dlFreeLibrary(lua_State *L)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
20 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
21 DLLib* pLib = (DLLib*) lua_touserdata(L, 1);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
22 dlFreeLibrary(pLib);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
23 return 0;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
24 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
25
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
26 int lua_dlFindSymbol(lua_State *L)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
27 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
28 DLLib* pLib = (void*) lua_touserdata(L, 1);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
29 const char* pSymbolName = (const char*) lua_tostring(L, 2);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
30 void* addr = dlFindSymbol(pLib, pSymbolName);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
31 if (addr == NULL) return 0;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
32 lua_pushlightuserdata(L, addr);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
33 return 1;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
34 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
35
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
36 static const struct luaL_Reg luareg_dynload[] =
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
37 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
38 { "dlLoadLibrary", lua_dlLoadLibrary },
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
39 { "dlFreeLibrary", lua_dlFreeLibrary },
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
40 { "dlFindSymbol", lua_dlFindSymbol },
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
41 { NULL, NULL }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
42 };
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
43
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
44 // LUALIB_API
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
45 int luaopen_ldynload(lua_State *L)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
46 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
47 lua_createtable(L, 0, 0);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
48 luaL_register(L, NULL, luareg_dynload);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
49 return 1;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
50 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
51