Skip to content
This repository was archived by the owner on Feb 1, 2025. It is now read-only.

Read left side of update assignment before right side yields. - #349

Merged
benjamn merged 2 commits into
masterfrom
issue-345-fix-yielding-update-assignments
Jun 23, 2018
Merged

benjamn merged 2 commits into
masterfrom
issue-345-fix-yielding-update-assignments

Conversation

@benjamn

@benjamn benjamn commented Jun 23, 2018

Copy link
Copy Markdown
Contributor

Fixes #345.

In addition to the examples provided by @loganfsmyth and @lovetingyuan in #345, I found another even trickier failing test case, where the left-hand side also yields:

let obj = { count: 0 };

function* test() {
  obj[yield "key"] += yield "value";
}

var gen = test();

assert.deepEqual(gen.next(), { value: "key", done: false });
assert.strictEqual(obj.count, 0);

assert.deepEqual(gen.next("count"), { value: "value", done: false });
assert.strictEqual(obj.count, 0);

obj.count += 1;
assert.strictEqual(obj.count, 1);

assert.deepEqual(gen.next(2), { value: void 0, done: true });
assert.strictEqual(obj.count, 2);

Here's how the test generator function was previously compiled, incorrectly:

function test() {
  return regeneratorRuntime.wrap(function test$(_context121) {
    while (1) {
      switch (_context121.prev = _context121.next) {
        case 0:
          _context121.next = 2;
          return "key";

        case 2:
          _context121.t0 = _context121.sent;
          _context121.next = 5;
          return "value";

        case 5:
          obj[_context121.t0] += _context121.sent;

        case 6:
        case "end":
          return _context121.stop();
      }
    }
  }, _marked115, this);
}

And here's how the test function is now compiled, correctly:

function test() {
  return regeneratorRuntime.wrap(function test$(_context121) {
    while (1) {  
      switch (_context121.prev = _context121.next) {
        case 0:  
          _context121.next = 2;
          return "key";

        case 2:  
          _context121.t0 = _context121.sent;
          _context121.t1 = obj[_context121.t0];
          _context121.next = 6;
          return "value";

        case 6:  
          obj[_context121.t0] = _context121.t1 += _context121.sent;

        case 7:  
        case "end":
          return _context121.stop();
      }
    }
  }, _marked115, this);
}

@benjamn benjamn self-assigned this Jun 23, 2018
@benjamn
benjamn force-pushed the issue-345-fix-yielding-update-assignments branch from af5d012 to 837527a Compare June 23, 2018 16:44
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants