All files context.js

98.39% Statements 61/62
94.12% Branches 16/17
100% Functions 18/18
98.36% Lines 60/61

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                1x 211x 211x 211x 211x 211x 211x             1x 1668x   1668x 1668x 1304x     1668x     1x 250x   54x   89x   15x       1x 250x 250x   250x             250x       1x 837x     1x 3678x   3678x     1x 81x 57x       1x 81x 57x       1x 27x     1x 27x     1x 54x 27x     1x 27x     1x 217x   6x         1x 217x 217x 45x               1x 3928x 3928x 3928x   3928x 2030x 3724x 3724x       3928x     1x  
//========================================================================================
// class Context
//========================================================================================
 
//----------------------------------------------------------------------------------------
// constructor
//----------------------------------------------------------------------------------------
 
var Context = function() {
  this.code = "";
  this.scopes = [["vars"]];
  this.isAsync = false;
  this.bitFields = [];
  this.tmpVariableCount = 0;
  this.references = {};
};
 
//----------------------------------------------------------------------------------------
// public methods
//----------------------------------------------------------------------------------------
 
Context.prototype.generateVariable = function(name) {
  var arr = [];
 
  Array.prototype.push.apply(arr, this.scopes[this.scopes.length - 1]);
  if (name) {
    arr.push(name);
  }
 
  return arr.join(".");
};
 
Context.prototype.generateOption = function(val) {
  switch (typeof val) {
    case "number":
      return val.toString();
    case "string":
      return this.generateVariable(val);
    case "function":
      return "(" + val + ").call(" + this.generateVariable() + ", vars)";
  }
};
 
Context.prototype.generateError = function() {
  var args = Array.prototype.slice.call(arguments);
  var err = Context.interpolate.apply(this, args);
 
  Iif (this.isAsync) {
    this.pushCode(
      "return process.nextTick(function() { callback(new Error(" +
        err +
        "), vars); });"
    );
  } else {
    this.pushCode("throw new Error(" + err + ");");
  }
};
 
Context.prototype.generateTmpVariable = function() {
  return "$tmp" + this.tmpVariableCount++;
};
 
Context.prototype.pushCode = function() {
  var args = Array.prototype.slice.call(arguments);
 
  this.code += Context.interpolate.apply(this, args) + "\n";
};
 
Context.prototype.pushPath = function(name) {
  if (name) {
    this.scopes[this.scopes.length - 1].push(name);
  }
};
 
Context.prototype.popPath = function(name) {
  if (name) {
    this.scopes[this.scopes.length - 1].pop();
  }
};
 
Context.prototype.pushScope = function(name) {
  this.scopes.push([name]);
};
 
Context.prototype.popScope = function() {
  this.scopes.pop();
};
 
Context.prototype.addReference = function(alias) {
  if (this.references[alias]) return;
  this.references[alias] = { resolved: false, requested: false };
};
 
Context.prototype.markResolved = function(alias) {
  this.references[alias].resolved = true;
};
 
Context.prototype.markRequested = function(aliasList) {
  aliasList.forEach(
    function(alias) {
      this.references[alias].requested = true;
    }.bind(this)
  );
};
 
Context.prototype.getUnresolvedReferences = function() {
  var references = this.references;
  return Object.keys(this.references).filter(function(alias) {
    return !references[alias].resolved && !references[alias].requested;
  });
};
 
//----------------------------------------------------------------------------------------
// private methods
//----------------------------------------------------------------------------------------
 
Context.interpolate = function(s) {
  var re = /{\d+}/g;
  var matches = s.match(re);
  var params = Array.prototype.slice.call(arguments, 1);
 
  if (matches) {
    matches.forEach(function(match) {
      var index = parseInt(match.substr(1, match.length - 2), 10);
      s = s.replace(match, params[index].toString());
    });
  }
 
  return s;
};
 
exports.Context = Context;