trafficserver-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jpe...@apache.org
Subject [3/50] git commit: Restore RRI method support by trampolining through __index
Date Tue, 12 Jun 2012 15:51:36 GMT
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/1d380fb2
Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/1d380fb2
Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/1d380fb2

Branch: refs/heads/jpeach/lua
Commit: 1d380fb2f6bdc73e79ee69f4b63f99880007c5c7
Parents: 01d9d9c
Author: James Peach <jpeach@apache.org>
Authored: Sat Apr 21 10:43:34 2012 -0700
Committer: James Peach <jpeach@apache.org>
Committed: Tue Jun 12 08:48:38 2012 -0700

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


http://git-wip-us.apache.org/repos/asf/trafficserver/blob/1d380fb2/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);
   }


Mime
View raw message