charly-lang / charly

🐈 The Charly Programming Language | Written by @KCreate
https://charly-lang.github.io/charly/
MIT License
199 stars 10 forks source link

Scoping bug when binding redirected primitive methods #68

Closed KCreate closed 8 years ago

KCreate commented 8 years ago

Reduced:

let Box = {
  let children = ["test", "what"];
  let iterate = children.each;

  func length() {
    2;
  };

  func __member(index) {
    children[index];
  };
};

Box.children.push(1);
Box.children.push(2);
Box.children.push(3);

Box.iterate(func(e) {
  print(e);
});

Output:

❯ charly test/debug.charly
test
what
KCreate commented 8 years ago

This is most likely a bug caused by TFunc having the same bound_stack for each call and primitive member redirection injecting something weird as the self keyword.

Another example:

let Box = {
  let children = ["test", "what"];
  let iterate = children.each;

  func length() {
    5;
  };

  func __member(index) {
    print("__member: " + index);
  };
};

Box.children.push(1);
Box.children.push(2);
Box.children.push(3);

Box.iterate(func(e) {
  print(e);
});

Output:

❯ charly test/debug.charly
__member: 0
null
__member: 1
null
__member: 2
null
__member: 3
null
__member: 4
null
KCreate commented 8 years ago

This is most likely where this bug is caused: https://github.com/KCreate/charly-lang/blob/master/src/charly/interpreter/interpreter.cr#L749-L767

KCreate commented 8 years ago

Duplicate of #71