?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. // https://github.com/vinniefalco/LuaBridge
  2. // Copyright 2019, Dmitry Tarakanov
  3. // Copyright 2012, Vinnie Falco <vinnie.falco@gmail.com>
  4. // SPDX-License-Identifier: MIT
  5.  
  6. #pragma once
  7.  
  8. #include <LuaBridge/detail/Config.h>
  9. #include <LuaBridge/detail/FuncTraits.h>
  10.  
  11. #include <string>
  12.  
  13. namespace luabridge {
  14.  
  15. namespace detail {
  16.  
  17. // We use a structure so we can define everything in the header.
  18. //
  19. struct CFunc
  20. {
  21.     static void addGetter(lua_State* L, const char* name, int tableIndex)
  22.     {
  23.         assert(lua_istable(L, tableIndex));
  24.         assert(lua_iscfunction(L, -1)); // Stack: getter
  25.  
  26.         lua_rawgetp(L, tableIndex, getPropgetKey()); // Stack: getter, propget table (pg)
  27.         lua_pushvalue(L, -2); // Stack: getter, pg, getter
  28.         rawsetfield(L, -2, name); // Stack: getter, pg
  29.         lua_pop(L, 2); // Stack: -
  30.     }
  31.  
  32.     static void addSetter(lua_State* L, const char* name, int tableIndex)
  33.     {
  34.         assert(lua_istable(L, tableIndex));
  35.         assert(lua_iscfunction(L, -1)); // Stack: setter
  36.  
  37.         lua_rawgetp(L, tableIndex, getPropsetKey()); // Stack: setter, propset table (ps)
  38.         lua_pushvalue(L, -2); // Stack: setter, ps, setter
  39.         rawsetfield(L, -2, name); // Stack: setter, ps
  40.         lua_pop(L, 2); // Stack: -
  41.     }
  42.  
  43.     //----------------------------------------------------------------------------
  44.     /**
  45.         __index metamethod for a namespace or class static and non-static members.
  46.         Retrieves functions from metatables and properties from propget tables.
  47.         Looks through the class hierarchy if inheritance is present.
  48.     */
  49.     static int indexMetaMethod(lua_State* L)
  50.     {
  51.         assert(lua_istable(L, 1) ||
  52.                lua_isuserdata(L, 1)); // Stack (further not shown): table | userdata, name
  53.  
  54.         lua_getmetatable(L, 1); // Stack: class/const table (mt)
  55.         assert(lua_istable(L, -1));
  56.  
  57.         for (;;)
  58.         {
  59.             lua_pushvalue(L, 2); // Stack: mt, field name
  60.             lua_rawget(L, -2); // Stack: mt, field | nil
  61.  
  62.             if (lua_iscfunction(L, -1)) // Stack: mt, field
  63.             {
  64.                 lua_remove(L, -2); // Stack: field
  65.                 return 1;
  66.             }
  67.  
  68.             assert(lua_isnil(L, -1)); // Stack: mt, nil
  69.             lua_pop(L, 1); // Stack: mt
  70.  
  71.             lua_rawgetp(L, -1, getPropgetKey()); // Stack: mt, propget table (pg)
  72.             assert(lua_istable(L, -1));
  73.  
  74.             lua_pushvalue(L, 2); // Stack: mt, pg, field name
  75.             lua_rawget(L, -2); // Stack: mt, pg, getter | nil
  76.             lua_remove(L, -2); // Stack: mt, getter | nil
  77.  
  78.             if (lua_iscfunction(L, -1)) // Stack: mt, getter
  79.             {
  80.                 lua_remove(L, -2); // Stack: getter
  81.                 lua_pushvalue(L, 1); // Stack: getter, table | userdata
  82.                 lua_call(L, 1, 1); // Stack: value
  83.                 return 1;
  84.             }
  85.  
  86.             assert(lua_isnil(L, -1)); // Stack: mt, nil
  87.             lua_pop(L, 1); // Stack: mt
  88.  
  89.             // It may mean that the field may be in const table and it's constness violation.
  90.             // Don't check that, just return nil
  91.  
  92.             // Repeat the lookup in the parent metafield,
  93.             // or return nil if the field doesn't exist.
  94.             lua_rawgetp(L, -1, getParentKey()); // Stack: mt, parent mt | nil
  95.  
  96.             if (lua_isnil(L, -1)) // Stack: mt, nil
  97.             {
  98.                 lua_remove(L, -2); // Stack: nil
  99.                 return 1;
  100.             }
  101.  
  102.             // Removethe  metatable and repeat the search in the parent one.
  103.             assert(lua_istable(L, -1)); // Stack: mt, parent mt
  104.             lua_remove(L, -2); // Stack: parent mt
  105.         }
  106.  
  107.         // no return
  108.     }
  109.  
  110.     //----------------------------------------------------------------------------
  111.     /**
  112.         __newindex metamethod for namespace or class static members.
  113.         Retrieves properties from propset tables.
  114.     */
  115.     static int newindexStaticMetaMethod(lua_State* L) { return newindexMetaMethod(L, false); }
  116.  
  117.     //----------------------------------------------------------------------------
  118.     /**
  119.         __newindex metamethod for non-static members.
  120.         Retrieves properties from propset tables.
  121.     */
  122.     static int newindexObjectMetaMethod(lua_State* L) { return newindexMetaMethod(L, true); }
  123.  
  124.     static int newindexMetaMethod(lua_State* L, bool pushSelf)
  125.     {
  126.         assert(
  127.             lua_istable(L, 1) ||
  128.             lua_isuserdata(L, 1)); // Stack (further not shown): table | userdata, name, new value
  129.  
  130.         lua_getmetatable(L, 1); // Stack: metatable (mt)
  131.         assert(lua_istable(L, -1));
  132.  
  133.         for (;;)
  134.         {
  135.             lua_rawgetp(L, -1, getPropsetKey()); // Stack: mt, propset table (ps) | nil
  136.  
  137.             if (lua_isnil(L, -1)) // Stack: mt, nil
  138.             {
  139.                 lua_pop(L, 2); // Stack: -
  140.                 return luaL_error(L, "No member named '%s'", lua_tostring(L, 2));
  141.             }
  142.  
  143.             assert(lua_istable(L, -1));
  144.  
  145.             lua_pushvalue(L, 2); // Stack: mt, ps, field name
  146.             lua_rawget(L, -2); // Stack: mt, ps, setter | nil
  147.             lua_remove(L, -2); // Stack: mt, setter | nil
  148.  
  149.             if (lua_iscfunction(L, -1)) // Stack: mt, setter
  150.             {
  151.                 lua_remove(L, -2); // Stack: setter
  152.                 if (pushSelf)
  153.                 {
  154.                     lua_pushvalue(L, 1); // Stack: setter, table | userdata
  155.                 }
  156.                 lua_pushvalue(L, 3); // Stack: setter, table | userdata, new value
  157.                 lua_call(L, pushSelf ? 2 : 1, 0); // Stack: -
  158.                 return 0;
  159.             }
  160.  
  161.             assert(lua_isnil(L, -1)); // Stack: mt, nil
  162.             lua_pop(L, 1); // Stack: mt
  163.  
  164.             lua_rawgetp(L, -1, getParentKey()); // Stack: mt, parent mt | nil
  165.  
  166.             if (lua_isnil(L, -1)) // Stack: mt, nil
  167.             {
  168.                 lua_pop(L, 1); // Stack: -
  169.                 return luaL_error(L, "No writable member '%s'", lua_tostring(L, 2));
  170.             }
  171.  
  172.             assert(lua_istable(L, -1)); // Stack: mt, parent mt
  173.             lua_remove(L, -2); // Stack: parent mt
  174.             // Repeat the search in the parent
  175.         }
  176.  
  177.         // no return
  178.     }
  179.  
  180.     //----------------------------------------------------------------------------
  181.     /**
  182.         lua_CFunction to report an error writing to a read-only value.
  183.  
  184.         The name of the variable is in the first upvalue.
  185.     */
  186.     static int readOnlyError(lua_State* L)
  187.     {
  188.         std::string s;
  189.  
  190.         s = s + "'" + lua_tostring(L, lua_upvalueindex(1)) + "' is read-only";
  191.  
  192.         return luaL_error(L, s.c_str());
  193.     }
  194.  
  195.     //----------------------------------------------------------------------------
  196.     /**
  197.         lua_CFunction to get a variable.
  198.  
  199.         This is used for global variables or class static data members.
  200.  
  201.         The pointer to the data is in the first upvalue.
  202.     */
  203.     template<class T>
  204.     static int getVariable(lua_State* L)
  205.     {
  206.         assert(lua_islightuserdata(L, lua_upvalueindex(1)));
  207.         T const* ptr = static_cast<T const*>(lua_touserdata(L, lua_upvalueindex(1)));
  208.         assert(ptr != 0);
  209.         Stack<T>::push(L, *ptr);
  210.         return 1;
  211.     }
  212.  
  213.     //----------------------------------------------------------------------------
  214.     /**
  215.         lua_CFunction to set a variable.
  216.  
  217.         This is used for global variables or class static data members.
  218.  
  219.         The pointer to the data is in the first upvalue.
  220.     */
  221.     template<class T>
  222.     static int setVariable(lua_State* L)
  223.     {
  224.         assert(lua_islightuserdata(L, lua_upvalueindex(1)));
  225.         T* ptr = static_cast<T*>(lua_touserdata(L, lua_upvalueindex(1)));
  226.         assert(ptr != 0);
  227.         *ptr = Stack<T>::get(L, 1);
  228.         return 0;
  229.     }
  230.  
  231.     //----------------------------------------------------------------------------
  232.     /**
  233.         lua_CFunction to call a function with a return value.
  234.  
  235.         This is used for global functions, global properties, class static methods,
  236.         and class static properties.
  237.  
  238.         The function pointer (lightuserdata) in the first upvalue.
  239.     */
  240.     template<class FnPtr>
  241.     struct Call
  242.     {
  243.         typedef typename FuncTraits<FnPtr>::Params Params;
  244.         typedef typename FuncTraits<FnPtr>::ReturnType ReturnType;
  245.  
  246.         static int f(lua_State* L)
  247.         {
  248.             assert(lua_islightuserdata(L, lua_upvalueindex(1)));
  249.             FnPtr fnptr = reinterpret_cast<FnPtr>(lua_touserdata(L, lua_upvalueindex(1)));
  250.             assert(fnptr != 0);
  251.             return Invoke<ReturnType, Params, 1>::run(L, fnptr);
  252.         }
  253.     };
  254.  
  255.     //----------------------------------------------------------------------------
  256.     /**
  257.         lua_CFunction to call a class member function with a return value.
  258.  
  259.         The member function pointer is in the first upvalue.
  260.         The class userdata object is at the top of the Lua stack.
  261.     */
  262.     template<class MemFnPtr>
  263.     struct CallMember
  264.     {
  265.         typedef typename FuncTraits<MemFnPtr>::ClassType T;
  266.         typedef typename FuncTraits<MemFnPtr>::Params Params;
  267.         typedef typename FuncTraits<MemFnPtr>::ReturnType ReturnType;
  268.  
  269.         static int f(lua_State* L)
  270.         {
  271.             assert(isfulluserdata(L, lua_upvalueindex(1)));
  272.             T* const t = Userdata::get<T>(L, 1, false);
  273.             MemFnPtr const& fnptr =
  274.                 *static_cast<MemFnPtr const*>(lua_touserdata(L, lua_upvalueindex(1)));
  275.             assert(fnptr != 0);
  276.             return Invoke<ReturnType, Params, 2>::run(L, t, fnptr);
  277.         }
  278.     };
  279.  
  280.     template<class MemFnPtr>
  281.     struct CallConstMember
  282.     {
  283.         typedef typename FuncTraits<MemFnPtr>::ClassType T;
  284.         typedef typename FuncTraits<MemFnPtr>::Params Params;
  285.         typedef typename FuncTraits<MemFnPtr>::ReturnType ReturnType;
  286.  
  287.         static int f(lua_State* L)
  288.         {
  289.             assert(isfulluserdata(L, lua_upvalueindex(1)));
  290.             T const* const t = Userdata::get<T>(L, 1, true);
  291.             MemFnPtr const& fnptr =
  292.                 *static_cast<MemFnPtr const*>(lua_touserdata(L, lua_upvalueindex(1)));
  293.             assert(fnptr != 0);
  294.             return Invoke<ReturnType, Params, 2>::run(L, t, fnptr);
  295.         }
  296.     };
  297.  
  298.     //--------------------------------------------------------------------------
  299.     /**
  300.         lua_CFunction to call a class member lua_CFunction.
  301.  
  302.         The member function pointer is in the first upvalue.
  303.         The object userdata ('this') value is at top ot the Lua stack.
  304.     */
  305.     template<class T>
  306.     struct CallMemberCFunction
  307.     {
  308.         static int f(lua_State* L)
  309.         {
  310.             assert(isfulluserdata(L, lua_upvalueindex(1)));
  311.             typedef int (T::*MFP)(lua_State * L);
  312.             T* const t = Userdata::get<T>(L, 1, false);
  313.             MFP const& fnptr = *static_cast<MFP const*>(lua_touserdata(L, lua_upvalueindex(1)));
  314.             assert(fnptr != 0);
  315.             return (t->*fnptr)(L);
  316.         }
  317.     };
  318.  
  319.     template<class T>
  320.     struct CallConstMemberCFunction
  321.     {
  322.         static int f(lua_State* L)
  323.         {
  324.             assert(isfulluserdata(L, lua_upvalueindex(1)));
  325.             typedef int (T::*MFP)(lua_State * L);
  326.             T const* const t = Userdata::get<T>(L, 1, true);
  327.             MFP const& fnptr = *static_cast<MFP const*>(lua_touserdata(L, lua_upvalueindex(1)));
  328.             assert(fnptr != 0);
  329.             return (t->*fnptr)(L);
  330.         }
  331.     };
  332.  
  333.     //--------------------------------------------------------------------------
  334.     /**
  335.         lua_CFunction to call on a object.
  336.  
  337.         The proxy function pointer (lightuserdata) is in the first upvalue.
  338.         The class userdata object is at the top of the Lua stack.
  339.     */
  340.     template<class FnPtr>
  341.     struct CallProxyFunction
  342.     {
  343.         using Params = typename FuncTraits<FnPtr>::Params;
  344.         using ReturnType = typename FuncTraits<FnPtr>::ReturnType;
  345.  
  346.         static int f(lua_State* L)
  347.         {
  348.             assert(lua_islightuserdata(L, lua_upvalueindex(1)));
  349.             auto fnptr = reinterpret_cast<FnPtr>(lua_touserdata(L, lua_upvalueindex(1)));
  350.             assert(fnptr != 0);
  351.             return Invoke<ReturnType, Params, 1>::run(L, fnptr);
  352.         }
  353.     };
  354.  
  355.     template<class Functor>
  356.     struct CallProxyFunctor
  357.     {
  358.         using Params = typename FuncTraits<Functor>::Params;
  359.         using ReturnType = typename FuncTraits<Functor>::ReturnType;
  360.  
  361.         static int f(lua_State* L)
  362.         {
  363.             assert(isfulluserdata(L, lua_upvalueindex(1)));
  364.             Functor& fn = *static_cast<Functor*>(lua_touserdata(L, lua_upvalueindex(1)));
  365.             return Invoke<ReturnType, Params, 1>::run(L, fn);
  366.         }
  367.     };
  368.  
  369.     //--------------------------------------------------------------------------
  370.  
  371.     // SFINAE Helpers
  372.  
  373.     template<class MemFnPtr, bool isConst>
  374.     struct CallMemberFunctionHelper
  375.     {
  376.         static void add(lua_State* L, char const* name, MemFnPtr mf)
  377.         {
  378.             new (lua_newuserdata(L, sizeof(MemFnPtr))) MemFnPtr(mf);
  379.             lua_pushcclosure(L, &CallConstMember<MemFnPtr>::f, 1);
  380.             lua_pushvalue(L, -1);
  381.             rawsetfield(L, -5, name); // const table
  382.             rawsetfield(L, -3, name); // class table
  383.         }
  384.     };
  385.  
  386.     template<class MemFnPtr>
  387.     struct CallMemberFunctionHelper<MemFnPtr, false>
  388.     {
  389.         static void add(lua_State* L, char const* name, MemFnPtr mf)
  390.         {
  391.             new (lua_newuserdata(L, sizeof(MemFnPtr))) MemFnPtr(mf);
  392.             lua_pushcclosure(L, &CallMember<MemFnPtr>::f, 1);
  393.             rawsetfield(L, -3, name); // class table
  394.         }
  395.     };
  396.  
  397.     //--------------------------------------------------------------------------
  398.     /**
  399.         __gc metamethod for a class.
  400.     */
  401.     template<class C>
  402.     static int gcMetaMethod(lua_State* L)
  403.     {
  404.         Userdata* const ud = Userdata::getExact<C>(L, 1);
  405.         ud->~Userdata();
  406.         return 0;
  407.     }
  408.  
  409.     /**
  410.         __gc metamethod for an arbitrary class.
  411.     */
  412.     template<class T>
  413.     static int gcMetaMethodAny(lua_State* L)
  414.     {
  415.         assert(isfulluserdata(L, 1));
  416.         T* t = static_cast<T*>(lua_touserdata(L, 1));
  417.         t->~T();
  418.         return 0;
  419.     }
  420.  
  421.     //--------------------------------------------------------------------------
  422.     /**
  423.         lua_CFunction to get a class data member.
  424.  
  425.         The pointer-to-member is in the first upvalue.
  426.         The class userdata object is at the top of the Lua stack.
  427.     */
  428.     template<class C, typename T>
  429.     static int getProperty(lua_State* L)
  430.     {
  431.         C* const c = Userdata::get<C>(L, 1, true);
  432.         T C::** mp = static_cast<T C::**>(lua_touserdata(L, lua_upvalueindex(1)));
  433.         try
  434.         {
  435.             Stack<T&>::push(L, c->**mp);
  436.         }
  437.         catch (const std::exception& e)
  438.         {
  439.             luaL_error(L, e.what());
  440.         }
  441.         return 1;
  442.     }
  443.  
  444.     //--------------------------------------------------------------------------
  445.     /**
  446.         lua_CFunction to set a class data member.
  447.  
  448.         The pointer-to-member is in the first upvalue.
  449.         The class userdata object is at the top of the Lua stack.
  450.     */
  451.     template<class C, typename T>
  452.     static int setProperty(lua_State* L)
  453.     {
  454.         C* const c = Userdata::get<C>(L, 1, false);
  455.         T C::** mp = static_cast<T C::**>(lua_touserdata(L, lua_upvalueindex(1)));
  456.         try
  457.         {
  458.             c->** mp = Stack<T>::get(L, 2);
  459.         }
  460.         catch (const std::exception& e)
  461.         {
  462.             luaL_error(L, e.what());
  463.         }
  464.         return 0;
  465.     }
  466. };
  467.  
  468. } // namespace detail
  469.  
  470. } // namespace luabridge
  471.