page_shared.lua 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. mg.write("HTTP/1.0 200 OK\r\n")
  2. mg.write("Connection: close\r\n")
  3. mg.write("Cache-Control: no-cache, no-store, must-revalidate, max-age=0\r\n")
  4. mg.write("Content-Type: text/plain\r\n")
  5. mg.write("\r\n")
  6. if not shared then
  7. mg.write("\"shared\" does not exist\n")
  8. return
  9. elseif type(shared) ~= "userdata" then
  10. mg.write("\"shared\" is not userdata\n")
  11. return
  12. end
  13. -- Test with number
  14. mg.write("\nNumber:\n")
  15. x = shared.count
  16. mg.write("Previous count was " .. tostring(x) .. " (type " .. type(x) .. ")\n")
  17. x = x or 0
  18. x = x + 1
  19. shared.count = x
  20. mg.write("Store new count " .. tostring(x) .. " (type " .. type(x) .. ")\n")
  21. x = shared.count
  22. mg.write("New count is " .. tostring(x) .. " (type " .. type(x) .. ")\n")
  23. -- Test with name
  24. mg.write("\nString:\n")
  25. x = shared.name
  26. mg.write("Previous name was " .. tostring(x) .. " (type " .. type(x) .. ")\n")
  27. x = x or ""
  28. l = string.len(x) % 26
  29. x = x .. string.char(string.byte("a") + l)
  30. shared.name = x
  31. mg.write("Store new name " .. tostring(x) .. " (type " .. type(x) .. ")\n")
  32. x = shared.name
  33. mg.write("New name is " .. tostring(x) .. " (type " .. type(x) .. ")\n")
  34. -- Test with boolean
  35. mg.write("\nBoolean:\n")
  36. x = shared.condition
  37. mg.write("Previous condition was " .. tostring(x) .. " (type " .. type(x) .. ")\n")
  38. x = not x
  39. shared.condition = x
  40. mg.write("Store new condition " .. tostring(x) .. " (type " .. type(x) .. ")\n")
  41. x = shared.condition
  42. mg.write("New condition is " .. tostring(x) .. " (type " .. type(x) .. ")\n")
  43. -- Test using "shared" as array
  44. mg.write("\nArray element:\n")
  45. mg.write("Previous array was: ")
  46. for i=1,10 do
  47. x = shared[i]
  48. mg.write(tostring(x) .. " (" .. type(x):sub(1,1) .. ") ")
  49. end
  50. mg.write("\n")
  51. for i=1,10 do
  52. shared[i] = shared[(i + 1) % 10 + 1] or i
  53. end
  54. mg.write("Shifted array is: ")
  55. for i=1,10 do
  56. x = shared[i]
  57. mg.write(tostring(x) .. " (" .. type(x):sub(1,1) .. ") ")
  58. end
  59. mg.write("\n")
  60. -- Test using "shared" as array
  61. mg.write("\nBoolean indexed element:\n")
  62. x = shared[true]
  63. y = shared[false]
  64. mg.write("Previous elements were "
  65. .. tostring(x) .. " (type " .. type(x) .. ") / "
  66. .. tostring(y) .. " (type " .. type(y) .. ")\n")
  67. x = not x
  68. y = not x
  69. shared[true] = x
  70. shared[false] = y
  71. mg.write("New elements are "
  72. .. tostring(x) .. " (type " .. type(x) .. ") / "
  73. .. tostring(y) .. " (type " .. type(y) .. ")\n")
  74. -- Check if experimental functions (starting with __) are available
  75. if not shared.__inc then
  76. mg.write("\nExperimental functions not available\n")
  77. return
  78. else
  79. mg.write("\nTesting experimental functions\n")
  80. end
  81. -- Test __inc/__dec functions
  82. if not shared.x then
  83. shared.x = 0
  84. shared.y = 0
  85. end
  86. mg.write("__inc(x) = " .. shared.__inc("x") .. "\n")
  87. mg.write("__dec(y) = " .. shared.__dec("y") .. "\n")
  88. -- Test __add function
  89. if not shared.x then
  90. shared.x = 0
  91. shared.y = 0
  92. end
  93. mg.write("__add(x, 10) = " .. shared.__add("x", 10) .. "\n")
  94. mg.write("__add(y, -10) = " .. shared.__add("y", -10) .. "\n")
  95. -- end