lua-users home
lua-l archive

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


>From lua-l@tecgraf.puc-rio.br Wed Jul 28 20:27:03 1999
>From: Fred Bertsch <fb@anise.ee.cornell.edu>

>Is there a way to tell if two lua_Objects are equal from within C?

Sorry, not with the current API.
Something to consider for the next version.

>I have to
>figure out if they are in the same chunk of memory, but getting a pointer
>is obviously not supported by the standard API. 

Right.
If you must go outside the API, then look at luaA_Address and luaO_equalval.
Something like (unofficial code ahead):

 int ObjectEqual(lua_Object x, lua_Object y)
 {
  return luaO_equalval(luaA_Address(x),luaA_Address(y));
 }

>The only technique I can come up with involves lua_dostring, but that's
>not particularly elegant.  

I don't know whether you mean this (untested code ahead):

 int ObjectEqual(lua_Object x, lua_Object y)
 {
  lua_Object eq;
  int rc;
  lua_beginblock();
  eq=lua_getglobal("EQUAL");
  if (lua_isnil(eq)) {
   lua_dostring("function EQUAL(x,y) return x==y end; return EQUAL");
   eq=lua_getresult(1);
  }
  lua_pushobject(x);
  lua_pushobject(y);
  lua_callfunction(eq);
  rc=!lua_isnil(lua_getresult(1));
  lua_endblock();
  return rc;
 }

For added "efficiency", you can lock the EQUAL function and save a reference
to it in a static var.
--lhf