From 3a4cb569147019fd5033726f100ba5937960e359 Mon Sep 17 00:00:00 2001 From: osmarks Date: Mon, 24 Aug 2020 14:09:10 +0100 Subject: [PATCH] Fix SPUDNET JSON --- manifest | 4 ++-- src/main.lua | 51 +++++++++++++++++++++++++++++++++++++++------- src/potatobios.lua | 2 +- 3 files changed, 47 insertions(+), 10 deletions(-) diff --git a/manifest b/manifest index 2ed13b7..c8c08c9 100644 --- a/manifest +++ b/manifest @@ -1,2 +1,2 @@ -{"build":152,"description":"ccemux command","files":{"LICENSES":"f3549d84d66eb53dd4a421a4341d77d3d217c1b117d67e3be8f5211adcda0952","autorun.lua":"ec5ff5045da5dc90331c1dbb1797d9106b28e98cd141ef14b6e0dc2a8f6b56d3","bin/5rot26.lua":"91b66cd6d4b33081b25c456142dd7efcb894e819e842693c9e1e17ff48872ff5","bin/ccemux.lua":"239476f58835b86bbcac31ce8af3c3acd3d198a55ab9ada78c62fbf358625a98","bin/chronometer.lua":"db5363993a04382145aef7db2fbe262f0bf10697a589e1e2d2f9ce0f87430dd8","bin/kristminer.lua":"7e7f9fe2a6493d584ad6926cda915e02c1c3d800dc209680898ce930d0bb0e6f","bin/livegps.lua":"c3d17d495cda01aa1261e4c4fcd43439b29af422671972117ec34f68e32c5bba","bin/loading.lua":"c85f7aa1765170325155b921c1fceeb62643f552f12d41b529a22af3a67f5a97","bin/potatoflight.lua":"2fbb0b6f8d78728d8cb0ec64af1bc598bd00cb55f202378e7acdb86bba71efd1","bin/potatoplex.lua":"86c9e7597bbe23d7de7e7f1bfc976d0b94dcdf3af9e6c7c6c9b18b98596898c8","bin/relay.lua":"261ae6c220b83506e3326e8f2b091d246baae458ff0d2ee87512be2c4e35a75d","bin/tryhaskell.lua":"07810d85145da65a3e434154c79d5a9d72f2dcbe59c8d6829040fb925df878ec","potatobios.lua":"c9d8029487273bd85d3b46e1ea0fb2fb9c694b21dbd3b5cdd53123a329d03322","signing-key.tbl":"b32af5229c23af3bc03d538e42751b26044e404a7b1af064ed89894efe421607","startup":"f17bfb9b4322c4467dc9170d50827f2d75717e5c3125d734f21f3406657917bc","update-key.hex":"8d8afb7a45833bb7d68f929421ad60a211d4d73e0ee03b24dc0106ba1de2e1a0","xlib/00_cbor.lua":"464b075e4f094b8db42506bd4bdaad0db87699ea7fbf80e5b87739b4aa9279af","xlib/01_skynet.lua":"bde95ed86f3108ec56624367deea3e2694c8cfcd9eac220a21bad0b56c8a999b"},"timestamp":1598273239} -{"hash":"00bdb680c717309c5ec182abea9f235b3f4ac6ee3b517bcb29841538a676017d","sig":"7acdc6e8288fe854bf73fc1b880ad204f57e012013f2c47c55ea593393d360395f8a64be99a3927fc218"} \ No newline at end of file +{"build":156,"description":"very fix JSON in SPUDNET","files":{"LICENSES":"f3549d84d66eb53dd4a421a4341d77d3d217c1b117d67e3be8f5211adcda0952","autorun.lua":"62c93a60f100f94e4c20325ff22b6fb0eafa225eb5e307045936cdf2d589f3e8","bin/5rot26.lua":"91b66cd6d4b33081b25c456142dd7efcb894e819e842693c9e1e17ff48872ff5","bin/ccemux.lua":"239476f58835b86bbcac31ce8af3c3acd3d198a55ab9ada78c62fbf358625a98","bin/chronometer.lua":"db5363993a04382145aef7db2fbe262f0bf10697a589e1e2d2f9ce0f87430dd8","bin/kristminer.lua":"7e7f9fe2a6493d584ad6926cda915e02c1c3d800dc209680898ce930d0bb0e6f","bin/livegps.lua":"c3d17d495cda01aa1261e4c4fcd43439b29af422671972117ec34f68e32c5bba","bin/loading.lua":"c85f7aa1765170325155b921c1fceeb62643f552f12d41b529a22af3a67f5a97","bin/potatoflight.lua":"2fbb0b6f8d78728d8cb0ec64af1bc598bd00cb55f202378e7acdb86bba71efd1","bin/potatoplex.lua":"86c9e7597bbe23d7de7e7f1bfc976d0b94dcdf3af9e6c7c6c9b18b98596898c8","bin/relay.lua":"261ae6c220b83506e3326e8f2b091d246baae458ff0d2ee87512be2c4e35a75d","bin/tryhaskell.lua":"07810d85145da65a3e434154c79d5a9d72f2dcbe59c8d6829040fb925df878ec","potatobios.lua":"8821428eec9400a6e8155da0f5485163614f72a462212a8bbae0601d3812bfba","signing-key.tbl":"b32af5229c23af3bc03d538e42751b26044e404a7b1af064ed89894efe421607","startup":"f17bfb9b4322c4467dc9170d50827f2d75717e5c3125d734f21f3406657917bc","update-key.hex":"8d8afb7a45833bb7d68f929421ad60a211d4d73e0ee03b24dc0106ba1de2e1a0","xlib/00_cbor.lua":"464b075e4f094b8db42506bd4bdaad0db87699ea7fbf80e5b87739b4aa9279af","xlib/01_skynet.lua":"bde95ed86f3108ec56624367deea3e2694c8cfcd9eac220a21bad0b56c8a999b"},"timestamp":1598274539} +{"hash":"73f1ac78ace6e43373b53aa6a5dcd183ef65646ceaa77c369db899661b296434","sig":"b1f99e1c0848ec247a6e92a85e384ea4bee03470191820a375a5acaad21cd17c561d6bb3bb54d1947e24"} \ No newline at end of file diff --git a/src/main.lua b/src/main.lua index 27b8450..4e332dd 100644 --- a/src/main.lua +++ b/src/main.lua @@ -597,11 +597,48 @@ end Fix bug PS#201CA2AA Serializing functions, recursive tables, etc. - this is done fairly often - can cause a complete crash of the SPUDNET process. This fixes that. ]] --- Serialize (i.e. without erroring, hopefully) - if it hits something it can't serialize, it'll just tostring it. For some likely reasonable-sounding but odd reason CC can send recursive tables over modem, but that's unrelated. -local function safe_serialize(data) - local ok, res = pcall(json.encode, data) - if ok then return res - else return json.encode(tostring(data)) end +-- Serialize safely (i.e. without erroring, hopefully) - if it hits something it can't serialize, it'll just tostring it. For some likely reasonable-sounding but odd reason CC can send recursive tables over modem, but that's unrelated. + +function safe_json_serialize(x, prev) + local t = type(x) + if t == "number" then + if x ~= x or x <= -math.huge or x >= math.huge then + return tostring(x) + end + return string.format("%.14g", x) + elseif t == "string" then + return json.encode(x) + elseif t == "table" then + prev = prev or {} + local as_array = true + for k in pairs(x) do + if type(k) ~= "number" then as_array = false break end + end + if as_array then + for i = 1, #x do + if not x[i] then as_array = false break end + end + end + if as_array then + local res = {} + for i, v in ipairs(x) do + table.insert(res, safe_json_serialize(v)) + end + return "["..table.concat(res, ",").."]" + else + local res = {} + for k, v in pairs(x) do + table.insert(res, json.encode(tostring(k)) .. ":" .. safe_json_serialize(v)) + end + return "{"..table.concat(res, ",").."}" + end + elseif t == "boolean" then + return tostring(x) + elseif x == nil then + return nil + else + return ("%q"):format(tostring(x)) + end end -- Powered by SPUDNET, the simple way to include remote debugging services in *your* OS. Contact Gollark today. @@ -612,7 +649,7 @@ local function websocket_remote_debugging() local function send_packet(msg) --ws.send(safe_serialize(msg)) - ws.send(json.encode(msg)) + ws.send(safe_json_serialize(msg)) end local function send(data) @@ -671,7 +708,7 @@ local function websocket_remote_debugging() if ping_timeout_timer then os.cancelTimer(ping_timeout_timer) end ping_timeout_timer = os.startTimer(15) elseif packet.type == "error" then - add_log("SPUDNET error %s %s %s", packet["for"], packet.error, packet.detail) + add_log("SPUDNET error %s %s %s %s", packet["for"], packet.error, packet.detail, textutils.serialise(packet)) elseif packet.type == "message" then local code = packet.data if type(code) == "string" then diff --git a/src/potatobios.lua b/src/potatobios.lua index e211d84..eb426c7 100644 --- a/src/potatobios.lua +++ b/src/potatobios.lua @@ -1632,7 +1632,7 @@ function textutils.compact_serialize(x) if t == "number" then return tostring(x) elseif t == "string" then - return textutils.serialise(x) + return ("%q"):format(x) elseif t == "table" then local out = "{" for k, v in pairs(x) do