more regenerator spring cleaning

This commit is contained in:
Sebastian McKenzie 2014-11-19 12:09:41 +11:00
parent cacee5c625
commit f43a3dec4b
5 changed files with 37 additions and 43 deletions

View File

@ -23,7 +23,7 @@ var n = types.namedTypes;
function Emitter(contextId) { function Emitter(contextId) {
assert.ok(this instanceof Emitter); assert.ok(this instanceof Emitter);
n.Identifier.assert(contextId); t.assertIdentifier(contextId);
// In order to make sure the context object does not collide with // In order to make sure the context object does not collide with
// anything in the local scope, we might have to rename it, so we // anything in the local scope, we might have to rename it, so we
@ -64,7 +64,7 @@ function loc() {
// Sets the exact value of the given location to the offset of the next // Sets the exact value of the given location to the offset of the next
// Statement emitted. // Statement emitted.
Emitter.prototype.mark = function(loc) { Emitter.prototype.mark = function(loc) {
n.Literal.assert(loc); t.assertLiteral(loc);
var index = this.listing.length; var index = this.listing.length;
if (loc.value === -1) { if (loc.value === -1) {
loc.value = index; loc.value = index;
@ -79,7 +79,7 @@ Emitter.prototype.mark = function(loc) {
Emitter.prototype.emit = function(node) { Emitter.prototype.emit = function(node) {
if (t.isExpression(node)) node = t.expressionStatement(node); if (t.isExpression(node)) node = t.expressionStatement(node);
n.Statement.assert(node); t.assertStatement(node);
this.listing.push(node); this.listing.push(node);
}; };
@ -145,7 +145,7 @@ Emitter.prototype.stop = function(rval) {
}; };
Emitter.prototype.setReturnValue = function(valuePath) { Emitter.prototype.setReturnValue = function(valuePath) {
n.Expression.assert(valuePath.value); t.assertExpression(valuePath.value);
this.emitAssign( this.emitAssign(
this.contextProperty("rval"), this.contextProperty("rval"),
@ -154,7 +154,7 @@ Emitter.prototype.setReturnValue = function(valuePath) {
}; };
Emitter.prototype.clearPendingException = function(tryLoc, assignee) { Emitter.prototype.clearPendingException = function(tryLoc, assignee) {
n.Literal.assert(tryLoc); t.assertLiteral(tryLoc);
var catchCall = t.callExpression( var catchCall = t.callExpression(
this.contextProperty("catch", true), this.contextProperty("catch", true),
@ -177,8 +177,8 @@ Emitter.prototype.jump = function(toLoc) {
// Conditional jump. // Conditional jump.
Emitter.prototype.jumpIf = function(test, toLoc) { Emitter.prototype.jumpIf = function(test, toLoc) {
n.Expression.assert(test); t.assertExpression(test);
n.Literal.assert(toLoc); t.assertLiteral(toLoc);
this.emit(t.ifStatement( this.emit(t.ifStatement(
test, test,
@ -191,8 +191,8 @@ Emitter.prototype.jumpIf = function(test, toLoc) {
// Conditional jump, with the condition negated. // Conditional jump, with the condition negated.
Emitter.prototype.jumpIfNot = function(test, toLoc) { Emitter.prototype.jumpIfNot = function(test, toLoc) {
n.Expression.assert(test); t.assertExpression(test);
n.Literal.assert(toLoc); t.assertLiteral(toLoc);
var negatedTest; var negatedTest;
if (t.isUnaryExpression(test) && test.operator === "!") { if (t.isUnaryExpression(test) && test.operator === "!") {
@ -352,7 +352,7 @@ Emitter.prototype.explode = function(path, ignoreResult) {
var node = path.value; var node = path.value;
var self = this; var self = this;
n.Node.assert(node); n.Node.check(node);
if (t.isStatement(node)) if (t.isStatement(node))
return self.explodeStatement(path); return self.explodeStatement(path);
@ -396,10 +396,10 @@ Emitter.prototype.explodeStatement = function(path, labelId) {
var self = this; var self = this;
var after, head; var after, head;
n.Statement.assert(stmt); t.assertStatement(stmt);
if (labelId) { if (labelId) {
n.Identifier.assert(labelId); t.assertIdentifier(labelId);
} else { } else {
labelId = null; labelId = null;
} }
@ -502,7 +502,7 @@ Emitter.prototype.explodeStatement = function(path, labelId) {
break; break;
case "ForInStatement": case "ForInStatement":
n.Identifier.assert(stmt.left); t.assertIdentifier(stmt.left);
head = loc(); head = loc();
after = loc(); after = loc();
@ -586,7 +586,7 @@ Emitter.prototype.explodeStatement = function(path, labelId) {
for (var i = cases.length - 1; i >= 0; --i) { for (var i = cases.length - 1; i >= 0; --i) {
var c = cases[i]; var c = cases[i];
n.SwitchCase.assert(c); t.assertSwitchCase(c);
if (c.test) { if (c.test) {
condition = t.conditionalExpression( condition = t.conditionalExpression(
@ -706,7 +706,7 @@ Emitter.prototype.explodeStatement = function(path, labelId) {
var catchScope = bodyPath.scope; var catchScope = bodyPath.scope;
var catchParamName = handler.param.name; var catchParamName = handler.param.name;
n.CatchClause.assert(catchScope.node); t.assertCatchClause(catchScope.node);
assert.strictEqual(catchScope.lookup(catchParamName), catchScope); assert.strictEqual(catchScope.lookup(catchParamName), catchScope);
types.visit(bodyPath, { types.visit(bodyPath, {
@ -770,11 +770,11 @@ Emitter.prototype.emitAbruptCompletion = function(record) {
var abruptArgs = [t.literal(record.type)]; var abruptArgs = [t.literal(record.type)];
if (record.type === "break" || record.type === "continue") { if (record.type === "break" || record.type === "continue") {
n.Literal.assert(record.target); t.assertLiteral(record.target);
abruptArgs[1] = record.target; abruptArgs[1] = record.target;
} else if (record.type === "return" || record.type === "throw") { } else if (record.type === "return" || record.type === "throw") {
if (record.value) { if (record.value) {
n.Expression.assert(record.value); t.assertExpression(record.value);
abruptArgs[1] = record.value; abruptArgs[1] = record.value;
} }
} }
@ -797,7 +797,7 @@ function isValidCompletion(record) {
} }
if (type === "break" || type === "continue") { if (type === "break" || type === "continue") {
return !_.has(record, "value") && t.isLiteral(record.target);; return !_.has(record, "value") && t.isLiteral(record.target);
} }
if (type === "return" || type === "throw") { if (type === "return" || type === "throw") {
@ -807,7 +807,6 @@ function isValidCompletion(record) {
return false; return false;
} }
// Not all offsets into emitter.listing are potential jump targets. For // Not all offsets into emitter.listing are potential jump targets. For
// example, execution typically falls into the beginning of a try block // example, execution typically falls into the beginning of a try block
// without jumping directly there. This method returns the current offset // without jumping directly there. This method returns the current offset
@ -833,7 +832,7 @@ Emitter.prototype.getUnmarkedCurrentLoc = function() {
// be costly and verbose to set context.prev before every statement. // be costly and verbose to set context.prev before every statement.
Emitter.prototype.updateContextPrevLoc = function(loc) { Emitter.prototype.updateContextPrevLoc = function(loc) {
if (loc) { if (loc) {
n.Literal.assert(loc); t.assertLiteral(loc);
if (loc.value === -1) { if (loc.value === -1) {
// If an uninitialized location literal was passed in, set its value // If an uninitialized location literal was passed in, set its value
@ -859,7 +858,7 @@ Emitter.prototype.explodeExpression = function(path, ignoreResult) {
var expr = path.value; var expr = path.value;
if (expr) { if (expr) {
n.Expression.assert(expr); t.assertExpression(expr);
} else { } else {
return expr; return expr;
} }
@ -868,7 +867,7 @@ Emitter.prototype.explodeExpression = function(path, ignoreResult) {
var result, after; // Used optionally by several cases below. var result, after; // Used optionally by several cases below.
function finish(expr) { function finish(expr) {
n.Expression.assert(expr); t.assertExpression(expr);
if (ignoreResult) { if (ignoreResult) {
self.emit(expr); self.emit(expr);
} else { } else {

View File

@ -13,20 +13,18 @@ var types = require("ast-types");
var t = require("../../../types"); var t = require("../../../types");
var _ = require("lodash"); var _ = require("lodash");
var n = types.namedTypes;
// The hoist function takes a FunctionExpression or FunctionDeclaration // The hoist function takes a FunctionExpression or FunctionDeclaration
// and replaces any Declaration nodes in its body with assignments, then // and replaces any Declaration nodes in its body with assignments, then
// returns a VariableDeclaration containing just the names of the removed // returns a VariableDeclaration containing just the names of the removed
// declarations. // declarations.
exports.hoist = function(funPath) { exports.hoist = function(funPath) {
assert.ok(funPath instanceof types.NodePath); assert.ok(funPath instanceof types.NodePath);
n.Function.assert(funPath.value); t.assertFunction(funPath.value);
var vars = {}; var vars = {};
function varDeclToExpr(vdec, includeIdentifiers) { function varDeclToExpr(vdec, includeIdentifiers) {
n.VariableDeclaration.assert(vdec); t.assertVariableDeclaration(vdec);
var exprs = []; var exprs = [];
vdec.declarations.forEach(function(dec) { vdec.declarations.forEach(function(dec) {

View File

@ -17,11 +17,10 @@ exports.LoopEntry = LoopEntry;
exports.TryEntry = TryEntry; exports.TryEntry = TryEntry;
var assert = require("assert"); var assert = require("assert");
var types = require("ast-types");
var util = require("util"); var util = require("util");
var t = require("../../../types");
var inherits = util.inherits; var inherits = util.inherits;
var n = types.namedTypes;
function Entry() { function Entry() {
assert.ok(this instanceof Entry); assert.ok(this instanceof Entry);
@ -30,7 +29,7 @@ function Entry() {
function FunctionEntry(returnLoc) { function FunctionEntry(returnLoc) {
Entry.call(this); Entry.call(this);
n.Literal.assert(returnLoc); t.assertLiteral(returnLoc);
this.returnLoc = returnLoc; this.returnLoc = returnLoc;
} }
@ -40,11 +39,11 @@ inherits(FunctionEntry, Entry);
function LoopEntry(breakLoc, continueLoc, label) { function LoopEntry(breakLoc, continueLoc, label) {
Entry.call(this); Entry.call(this);
n.Literal.assert(breakLoc); t.assertLiteral(breakLoc);
n.Literal.assert(continueLoc); t.assertLiteral(continueLoc);
if (label) { if (label) {
n.Identifier.assert(label); t.assertIdentifier(label);
} else { } else {
label = null; label = null;
} }
@ -59,7 +58,7 @@ inherits(LoopEntry, Entry);
function SwitchEntry(breakLoc) { function SwitchEntry(breakLoc) {
Entry.call(this); Entry.call(this);
n.Literal.assert(breakLoc); t.assertLiteral(breakLoc);
this.breakLoc = breakLoc; this.breakLoc = breakLoc;
} }
@ -69,7 +68,7 @@ inherits(SwitchEntry, Entry);
function TryEntry(firstLoc, catchEntry, finallyEntry) { function TryEntry(firstLoc, catchEntry, finallyEntry) {
Entry.call(this); Entry.call(this);
n.Literal.assert(firstLoc); t.assertLiteral(firstLoc);
if (catchEntry) { if (catchEntry) {
assert.ok(catchEntry instanceof CatchEntry); assert.ok(catchEntry instanceof CatchEntry);
@ -96,8 +95,8 @@ inherits(TryEntry, Entry);
function CatchEntry(firstLoc, paramId) { function CatchEntry(firstLoc, paramId) {
Entry.call(this); Entry.call(this);
n.Literal.assert(firstLoc); t.assertLiteral(firstLoc);
n.Identifier.assert(paramId); t.assertIdentifier(paramId);
this.firstLoc = firstLoc; this.firstLoc = firstLoc;
this.paramId = paramId; this.paramId = paramId;
@ -108,7 +107,7 @@ inherits(CatchEntry, Entry);
function FinallyEntry(firstLoc) { function FinallyEntry(firstLoc) {
Entry.call(this); Entry.call(this);
n.Literal.assert(firstLoc); t.assertLiteral(firstLoc);
this.firstLoc = firstLoc; this.firstLoc = firstLoc;
} }

View File

@ -19,7 +19,7 @@ var n = types.namedTypes;
function makePredicate(propertyName, knownTypes) { function makePredicate(propertyName, knownTypes) {
function onlyChildren(node) { function onlyChildren(node) {
n.Node.assert(node); n.Node.check(node);
// Assume no side effects until we find out otherwise. // Assume no side effects until we find out otherwise.
var result = false; var result = false;
@ -44,7 +44,7 @@ function makePredicate(propertyName, knownTypes) {
} }
function predicate(node) { function predicate(node) {
n.Node.assert(node); n.Node.check(node);
var meta = m(node); var meta = m(node);
if (_.has(meta, propertyName)) return meta[propertyName]; if (_.has(meta, propertyName)) return meta[propertyName];

View File

@ -14,8 +14,6 @@ var hoist = require("./hoist").hoist;
var types = require("ast-types"); var types = require("ast-types");
var t = require("../../../types"); var t = require("../../../types");
var n = types.namedTypes;
var runtimeAsyncMethod = runtimeProperty("async"); var runtimeAsyncMethod = runtimeProperty("async");
var runtimeWrapMethod = runtimeProperty("wrap"); var runtimeWrapMethod = runtimeProperty("wrap");
var runtimeMarkMethod = runtimeProperty("mark"); var runtimeMarkMethod = runtimeProperty("mark");
@ -179,7 +177,7 @@ var visitor = types.PathVisitor.fromMethodsObject({
bodyPath.push(varDecl); bodyPath.push(varDecl);
} else { } else {
n.FunctionExpression.assert(node); t.assertFunctionExpression(node);
return t.callExpression(runtimeMarkMethod, [node]); return t.callExpression(runtimeMarkMethod, [node]);
} }
} }
@ -187,7 +185,7 @@ var visitor = types.PathVisitor.fromMethodsObject({
function shouldNotHoistAbove(stmtPath) { function shouldNotHoistAbove(stmtPath) {
var value = stmtPath.value; var value = stmtPath.value;
n.Statement.assert(value); t.assertStatement(value);
// If the first statement is a "use strict" declaration, make sure to // If the first statement is a "use strict" declaration, make sure to
// insert hoisted declarations afterwards. // insert hoisted declarations afterwards.