karaxnim / karax

Karax. Single page applications for Nim.
MIT License
1.05k stars 90 forks source link

stack size exceeded error on js target #268

Closed chancyk closed 10 months ago

chancyk commented 10 months ago

Using Nim 2.0.0 on Windows 11

The following code is a minimal example that produces a Maximum call stack size exceeded error when compiled to JS and run from Node 16.15.1 or Chromium 116:

import karax / [kbase, karaxdsl, vdom]

proc render*(): kstring =
  let vnode = buildHtml(html()):
    body:
      section(id = "section1"):
        text "Section 1 content..."

  result = $vnode

echo render()

and here's the offending recursive function that's produced:

function len_738198497(a_738198498) {
    var Temporary1;

  var result_738198499 = 0;

  var F = {procname: "vdom.len", prev: framePtr, filename: "C:\\Users\\username\\.nimble\\pkgs2\\karax-1.3.0-481742b3d0c0e3c92174b1bdd08a4a94853250af\\karax\\vdom.nim", line: 0};
  framePtr = F;
    F.line = 352;
    F.filename = "vdom.nim";
    if ((a_738198498 == null)) {
      Temporary1 = 0;
    }
    else {
      Temporary1 = len_738198497(a_738198498);
    }

    result_738198499 = Temporary1;
  framePtr = F.prev;

  return result_738198499;
}

by this Nim code that was introduced in pull request #159:

when kstring is cstring:
  proc len(a: kstring): int =
    # xxx: maybe move where kstring is defined
    # without this, `n.field.len` fails on js (non web) platform
    if a == nil: 0 else: a.len

It seems like a.len should be calling a version of length without the guard instead of it being recursive? Is that possible?

chancyk commented 10 months ago

Created a pull request (#269) which fixes the issue for me. Is this an acceptable approach?