1 /**
2 @file
3 @brief Implementation
4
5 @date 2012
6
7 @author
8 Ryan Pavlik
9 <rpavlik@iastate.edu> and <abiryan@ryand.net>
10 http://academic.cleardefinition.com/
11 Iowa State University Virtual Reality Applications Center
12 Human-Computer Interaction Graduate Program
13 */
14
15 // Copyright Iowa State University 2012.
16 // Permission is hereby granted, free of charge, to any person obtaining a
17 // copy of this software and associated documentation files (the "Software"),
18 // to deal in the Software without restriction, including without limitation
19 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
20 // and/or sell copies of the Software, and to permit persons to whom the
21 // Software is furnished to do so, subject to the following conditions:
22
23 // The above copyright notice and this permission notice shall be included
24 // in all copies or substantial portions of the Software.
25
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
27 // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
28 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
29 // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
30 // SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
31 // ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
32 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
33 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
34 // OR OTHER DEALINGS IN THE SOFTWARE.
35
36 #define LUABIND_BUILDING
37
38 // Internal Includes
39 #include <luabind/config.hpp> // for LUABIND_API
40 #include <luabind/object.hpp>
41 #include <luabind/set_package_preload.hpp>
42
43 // Library/third-party includes
44 #include <luabind/lua_include.hpp> // for lua_pushstring, lua_rawset, etc
45
46 // Standard includes
47 // - none
48
do_set_package_preload(lua_State * L)49 static int do_set_package_preload(lua_State* L)
50 {
51 // Note: Use ordinary set/get instead of the raw variants, because this
52 // function should not be performance sensitive anyway.
53 lua_pushglobaltable(L);
54 lua_getfield(L, -1, "package");
55 lua_remove(L, -2); // Remove global table.
56 lua_getfield(L, -1, "preload");
57 lua_remove(L, -2); // Remove package table.
58 lua_insert(L, -3); // Move package.preload beneath key and value.
59 lua_settable(L, -3); // package.preload[modulename] = loader
60 return 0;
61 }
62
proxy_loader(lua_State * L)63 static int proxy_loader(lua_State* L)
64 {
65 luaL_checkstring(L, 1); // First argument should be the module name.
66 lua_settop(L, 1); // Ignore any other arguments.
67 lua_pushvalue(L, lua_upvalueindex(1)); // Push the real loader.
68 lua_insert(L, 1); // Move it beneath the argument.
69 lua_call(L, 1, LUA_MULTRET); // Pops everyhing.
70 return lua_gettop(L);
71 }
72
73
74 namespace luabind {
set_package_preload(lua_State * L,char const * modulename,object const & loader)75 LUABIND_API void set_package_preload(
76 lua_State * L, char const* modulename, object const& loader)
77 {
78 loader.push(L);
79 lua_pushcclosure(L, &proxy_loader, 1);
80 object const proxy_ldr(from_stack(L, -1));
81 lua_pop(L, 1); // pop do_load.
82 lua_pushcfunction(L, &do_set_package_preload);
83 // Must use object for correct popping in case of errors:
84 object do_set(from_stack(L, -1));
85 lua_pop(L, 1);
86 do_set(modulename, proxy_ldr);
87 }
88
89 } // namespace luabind
90