Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Generator function.sent Meta Property

License

NotificationsYou must be signed in to change notification settings

tc39/proposal-function.sent

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Status

Stage:
2
Author:
Allen Wirfs-Brock (May 13, 2015)
Champion:
HE Shi-Jun (@hax)

The Problem

When thenext method is invoked on a generator objects, the value passed as the first argument tonext is "sent" to the generator object and becomes available within the body of the generator function as the value of theyield expression that most recently suspended the generator function. This supports two-way communications between the a generator object and its consumer.

However, the firstnext that a generator's consumer invokes to start a generator object does not correspond to anyyield within the body of the generator function. Instead, the firstnext simply causes execution of the generator function body to begin at the top of the body.

Because there the firstnext call does not correspond to ayield within the generator function body there is currently no way for the code with the body to access the initialnext argument. For example:

function*adder(total=0){letincrement=1;while(true){switch(request=yieldtotal+=increment){caseundefined:break;case"done":returntotal;default:increment=Number(request);}}}lettally=adder();tally.next(0.1);// argument will be ignoredtally.next(0.1);tally.next(0.1);letlast=tally.next("done");console.log(last.value);//1.2 instead of 0.3

In the above example, the argument to thenext method normally supplies the value to added to a running tally. Except that the increment value supplied to the first next is ignored.

This proposal provides an alternative way to access thenext parameter that works on the first and all subsequent invocations of a generator'snext method.

The Proposal

A new meta-property:function.sent

Value and Context

The value offunction.sent within the body of a Generator Function is the value passed to the generator by thenext method that most recently resumed execution of the generator. In particular, referencingfunction.sent prior to the first evaluation of ayield operator returns the argument value passed by thenext call that started evaluation of theGeneratorBody.

function.sent can appear anywhere aYieldExpress would be legal. Referencingfunction.sent outside of aGeneratorBody is a Syntax Error.

Usage Example

Here is how the above example might be rewritten usingfunction.sent

function*adder(total=0){letincrement=1;do{switch(request=function.sent){caseundefined:break;case"done":returntotal;default:increment=Number(request);}yieldtotal+=increment;}while(true)}lettally=adder();tally.next(0.1);// argument no longer ignoredtally.next(0.1);tally.next(0.1);letlast=tally.next("done");console.log(last.value);//0.3

Specification Updates

The following are deltas to the ECMAScript 2015 Language Specification

8.3 Execution Contests

The following row is added toTable 24:

ComponentDescription
LastYieldValueThe value of the most recently evaluatedYieldExpression

12.3 Left-Hand-Side Expression

Syntax

MemberExpression[Yield]  :
            ...
            MetaProperty[?Yield]
            ...

MetaProperty[Yield]  :
            NewTarget
             [+Yield]FunctionSent

14.4 Generator Function Definitions

Syntax

FunctionSent  :
            function . sent

14.4.14 Evaluation

FunctionSent : function . sent
    1.  Assert: the running execution context is a Generator Context.
    2.  LetgenContext be the running execution context.
    3.  Return the value of the LastYieldValue component ofgenContext .

25.3.3.1 GeneratorStart(generator, generatorBody)

Between lines 3 and 4 of the ES6 algorithm add the following step:

    3.5.  Set the LastYieldValue component ofgenContext toundefined.

25.3.3.3 GeneratorResume(generator, value)

Between lines 8 and 9 of the ES6 algorithm add the following step:

    8.5.  Set the LastYieldValue component ofgenContext tovalue.

25.3.3.5 GeneratorYield(iterNextObj)

Between lines 5 and 6 of the ES6 algorithm add the following step:

    5.5.  Set the LastYieldValue component ofgenContext toundefined.

About

Generator function.sent Meta Property

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Languages


[8]ページ先頭

©2009-2025 Movatter.jp