1 /*
2                                     __
3                                    / _|
4   __ _ _   _ _ __ ___  _ __ __ _  | |_ ___  ___ ___
5  / _` | | | | '__/ _ \| '__/ _` | |  _/ _ \/ __/ __|
6 | (_| | |_| | | | (_) | | | (_| | | || (_) \__ \__ \
7  \__,_|\__,_|_|  \___/|_|  \__,_| |_| \___/|___/___/
8 
9 Copyright (C) 1994-2020 Lua.org, PUC-Rio.
10 Copyright (C) 2018-2020 Aurora Free Open Source Software.
11 Copyright (C) 2018-2020 Luís Ferreira <luis@aurorafoss.org>
12 
13 This file is part of the Aurora Free Open Source Software. This
14 organization promote free and open source software that you can
15 redistribute and/or modify under the terms of the GNU Lesser General
16 Public License Version 3 as published by the Free Software Foundation or
17 (at your option) any later version approved by the Aurora Free Open Source
18 Software Organization. The license is available in the package root path
19 as 'LICENSE' file. Please review the following information to ensure the
20 GNU Lesser General Public License version 3 requirements will be met:
21 https://www.gnu.org/licenses/lgpl.html .
22 
23 Alternatively, this file may be used under the terms of the GNU General
24 Public License version 3 or later as published by the Free Software
25 Foundation. Please review the following information to ensure the GNU
26 General Public License requirements will be met:
27 http://www.gnu.org/licenses/gpl-3.0.html.
28 
29 NOTE: All products, services or anything associated to trademarks and
30 service marks used or referenced on this file are the property of their
31 respective companies/owners or its subsidiaries. Other names and brands
32 may be claimed as the property of others.
33 
34 For more info about intellectual property visit: aurorafoss.org or
35 directly send an email to: contact (at) aurorafoss.org .
36 */
37 
38 /++
39 Dynamic library loader for Lua bindings
40 
41 This file defines the dynamic loader for Lua library bindings.
42 
43 Authors: Luís Ferreira <luis@aurorafoss.org>
44 Copyright: All rights reserved, Aurora Free Open Source Software
45 License: GNU Lesser General Public License (Version 3, 29 June 2007)
46 Date: 2018-2019
47 +/
48 module riverd.lua.dynload;
49 
50 import riverd.loader;
51 
52 public import riverd.lua.dynfun;
53 
54 version(D_BetterC)
55 {
56 	/** Dynamic loader for lua library
57 	 *
58 	 * This function load the lua library and bind all dynamic function symbols
59 	 *
60 	 * Returns: Returns the loader handler
61 	 */
62 	@trusted
63 	void* dylib_load_lua() {
64 		version(Windows) void* handle = dylib_load("lua53.dll");
65 		else version(OSX) void* handle = dylib_load("liblua.5.3.dylib");
66 		else version(Posix) void* handle = dylib_load("liblua.so.5.3,liblua5.3.so");
67 
68 		if(handle is null) return null;
69 
70 		dylib_bindSymbol(handle, cast(void**)&lua_newstate, "lua_newstate");
71 		dylib_bindSymbol(handle, cast(void**)&lua_close, "lua_close");
72 		dylib_bindSymbol(handle, cast(void**)&lua_newthread, "lua_newthread");
73 		dylib_bindSymbol(handle, cast(void**)&lua_resetthread, "lua_resetthread");
74 		dylib_bindSymbol(handle, cast(void**)&lua_atpanic, "lua_atpanic");
75 		dylib_bindSymbol(handle, cast(void**)&lua_version, "lua_version");
76 		dylib_bindSymbol(handle, cast(void**)&lua_absindex, "lua_absindex");
77 		dylib_bindSymbol(handle, cast(void**)&lua_gettop, "lua_gettop");
78 		dylib_bindSymbol(handle, cast(void**)&lua_settop, "lua_settop");
79 		dylib_bindSymbol(handle, cast(void**)&lua_pushvalue, "lua_pushvalue");
80 		dylib_bindSymbol(handle, cast(void**)&lua_rotate, "lua_rotate");
81 		dylib_bindSymbol(handle, cast(void**)&lua_copy, "lua_copy");
82 		dylib_bindSymbol(handle, cast(void**)&lua_checkstack, "lua_checkstack");
83 		dylib_bindSymbol(handle, cast(void**)&lua_xmove, "lua_xmove");
84 		dylib_bindSymbol(handle, cast(void**)&lua_isnumber, "lua_isnumber");
85 		dylib_bindSymbol(handle, cast(void**)&lua_isstring, "lua_isstring");
86 		dylib_bindSymbol(handle, cast(void**)&lua_iscfunction, "lua_iscfunction");
87 		dylib_bindSymbol(handle, cast(void**)&lua_isinteger, "lua_isinteger");
88 		dylib_bindSymbol(handle, cast(void**)&lua_isuserdata, "lua_isuserdata");
89 		dylib_bindSymbol(handle, cast(void**)&lua_type, "lua_type");
90 		dylib_bindSymbol(handle, cast(void**)&lua_typename, "lua_typename");
91 		dylib_bindSymbol(handle, cast(void**)&lua_tonumberx, "lua_tonumberx");
92 		dylib_bindSymbol(handle, cast(void**)&lua_tointegerx, "lua_tointegerx");
93 		dylib_bindSymbol(handle, cast(void**)&lua_toboolean, "lua_toboolean");
94 		dylib_bindSymbol(handle, cast(void**)&lua_tolstring, "lua_tolstring");
95 		dylib_bindSymbol(handle, cast(void**)&lua_rawlen, "lua_rawlen");
96 		dylib_bindSymbol(handle, cast(void**)&lua_tocfunction, "lua_tocfunction");
97 		dylib_bindSymbol(handle, cast(void**)&lua_touserdata, "lua_touserdata");
98 		dylib_bindSymbol(handle, cast(void**)&lua_tothread, "lua_tothread");
99 		dylib_bindSymbol(handle, cast(void**)&lua_topointer, "lua_topointer");
100 		dylib_bindSymbol(handle, cast(void**)&lua_arith, "lua_arith");
101 		dylib_bindSymbol(handle, cast(void**)&lua_rawequal, "lua_rawequal");
102 		dylib_bindSymbol(handle, cast(void**)&lua_compare, "lua_compare");
103 		dylib_bindSymbol(handle, cast(void**)&lua_pushnil, "lua_pushnil");
104 		dylib_bindSymbol(handle, cast(void**)&lua_pushnumber, "lua_pushnumber");
105 		dylib_bindSymbol(handle, cast(void**)&lua_pushinteger, "lua_pushinteger");
106 		dylib_bindSymbol(handle, cast(void**)&lua_pushlstring, "lua_pushlstring");
107 		dylib_bindSymbol(handle, cast(void**)&lua_pushstring, "lua_pushstring");
108 		dylib_bindSymbol(handle, cast(void**)&lua_pushvfstring, "lua_pushvfstring");
109 		dylib_bindSymbol(handle, cast(void**)&lua_pushfstring, "lua_pushfstring");
110 		dylib_bindSymbol(handle, cast(void**)&lua_pushcclosure, "lua_pushcclosure");
111 		dylib_bindSymbol(handle, cast(void**)&lua_pushboolean, "lua_pushboolean");
112 		dylib_bindSymbol(handle, cast(void**)&lua_pushlightuserdata, "lua_pushlightuserdata");
113 		dylib_bindSymbol(handle, cast(void**)&lua_pushthread, "lua_pushthread");
114 		dylib_bindSymbol(handle, cast(void**)&lua_getglobal, "lua_getglobal");
115 		dylib_bindSymbol(handle, cast(void**)&lua_gettable, "lua_gettable");
116 		dylib_bindSymbol(handle, cast(void**)&lua_getfield, "lua_getfield");
117 		dylib_bindSymbol(handle, cast(void**)&lua_geti, "lua_geti");
118 		dylib_bindSymbol(handle, cast(void**)&lua_rawget, "lua_rawget");
119 		dylib_bindSymbol(handle, cast(void**)&lua_rawgeti, "lua_rawgeti");
120 		dylib_bindSymbol(handle, cast(void**)&lua_rawgetp, "lua_rawgetp");
121 		dylib_bindSymbol(handle, cast(void**)&lua_createtable, "lua_createtable");
122 		dylib_bindSymbol(handle, cast(void**)&lua_newuserdatauv, "lua_newuserdatauv");
123 		dylib_bindSymbol(handle, cast(void**)&lua_getmetatable, "lua_getmetatable");
124 		dylib_bindSymbol(handle, cast(void**)&lua_getiuservalue, "lua_getiuservalue");
125 		dylib_bindSymbol(handle, cast(void**)&lua_setglobal, "lua_setglobal");
126 		dylib_bindSymbol(handle, cast(void**)&lua_settable, "lua_settable");
127 		dylib_bindSymbol(handle, cast(void**)&lua_setfield, "lua_setfield");
128 		dylib_bindSymbol(handle, cast(void**)&lua_rawset, "lua_rawset");
129 		dylib_bindSymbol(handle, cast(void**)&lua_rawseti, "lua_rawseti");
130 		dylib_bindSymbol(handle, cast(void**)&lua_rawsetp, "lua_rawsetp");
131 		dylib_bindSymbol(handle, cast(void**)&lua_setmetatable, "lua_setmetatable");
132 		dylib_bindSymbol(handle, cast(void**)&lua_setiuservalue, "lua_setiuservalue");
133 		dylib_bindSymbol(handle, cast(void**)&lua_callk, "lua_callk");
134 		dylib_bindSymbol(handle, cast(void**)&lua_pcallk, "lua_pcallk");
135 		dylib_bindSymbol(handle, cast(void**)&lua_load, "lua_load");
136 		dylib_bindSymbol(handle, cast(void**)&lua_dump, "lua_dump");
137 		dylib_bindSymbol(handle, cast(void**)&lua_yieldk, "lua_yieldk");
138 		dylib_bindSymbol(handle, cast(void**)&lua_resume, "lua_resume");
139 		dylib_bindSymbol(handle, cast(void**)&lua_status, "lua_status");
140 		dylib_bindSymbol(handle, cast(void**)&lua_isyieldable, "lua_isyieldable");
141 		dylib_bindSymbol(handle, cast(void**)&lua_setwarnf, "lua_setwarnf");
142 		dylib_bindSymbol(handle, cast(void**)&lua_warning, "lua_warning");
143 		dylib_bindSymbol(handle, cast(void**)&lua_gc, "lua_gc");
144 		dylib_bindSymbol(handle, cast(void**)&lua_error, "lua_error");
145 		dylib_bindSymbol(handle, cast(void**)&lua_next, "lua_next");
146 		dylib_bindSymbol(handle, cast(void**)&lua_concat, "lua_concat");
147 		dylib_bindSymbol(handle, cast(void**)&lua_len, "lua_len");
148 		dylib_bindSymbol(handle, cast(void**)&lua_stringtonumber, "lua_stringtonumber");
149 		dylib_bindSymbol(handle, cast(void**)&lua_getallocf, "lua_getallocf");
150 		dylib_bindSymbol(handle, cast(void**)&lua_setallocf, "lua_setallocf");
151 		dylib_bindSymbol(handle, cast(void**)&lua_toclose, "lua_toclose");
152 		dylib_bindSymbol(handle, cast(void**)&lua_getstack, "lua_getstack");
153 		dylib_bindSymbol(handle, cast(void**)&lua_getinfo, "lua_getinfo");
154 		dylib_bindSymbol(handle, cast(void**)&lua_getlocal, "lua_getlocal");
155 		dylib_bindSymbol(handle, cast(void**)&lua_setlocal, "lua_setlocal");
156 		dylib_bindSymbol(handle, cast(void**)&lua_getupvalue, "lua_getupvalue");
157 		dylib_bindSymbol(handle, cast(void**)&lua_setupvalue, "lua_setupvalue");
158 		dylib_bindSymbol(handle, cast(void**)&lua_upvalueid, "lua_upvalueid");
159 		dylib_bindSymbol(handle, cast(void**)&lua_upvaluejoin, "lua_upvaluejoin");
160 		dylib_bindSymbol(handle, cast(void**)&lua_sethook, "lua_sethook");
161 		dylib_bindSymbol(handle, cast(void**)&lua_gethook, "lua_gethook");
162 		dylib_bindSymbol(handle, cast(void**)&lua_gethookmask, "lua_gethookmask");
163 		dylib_bindSymbol(handle, cast(void**)&lua_gethookcount, "lua_gethookcount");
164 		dylib_bindSymbol(handle, cast(void**)&lua_setcstacklimit, "lua_setcstacklimit");
165 
166 		dylib_bindSymbol(handle, cast(void**)&luaL_checkversion_, "luaL_checkversion_");
167 		dylib_bindSymbol(handle, cast(void**)&luaL_getmetafield, "luaL_getmetafield");
168 		dylib_bindSymbol(handle, cast(void**)&luaL_callmeta, "luaL_callmeta");
169 		dylib_bindSymbol(handle, cast(void**)&luaL_tolstring, "luaL_tolstring");
170 		dylib_bindSymbol(handle, cast(void**)&luaL_argerror, "luaL_argerror");
171 		dylib_bindSymbol(handle, cast(void**)&luaL_checklstring, "luaL_checklstring");
172 		dylib_bindSymbol(handle, cast(void**)&luaL_optlstring, "luaL_optlstring");
173 		dylib_bindSymbol(handle, cast(void**)&luaL_checknumber, "luaL_checknumber");
174 		dylib_bindSymbol(handle, cast(void**)&luaL_optnumber, "luaL_optnumber");
175 		dylib_bindSymbol(handle, cast(void**)&luaL_checkinteger, "luaL_checkinteger");
176 		dylib_bindSymbol(handle, cast(void**)&luaL_optinteger, "luaL_optinteger");
177 		dylib_bindSymbol(handle, cast(void**)&luaL_checkstack, "luaL_checkstack");
178 		dylib_bindSymbol(handle, cast(void**)&luaL_checktype, "luaL_checktype");
179 		dylib_bindSymbol(handle, cast(void**)&luaL_checkany, "luaL_checkany");
180 		dylib_bindSymbol(handle, cast(void**)&luaL_newmetatable, "luaL_newmetatable");
181 		dylib_bindSymbol(handle, cast(void**)&luaL_setmetatable, "luaL_setmetatable");
182 		dylib_bindSymbol(handle, cast(void**)&luaL_testudata, "luaL_testudata");
183 		dylib_bindSymbol(handle, cast(void**)&luaL_checkudata, "luaL_checkudata");
184 		dylib_bindSymbol(handle, cast(void**)&luaL_where, "luaL_where");
185 		dylib_bindSymbol(handle, cast(void**)&luaL_error, "luaL_error");
186 		dylib_bindSymbol(handle, cast(void**)&luaL_checkoption, "luaL_checkoption");
187 		dylib_bindSymbol(handle, cast(void**)&luaL_fileresult, "luaL_fileresult");
188 		dylib_bindSymbol(handle, cast(void**)&luaL_execresult, "luaL_execresult");
189 		dylib_bindSymbol(handle, cast(void**)&luaL_ref, "luaL_ref");
190 		dylib_bindSymbol(handle, cast(void**)&luaL_unref, "luaL_unref");
191 		dylib_bindSymbol(handle, cast(void**)&luaL_loadfilex, "luaL_loadfilex");
192 		dylib_bindSymbol(handle, cast(void**)&luaL_loadbufferx, "luaL_loadbufferx");
193 		dylib_bindSymbol(handle, cast(void**)&luaL_loadstring, "luaL_loadstring");
194 		dylib_bindSymbol(handle, cast(void**)&luaL_newstate, "luaL_newstate");
195 		dylib_bindSymbol(handle, cast(void**)&luaL_len, "luaL_len");
196 		dylib_bindSymbol(handle, cast(void**)&luaL_gsub, "luaL_gsub");
197 		dylib_bindSymbol(handle, cast(void**)&luaL_setfuncs, "luaL_setfuncs");
198 		dylib_bindSymbol(handle, cast(void**)&luaL_getsubtable, "luaL_getsubtable");
199 		dylib_bindSymbol(handle, cast(void**)&luaL_traceback, "luaL_traceback");
200 		dylib_bindSymbol(handle, cast(void**)&luaL_requiref, "luaL_requiref");
201 		dylib_bindSymbol(handle, cast(void**)&luaL_buffinit, "luaL_buffinit");
202 		dylib_bindSymbol(handle, cast(void**)&luaL_prepbuffsize, "luaL_prepbuffsize");
203 		dylib_bindSymbol(handle, cast(void**)&luaL_addlstring, "luaL_addlstring");
204 		dylib_bindSymbol(handle, cast(void**)&luaL_addstring, "luaL_addstring");
205 		dylib_bindSymbol(handle, cast(void**)&luaL_addvalue, "luaL_addvalue");
206 		dylib_bindSymbol(handle, cast(void**)&luaL_pushresult, "luaL_pushresult");
207 		dylib_bindSymbol(handle, cast(void**)&luaL_pushresultsize, "luaL_pushresultsize");
208 		dylib_bindSymbol(handle, cast(void**)&luaL_buffinitsize, "luaL_buffinitsize");
209 		dylib_bindSymbol(handle, cast(void**)&luaL_pushmodule, "luaL_pushmodule");
210 		dylib_bindSymbol(handle, cast(void**)&luaL_openlib, "luaL_openlib");
211 		dylib_bindSymbol(handle, cast(void**)&luaopen_base, "luaopen_base");
212 		dylib_bindSymbol(handle, cast(void**)&luaopen_coroutine, "luaopen_coroutine");
213 		dylib_bindSymbol(handle, cast(void**)&luaopen_table, "luaopen_table");
214 		dylib_bindSymbol(handle, cast(void**)&luaopen_io, "luaopen_io");
215 		dylib_bindSymbol(handle, cast(void**)&luaopen_os, "luaopen_os");
216 		dylib_bindSymbol(handle, cast(void**)&luaopen_string, "luaopen_string");
217 		dylib_bindSymbol(handle, cast(void**)&luaopen_utf8, "luaopen_utf8");
218 		dylib_bindSymbol(handle, cast(void**)&luaopen_bit32, "luaopen_bit32");
219 		dylib_bindSymbol(handle, cast(void**)&luaopen_math, "luaopen_math");
220 		dylib_bindSymbol(handle, cast(void**)&luaopen_debug, "luaopen_debug");
221 		dylib_bindSymbol(handle, cast(void**)&luaopen_package, "luaopen_package");
222 		dylib_bindSymbol(handle, cast(void**)&luaL_openlibs, "luaL_openlibs");
223 
224 		return handle;
225 	}
226 }
227 else
228 {
229 	version(Windows) private enum string[] _lua_libs = ["lua54.dll"];
230 	else version(OSX) private enum string[] _lua_libs = ["liblua.5.4.dylib"];
231 	else version(Posix) private enum string[] _lua_libs = ["liblua.so.5.4", "liblua5.4.so"];
232 
233 	mixin(DylibLoaderBuilder!("Lua", _lua_libs, riverd.lua.dynfun));
234 }
235 
236 @system unittest {
237 	void* lua_handle = dylib_load_lua();
238 	assert(dylib_is_loaded(lua_handle));
239 
240 	dylib_unload(lua_handle);
241 }