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

System.print causes different output #801

Closed
mathewmariani opened this issue Aug 19, 2020 · 4 comments
Closed

System.print causes different output #801

mathewmariani opened this issue Aug 19, 2020 · 4 comments

Comments

@mathewmariani
Copy link
Contributor

I came across this strange error were adding a System.print statement changes the expected output of a function.

class SudoList {
  construct new() {
    _a = [10, 7, 8, 9, 1, 5]
  }
  
  sort(fn) {
    quicksort_(0, _a.count - 1, fn)
    System.print(_a)
  }

  quicksort_(a, b, fn) {
    if (a < b) {
      var p = partition_(a, b, fn)
      quicksort_(a, p - 1, fn)
      quicksort_(p + 1, b, fn)
    }
  }

  partition_(a, b, fn) {
    var p = _a[b]
    var i = a - 1    
    for (j in a..(b-1)) {
      // commenting and uncommenting this line yields different results
      System.print("bug stopper") 
      if (fn.call(_a[j], p)) {
        i = i + 1
        var t = _a[i]
        _a[i] = _a[j]
        _a[j] = t
      }
    }
    var t = _a[i+1]
    _a[i+1] = _a[b]
    _a[b] = t
    return i+1
  }
}

var w = SudoList.new()
w.sort{|a, b| a < b }

I've run this code in both the VM and in the browser at wren.io/try; additionally, the error is not present when running the script on wren-nest.

When System.print("bug stopper") is uncommented the program runs as expected.
When commented out the program crashes giving a Right operand must be a number. error.

Complete error:

Right operand must be a number.
[compile line 41] in sort(_) block argument
[compile line 25] in partition_(_,_,_)
[compile line 14] in quicksort_(_,_,_)
[compile line 16] in quicksort_(_,_,_)
[compile line 8] in sort(_)
[compile line 41] in (script)
@mhermier
Copy link
Contributor

mhermier commented Aug 19, 2020 via email

@ruby0x1
Copy link
Member

ruby0x1 commented Aug 19, 2020

This should hopefully be fixed soon, just waiting on some feedback. Thanks for the report and examples!

@mathewmariani
Copy link
Contributor Author

If you are on Linux try to run wren inside valgrind. I suspect you are hitting a variation of the well-known stack corruption.

I'm running on macOS currently.

@ruby0x1
Copy link
Member

ruby0x1 commented Sep 18, 2020

Hi @mathewmariani, try with #807 merged, I tested your code and the results are consistent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants