ringo / ringojs

RingoJS is a JavaScript platform built on the JVM and optimized for server-side applications.
https://ringojs.org
Apache License 2.0
838 stars 101 forks source link

`for...in` over `system.stdin` causes a hang in RingoJS v0.8.0 #157

Closed jdalton closed 13 years ago

jdalton commented 13 years ago

Doing this..

for (var i in system.stdin) {
  print(i);
}
print('done');

print('done'); is never called

hns commented 13 years ago

This is an interesting one. Input streams in Ringo have an iterator()/__iterator__() method that iterates over the stream's input as described in CommonJS IO/A. So what that loop does is basically wait for input from stdin (you can try it by typing on the keyboard and hitting return).

Not sure what to think about this. It definitely doesn't obey to the principle of least astonishment. However it's not a bug, so closing.

jdalton commented 13 years ago

FWIW I didn't run into this issue in v0.7.0. Not sure how the behavior of for...in relates to an iterator() method.

hns commented 13 years ago

That's a JavaScript 1.7 extension. If you define a method called __iterator__() it will be called to iterate the object's properties.

(Sorry, underscores in my previous comment were not escaped and thus interpreted as Markdown)

jdalton commented 13 years ago

Ok that's for __iterator__() the CommonJS IO/A proposal doesn't seem to mention that, I only see iterator(). The other project currently supporting the IO proposal, Common-Node, allows iterating over the system.stdin object via for...in without problems (because it lacks support for the non-standard __iterator__() method).

I don't see how it's acceptable to break the language or dev expectations by pointing to non-standard functionality and proposed/draft spec by a non-formal standards body.

hns commented 13 years ago

You're completely right about the breaking dev expectations part. I was myself baffled when I first read your report.

I was going to respond that iteration hooks are planned for Harmony and that you're going to get used to things like that, but I see that the Harmony proposal uses a new for-of syntax for exactly those reasons.

The intention of the CommonJS proposal obviously was to make text streams iterate over input lines, but it doesn't say so explicitly, and the means to make it work are not available anywhere else than JS 1.7.

I think we went a bit too far here. I'll try to remove the for-in behavrior while keeping everything else working.

jdalton commented 12 years ago

@hns Thanks!

To avoid this issue in current versions of RingoJS I detect support for iteratorsand then wrap objects with custom __iterator__ methods as Iterator(object) to avoid problems.