From 5bedc226eae70e557746428830f7b583f892a864 Mon Sep 17 00:00:00 2001 From: s-ol Date: Wed, 18 Dec 2019 16:27:51 +0100 Subject: protect print/deep_tostring from recursion --- mmm/init.client.moon | 10 +++++----- mmm/init.server.moon | 7 ++++--- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/mmm/init.client.moon b/mmm/init.client.moon index 5fb3450..25c7da0 100644 --- a/mmm/init.client.moon +++ b/mmm/init.client.moon @@ -7,15 +7,15 @@ window = js.global MODE = 'CLIENT' UNSAFE = true -deep_tostring = (tbl, space='') -> - return tbl if 'userdata' == type tbl - +deep_tostring = (tbl, space='', recur={}) -> buf = space .. tostring tbl - return buf unless 'table' == (type tbl) and not tbl.__tostring + return buf unless 'table' == (type tbl) and not tbl.__tostring and not recur[tbl] + + recur[tbl] = true buf = buf .. ' {\n' for k,v in pairs tbl - buf = buf .. "#{space} [#{k}]: #{deep_tostring v, space .. ' '}\n" + buf = buf .. "#{space} [#{k}]: #{deep_tostring v, space .. ' ', recur}\n" buf = buf .. "#{space}}" buf diff --git a/mmm/init.server.moon b/mmm/init.server.moon index fbce18f..0e3534b 100644 --- a/mmm/init.server.moon +++ b/mmm/init.server.moon @@ -1,14 +1,15 @@ export MODE, print, warn, relative MODE = 'SERVER' -deep_tostring = (tbl, space='') -> +deep_tostring = (tbl, space='', recur={}) -> buf = space .. tostring tbl - return buf unless 'table' == (type tbl) and not tbl.__tostring + return buf unless 'table' == (type tbl) and not tbl.__tostring and not recur[tbl] + recur[tbl] = true buf = buf .. ' {\n' for k,v in pairs tbl - buf = buf .. "#{space} [#{k}]: #{deep_tostring v, space .. ' '}\n" + buf = buf .. "#{space} [#{k}]: #{deep_tostring v, space .. ' ', recur}\n" buf = buf .. "#{space}}" buf -- cgit v1.2.3