1 // Copyright (c) 2003 Daniel Wallin and Arvid Norberg 2 3 // Permission is hereby granted, free of charge, to any person obtaining a 4 // copy of this software and associated documentation files (the "Software"), 5 // to deal in the Software without restriction, including without limitation 6 // the rights to use, copy, modify, merge, publish, distribute, sublicense, 7 // and/or sell copies of the Software, and to permit persons to whom the 8 // Software is furnished to do so, subject to the following conditions: 9 10 // The above copyright notice and this permission notice shall be included 11 // in all copies or substantial portions of the Software. 12 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 14 // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 15 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 16 // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 17 // SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR 18 // ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 19 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 21 // OR OTHER DEALINGS IN THE SOFTWARE. 22 23 #define LUABIND_BUILDING 24 25 #include <luabind/class.hpp> 26 #include <luabind/detail/garbage_collector.hpp> 27 #include <luabind/get_main_thread.hpp> 28 #include <luabind/set_package_preload.hpp> 29 30 #include <luabind/lua_include.hpp> 31 32 33 namespace luabind { 34 35 namespace 36 { 37 make_property(lua_State * L)38 int make_property(lua_State* L) 39 { 40 int args = lua_gettop(L); 41 42 if (args == 0 || args > 2) 43 { 44 lua_pushliteral(L, "make_property() called with wrong number of arguments."); 45 lua_error(L); 46 } 47 48 if (args == 1) 49 lua_pushnil(L); 50 51 lua_pushcclosure(L, &detail::property_tag, 2); 52 return 1; 53 } 54 55 int main_thread_tag; 56 deprecated_super(lua_State * L)57 int deprecated_super(lua_State* L) 58 { 59 lua_pushliteral(L, 60 "DEPRECATION: 'super' has been deprecated in favor of " 61 "directly calling the base class __init() function. " 62 "This error can be disabled by calling 'luabind::disable_super_deprecation()'." 63 ); 64 lua_error(L); 65 66 return 0; 67 } 68 69 } // namespace unnamed 70 get_main_thread(lua_State * L)71 LUABIND_API lua_State* get_main_thread(lua_State* L) 72 { 73 lua_pushlightuserdata(L, &main_thread_tag); 74 lua_rawget(L, LUA_REGISTRYINDEX); 75 lua_State* result = static_cast<lua_State*>(lua_touserdata(L, -1)); 76 lua_pop(L, 1); 77 78 if (!result) 79 throw std::runtime_error("Unable to get main thread, luabind::open() not called?"); 80 81 return result; 82 } 83 namespace { 84 template<typename T> create_gc_udata(lua_State * L,void * tag)85 inline void* create_gc_udata(lua_State* L, void* tag) { 86 void* storage = lua_newuserdata(L, sizeof(T)); 87 88 // set gc metatable 89 lua_createtable(L, 0, 1); // one (non-sequence) element. 90 lua_pushcfunction(L, &detail::garbage_collector<T>); 91 lua_setfield(L, -2, "__gc"); 92 lua_setmetatable(L, -2); 93 94 lua_rawsetp(L, LUA_REGISTRYINDEX, tag); 95 return storage; 96 } 97 98 template<typename T> push_gc_udata(lua_State * L,void * tag)99 inline void push_gc_udata(lua_State* L, void* tag) { 100 void* storage = create_gc_udata<T>(L, tag); 101 new (storage) T; 102 } 103 104 template<typename T, typename A1> push_gc_udata(lua_State * L,void * tag,A1 constructorArg)105 inline void push_gc_udata(lua_State* L, void* tag, A1 constructorArg) { 106 void* storage = create_gc_udata<T>(L, tag); 107 new (storage) T(constructorArg); 108 } 109 } 110 open(lua_State * L)111 LUABIND_API void open(lua_State* L) 112 { 113 bool is_main_thread = lua_pushthread(L) == 1; 114 lua_pop(L, 1); 115 116 if (!is_main_thread) 117 { 118 throw std::runtime_error( 119 "luabind::open() must be called with the main thread " 120 "lua_State*" 121 ); 122 } 123 124 push_gc_udata<detail::class_registry>(L, &detail::classes_tag, L); 125 push_gc_udata<detail::class_id_map>(L, &detail::classid_map_tag); 126 push_gc_udata<detail::cast_graph>(L, &detail::cast_graph_tag); 127 push_gc_udata<detail::class_map>(L, &detail::class_map_tag); 128 129 // add functions (class, cast etc...) 130 lua_pushcfunction(L, detail::create_class::stage1); 131 lua_setglobal(L, "class"); 132 133 lua_pushcfunction(L, &make_property); 134 lua_setglobal(L, "property"); 135 136 lua_pushlightuserdata(L, &main_thread_tag); 137 lua_pushlightuserdata(L, L); 138 lua_rawset(L, LUA_REGISTRYINDEX); 139 140 lua_pushcfunction(L, &deprecated_super); 141 lua_setglobal(L, "super"); 142 } 143 144 } // namespace luabind 145