SWIG and Lua: typemap for array

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

SWIG and Lua: typemap for array

Ervin Hegedüs
Hi,

I'ld like to make a library for Lua from a C code. There is a variable, which has int[10] type.

Without typemap, Lua shows that variable as userdata. (In other languages also shows any different types in case of referenc of that variable, eg. Python shows it as SWIG_object (or something like this - it doesn't matter). In case of other languages (Python, Perl, Tcl) I could fixup this problem with own typemap.)

I'm trying this way:

%typemap(out) int [ANY] {
  int len,i;
  len = $1_dim0;
  lua_createtable (L, len, 0);
  for(i=0; i<len; i++) {
    lua_pushinteger(L, (lua_Integer)$1[i]);
  }
  lua_settable(L, (-1)*(len+1));
}

but in Lua script shows only this:

nil

What'em I missing? Could anybody helps in this problem?


Thanks,

a.



------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|

Re: SWIG and Lua: typemap for array

Alan W. Irwin
On 2016-10-22 19:43+0200 Ervin Hegedüs wrote:

> Hi,
>
> I'ld like to make a library for Lua from a C code. There is a variable,
> which has int[10] type.
>
> Without typemap, Lua shows that variable as userdata. (In other languages
> also shows any different types in case of referenc of that variable, eg.
> Python shows it as SWIG_object (or something like this - it doesn't
> matter). In case of other languages (Python, Perl, Tcl) I could fixup this
> problem with own typemap.)
>
> I'm trying this way:
>
> %typemap(out) int [ANY] {
>  int len,i;
>  len = $1_dim0;
>  lua_createtable (L, len, 0);
>  for(i=0; i<len; i++) {
>    lua_pushinteger(L, (lua_Integer)$1[i]);
>  }
>  lua_settable(L, (-1)*(len+1));
> }
>
> but in Lua script shows only this:
>
> nil
>
> What'em I missing? Could anybody helps in this problem?

Hi Ervin:

I can't help you with any details because the swig-generated PLplot lua
binding is not my code, but PLplot uses the following typemap for input
integer arrays.

//--------------------------------------------------------------------------
//                         PLINT arrays
//--------------------------------------------------------------------------

// With preceding count
%typemap ( in ) ( PLINT n, const PLINT * Array )
{
     $2 = (PLINT *) LUA_get_int_num_array_var( L, $input, &$1 );
     if ( !$2 )
         SWIG_fail;
     Alen = $1;
}
%typemap ( freearg ) ( PLINT n, const PLINT * Array )
{
     LUA_FREE_ARRAY( $2 );
}

Note, that PLINT is a macro defining a specific integer type for
PLplot, and we use redacted argument lists (where the array dimension
is not specifically mentioned in the Lua call because that information
is already available via the Lua array).  That redaction is why you
see the "n" argument included above. Also, LUA_get_int_num_array_var
and LUA_FREE_ARRAY are much! more complex macros that we #define to
vastly simplify implementing the above typemap and many others that we
use.  Most of PLplot array use in argument lists is for input arrays
like above, but some of our typemaps are also for output arrays.  So
the above is just a tiny part of the picture but should be enough so
you will know whether you want to look further at our Lua binding to
get some ideas.  You can find the above typemap in
bindings/lua/plplotluac.i in the working directory of our git
repository which you can access following the prescription at
<https://sourceforge.net/p/plplot/plplot/ci/master/tree/>.

Just for your information the core PLplot library is written in C; we
generate our Python, Java, Octave, and Lua bindings of that library
using swig; and we confirm with tests that our standard set of ~30
examples written in all those languages produce identical plot results
as our standard set of those same examples written in C. So we are
pretty happy with the completeness and accuracy of our swig-generated
bindings!

Alan
__________________________
Alan W. Irwin

Astronomical research affiliation with Department of Physics and Astronomy,
University of Victoria (astrowww.phys.uvic.ca).

Programming affiliations with the FreeEOS equation-of-state
implementation for stellar interiors (freeeos.sf.net); the Time
Ephemerides project (timeephem.sf.net); PLplot scientific plotting
software package (plplot.sf.net); the libLASi project
(unifont.org/lasi); the Loads of Linux Links project (loll.sf.net);
and the Linux Brochure Project (lbproject.sf.net).
__________________________

Linux-powered Science
__________________________

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|

Re: SWIG and Lua: typemap for array

Ervin Hegedüs
HI Alan,

many thanks for your answer,

On Sun, Oct 23, 2016 at 10:26:49AM -0700, Alan W. Irwin wrote:
> On 2016-10-22 19:43+0200 Ervin Hegedüs wrote:
>
> >Hi,
> >
> >I'ld like to make a library for Lua from a C code. There is a variable,
> >which has int[10] type.
[...]

> >I'm trying this way:
> >
> >%typemap(out) int [ANY] {
> > int len,i;
> > len = $1_dim0;
> > lua_createtable (L, len, 0);
> > for(i=0; i<len; i++) {
> >   lua_pushinteger(L, (lua_Integer)$1[i]);
> > }
> > lua_settable(L, (-1)*(len+1));
> >}
> >
> >but in Lua script shows only this:
> >
> >nil
> >
> >What'em I missing? Could anybody helps in this problem?
>
> Hi Ervin:
>
> I can't help you with any details because the swig-generated PLplot lua
> binding is not my code, but PLplot uses the following typemap for input
> integer arrays.
>
> //--------------------------------------------------------------------------
> //                         PLINT arrays
> //--------------------------------------------------------------------------
>
> // With preceding count
> %typemap ( in ) ( PLINT n, const PLINT * Array )
> {
>     $2 = (PLINT *) LUA_get_int_num_array_var( L, $input, &$1 );
>     if ( !$2 )
>         SWIG_fail;
>     Alen = $1;
> }
> %typemap ( freearg ) ( PLINT n, const PLINT * Array )
> {
>     LUA_FREE_ARRAY( $2 );
> }
>
> Note, that PLINT is a macro defining a specific integer type for
> PLplot, and we use redacted argument lists (where the array dimension
> is not specifically mentioned in the Lua call because that information
> is already available via the Lua array).  That redaction is why you
> see the "n" argument included above. Also, LUA_get_int_num_array_var
> and LUA_FREE_ARRAY are much! more complex macros that we #define to
> vastly simplify implementing the above typemap and many others that we
> use.  Most of PLplot array use in argument lists is for input arrays
> like above, but some of our typemaps are also for output arrays.  So
> the above is just a tiny part of the picture but should be enough so
> you will know whether you want to look further at our Lua binding to
> get some ideas.  You can find the above typemap in
> bindings/lua/plplotluac.i in the working directory of our git
> repository which you can access following the prescription at
> <https://sourceforge.net/p/plplot/plplot/ci/master/tree/>.

thanks for detailed notes - may be I'll see the source of Plplot
to understand, how Lua binding works.

> Just for your information the core PLplot library is written in C; we
> generate our Python, Java, Octave, and Lua bindings of that library
> using swig; and we confirm with tests that our standard set of ~30
> examples written in all those languages produce identical plot results
> as our standard set of those same examples written in C. So we are
> pretty happy with the completeness and accuracy of our swig-generated
> bindings!

right, thanks for this info too.

Now I'ld have to solve this problem in Swig for Lua, there are
too much bindings had written and tested with it.

If everything works as well (and I'll have enough time :)), I'll
see that depptly.


Anyway, there are several other problems in Lua binding - see my
other e-mails.


Many thanks again,


a.
 

--
I � UTF-8

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|

Re: SWIG and Lua: typemap for array

William S Fulton
In reply to this post by Ervin Hegedüs
On 22 October 2016 at 18:43, Ervin Hegedüs <[hidden email]> wrote:

> Hi,
>
> I'ld like to make a library for Lua from a C code. There is a variable,
> which has int[10] type.
>
> Without typemap, Lua shows that variable as userdata. (In other languages
> also shows any different types in case of referenc of that variable, eg.
> Python shows it as SWIG_object (or something like this - it doesn't matter).
> In case of other languages (Python, Perl, Tcl) I could fixup this problem
> with own typemap.)
>
> I'm trying this way:
>
> %typemap(out) int [ANY] {
>   int len,i;
>   len = $1_dim0;
>   lua_createtable (L, len, 0);
>   for(i=0; i<len; i++) {
>     lua_pushinteger(L, (lua_Integer)$1[i]);
>   }
>   lua_settable(L, (-1)*(len+1));
> }
>
> but in Lua script shows only this:
>
> nil
>
> What'em I missing? Could anybody helps in this problem?


Have you looked at the Lua typemaps shipped with SWIG? I don't know
Lua, but all the "out" typemaps increment SWIG_arg, so I'd try adding
that in.

William

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|

Re: SWIG and Lua: typemap for array

Ervin Hegedüs
Hi William,

thanks for the answer,

On Sun, Oct 23, 2016 at 08:15:28PM +0100, William S Fulton wrote:

> On 22 October 2016 at 18:43, Ervin Hegedüs <[hidden email]> wrote:
> > I'm trying this way:
> >
> > %typemap(out) int [ANY] {
> >   int len,i;
> >   len = $1_dim0;
> >   lua_createtable (L, len, 0);
> >   for(i=0; i<len; i++) {
> >     lua_pushinteger(L, (lua_Integer)$1[i]);
> >   }
> >   lua_settable(L, (-1)*(len+1));
> > }
> >
> > but in Lua script shows only this:
> >
> > nil
> >
> > What'em I missing? Could anybody helps in this problem?
>
>
> Have you looked at the Lua typemaps shipped with SWIG? I don't know
> Lua, but all the "out" typemaps increment SWIG_arg, so I'd try adding
> that in.

do you mean should I return the SWIG_arg from that typemap?

I've tested, but it doesn't work: the array in Lua is still "nil"
value, the expected value is "table: 0xaddr", when I just
reference that in a print() function.


thanks,

a.

--
I � UTF-8

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|

Re: SWIG and Lua: typemap for array

William S Fulton
On 23 October 2016 at 20:30, Ervin Hegedüs <[hidden email]> wrote:

> Hi William,
>
> thanks for the answer,
>
> On Sun, Oct 23, 2016 at 08:15:28PM +0100, William S Fulton wrote:
>> On 22 October 2016 at 18:43, Ervin Hegedüs <[hidden email]> wrote:
>> > I'm trying this way:
>> >
>> > %typemap(out) int [ANY] {
>> >   int len,i;
>> >   len = $1_dim0;
>> >   lua_createtable (L, len, 0);
>> >   for(i=0; i<len; i++) {
>> >     lua_pushinteger(L, (lua_Integer)$1[i]);
>> >   }
>> >   lua_settable(L, (-1)*(len+1));
>> > }
>> >
>> > but in Lua script shows only this:
>> >
>> > nil
>> >
>> > What'em I missing? Could anybody helps in this problem?
>>
>>
>> Have you looked at the Lua typemaps shipped with SWIG? I don't know
>> Lua, but all the "out" typemaps increment SWIG_arg, so I'd try adding
>> that in.
>
> do you mean should I return the SWIG_arg from that typemap?
>
> I've tested, but it doesn't work: the array in Lua is still "nil"
> value, the expected value is "table: 0xaddr", when I just
> reference that in a print() function.
>
The reference typemaps I suggested you look at are in the Lib
directory in the SWIG distribution tarball.

William

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|

Re: SWIG and Lua: typemap for array

Ervin Hegedüs
Hí William,

Thanks - I've checked that, but there aren't typemap  for scalar array.


Regards,

a.

On Sunday, October 23, 2016, William S Fulton <[hidden email]> wrote:
On 23 October 2016 at 20:30, Ervin Hegedüs <<a href="javascript:;" onclick="_e(event, &#39;cvml&#39;, &#39;airween@gmail.com&#39;)">airween@...> wrote:
> Hi William,
>
> thanks for the answer,
>
> On Sun, Oct 23, 2016 at 08:15:28PM +0100, William S Fulton wrote:
>> On 22 October 2016 at 18:43, Ervin Hegedüs <<a href="javascript:;" onclick="_e(event, &#39;cvml&#39;, &#39;airween@gmail.com&#39;)">airween@...> wrote:
>> > I'm trying this way:
>> >
>> > %typemap(out) int [ANY] {
>> >   int len,i;
>> >   len = $1_dim0;
>> >   lua_createtable (L, len, 0);
>> >   for(i=0; i<len; i++) {
>> >     lua_pushinteger(L, (lua_Integer)$1[i]);
>> >   }
>> >   lua_settable(L, (-1)*(len+1));
>> > }
>> >
>> > but in Lua script shows only this:
>> >
>> > nil
>> >
>> > What'em I missing? Could anybody helps in this problem?
>>
>>
>> Have you looked at the Lua typemaps shipped with SWIG? I don't know
>> Lua, but all the "out" typemaps increment SWIG_arg, so I'd try adding
>> that in.
>
> do you mean should I return the SWIG_arg from that typemap?
>
> I've tested, but it doesn't work: the array in Lua is still "nil"
> value, the expected value is "table: 0xaddr", when I just
> reference that in a print() function.
>
The reference typemaps I suggested you look at are in the Lib
directory in the SWIG distribution tarball.

William

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|

Re: SWIG and Lua: typemap for array

Ervin Hegedüs
In reply to this post by William S Fulton
Hi William

On Sun, Oct 23, 2016 at 08:51:40PM +0100, William S Fulton wrote:

> >>
> >> Have you looked at the Lua typemaps shipped with SWIG? I don't know
> >> Lua, but all the "out" typemaps increment SWIG_arg, so I'd try adding
> >> that in.
> >
> > do you mean should I return the SWIG_arg from that typemap?
> >
> > I've tested, but it doesn't work: the array in Lua is still "nil"
> > value, the expected value is "table: 0xaddr", when I just
> > reference that in a print() function.
> >
> The reference typemaps I suggested you look at are in the Lib
> directory in the SWIG distribution tarball.

I reviewed Lua specified typemaps again, and now I saw, there
isn't return value, but at the end of the typemap function is a
SWIG_arg++ operand.

Now I put this to the end of my own typemap. The Lua interpreted
now gives me a scalas 0 (numeric type), not nil.

Here is the typemap:

%typemap(out) int [ANY] {
  int len,i;
  len = $1_dim0;
  lua_createtable (L, len, 0);
  for(i=0; i<len; i++) {
    lua_pushinteger(L, (lua_Integer)$1[i]);
  }
  /* lua_settable(L, (-1)*(len+1)); */
  SWIG_arg++;
}

The lua_settable() is no matter, if that commented out or not.
The Lua gives "0" in both cases.


Any other idea?


Thanks,


a.

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|

Re: SWIG and Lua: typemap for array - solved

Ervin Hegedüs
In reply to this post by Ervin Hegedüs
Hi all,

looks like I've found the solution:


%typemap(out) int [ANY] {
  int len,i;
  len = $1_dim0;
  lua_createtable (L, len, 0);
  for(i=0; i<len; i++) {
    lua_pushinteger(L, (i+1));              // list index - Lua starts list index with 1
    lua_pushinteger(L, (lua_Integer)$1[i]); // list value
    lua_rawset(L, -3);                      // push item pair to table
  }
  SWIG_arg++;
}


That's it.


Thanks for all.


Anyway, there are few other problems with Lua binding:

https://sourceforge.net/p/swig/mailman/message/35444939/

If anybody has any idea, please let me share!


Thanks again,


a.

On Sat, Oct 22, 2016 at 07:43:49PM +0200, Ervin Hegedüs wrote:

> Hi,
>
> I'ld like to make a library for Lua from a C code. There is a variable,
> which has int[10] type.
>
> Without typemap, Lua shows that variable as userdata. (In other languages
> also shows any different types in case of referenc of that variable, eg.
> Python shows it as SWIG_object (or something like this - it doesn't
> matter). In case of other languages (Python, Perl, Tcl) I could fixup this
> problem with own typemap.)
>
> I'm trying this way:
>
> %typemap(out) int [ANY] {
>   int len,i;
>   len = $1_dim0;
>   lua_createtable (L, len, 0);
>   for(i=0; i<len; i++) {
>     lua_pushinteger(L, (lua_Integer)$1[i]);
>   }
>   lua_settable(L, (-1)*(len+1));
> }
>
> but in Lua script shows only this:
>
> nil
>
> What'em I missing? Could anybody helps in this problem?
>
>
> Thanks,
>
> a.

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user