How to map a C++ object as a global "context" of a lua_State ?

Sébastien Royer <[email protected]> Thu, 20 Sep 2012 16:18:14 +0200
Newsgroups gmane.comp.lang.lua.bind.user
Message-ID <CAASp29PXdMEvSZo5uibasw8vxb4w3FcRfL3kbzq3f9c3bu+sLA@mail.gmail.com>
Hi,

   I have C++ classes bound to Lua with luabind... So I can use
instances of them in Lua scripts without any problem.
   Also I have a special need.
   If I have one class "MyClass" bound to lua, I would like my script
to be executed in the "context" of a MyClass instance.

   For example:

   Class MyClass
   {
   public:
         MyClass() {}
         virtual ~MyClass() {}

   public:
         void method(const std::string& p_rstrText) { std::cout <<
"method() called with " << p_rstrText << std::endl; }
         int property() { return 123; }
   };

   In a "classical" binding, my script can access MyClass methods like this:

     myInstance = MyClass()
     myInstance.method("Hello")
     a = myInstance.property

   ... but ...
   I need to "declare" one existing instance of MyClass in a lua_State
so that the following script can access method() & property() members
like this:

   Lua script example:

      print("Example")
      method("Hello")
      print("MyCalls.property = ", property)

(so without "myInstance." !)

   The idea is that MyClass instances will have one script associated
to it that could be executed in the context of its "embedding"
instance. (Like in Unity3D: A GameObject "Monobehaviour" script
(Javascript) that can access "its" GameObject instance properties
without any "this" or "self" accessors).

  I believe it is really feasible without luabind, but I would like
(and need in fact) to keep all the benefits of luabind, and also do
this stuff properly with luabind.

  Does anyone have an idea of an how to ?

Thx in advance

Sebastien

------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://ad.doubleclick.net/clk;258768047;13503038;j?
http://info.appdynamics.com/FreeJavaPerformanceDownload.html