Code coverage report for 6to5/transformation/transformers/es6-generators/emit/explode-statements.js

Statements: 92.2% (130 / 141)      Branches: 80.95% (34 / 42)      Functions: 91.3% (21 / 23)      Lines: 92.2% (130 / 141)      Ignored: none     

All files » 6to5/transformation/transformers/es6-generators/emit/ » explode-statements.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335                    1 1 1 1 1   1 1   1 173     1 1     1 6 6   6 6 6   6   6 6     1                             1 9 9 9   9     9     9   9 9         9   9     9   9     7     9   9     1 6   6 6   6 6               6   6 6                         6                 6   6     6   6     1 2           1 1           1     1         1 1 1 1 1     1   1 2 2   2 2                   1       1     1 2   2   2               1 1 1 1       1 17 17   17         17   17 4 4 4     17     1 56           1 37 37   37 37       37 37         37 37   37           37 37   37 37   37 24       5         19     24   24 24 24   24 24 24 24   24   124   29   95       24 24       37 18   18 18     18             37     1 12        
/**
 * Copyright (c) 2014, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * https://raw.github.com/facebook/regenerator/master/LICENSE file. An
 * additional grant of patent rights can be found in the PATENTS file in
 * the same directory.
 */
 
var assert = require("assert");
var types  = require("ast-types");
var leap   = require("../leap");
var util   = require("../util");
var t      = require("../../../../types");
 
var runtimeKeysMethod = util.runtimeProperty("keys");
var loc               = util.loc;
 
exports.ExpressionStatement = function (path) {
  this.explodeExpression(path.get("expression"), true);
};
 
exports.LabeledStatement = function (path, stmt) {
  this.explodeStatement(path.get("body"), stmt.label);
};
 
exports.WhileStatement = function (path, stmt, labelId) {
  var before = loc();
  var after = loc();
 
  this.mark(before);
  this.jumpIfNot(this.explodeExpression(path.get("test")), after);
  this.leapManager.withEntry(
    new leap.LoopEntry(after, before, labelId),
    function () { this.explodeStatement(path.get("body")); }
  );
  this.jump(before);
  this.mark(after);
};
 
exports.DoWhileStatement = function (path, stmt, labelId) {
  var first = loc();
  var test = loc();
  var after = loc();
 
  this.mark(first);
  this.leapManager.withEntry(
    new leap.LoopEntry(after, test, labelId),
    function () { this.explode(path.get("body")); }
  );
  this.mark(test);
  this.jumpIf(this.explodeExpression(path.get("test")), first);
  this.mark(after);
};
 
exports.ForStatement = function (path, stmt, labelId) {
  var head = loc();
  var update = loc();
  var after = loc();
 
  Eif (stmt.init) {
    // We pass true here to indicate that if stmt.init is an expression
    // then we do not care about its result.
    this.explode(path.get("init"), true);
  }
 
  this.mark(head);
 
  Eif (stmt.test) {
    this.jumpIfNot(this.explodeExpression(path.get("test")), after);
  } else {
    // No test means continue unconditionally.
  }
 
  this.leapManager.withEntry(
    new leap.LoopEntry(after, update, labelId),
    function () { this.explodeStatement(path.get("body")); }
  );
 
  this.mark(update);
 
  if (stmt.update) {
    // We pass true here to indicate that if stmt.update is an
    // expression then we do not care about its result.
    this.explode(path.get("update"), true);
  }
 
  this.jump(head);
 
  this.mark(after);
};
 
exports.ForInStatement = function (path, stmt, labelId) {
  t.assertIdentifier(stmt.left);
 
  var head = loc();
  var after = loc();
 
  var keyIterNextFn = this.makeTempVar();
  this.emitAssign(
    keyIterNextFn,
    t.callExpression(
      runtimeKeysMethod,
      [this.explodeExpression(path.get("right"))]
    )
  );
 
  this.mark(head);
 
  var keyInfoTmpVar = this.makeTempVar();
  this.jumpIf(
    t.memberExpression(
      t.assignmentExpression(
        "=",
        keyInfoTmpVar,
        t.callExpression(keyIterNextFn, [])
      ),
      t.identifier("done"),
      false
    ),
    after
  );
 
  this.emitAssign(
    stmt.left,
    t.memberExpression(
      keyInfoTmpVar,
      t.identifier("value"),
      false
    )
  );
 
  this.leapManager.withEntry(
    new leap.LoopEntry(after, head, labelId),
    function () { this.explodeStatement(path.get("body")); }
  );
 
  this.jump(head);
 
  this.mark(after);
};
 
exports.BreakStatement = function (path, stmt) {
  this.emitAbruptCompletion({
    type: "break",
    target: this.leapManager.getBreakLoc(stmt.label)
  });
};
 
exports.ContinueStatement = function (path, stmt) {
  this.emitAbruptCompletion({
    type: "continue",
    target: this.leapManager.getContinueLoc(stmt.label)
  });
};
 
exports.SwitchStatement = function (path, stmt) {
  // Always save the discriminant into a temporary variable in case the
  // test expressions overwrite values like context.sent.
  var disc = this.emitAssign(
    this.makeTempVar(),
    this.explodeExpression(path.get("discriminant"))
  );
 
  var after = loc();
  var defaultLoc = loc();
  var condition = defaultLoc;
  var caseLocs = [];
  var self = this;
 
  // If there are no cases, .cases might be undefined.
  var cases = stmt.cases || [];
 
  for (var i = cases.length - 1; i >= 0; --i) {
    var c = cases[i];
    t.assertSwitchCase(c);
 
    Eif (c.test) {
      condition = t.conditionalExpression(
        t.binaryExpression("===", disc, c.test),
        caseLocs[i] = loc(),
        condition
      );
    } else {
      caseLocs[i] = defaultLoc;
    }
  }
 
  this.jump(this.explodeExpression(
    new types.NodePath(condition, path, "discriminant")
  ));
 
  this.leapManager.withEntry(
    new leap.SwitchEntry(after),
    function () {
      path.get("cases").each(function (casePath) {
        var i = casePath.name;
 
        self.mark(caseLocs[i]);
 
        casePath.get("consequent").each(
          self.explodeStatement,
          self
        );
      });
    }
  );
 
  this.mark(after);
  Eif (defaultLoc.value === -1) {
    this.mark(defaultLoc);
    assert.strictEqual(after.value, defaultLoc.value);
  }
};
 
exports.IfStatement = function (path, stmt) {
  var elseLoc = stmt.alternate && loc();
  var after = loc();
 
  this.jumpIfNot(
    this.explodeExpression(path.get("test")),
    elseLoc || after
  );
 
  this.explodeStatement(path.get("consequent"));
 
  if (elseLoc) {
    this.jump(after);
    this.mark(elseLoc);
    this.explodeStatement(path.get("alternate"));
  }
 
  this.mark(after);
};
 
exports.ReturnStatement = function (path) {
  this.emitAbruptCompletion({
    type: "return",
    value: this.explodeExpression(path.get("argument"))
  });
};
 
exports.TryStatement = function (path, stmt) {
  var after = loc();
  var self = this;
 
  var handler = stmt.handler;
  Iif (!handler && stmt.handlers) {
    handler = stmt.handlers[0] || null;
  }
 
  var catchLoc = handler && loc();
  var catchEntry = catchLoc && new leap.CatchEntry(
    catchLoc,
    handler.param
  );
 
  var finallyLoc = stmt.finalizer && loc();
  var finallyEntry = finallyLoc && new leap.FinallyEntry(finallyLoc);
 
  var tryEntry = new leap.TryEntry(
    this.getUnmarkedCurrentLoc(),
    catchEntry,
    finallyEntry
  );
 
  this.tryEntries.push(tryEntry);
  this.updateContextPrevLoc(tryEntry.firstLoc);
 
  this.leapManager.withEntry(tryEntry, function () {
    this.explodeStatement(path.get("block"));
 
    if (catchLoc) {
      if (finallyLoc) {
        // If we have both a catch block and a finally block, then
        // because we emit the catch block first, we need to jump over
        // it to the finally block.
        this.jump(finallyLoc);
 
      } else {
        // If there is no finally block, then we need to jump over the
        // catch block to the fall-through location.
        this.jump(after);
      }
 
      this.updateContextPrevLoc(self.mark(catchLoc));
 
      var bodyPath = path.get("handler", "body");
      var safeParam = this.makeTempVar();
      this.clearPendingException(tryEntry.firstLoc, safeParam);
 
      var catchScope = bodyPath.scope;
      var catchParamName = handler.param.name;
      t.assertCatchClause(catchScope.node);
      assert.strictEqual(catchScope.lookup(catchParamName), catchScope);
 
      types.visit(bodyPath, {
        visitIdentifier: function (path) {
          if (path.value.name === catchParamName &&
              path.scope.lookup(catchParamName) === catchScope) {
            return safeParam;
          }
          this.traverse(path);
        }
      });
 
      this.leapManager.withEntry(catchEntry, function () {
        this.explodeStatement(bodyPath);
      });
    }
 
    if (finallyLoc) {
      this.updateContextPrevLoc(this.mark(finallyLoc));
 
      this.leapManager.withEntry(finallyEntry, function () {
        this.explodeStatement(path.get("finalizer"));
      });
 
      this.emit(t.callExpression(
        this.contextProperty("finish"),
        [finallyEntry.firstLoc]
      ));
    }
  });
 
  this.mark(after);
};
 
exports.ThrowStatement = function (path) {
  this.emit(t.throwStatement(
    this.explodeExpression(path.get("argument"))
  ));
};