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

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Sébastien Royer
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
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

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

Nigel Atkinson-2

Hi Sebastien,

Just thinking off the top of my head but you might have some luck with lua_setfenv in Lua 5.1 or the _ENV table in Lua 5.2

You might be able to set the class instance as the environment with setfenv, and then load your script and run it, using lua_loadfile, etc.

HTH

Nigel

 

On Fri, September 21, 2012 12:18 am, S�bastien Royer wrote:
> 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
> _______________________________________________
> luabind-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/luabind-user
>


------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

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

Nigel Atkinson-2
In reply to this post by Sébastien Royer

Hi Sebastien,

Just thinking off the top of my head but you might have some luck with lua_setfenv in Lua 5.1 or the _ENV table in Lua 5.2

You might be able to set the class instance as the environment with setfenv, and then load your script and run it, using lua_loadfile, etc.

HTH

Nigel

 

On Fri, September 21, 2012 12:18 am, S�bastien Royer wrote:
> 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
> _______________________________________________
> luabind-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/luabind-user
>


------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user