tolua 3.0b issue[3]

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

tolua 3.0b issue[3]

DASWorks
Hi Waldemar:

>I was assuming you had included a funtion like:
>file_into * create_file_info(...);

Yes, you are correct, I was not sure that we were talking about the same
thing.  I now understand the issues discussed.  Thank you very much for your
time and assistance.

// BUG REPORT:
It appears that I may have found a bug or problem with regard to arrays in
function parameters.  For example:

short getfileinfo(file_info *zfile,unsigned char type[5],unsigned char
creator[5]);

In tolua, the unsigned char array is assumed to be numeric, which obviously
causes problems with char arrays.  (The below tolua generated binding function
has the corrections.)  I think tolua needs to consider the array type in this
situation.


// COMMENT:
More importantly, is the following the only way for tolua to recognize
multiple return values?  What I want to do is this:

err,type,creator = getfileinfo(fi)
print(type)
print(creator)

instead I have to do this:

type={}
creator={}
err = getfileinfo(fi,type,creator)
print(type[1])
print(creator[1])

I don't need to to pass type and creator information into my C code.  It only
needs to be sent out.  Is there a way to set the .pkg file or "tell" tolua
that this is what is required.  It may not be possible...

Thanks again for your help.  I haven't test everything about tolua yet, but
things are progressing.

Warm Regards,

David

--------------------------------------------
/* function: getfileinfo */
static void toluaI_getfileinfo0(void)
{
 if (
     !tolua_istype(1,tolua_tag_file_info,0) ||
     !tolua_istype(2,tolua_tag_table,0) ||
     !tolua_istype(3,tolua_tag_table,0) ||
     !tolua_isnoobj(4)
 )
  goto error;
 else
 {
  file_info* zfile = ((file_info*)  tolua_getusertype(1,0));
  unsigned char type[5];
  unsigned char creator[5];
  {
   if (!tolua_arrayistype(2,tolua_tag_number,5,1))
    goto error;
   else
   {
    int i;
    lua_Object lo = lua_getparam(2);
    for(i=0; i<5;i++)
     type[i] = ((unsigned char)  tolua_getfieldstring(lo,i+1,0));
     //type[i] = ((unsigned char)  tolua_getfieldnumber(lo,i+1,0)); //
original
   }
  }
  {
   if (!tolua_arrayistype(3,tolua_tag_number,5,1))
    goto error;
   else
   {
    int i;
    lua_Object lo = lua_getparam(3);
    for(i=0; i<5;i++)
     creator[i] = ((unsigned char)  tolua_getfieldstring(lo,i+1,0));
     //creator[i] = ((unsigned char)  tolua_getfieldnumber(lo,i+1,0)); //
original
   }
  }
  {
   short toluaI_ret = getfileinfo(zfile,type,creator);
   tolua_pushnumber((double)toluaI_ret);
  }
  {
   int i;
   lua_Object lo = lua_getparam(2);
   for(i=0; i<5;i++)
   	tolua_pushfieldstring(lo,i+1,(char*) &type[i]);
   //tolua_pushfieldnumber(lo,i+1,(double) type[i]); // original
  }
  {
   int i;
   lua_Object lo = lua_getparam(3);
   for(i=0; i<5;i++)
    tolua_pushfieldstring(lo,i+1,(char*) &creator[i]);
   //tolua_pushfieldnumber(lo,i+1,(double) creator[i]); // original
  }
 }
 return;
error:
 tolua_error("#ferror in function 'getfileinfo'.");
}


Here are some casting problems also (they may be compiler related however):

/* get function: myFileName of class  file_info */
static void toluaI_get_file_info_myFileName(void)
{
  // original:
  //const unsigned file_info* self = (const unsigned file_info*)
tolua_getusertype(1,0);
  // changed to:
  file_info* self = (file_info*)  tolua_getusertype(1,0);
  if (!self) tolua_error("invalid 'self' in accessing variable 'myFileName'");
  // original:
  //tolua_pushstring(self->myFileName);
  // changed to:
  tolua_pushstring((const char*)self->myFileName);
}

/* get function: myFilePath of class  file_info */
static void toluaI_get_file_info_myFilePath(void)
{
   // original:
  //const unsigned file_info* self = (const unsigned file_info*)
tolua_getusertype(1,0);
// changed to:
  file_info* self = (file_info*)  tolua_getusertype(1,0);
  if (!self) tolua_error("invalid 'self' in accessing variable 'myFilePath'");
  //tolua_pushstring(self->myFilePath);
// changed to:
  tolua_pushstring((const char*)self->myFilePath);
}

Reply | Threaded
Open this post in threaded view
|

Re: tolua 3.0b issue[3]

Waldemar Celes-3
hi David,

> // BUG REPORT:
> It appears that I may have found a bug or problem with regard to arrays in
> function parameters.  For example:
> 
> short getfileinfo(file_info *zfile,unsigned char type[5],unsigned char
> creator[5]);

Well, it might be a wrong design decision, but it's not a bug.
For tolua, char* represents string while 
char[] represent array of numeric values (varying from 0 to 255). 

> (The below tolua generated binding function
> has the corrections.)  

The corrections you made are not correct :-(

> type[i] = ((unsigned char)  tolua_getfieldstring(lo,i+1,0));

"tolua_getfieldstring" returns a char* not a char.

> I think tolua needs to consider the array type in this
> situation.

Sorry, but for now I prefer to have both approaches: 
string (char*) and array of bytes (char[]).

you can also use the lua functions "strbyte" and "strchar" to
convert from numeric array to string and vice-versa.

> More importantly, is the following the only way for tolua to recognize
> multiple return values?  What I want to do is this:
> 
> err,type,creator = getfileinfo(fi)
> print(type)
> print(creator)
> 
Yes! You can do that.
Assuming 'err' and 'type' are integers and 'creator' is a string,
you should write in the .pkg file:

int getfileinfo (file_info* fi, int* type=0, char** creator=0);

the use of default values for type and creator allows you not to
pass them when calling the function.


> Thanks again for your help.  I haven't test everything about tolua yet, but
> things are progressing.

thank you for your interest in tolua. hope it can help you.

regards,

-- waldemar

Reply | Threaded
Open this post in threaded view
|

Bitshift function in Lua

Erik Hougaard
This might be a stupid function, but is Lua missing a << >> bitshift
function and if so, how do I make it ?

Erik

Reply | Threaded
Open this post in threaded view
|

Re: Bitshift function in Lua

Luiz Henrique de Figueiredo
>From: [hidden email] (Erik Hougaard)
>This might be a stupid function, but is Lua missing a << >> bitshift function

yes, since it does not make sense for real numbers.

>and if so, how do I make it ?

how about this:

 static void math_lshift (void)
 {
  int v=luaL_check_number(1);
  int n=luaL_check_number(2);
  lua_pushnumber(v<<n);
 }

 static void math_rshift (void)
 {
  int v=luaL_check_number(1);
  int n=luaL_check_number(2);
  lua_pushnumber(v>>n);
 }

 lua_register("lshift",math_lshift);
 lua_register("rshift",math_rshift);

--lhf

Reply | Threaded
Open this post in threaded view
|

Re: Bitshift function in Lua

Roberto Ierusalimschy
Another option is simply (x*2) and floor(x/2).

-- Roberto