1 //------------------------------------------------------------------------------
3 https://github.com/vinniefalco/LuaBridge
5 Copyright 2012, Vinnie Falco <vinnie.falco@gmail.com>
6 Copyright 2007, Nathan Reed
8 License: The MIT License (http://www.opensource.org/licenses/mit-license.php)
10 Permission is hereby granted, free of charge, to any person obtaining a copy
11 of this software and associated documentation files (the "Software"), to deal
12 in the Software without restriction, including without limitation the rights
13 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 copies of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions:
17 The above copyright notice and this permission notice shall be included in all
18 copies or substantial portions of the Software.
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 //==============================================================================
30 /** Provides C++ to Lua registration capabilities.
32 This class is not instantiated directly, call `getGlobalNamespace` to start
33 the registration process.
38 Namespace& operator= (Namespace const& other);
41 int mutable m_stackSize;
44 //============================================================================
48 VF: This function looks handy, why aren't we using it?
51 static int luaError (lua_State* L, std::string message)
53 assert (lua_isstring (L, lua_upvalueindex (1)));
56 // Get information on the caller's caller to format the message,
57 // so the error appears to originate from the Lua source.
59 int result = lua_getstack (L, 2, &ar);
62 lua_getinfo (L, "Sl", &ar);
64 if (ar.currentline != -1)
66 // poor mans int to string to avoid <strstrream>.
67 lua_pushnumber (L, ar.currentline);
68 s = s + ":" + lua_tostring (L, -1) + ": ";
75 return luaL_error (L, s.c_str ());
79 //----------------------------------------------------------------------------
83 void pop (int n) const
85 if (m_stackSize >= n && lua_gettop (L) >= n)
92 throw std::logic_error ("invalid stack");
98 Factored base to reduce template instantiations.
103 ClassBase& operator= (ClassBase const& other);
106 friend class Namespace;
109 int mutable m_stackSize;
112 //--------------------------------------------------------------------------
114 __index metamethod for a class.
116 This implements member functions, data members, and property members.
117 Functions are stored in the metatable and const metatable. Data members
118 and property members are in the __propget table.
120 If the key is not found, the search proceeds up the hierarchy of base
123 static int indexMetaMethod (lua_State* L)
127 assert (lua_isuserdata (L, 1)); // warn on security bypass
128 lua_getmetatable (L, 1); // get metatable for object
131 lua_pushvalue (L, 2); // push key arg2
132 lua_rawget (L, -2); // lookup key in metatable
133 if (lua_iscfunction (L, -1)) // ensure its a cfunction
135 lua_remove (L, -2); // remove metatable
139 else if (lua_isnil (L, -1))
146 throw std::logic_error ("not a cfunction");
149 rawgetfield (L, -1, "__propget"); // get __propget table
150 if (lua_istable (L, -1)) // ensure it is a table
152 lua_pushvalue (L, 2); // push key arg2
153 lua_rawget (L, -2); // lookup key in __propget
154 lua_remove (L, -2); // remove __propget
155 if (lua_iscfunction (L, -1)) // ensure its a cfunction
157 lua_remove (L, -2); // remove metatable
158 lua_pushvalue (L, 1); // push class arg1
163 else if (lua_isnil (L, -1))
171 // We only put cfunctions into __propget.
172 throw std::logic_error ("not a cfunction");
179 // __propget is missing, or not a table.
180 throw std::logic_error ("missing __propget table");
183 // Repeat the lookup in the __parent metafield,
184 // or return nil if the field doesn't exist.
185 rawgetfield (L, -1, "__parent");
186 if (lua_istable (L, -1))
188 // Remove metatable and repeat the search in __parent.
191 else if (lua_isnil (L, -1))
200 throw std::logic_error ("__parent is not a table");
207 //--------------------------------------------------------------------------
209 __newindex metamethod for classes.
211 This supports writable variables and properties on class objects. The
212 corresponding object is passed in the first parameter to the set function.
214 static int newindexMetaMethod (lua_State* L)
218 lua_getmetatable (L, 1);
223 rawgetfield (L, -1, "__propset");
224 if (!lua_isnil (L, -1))
226 lua_pushvalue (L, 2);
228 if (!lua_isnil (L, -1))
230 // found it, call the setFunction.
231 assert (lua_isfunction (L, -1));
232 lua_pushvalue (L, 1);
233 lua_pushvalue (L, 3);
242 // Repeat the lookup in the __parent metafield.
243 rawgetfield (L, -1, "__parent");
244 if (lua_isnil (L, -1))
246 // Either the property or __parent must exist.
247 result = luaL_error (L,
248 "no member named '%s'", lua_tostring (L, 2));
256 //--------------------------------------------------------------------------
258 Create the const table.
260 void createConstTable (char const* name)
263 lua_pushvalue (L, -1);
264 lua_setmetatable (L, -2);
265 lua_pushboolean (L, 1);
266 lua_rawsetp (L, -2, getIdentityKey ());
267 lua_pushstring (L, (std::string ("const ") + name).c_str ());
268 rawsetfield (L, -2, "__type");
269 lua_pushcfunction (L, &indexMetaMethod);
270 rawsetfield (L, -2, "__index");
271 lua_pushcfunction (L, &newindexMetaMethod);
272 rawsetfield (L, -2, "__newindex");
274 rawsetfield (L, -2, "__propget");
276 if (Security::hideMetatables ())
279 rawsetfield (L, -2, "__metatable");
283 //--------------------------------------------------------------------------
285 Create the class table.
287 The Lua stack should have the const table on top.
289 void createClassTable (char const* name)
292 lua_pushvalue (L, -1);
293 lua_setmetatable (L, -2);
294 lua_pushboolean (L, 1);
295 lua_rawsetp (L, -2, getIdentityKey ());
296 lua_pushstring (L, name);
297 rawsetfield (L, -2, "__type");
298 lua_pushcfunction (L, &indexMetaMethod);
299 rawsetfield (L, -2, "__index");
300 lua_pushcfunction (L, &newindexMetaMethod);
301 rawsetfield (L, -2, "__newindex");
303 rawsetfield (L, -2, "__propget");
305 rawsetfield (L, -2, "__propset");
307 lua_pushvalue (L, -2);
308 rawsetfield (L, -2, "__const"); // point to const table
310 lua_pushvalue (L, -1);
311 rawsetfield (L, -3, "__class"); // point const table to class table
313 if (Security::hideMetatables ())
316 rawsetfield (L, -2, "__metatable");
320 //--------------------------------------------------------------------------
322 Create the static table.
324 The Lua stack should have:
327 -3 enclosing namespace
329 void createStaticTable (char const* name)
333 lua_pushvalue (L, -1);
334 lua_setmetatable (L, -3);
336 rawsetfield (L, -5, name);
339 lua_pushlightuserdata (L, this);
340 lua_pushcclosure (L, &tostringMetaMethod, 1);
341 rawsetfield (L, -2, "__tostring");
343 lua_pushcfunction (L, &CFunc::indexMetaMethod);
344 rawsetfield (L, -2, "__index");
345 lua_pushcfunction (L, &CFunc::newindexMetaMethod);
346 rawsetfield (L, -2, "__newindex");
348 rawsetfield (L, -2, "__propget");
350 rawsetfield (L, -2, "__propset");
352 lua_pushvalue (L, -2);
353 rawsetfield (L, -2, "__class"); // point to class table
355 if (Security::hideMetatables ())
358 rawsetfield (L, -2, "__metatable");
362 //==========================================================================
364 lua_CFunction to construct a class object wrapped in a container.
366 template <class Params, class C>
367 static int ctorContainerProxy (lua_State* L)
369 typedef typename ContainerTraits <C>::Type T;
370 ArgList <Params, 2> args (L);
371 T* const p = Constructor <T, Params>::call (args);
372 UserdataSharedHelper <C, false>::push (L, p);
376 //--------------------------------------------------------------------------
378 lua_CFunction to construct a class object in-place in the userdata.
380 template <class Params, class T>
381 static int ctorPlacementProxy (lua_State* L)
383 ArgList <Params, 2> args (L);
384 Constructor <T, Params>::call (UserdataValue <T>::place (L), args);
388 //--------------------------------------------------------------------------
392 void pop (int n) const
394 if (m_stackSize >= n && lua_gettop (L) >= n)
401 throw std::logic_error ("invalid stack");
406 //--------------------------------------------------------------------------
407 explicit ClassBase (lua_State* L_)
413 //--------------------------------------------------------------------------
417 ClassBase (ClassBase const& other)
421 m_stackSize = other.m_stackSize;
422 other.m_stackSize = 0;
431 //============================================================================
435 //============================================================================
437 Provides a class registration in a lua_State.
439 After contstruction the Lua stack holds these objects:
443 -4 (enclosing namespace)
446 class Class : public ClassBase
449 //==========================================================================
451 Register a new class or add to an existing class registration.
453 Class (char const* name, Namespace const* parent) : ClassBase (parent->L)
455 m_stackSize = parent->m_stackSize + 3;
456 parent->m_stackSize = 0;
458 assert (lua_istable (L, -1));
459 rawgetfield (L, -1, name);
461 if (lua_isnil (L, -1))
465 createConstTable (name);
466 lua_pushcfunction (L, &CFunc::gcMetaMethod <T>);
467 rawsetfield (L, -2, "__gc");
469 createClassTable (name);
470 lua_pushcfunction (L, &CFunc::gcMetaMethod <T>);
471 rawsetfield (L, -2, "__gc");
473 createStaticTable (name);
475 // Map T back to its tables.
476 lua_pushvalue (L, -1);
477 lua_rawsetp (L, LUA_REGISTRYINDEX, ClassInfo <T>::getStaticKey ());
478 lua_pushvalue (L, -2);
479 lua_rawsetp (L, LUA_REGISTRYINDEX, ClassInfo <T>::getClassKey ());
480 lua_pushvalue (L, -3);
481 lua_rawsetp (L, LUA_REGISTRYINDEX, ClassInfo <T>::getConstKey ());
485 rawgetfield (L, -1, "__class");
486 rawgetfield (L, -1, "__const");
488 // Reverse the top 3 stack elements
494 //==========================================================================
498 Class (char const* name, Namespace const* parent, void const* const staticKey)
499 : ClassBase (parent->L)
501 m_stackSize = parent->m_stackSize + 3;
502 parent->m_stackSize = 0;
504 assert (lua_istable (L, -1));
506 createConstTable (name);
507 lua_pushcfunction (L, &CFunc::gcMetaMethod <T>);
508 rawsetfield (L, -2, "__gc");
510 createClassTable (name);
511 lua_pushcfunction (L, &CFunc::gcMetaMethod <T>);
512 rawsetfield (L, -2, "__gc");
514 createStaticTable (name);
516 lua_rawgetp (L, LUA_REGISTRYINDEX, staticKey);
517 assert (lua_istable (L, -1));
518 rawgetfield (L, -1, "__class");
519 assert (lua_istable (L, -1));
520 rawgetfield (L, -1, "__const");
521 assert (lua_istable (L, -1));
523 rawsetfield (L, -6, "__parent");
524 rawsetfield (L, -4, "__parent");
525 rawsetfield (L, -2, "__parent");
527 lua_pushvalue (L, -1);
528 lua_rawsetp (L, LUA_REGISTRYINDEX, ClassInfo <T>::getStaticKey ());
529 lua_pushvalue (L, -2);
530 lua_rawsetp (L, LUA_REGISTRYINDEX, ClassInfo <T>::getClassKey ());
531 lua_pushvalue (L, -3);
532 lua_rawsetp (L, LUA_REGISTRYINDEX, ClassInfo <T>::getConstKey ());
535 //--------------------------------------------------------------------------
537 Continue registration in the enclosing namespace.
539 Namespace endClass ()
541 return Namespace (this);
544 //--------------------------------------------------------------------------
546 Add or replace a static data member.
549 Class <T>& addStaticData (char const* name, U* pu, bool isWritable = true)
551 assert (lua_istable (L, -1));
553 rawgetfield (L, -1, "__propget");
554 assert (lua_istable (L, -1));
555 lua_pushlightuserdata (L, pu);
556 lua_pushcclosure (L, &CFunc::getVariable <U>, 1);
557 rawsetfield (L, -2, name);
560 rawgetfield (L, -1, "__propset");
561 assert (lua_istable (L, -1));
564 lua_pushlightuserdata (L, pu);
565 lua_pushcclosure (L, &CFunc::setVariable <U>, 1);
569 lua_pushstring (L, name);
570 lua_pushcclosure (L, &CFunc::readOnlyError, 1);
572 rawsetfield (L, -2, name);
578 //--------------------------------------------------------------------------
580 Add or replace a static property member.
582 If the set function is null, the property is read-only.
585 Class <T>& addStaticProperty (char const* name, U (*get)(), void (*set)(U) = 0)
587 typedef U (*get_t)();
588 typedef void (*set_t)(U);
590 assert (lua_istable (L, -1));
592 rawgetfield (L, -1, "__propget");
593 assert (lua_istable (L, -1));
594 new (lua_newuserdata (L, sizeof (get))) get_t (get);
595 lua_pushcclosure (L, &CFunc::Call <U (*) (void)>::f, 1);
596 rawsetfield (L, -2, name);
599 rawgetfield (L, -1, "__propset");
600 assert (lua_istable (L, -1));
603 new (lua_newuserdata (L, sizeof (set))) set_t (set);
604 lua_pushcclosure (L, &CFunc::Call <void (*) (U)>::f, 1);
608 lua_pushstring (L, name);
609 lua_pushcclosure (L, &CFunc::readOnlyError, 1);
611 rawsetfield (L, -2, name);
617 //--------------------------------------------------------------------------
619 Add or replace a static member function.
622 Class <T>& addStaticFunction (char const* name, FP const fp)
624 new (lua_newuserdata (L, sizeof (fp))) FP (fp);
625 lua_pushcclosure (L, &CFunc::Call <FP>::f, 1);
626 rawsetfield (L, -2, name);
631 //--------------------------------------------------------------------------
633 Add or replace a lua_CFunction.
635 Class <T>& addStaticCFunction (char const* name, int (*const fp)(lua_State*))
637 lua_pushcfunction (L, fp);
638 rawsetfield (L, -2, name);
642 //--------------------------------------------------------------------------
644 Add or replace a data member.
647 Class <T>& addData (char const* name, const U T::* mp, bool isWritable = true)
649 typedef const U T::*mp_t;
651 // Add to __propget in class and const tables.
653 rawgetfield (L, -2, "__propget");
654 rawgetfield (L, -4, "__propget");
655 new (lua_newuserdata (L, sizeof (mp_t))) mp_t (mp);
656 lua_pushcclosure (L, &CFunc::getProperty <T,U>, 1);
657 lua_pushvalue (L, -1);
658 rawsetfield (L, -4, name);
659 rawsetfield (L, -2, name);
665 // Add to __propset in class table.
666 rawgetfield (L, -2, "__propset");
667 assert (lua_istable (L, -1));
668 new (lua_newuserdata (L, sizeof (mp_t))) mp_t (mp);
669 lua_pushcclosure (L, &CFunc::setProperty <T,U>, 1);
670 rawsetfield (L, -2, name);
677 //--------------------------------------------------------------------------
679 Add or replace a property member.
681 template <class TG, class TS>
682 Class <T>& addProperty (char const* name, TG (T::* get) () const, void (T::* set) (TS))
684 // Add to __propget in class and const tables.
686 rawgetfield (L, -2, "__propget");
687 rawgetfield (L, -4, "__propget");
688 typedef TG (T::*get_t) () const;
689 new (lua_newuserdata (L, sizeof (get_t))) get_t (get);
690 lua_pushcclosure (L, &CFunc::CallConstMember <get_t>::f, 1);
691 lua_pushvalue (L, -1);
692 rawsetfield (L, -4, name);
693 rawsetfield (L, -2, name);
698 // Add to __propset in class table.
699 rawgetfield (L, -2, "__propset");
700 assert (lua_istable (L, -1));
701 typedef void (T::* set_t) (TS);
702 new (lua_newuserdata (L, sizeof (set_t))) set_t (set);
703 lua_pushcclosure (L, &CFunc::CallMember <set_t>::f, 1);
704 rawsetfield (L, -2, name);
713 Class <T>& addProperty (char const* name, TG (T::* get) () const)
715 // Add to __propget in class and const tables.
716 rawgetfield (L, -2, "__propget");
717 rawgetfield (L, -4, "__propget");
718 typedef TG (T::*get_t) () const;
719 new (lua_newuserdata (L, sizeof (get_t))) get_t (get);
720 lua_pushcclosure (L, &CFunc::CallConstMember <get_t>::f, 1);
721 lua_pushvalue (L, -1);
722 rawsetfield (L, -4, name);
723 rawsetfield (L, -2, name);
729 //--------------------------------------------------------------------------
731 Add or replace a property member, by proxy.
733 When a class is closed for modification and does not provide (or cannot
734 provide) the function signatures necessary to implement get or set for
735 a property, this will allow non-member functions act as proxies.
737 Both the get and the set functions require a T const* and T* in the first
738 argument respectively.
740 template <class TG, class TS>
741 Class <T>& addProperty (char const* name, TG (*get) (T const*), void (*set) (T*, TS))
743 // Add to __propget in class and const tables.
745 rawgetfield (L, -2, "__propget");
746 rawgetfield (L, -4, "__propget");
747 typedef TG (*get_t) (T const*);
748 new (lua_newuserdata (L, sizeof (get_t))) get_t (get);
749 lua_pushcclosure (L, &CFunc::Call <get_t>::f, 1);
750 lua_pushvalue (L, -1);
751 rawsetfield (L, -4, name);
752 rawsetfield (L, -2, name);
758 // Add to __propset in class table.
759 rawgetfield (L, -2, "__propset");
760 assert (lua_istable (L, -1));
761 typedef void (*set_t) (T*, TS);
762 new (lua_newuserdata (L, sizeof (set_t))) set_t (set);
763 lua_pushcclosure (L, &CFunc::Call <set_t>::f, 1);
764 rawsetfield (L, -2, name);
772 template <class TG, class TS>
773 Class <T>& addProperty (char const* name, TG (*get) (T const*))
775 // Add to __propget in class and const tables.
776 rawgetfield (L, -2, "__propget");
777 rawgetfield (L, -4, "__propget");
778 typedef TG (*get_t) (T const*);
779 new (lua_newuserdata (L, sizeof (get_t))) get_t (get);
780 lua_pushcclosure (L, &CFunc::Call <get_t>::f, 1);
781 lua_pushvalue (L, -1);
782 rawsetfield (L, -4, name);
783 rawsetfield (L, -2, name);
789 //--------------------------------------------------------------------------
791 Add or replace a member function.
793 template <class MemFn>
794 Class <T>& addFunction (char const* name, MemFn mf)
796 CFunc::CallMemberFunctionHelper <MemFn, FuncTraits <MemFn>::isConstMemberFunction>::add (L, name, mf);
800 //--------------------------------------------------------------------------
802 Add or replace a member lua_CFunction.
804 Class <T>& addCFunction (char const* name, int (T::*mfp)(lua_State*))
806 typedef int (T::*MFP)(lua_State*);
807 assert (lua_istable (L, -1));
808 new (lua_newuserdata (L, sizeof (mfp))) MFP (mfp);
809 lua_pushcclosure (L, &CFunc::CallMemberCFunction <T>::f, 1);
810 rawsetfield (L, -3, name); // class table
815 //--------------------------------------------------------------------------
817 Add or replace a const member lua_CFunction.
819 Class <T>& addCFunction (char const* name, int (T::*mfp)(lua_State*) const)
821 typedef int (T::*MFP)(lua_State*) const;
822 assert (lua_istable (L, -1));
823 new (lua_newuserdata (L, sizeof (mfp))) MFP (mfp);
824 lua_pushcclosure (L, &CFunc::CallConstMemberCFunction <T>::f, 1);
825 lua_pushvalue (L, -1);
826 rawsetfield (L, -5, name); // const table
827 rawsetfield (L, -3, name); // class table
832 //--------------------------------------------------------------------------
834 Add or replace a primary Constructor.
836 The primary Constructor is invoked when calling the class type table
839 The template parameter should be a function pointer type that matches
840 the desired Constructor (since you can't take the address of a Constructor
841 and pass it as an argument).
843 template <class MemFn, class C>
844 Class <T>& addConstructor ()
847 &ctorContainerProxy <typename FuncTraits <MemFn>::Params, C>, 0);
848 rawsetfield(L, -2, "__call");
853 template <class MemFn>
854 Class <T>& addConstructor ()
857 &ctorPlacementProxy <typename FuncTraits <MemFn>::Params, T>, 0);
858 rawsetfield(L, -2, "__call");
865 //----------------------------------------------------------------------------
867 Open the global namespace for registrations.
869 explicit Namespace (lua_State* L_)
873 lua_getglobal (L, "_G");
877 //----------------------------------------------------------------------------
879 Open a namespace for registrations.
881 The namespace is created if it doesn't already exist.
882 The parent namespace is at the top of the Lua stack.
884 Namespace (char const* name, Namespace const* parent)
888 m_stackSize = parent->m_stackSize + 1;
889 parent->m_stackSize = 0;
891 assert (lua_istable (L, -1));
892 rawgetfield (L, -1, name);
893 if (lua_isnil (L, -1))
898 lua_pushvalue (L, -1);
899 lua_setmetatable (L, -2);
900 lua_pushcfunction (L, &CFunc::indexMetaMethod);
901 rawsetfield (L, -2, "__index");
902 lua_pushcfunction (L, &CFunc::newindexMetaMethod);
903 rawsetfield (L, -2, "__newindex");
905 rawsetfield (L, -2, "__propget");
907 rawsetfield (L, -2, "__propset");
908 lua_pushvalue (L, -1);
909 rawsetfield (L, -3, name);
911 lua_pushcfunction (L, &tostringMetaMethod);
912 rawsetfield (L, -2, "__tostring");
917 //----------------------------------------------------------------------------
919 Creates a continued registration from a child namespace.
921 explicit Namespace (Namespace const* child)
925 m_stackSize = child->m_stackSize - 1;
926 child->m_stackSize = 1;
929 // It is not necessary or valid to call
930 // endNamespace() for the global namespace!
932 assert (m_stackSize != 0);
935 //----------------------------------------------------------------------------
937 Creates a continued registration from a child class.
939 explicit Namespace (ClassBase const* child)
943 m_stackSize = child->m_stackSize - 3;
944 child->m_stackSize = 3;
949 //----------------------------------------------------------------------------
953 Ownership of the stack is transferred to the new object. This happens
954 when the compiler emits temporaries to hold these objects while chaining
955 registrations across namespaces.
957 Namespace (Namespace const& other) : L (other.L)
959 m_stackSize = other.m_stackSize;
960 other.m_stackSize = 0;
963 //----------------------------------------------------------------------------
965 Closes this namespace registration.
972 //----------------------------------------------------------------------------
974 Open the global namespace.
976 static Namespace getGlobalNamespace (lua_State* L)
978 return Namespace (L);
981 //----------------------------------------------------------------------------
983 Open a new or existing namespace for registrations.
985 Namespace beginNamespace (char const* name)
987 return Namespace (name, this);
990 //----------------------------------------------------------------------------
992 Continue namespace registration in the parent.
994 Do not use this on the global namespace.
996 Namespace endNamespace ()
998 return Namespace (this);
1001 //----------------------------------------------------------------------------
1003 Add or replace a variable.
1006 Namespace& addVariable (char const* name, T* pt, bool isWritable = true)
1008 assert (lua_istable (L, -1));
1010 rawgetfield (L, -1, "__propget");
1011 assert (lua_istable (L, -1));
1012 lua_pushlightuserdata (L, pt);
1013 lua_pushcclosure (L, &CFunc::getVariable <T>, 1);
1014 rawsetfield (L, -2, name);
1017 rawgetfield (L, -1, "__propset");
1018 assert (lua_istable (L, -1));
1021 lua_pushlightuserdata (L, pt);
1022 lua_pushcclosure (L, &CFunc::setVariable <T>, 1);
1026 lua_pushstring (L, name);
1027 lua_pushcclosure (L, &CFunc::readOnlyError, 1);
1029 rawsetfield (L, -2, name);
1035 //----------------------------------------------------------------------------
1037 Add or replace a property.
1039 If the set function is omitted or null, the property is read-only.
1041 template <class TG, class TS>
1042 Namespace& addProperty (char const* name, TG (*get) (), void (*set)(TS) = 0)
1044 assert (lua_istable (L, -1));
1046 rawgetfield (L, -1, "__propget");
1047 assert (lua_istable (L, -1));
1048 typedef TG (*get_t) ();
1049 new (lua_newuserdata (L, sizeof (get_t))) get_t (get);
1050 lua_pushcclosure (L, &CFunc::Call <TG (*) (void)>::f, 1);
1051 rawsetfield (L, -2, name);
1054 rawgetfield (L, -1, "__propset");
1055 assert (lua_istable (L, -1));
1058 typedef void (*set_t) (TS);
1059 new (lua_newuserdata (L, sizeof (set_t))) set_t (set);
1060 lua_pushcclosure (L, &CFunc::Call <void (*) (TS)>::f, 1);
1064 lua_pushstring (L, name);
1065 lua_pushcclosure (L, &CFunc::readOnlyError, 1);
1067 rawsetfield (L, -2, name);
1073 //----------------------------------------------------------------------------
1075 Add or replace a free function.
1078 Namespace& addFunction (char const* name, FP const fp)
1080 assert (lua_istable (L, -1));
1082 new (lua_newuserdata (L, sizeof (fp))) FP (fp);
1083 lua_pushcclosure (L, &CFunc::Call <FP>::f, 1);
1084 rawsetfield (L, -2, name);
1089 //----------------------------------------------------------------------------
1091 Add or replace a lua_CFunction.
1093 Namespace& addCFunction (char const* name, int (*const fp)(lua_State*))
1095 lua_pushcfunction (L, fp);
1096 rawsetfield (L, -2, name);
1101 //----------------------------------------------------------------------------
1103 Open a new or existing class for registrations.
1106 Class <T> beginClass (char const* name)
1108 return Class <T> (name, this);
1111 //----------------------------------------------------------------------------
1113 Derive a new class for registrations.
1115 To continue registrations for the class later, use beginClass().
1116 Do not call deriveClass() again.
1118 template <class T, class U>
1119 Class <T> deriveClass (char const* name)
1121 return Class <T> (name, this, ClassInfo <U>::getStaticKey ());
1125 //------------------------------------------------------------------------------
1127 Retrieve the global namespace.
1129 It is recommended to put your namespace inside the global namespace, and
1130 then add your classes and functions to it, rather than adding many classes
1131 and functions directly to the global namespace.
1133 inline Namespace getGlobalNamespace (lua_State* L)
1135 return Namespace::getGlobalNamespace (L);