Sfoglia il codice sorgente

Shared Lua state for all server pages

bel2125 7 anni fa
parent
commit
01a55ac23b
2 ha cambiato i file con 105 aggiunte e 5 eliminazioni
  1. 21 5
      src/mod_lua_shared.inl
  2. 84 0
      test/page_shared.lua

+ 21 - 5
src/mod_lua_shared.inl

@@ -54,6 +54,7 @@ lua_shared_index(struct lua_State *L)
 {
 {
 	void *ud = lua_touserdata(L, 1);
 	void *ud = lua_touserdata(L, 1);
 	int key_type = lua_type(L, 2);
 	int key_type = lua_type(L, 2);
+	int val_type;
 
 
 	printf("lua_shared_index call (%p)\n", ud);
 	printf("lua_shared_index call (%p)\n", ud);
 
 
@@ -83,11 +84,24 @@ lua_shared_index(struct lua_State *L)
 
 
 	lua_rawget(L_shared, -2);
 	lua_rawget(L_shared, -2);
 
 
-	size_t len;
-	const char *s = lua_tolstring(L_shared, -1, &len);
-	printf("<%s>\n", s);
+	val_type = lua_type(L_shared, -1);
 
 
-	lua_pushlstring(L, s, len);
+	if (val_type == LUA_TNUMBER) {
+		double num = lua_tonumber(L_shared, -1);
+		printf("value: %G\n", num);
+		lua_pushnumber(L, num);
+	} else if (val_type == LUA_TBOOLEAN) {
+		int i = lua_toboolean(L_shared, -1);
+		printf("value: %s\n", i ? "true" : "false");
+		lua_pushboolean(L, i);
+	} else if (val_type == LUA_TNIL) {
+		lua_pushnil(L);
+	} else {
+		size_t len = 0;
+		const char *str = lua_tolstring(L_shared, -1, &len);
+		printf("value: %s\n", str);
+		lua_pushlstring(L, str, len);
+	}
 
 
 	lua_pop(L_shared, 2);
 	lua_pop(L_shared, 2);
 
 
@@ -163,13 +177,15 @@ lua_shared_register(struct lua_State *L)
 {
 {
 	lua_newuserdata(L, 0);
 	lua_newuserdata(L, 0);
 	lua_newtable(L);
 	lua_newtable(L);
+
 	lua_pushliteral(L, "__index");
 	lua_pushliteral(L, "__index");
 	lua_pushcclosure(L, lua_shared_index, 0);
 	lua_pushcclosure(L, lua_shared_index, 0);
 	lua_rawset(L, -3);
 	lua_rawset(L, -3);
+
 	lua_pushliteral(L, "__newindex");
 	lua_pushliteral(L, "__newindex");
 	lua_pushcclosure(L, lua_shared_newindex, 0);
 	lua_pushcclosure(L, lua_shared_newindex, 0);
 	lua_rawset(L, -3);
 	lua_rawset(L, -3);
-	lua_setmetatable(L, -2);
 
 
+	lua_setmetatable(L, -2);
 	lua_setglobal(L, "shared");
 	lua_setglobal(L, "shared");
 }
 }

+ 84 - 0
test/page_shared.lua

@@ -0,0 +1,84 @@
+mg.write("HTTP/1.0 200 OK\r\n")
+mg.write("Connection: close\r\n")
+mg.write("Cache-Control: no-cache, no-store, must-revalidate, max-age=0\r\n")
+mg.write("Content-Type: text/plain\r\n")
+mg.write("\r\n")
+
+if not shared then
+  mg.write("\"shared\" does not exist\n")
+  return
+elseif type(shared) ~= "userdata" then
+  mg.write("\"shared\" is not userdata\n")
+  return
+end
+
+-- Test with number
+mg.write("\nNumber:\n")
+x = shared.count
+mg.write("Previous count was " .. tostring(x) .. " (type " .. type(x) .. ")\n")
+x = x or 0
+x = x + 1
+shared.count = x
+mg.write("Store new count " .. tostring(x) .. " (type " .. type(x) .. ")\n")
+x = shared.count
+mg.write("New count is " .. tostring(x) .. " (type " .. type(x) .. ")\n")
+
+-- Test with name
+mg.write("\nString:\n")
+x = shared.name
+mg.write("Previous name was " .. tostring(x) .. " (type " .. type(x) .. ")\n")
+x = x or ""
+l = string.len(x) % 26
+x = x .. string.char(string.byte("a") + l)
+shared.name = x
+mg.write("Store new name " .. tostring(x) .. " (type " .. type(x) .. ")\n")
+x = shared.name
+mg.write("New name is " .. tostring(x) .. " (type " .. type(x) .. ")\n")
+
+
+-- Test with boolean
+mg.write("\nBoolean:\n")
+x = shared.condition
+mg.write("Previous condition was " .. tostring(x) .. " (type " .. type(x) .. ")\n")
+x = not x
+shared.condition = x
+mg.write("Store new condition " .. tostring(x) .. " (type " .. type(x) .. ")\n")
+x = shared.condition
+mg.write("New condition is " .. tostring(x) .. " (type " .. type(x) .. ")\n")
+
+
+-- Test using "shared" as array
+mg.write("\nArray element:\n")
+mg.write("Previous array was: ")
+for i=1,10 do
+  x = shared[i]
+  mg.write(tostring(x) .. " (" .. type(x):sub(1,1) .. ") ")
+end
+mg.write("\n")
+for i=1,10 do
+  shared[i] = shared[(i + 1) % 10 + 1] or i
+end
+mg.write("Shifted array is:   ")
+for i=1,10 do
+  x = shared[i]
+  mg.write(tostring(x) .. " (" .. type(x):sub(1,1) .. ") ")
+end
+mg.write("\n")
+
+
+-- Test using "shared" as array
+mg.write("\nBoolean indexed element:\n")
+x = shared[true]
+y = shared[false]
+mg.write("Previous elements were "
+         .. tostring(x) .. " (type " .. type(x) .. ") / "
+         .. tostring(y) .. " (type " .. type(y) .. ")\n")
+x = not x
+y = not x
+shared[true] = x
+shared[false] = y
+mg.write("New elements are "
+         .. tostring(x) .. " (type " .. type(x) .. ") / "
+         .. tostring(y) .. " (type " .. type(y) .. ")\n")
+
+-- end