lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Unfortunately one can't emulate numbers with userdata in Lua. This makes the original poster's example impossible to do with no modifications to Lua core. For whatever reasons (minimalism, simplicity and performance I would think) one can only do math on two values of the same type in Lua and type coercion is not supported.

Alex

 -----Original Message-----
From: 	Tobias Käs [mailto:tobias.kaes@gmx.de] 
Sent:	Monday, June 16, 2003 5:28 AM
To:	Lua list
Subject:	Re: Passing values by reference?

Tables and Userdata are passed by reference, so you can use it to your
advantage, by wrapping the actual values in a Table or Userdata. Most times
Tables are best (at least for me) since you can wrap all variables of a
group in the same table.

----- Original Message ----- 
From: "Sacha Varma" <sacha@ssl.co.uk>
To: <lua@bazar2.conectiva.com.br>
Sent: Monday, June 16, 2003 12:03 AM
Subject: Passing values by reference?


>
> Can arguments and return values be passed by reference?
>
> I'm trying to work out how I would bind the following C function:
>
>   int * inc (int *p)
>   {
>     (*p)++;
>     return p;
>   }
>
> e.g.
>
>   int x = 6;
>   inc(inc(&x));
>   assert(x == 8);
>
> (This is a trivial example but is representative of a class of functions
> in the C++ API I am trying to bind.)
>
> In my bindings, C ints are represented in Lua as numbers. So, the above C
> example would be as follows in lua:
>
>   x = 6
>   inc(inc(x))
>   -- x now equals 8
>
> Any thoughts? If there is no way to pass or return values by reference, do
> I just outlaw pointers (and C++ references) in the C++ API?
>
>
> BTW the binding is automated by way of a SWIG Lua 5.0 language module; if
> anyone else is working on one, I would be happy to stop developing it and
> be a guinea pig for someone else's.
>
> A real function I'm trying to bind (if the above does not please you) is
>
>         const Vector3 & Vector3::Normalize()
>         {
>                 float mag = Magnitude();
>
>                 if(mag == 0.0f)
>                         return *this;
>
>                 float oo_mag = 1.0f/mag;
>                 mx *= oo_mag;
>                 my *= oo_mag;
>                 mz *= oo_mag;
>
>                 return *this;
>         }
>
>
>