title: Lua userdata: Unable to have simultaneous array access and methods
body: <p>I had this guy's problem:
  <a href="https://stackoverflow.com/questions/26970316/lua-userdata-array-access-and-methods">Lua userdata array access and methods</a></p>
  
  <p>wherein when I set the __index of my userdata's metatable, it always called the getter, instead of my other methods that weren't declared for meta-events. The solution to the above link is in Lua, and I attempted a C implementation which seems inelegant, but regardless, it creates a new problem in that my new methods can no longer take arguments, and I get this error:</p>
  
  <p><code>attempt to call method 'asTable' (a table value)</code></p>
  
  <p>on this Lua statement:</p>
  
  <p><code>print_r(c:asTable() )</code></p>
  
  <p>This is how I set everything up:</p>
  
  <pre><code>//Methods, many of which are overridden Lua meta-events (with the underscores)
  static const struct luaL_reg vallib_m [] = {
      {"asTable", PushLuaTable}, //these functions are not called
      {"asCopy", CopyLuaVal}, 
  
      {"__newindex", SetLuaVal},
      {"__index", GetLuaVal},
      {"__tostring", ValToString},
      {"__gc", GarbageCollectVal},
      {"__metatable", HideMetaTable},
  
      {NULL, NULL}
  };
  
  //Static library functions
  static const struct luaL_reg vallib_f [] = {
      {"specialprint", PrintVals}, 
      {NULL, NULL}
  };
  
  
  int luaopen_custom(lua_State *L)
  {
      luaL_newmetatable(L, "custom.Value");
      lua_pushstring(L, "__index");
      lua_pushvalue(L, -2);  /* pushes the metatable */
      lua_settable(L, -3);  /* metatable.__index = metatable */
  
      luaL_register(L, NULL, vallib_m);
      luaL_register(L, "special", vallib_f);
  
      return 0;
  }
  </code></pre>
  
  <p>Then in my getter, which is called by default (via __index), I first check for other events that I intended to be called and transfer control to them as follows. Note that I remove the argument containing the name of the function from the stack.</p>
  
  <pre><code>//TODO: this is a tentative fix, I would rather do this with metatables
  //checking for methods
  if (lua_isstring(L, 2))
  {
      field = luaL_checkstring(L, 2);
      if (unlikely(!field))
      {
          reporter-&gt;Warning("Fail in getter -- bad string as method attempt");
          return LUA_FAILURE;
      }
  
      if (strcmp(field, "asTable") == 0)
      {
          lua_remove(L, 2); //delete string "asTable"
          return PushLuaTable(L);
      }
      else if (strcmp(field, "asCopy") == 0)
      {
          lua_remove(L, 2); //delete string "asCopy"
          return CopyLuaVal(L);
      }
      //... other methods. 
      else
      {
          //Insert string back into stack??
      }
  
  }
  </code></pre>
  
  <p>It doesn't treat my method as a function regardless of how many arguments are passed, and throws an error if there are even any parentheses or a colon. (It can be accessed by <code>c.asTable</code>, which works fine for methods that takes no arguments but I plan to add some that do, and regardless, the syntax is inconsistent with methods.</p>
  
  <p>In any case, it would be preferable to NOT call these functions through my C getter, and instead solve this with metatables. If this is possible, please provide an example using the C API -- there are already StackOverflow solutions in Lua, but I haven't been able to translate them to C.</p>