Quantcast

Lua - interesting generated code

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Lua - interesting generated code

Ervin Hegedüs
Hi,

I could generated a wrapper code in C from a source, and I can
built it as Lua module.

Looks like it works, but there are several interesting things...

So, please help me to explain those issues.

The target code is Hamlib: https://github.com/N0NB/hamlib
I help to maintain the bindings since few years. It works for
Python, Perl and Tcl.

Here are the Python and Lua functions:

Py:
my_rig = Hamlib.Rig (Hamlib.RIG_MODEL_DUMMY)

Lua:
my_rig = Hamlib.Rig (Hamlib.RIG_MODEL_DUMMY)


Okay, that's right.

Then I have an object in both cases, called "Rig" object.

Now the rig object have several methods:

Py:
my_rig.set_conf ("rig_pathname","/dev/Rig")

Lua:
my_rig.set_conf(my_rig, "rig_pathname", "/dev/Rig")


This is what I don't understand: why do I neet to pass the oject
to itself as 1st argument? If I didn't pass it, I get an error
from Lua:

Wrong arguments for overloaded function 'Rig_set_conf'

because Swig checks the number and types of arguments.


Ok, nevermind, it works as well with the formula above.

Another example:

Open the rig device:

Py:
my_rig.open ()

Lua:
my_rig.open(my_rig)


Hmm... same situation - why?


The most confused functoin is get_conf().


Py:
rpath = my_rig.get_conf("rig_pathname")
print rpath

result: "/dev/Rig"

Lua:
rpath = ""
my_rig.get_conf(my_rig, "rig_pathname", rpath)
print(rpath)

result: "nil"

So, in case of get_conf, also need to pass the object to itself,
and an allocated string - which will not filled. (I just checked
the original C source, and Swig wrapper file. The 3rd argument is
a string pointer, and library will copy to that the requested
value). If I don't declare the string, then Lua gives me an
exception. If I allocated a new one to see the result value (I
mean: rc = my_rig.get_conf(my_rig, "rig_pathname", rpath)), then
both variables will nil value. If I don't pass the 3 necessary
arguments, I also get a "Wrong arguments for overloaded..."
exception.



I'm totally confused.


So, could anybody helps me to understand the Lua wrapper?


Many many thanks,


a.


ps: here are the related files:
https://www.dropbox.com/sh/0hp5cdpq9f3rhgt/AAAJI3_fhUQLS87xP2bnk5hva


------------------------------------------------------------------------------
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
|  
Report Content as Inappropriate

Re: Lua - interesting generated code

Daniel Schepler-5
On the first couple: The Lua idiom for calling an object method is:

my_rig:set_conf("rig_pathname","/dev/Rig")
my_rig:open()

(Internally, this duplicates the object as the first argument as your code did manually.)

On get_conf, I'd suspect some sort of issue with typemapping that the argument somehow isn't properly getting marked as an "output variable", or something along those lines.
--
Daniel Schepler
________________________________________
From: Ervin Hegedüs [[hidden email]]
Sent: Monday, October 24, 2016 9:13 AM
To: swig-user
Subject: [Swig-user] Lua - interesting generated code

Hi,

I could generated a wrapper code in C from a source, and I can
built it as Lua module.

Looks like it works, but there are several interesting things...

So, please help me to explain those issues.

The target code is Hamlib: https://github.com/N0NB/hamlib
I help to maintain the bindings since few years. It works for
Python, Perl and Tcl.

Here are the Python and Lua functions:

Py:
my_rig = Hamlib.Rig (Hamlib.RIG_MODEL_DUMMY)

Lua:
my_rig = Hamlib.Rig (Hamlib.RIG_MODEL_DUMMY)


Okay, that's right.

Then I have an object in both cases, called "Rig" object.

Now the rig object have several methods:

Py:
my_rig.set_conf ("rig_pathname","/dev/Rig")

Lua:
my_rig.set_conf(my_rig, "rig_pathname", "/dev/Rig")


This is what I don't understand: why do I neet to pass the oject
to itself as 1st argument? If I didn't pass it, I get an error
from Lua:

Wrong arguments for overloaded function 'Rig_set_conf'

because Swig checks the number and types of arguments.


Ok, nevermind, it works as well with the formula above.

Another example:

Open the rig device:

Py:
my_rig.open ()

Lua:
my_rig.open(my_rig)


Hmm... same situation - why?


The most confused functoin is get_conf().


Py:
rpath = my_rig.get_conf("rig_pathname")
print rpath

result: "/dev/Rig"

Lua:
rpath = ""
my_rig.get_conf(my_rig, "rig_pathname", rpath)
print(rpath)

result: "nil"

So, in case of get_conf, also need to pass the object to itself,
and an allocated string - which will not filled. (I just checked
the original C source, and Swig wrapper file. The 3rd argument is
a string pointer, and library will copy to that the requested
value). If I don't declare the string, then Lua gives me an
exception. If I allocated a new one to see the result value (I
mean: rc = my_rig.get_conf(my_rig, "rig_pathname", rpath)), then
both variables will nil value. If I don't pass the 3 necessary
arguments, I also get a "Wrong arguments for overloaded..."
exception.



I'm totally confused.


So, could anybody helps me to understand the Lua wrapper?


Many many thanks,


a.


ps: here are the related files:
https://www.dropbox.com/sh/0hp5cdpq9f3rhgt/AAAJI3_fhUQLS87xP2bnk5hva


------------------------------------------------------------------------------
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



------------------------------------------------------------------------------
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
|  
Report Content as Inappropriate

Re: Lua - interesting generated code

Ervin Hegedüs
Hi Daniel,

thanks for the answer,

On Mon, Oct 24, 2016 at 04:35:12PM +0000, Daniel Schepler wrote:
> On the first couple: The Lua idiom for calling an object method is:
>
> my_rig:set_conf("rig_pathname","/dev/Rig")
> my_rig:open()

I'm not a Lua-guru, so I completely missed this :).

Now it works as you wrote - many thanks!

> (Internally, this duplicates the object as the first argument as your code did manually.)
>
> On get_conf, I'd suspect some sort of issue with typemapping that the argument somehow isn't properly getting marked as an "output variable", or something along those lines.

right, I'll see the char* out typemaps. Anyway, the problem
exists only the case of functions, the members of module are
showing correctly.


Many thanks again!


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
|  
Report Content as Inappropriate

Re: Lua - interesting generated code

Ervin Hegedüs
In reply to this post by Daniel Schepler-5
Hi,

On Mon, Oct 24, 2016 at 04:35:12PM +0000, Daniel Schepler wrote:
>
> On get_conf, I'd suspect some sort of issue with typemapping that the argument somehow isn't properly getting marked as an "output variable", or something along those lines.

could you show me an example for argout typemap with output
variable?

When I'm using this:

%typemap(argout) const char *, char * {
    printf("ARGOUT, '%s'\n", $1);
    lua_pushstring(L,(const char*)$1); SWIG_arg++;
}

I see the correct value in place of $1, that's the return value
of hamlib's original get_conf() function (so, that's the 3rd
argument, which is a char *).

But what I need to do to pass this object to the caller function?


Anyway, why do I need to pass an empty string? In case of Python,
Perl and Tcl, there isn't need to do that, the function returns
the value...



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
Loading...