All files / lib 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 202x 202x 202x 202x 202x 202x             1x 1553x   1553x 1553x 1198x     1553x     1x 236x   54x   89x   15x       1x 241x 241x   241x             241x       1x 754x     1x 3393x   3393x     1x 81x 57x       1x 81x 57x       1x 27x     1x 27x     1x 54x 27x     1x 27x     1x 208x   6x         1x 208x 208x 45x               1x 3634x 3634x 3634x   3634x 1811x 3248x 3248x       3634x     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;