Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proc calls in component misbehaving when in slot of another component: Found Bug πŸ› #229

Closed
quimt opened this issue Jan 1, 2024 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@quimt
Copy link

quimt commented Jan 1, 2024

Describe the bug πŸ›
Analogous to previous issues like #139 with nested loops, we are now having problems with iteration in nested components.

To Reproduce πŸ‘¨β€πŸ”¬

import happyx
import std / [random]

component withRandom:
  n: int = rand(99)
  `template`:
    tDiv: "self random: {self.n.val}"
    tDiv: "random in template: {rand(99)}"

var counter: int = 0
var counter2: int = 0
proc nextCounter(c: var int = counter): int =
  c += 1
  result = c

component withCounter:
  count: int = nextCounter()
  `template`:
    tDiv: "self count: {self.count}"
    tDiv: "count in template: {nextCounter(counter2)}"

component withReps:
  n: int = 5
  `template`:
    for i in 1..self.n.val:
      tDiv(style="border: 0.2em dotted gray;"): slot

appRoutes("ROOT"):
  "/":
    component withReps():
      withCounter()
      withRandom()
      "non-component rand {rand(99)}"

image

Expected behavior πŸ€”
We should see self count and self random independently varying between repetitions, since they are declared in a loop inside of a component.

We should see the count in template starting from 1, since it depends on an independent counter and the first output of nextCounter() should be 1. Though it is correctly changing as the global variable increases, it is being pre-incremented by the internal logic of the loop even though not all of the values are displayed.

Please complete the following information πŸ“ƒ

  • HappyX version @#head, nim v. 2.0.0

Additional context ✌
Add any other context about the problem here.

@quimt quimt added the bug Something isn't working label Jan 1, 2024
@abhishekkumartiwari049
Copy link

import strformat

proc rand(max: int): int =
result = Random(max + 1)

component withRandom:
n: int = rand(99)
template:
tDiv: fmt"self random: {self.n}"
tDiv: fmt"random in template: {rand(99)}"

var counter: int = 0
var counter2: int = 0
proc nextCounter(c: var int = counter): int =
c += 1
result = c

component withCounter:
count: int = nextCounter()
template:
tDiv: fmt"self count: {self.count}"
tDiv: fmt"count in template: {nextCounter(counter2)}"

component withReps:
n: int = 5
template:
for i in 1..self.n:
tDiv(style="border: 0.2em dotted gray;"): slot

component wGrid:
ncols: int = 1
stretch: float = 1.0
nrows: int = 1
template:
ProbGrid(self.ncols, self.stretch):
for i in 1 .. self.ncols * self.nrows:
slot

appRoutes("ROOT"):
"/":
component withReps():
withCounter()
withRandom()
"non-component rand {rand(99)}"

@Ethosa
Copy link
Contributor

Ethosa commented Jan 4, 2024

image
Reason - slot procedure not handle outer cycles and components. Solved in the next commit.

Ethosa added a commit that referenced this issue Jan 4, 2024
@Ethosa
Copy link
Contributor

Ethosa commented Jan 4, 2024

import
  ../src/happyx,
  random


randomize()

component withRandom:
  n: int = rand(99)
  `template`:
    tDiv: "self random: {self.n.val}"
    tDiv: "random in template: {rand(99)}"

var counter: int = 0
var counter2: int = 0
proc nextCounter(c: var int = counter): int =
  c += 1
  result = c

component withCounter:
  count: int = nextCounter()
  `template`:
    tDiv: "self count: {self.count}"
    tDiv: "count in template: {nextCounter(counter2)}"

component withReps:
  n: int = 5
  `template`:
    for i in 1..self.n.val:
      tDiv(style="border: 0.2em dotted gray;"):
        slot


appRoutes "app":
  "/":
    component withReps():
      withCounter()
      withRandom()
      "non-component rand {rand(99)}"

Ethosa added a commit that referenced this issue Jan 4, 2024
@Ethosa Ethosa self-assigned this Jan 4, 2024
Ethosa added a commit that referenced this issue Jan 4, 2024
@Ethosa Ethosa closed this as completed Jan 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants