You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by jp...@apache.org on 2012/05/11 06:01:17 UTC

[6/32] git commit: Restore RRI method support by trampolining through __index

Restore RRI method support by trampolining through __index


Project: http://git-wip-us.apache.org/repos/asf/trafficserver/repo
Commit: http://git-wip-us.apache.org/repos/asf/trafficserver/commit/68d18a28
Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/68d18a28
Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/68d18a28

Branch: refs/heads/jpeach/lua
Commit: 68d18a285a4359d52937f7b7c4fd8e96672d844b
Parents: 6b4a79e
Author: James Peach <jp...@apache.org>
Authored: Sat Apr 21 10:43:34 2012 -0700
Committer: James Peach <jp...@apache.org>
Committed: Thu May 10 20:23:43 2012 -0700

----------------------------------------------------------------------
 plugins/lua/lapi.cc |   23 ++++++++++++++++++++++-
 1 files changed, 22 insertions(+), 1 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/trafficserver/blob/68d18a28/plugins/lua/lapi.cc
----------------------------------------------------------------------
diff --git a/plugins/lua/lapi.cc b/plugins/lua/lapi.cc
index 885e24a..3ae2645 100644
--- a/plugins/lua/lapi.cc
+++ b/plugins/lua/lapi.cc
@@ -234,6 +234,21 @@ LuaRemapIndex(lua_State * lua)
 
   TSDebug("lua", "%s[%s]", __func__, index);
 
+  // XXX When we set __index in the metatable, Lua routes all method calls through here rather than checking for the
+  // existing key first. That's a bit surprising and I wonder whether there's a better way to handle this.
+
+  // Get the userdata's metatable and look up the index in it.
+  lua_getmetatable(lua, 1);
+  lua_getfield(lua, -1, index);
+  if (!lua_isnoneornil(lua, -1)) {
+    // Pop the metatable, leaving the field value on top.
+    lua_remove(lua, -2);
+    return 1;
+  }
+
+  // Pop the field value and the metatable.
+  lua_pop(lua, 2);
+
   if (strcmp(index, "headers") != 0) {
     return 0;
   }
@@ -246,7 +261,13 @@ LuaRemapIndex(lua_State * lua)
   if (lua_isnoneornil(lua, -1)) {
     TSDebug("lua", "populating '%s' field", index);
     lua_pop(lua, 1);
-    lua_pushstring(lua, "ping");
+
+    // Make a new header table.
+    lua_newtable(lua);
+    luaL_getmetatable(lua, "ts.meta.headers");
+    lua_setmetatable(lua, -2);
+
+    // Set it for the 'headers' index and push it on the stack.
     lua_setfield(lua, -2, index);
     lua_getfield(lua, -1, index);
   }