JavaScript 1.7 yield

K

Kaleb Hornsby

I am having difficulty finding a working example of the yield
statement's .send() method in action.
This is what I have:

var f = function(x) {
var i = isNaN(x)? 0: x;
while(true) {
yield i++;
}
}
g = f();
g.next(); // 0
g.next(); // 1
g.next(); // 2
g.next(); // 3
g.send(2); // 4 should these return 2???
g.send(2); // 5
g.send(2); // 6
 
T

Thomas 'PointedEars' Lahn

Kaleb said:
I am having difficulty finding a working example of the yield
statement's .send() method in action.
This is what I have:

var f = function(x) {
var i = isNaN(x)? 0: x;
while(true) {
yield i++;
}
}
g = f();
g.next(); // 0
g.next(); // 1
g.next(); // 2
g.next(); // 3
g.send(2); // 4 should these return 2???

I'm not sure about that, the specification of the method is rather vague on
that:

,-<https://developer.mozilla.org/en/New_in_JavaScript_1.7#Resuming_a_generator_at_a_specific_point>
|
| Once a generator has been started by calling its next() method,
| you can use send(), passing a specific value that will be treated
| as the result of the last yield. The generator will then return
| the operand of the subsequent yield.

However, the next generator.next() call should definitely return 3 if the
generator.send() method worked as described, and it doesn't. So it appears
that generator.send() is borken in Firefox 3(.0.6 for Linux), that is, it
works as if its argument is ignored. Barring further corrections, you
should file a bug or vote for an existing one (I don't know one, but I'm not
as up-to-date on Bugzilla as before.)

BTW, this is how I tested it in Firebug 1.3X3 which does not appear to
provide means to define the JavaScript version for the console (Joe
[Hewitt], please add that feature, like version(170)! TIA):

var s = document.createElement("script");
s.type = "text/javascript; version=1.7";
s.appendChild(document.createTextNode(<xml>
<![CDATA[
var f = function(x) {
var i = isNaN(x) ? 0 : x;active
while (true)
{
yield i++;
}
};

function bar()
{
try
{
var g = f();

/* 0 */
console.log(g.next());

/* 1 */
console.log(g.next());

/* 2 */
console.log(g.next());

/* 3 */
console.log(g.send(1));

/* yields 4, not 2 */
console.log(g.next());
}
catch (e)
{
/* not thrown */
e
}
}
]]>
</xml>));

document.body.appendChild(s);

bar();


PointedEars
 
K

Kaleb Hornsby

Yes, I have read this specification and have been using the
spidermonkey javascript engine to try and implement my code. I can get
the .next() and .close() methods to work, but I have no idea how to
get the .send() method to do what I think it is supposed to do.
 
M

Martin Honnen

Kaleb said:
I am having difficulty finding a working example of the yield
statement's .send() method in action.

See answer in the Mozilla mozilla.dev.tech.js-engine group where you
posted too.
 
T

Thomas 'PointedEars' Lahn

Kaleb said:

Yes -- to what of what I posted?
I have read this specification and have been using the
spidermonkey javascript engine to try and implement my code. I can get
the .next() and .close() methods to work, but I have no idea how to
get the .send() method to do what I think it is supposed to do.

If it's actually borken, you can't until the fix is checked in.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top