pietroppeter / nimib

nimib 🐳 - nim 👑 driven ⛵ publishing ✍
https://pietroppeter.github.io/nimib/
MIT License
175 stars 10 forks source link

gensym captured variables that haven't already been gensymed #136

Closed HugoGranstrom closed 1 year ago

HugoGranstrom commented 1 year ago

If I tried to capture a global variable in nbJsFromCode inside a template, it didn't gensym the variable correctly (because it wasn't gensym'd by the template, the code didn't know to gensym it itself). This seems to fix it, though.

pietroppeter commented 1 year ago

Trust you on this one :), maybe add a change log line and then squash and merge ahead!

HugoGranstrom commented 1 year ago

Trust you on this one :), maybe add a change log line and then squash and merge ahead!

Hehe, I barely understand it either, but it doesn't seem to brake any existing code and fixes broken code :upside_down_face:

pietroppeter commented 1 year ago

do you happen to have a small example for the issue? it is probably not worth making a test about it but at least it have it here documented in the issue for when we have to refer back for some reason could be good.

HugoGranstrom commented 1 year ago
import nimib

nbInit

var counter: int # the gloabl variable in question
template component() =
  nbJsFromCode(counter):
    import std / dom
    window.addEventListener("load", proc (e: Event) =
      echo counter
    )
  counter += 1 # the counter should be different for the two components

component()
component()

nbSave

The counter should be different for the two components, but if you look in the JS console you see that both of them are printing 1 instead of 0 and 1. This is because they are both named counter_469762080 in the JavaScript so the second component's assignment, overrides the first one's. Hence why we have to gensym captured global variables.

Note: this is only relevant if we try to access the variable inside a function, had we echo'd counter directly in the body we wouldn't have had any problems.