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

[bug] Macros incorrectly bind to mutable variables #190

Open
JeanJPNM opened this issue Jul 10, 2023 · 0 comments
Open

[bug] Macros incorrectly bind to mutable variables #190

JeanJPNM opened this issue Jul 10, 2023 · 0 comments
Labels
bug Something isn't working
Milestone

Comments

@JeanJPNM
Copy link
Collaborator

Some macros "bind" their properties directly to mutable variables instead of copying them.

Memory

In the example bellow it is possible to see that the a memory macro refers directly to cell to execute read/write
operations, even though the value that cell holds may change.

But the expected behavior was for a to hold a copy of the value contained in cell.

let cell = getBuilding("cell1")
const a = new Memory(cell)
cell = getBuilding("cell2")
const b = new Memory(cell)

a[0] = 1;

print`
a[0]: ${a[0]}
b[0]: ${b[0]}
`

Output:

set cell:1:4 cell1
set cell:1:4 cell2
write 1 cell:1:4 0
read &t0 cell:1:4 0
read &t1 cell:1:4 0
print "\na[0]: "
print &t0
print "\nb[0]: "
print &t1
print "\n"

Object/array literals

The same happens with object and array literals. In the example bellow it is possible to see that changes in a
reflect on the objects and vice-versa:

let a = 10
const array = [a]
const object = {
    value: a
}

a = 20
print`${a}, ${object.value} ${array[0]}\n`
array[0] = 30;
print`${a}, ${object.value} ${array[0]}\n`
printFlush()

Output:

set a:1:4 10
set a:1:4 20
print a:1:4
print ", "
print a:1:4
print " "
print a:1:4
print "\n"
set a:1:4 30
print a:1:4
print ", "
print a:1:4
print " "
print a:1:4
print "\n"
printflush message1

Fixing this issue is as simple as creating a copy of the value if it is not constant, but may create unecessary assignments that reduce the performance of the generated code.

@JeanJPNM JeanJPNM added the bug Something isn't working label Jul 10, 2023
@JeanJPNM JeanJPNM modified the milestones: v0.6, v0.7 Jul 16, 2023
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

1 participant