about summary refs log tree commit diff stats
path: root/src/render/renderdocument.nim
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2022-01-30 11:05:56 +0100
committerbptato <nincsnevem662@gmail.com>2022-01-30 11:05:56 +0100
commit933a114887e66502ffbd47efbd60a7d99527eed4 (patch)
tree41fee487c1245e36fd03642e19c92a30b0518abd /src/render/renderdocument.nim
parent87a47d56cca6ded5f5cd900a9bff2e4d8292629e (diff)
downloadchawan-933a114887e66502ffbd47efbd60a7d99527eed4.tar.gz
Avoid stack overflow in renderBlockContext
Diffstat (limited to 'src/render/renderdocument.nim')
-rw-r--r--src/render/renderdocument.nim21
1 files changed, 13 insertions, 8 deletions
diff --git a/src/render/renderdocument.nim b/src/render/renderdocument.nim
index e6020c6e..2d4dac82 100644
--- a/src/render/renderdocument.nim
+++ b/src/render/renderdocument.nim
@@ -145,14 +145,19 @@ proc renderInlineContext(grid: var FlexibleGrid, ctx: InlineContext, x, y: int,
         grid.setSpacing(spacing, x, y, term)
 
 proc renderBlockContext(grid: var FlexibleGrid, ctx: BlockContext, x, y: int, term: TermAttributes) =
-  let x = x + ctx.relx
-  let y = y + ctx.rely
-  if ctx.inline != nil:
-    assert ctx.nested.len == 0
-    grid.renderInlineContext(ctx.inline, x, y, term)
-  else:
-    for ctx in ctx.nested:
-      grid.renderBlockContext(ctx, x, y, term)
+  var stack = newSeqOfCap[(BlockContext, int, int)](100)
+  stack.add((ctx, x, y))
+
+  while stack.len > 0:
+    var (ctx, x, y) = stack.pop()
+    x += ctx.relx
+    y += ctx.rely
+    if ctx.inline != nil:
+      assert ctx.nested.len == 0
+      grid.renderInlineContext(ctx.inline, x, y, term)
+    else:
+      for i in countdown(ctx.nested.high, 0):
+        stack.add((ctx.nested[i], x, y))
 
 const css = staticRead"res/ua.css"
 let uastyle = css.parseStylesheet()