all files / lib/ interpreter.js

90.51% Statements 124/137
82.71% Branches 110/133
84.62% Functions 11/13
90.51% Lines 124/137
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       256619×     59303× 59303× 59303× 59303× 59303× 59289× 291×       291× 291×   291×     59303× 59289× 291×   59289× 291×     59303× 59303× 59303× 59303× 321248× 321248× 204446×     59303×   59303× 59303× 59303× 59303× 204446× 204446× 168508×     59303×   59303× 57766× 57766× 32479×     59303× 52173× 6403×   52173× 16699× 16695×   16699× 16695×   16699× 16695× 16695×     16695× 16695× 16695× 75846× 75846× 15193×   60653× 60653× 367489× 367489× 93165×   274324× 274324×       16695×   16695× 16695× 16695× 5545×     16695× 5545×             303× 303× 303× 303× 303× 28×   303× 274×         303× 303× 303× 81×   303× 303× 5151× 5151×   303× 303× 303× 303× 24× 279× 142×     276×   303×     24× 926× 926× 926× 926× 77× 73× 27× 27×     46× 19× 19× 19× 19×                 907×                               303× 303× 303× 59305×   59303×          
(function() {
  var addedGlobals, isStatement, makeYieldFilter, updateState, upgradeEvaluator, _, _ref, _ref1, _ref2;
 
  _ = (_ref = (_ref1 = (_ref2 = typeof window !== "undefined" && window !== null ? window._ : void 0) != null ? _ref2 : typeof self !== "undefined" && self !== null ? self._ : void 0) != null ? _ref1 : typeof global !== "undefined" && global !== null ? global._ : void 0) != null ? _ref : require('lodash');
 
  addedGlobals = require('./protectBuiltins').addedGlobals;
 
  isStatement = function(name) {
    return name !== 'Literal' && name !== 'Identifier' && name !== 'ThisExpression' && name !== 'BlockStatement' && name !== 'MemberExpression' && name !== 'FunctionExpression' && name !== 'LogicalExpression' && name !== 'BinaryExpression' && name !== 'UnaryExpression';
  };
 
  updateState = function(aether, evaluator) {
    var astStack, bottom, f, frame_stack, n, p, rng, s, statementStack, top, variables, x, _base, _base1, _base2, _i, _j, _len, _ref3, _ref4;
    frame_stack = evaluator.frames;
    top = frame_stack[0];
    bottom = frame_stack[frame_stack.length - 1];
    if (aether.options.includeFlow) {
      if (bottom.flow == null) {
        bottom.flow = {
          statementsExecuted: 0,
          statements: []
        };
        Eif ((_base = aether.flow).states == null) {
          _base.states = [];
        }
        aether.flow.states.push(bottom.flow);
      }
    }
    if (aether.options.includeMetrics) {
      if ((_base1 = aether.metrics).statementsExecuted == null) {
        _base1.statementsExecuted = 0;
      }
      if ((_base2 = aether.metrics).callsExecuted == null) {
        _base2.callsExecuted = 0;
      }
    }
    astStack = (function() {
      var _i, _len, _results;
      _results = [];
      for (_i = 0, _len = frame_stack.length; _i < _len; _i++) {
        x = frame_stack[_i];
        if (x.ast != null) {
          _results.push(x.ast);
        }
      }
      return _results;
    })();
    statementStack = (function() {
      var _i, _len, _results;
      _results = [];
      for (_i = 0, _len = astStack.length; _i < _len; _i++) {
        x = astStack[_i];
        if (isStatement(x.type)) {
          _results.push(x);
        }
      }
      return _results;
    })();
    if (statementStack[0] != null) {
      rng = statementStack[0].originalRange;
      if (rng) {
        aether.lastStatementRange = [rng.start, rng.end];
      }
    }
    if (top.ast != null) {
      if (aether.options.includeMetrics && top.ast.type === 'CallExpression') {
        ++aether.metrics.callsExecuted;
      }
      if (isStatement(top.ast.type)) {
        if (aether.options.includeMetrics) {
          ++aether.metrics.statementsExecuted;
        }
        if (bottom.flow != null) {
          ++bottom.flow.statementsExecuted;
        }
        if (bottom.flow != null) {
          f = {};
          Iif (aether._userInfo != null) {
            f.userInfo = _.cloneDeep(aether._userInfo);
          }
          Eif (!aether.options.noVariablesInFlow) {
            variables = {};
            for (s = _i = _ref3 = frame_stack.length - 2; _ref3 <= 0 ? _i <= 0 : _i >= 0; s = _ref3 <= 0 ? ++_i : --_i) {
              p = frame_stack[s];
              if (!p.scope) {
                continue;
              }
              _ref4 = Object.keys(p.scope.object.properties);
              for (_j = 0, _len = _ref4.length; _j < _len; _j++) {
                n = _ref4[_j];
                if (n[0] === '_') {
                  continue;
                }
                Eif (p.value) {
                  variables[n] = p.value.debugString;
                }
              }
            }
            f.variables = variables;
          }
          Eif (astStack[0] != null) {
            rng = astStack[0].originalRange;
            if (rng) {
              f.range = [rng.start, rng.end];
            }
          }
          if (!!f.range) {
            return bottom.flow.statements.push(f);
          }
        }
      }
    }
  };
 
  module.exports.createFunction = function(aether, code) {
    var engine, esper, fx, fxName, messWithLoops, name, state, x, _i, _len, _ref3, _ref4, _ref5, _ref6;
    esper = (_ref3 = (_ref4 = (_ref5 = typeof window !== "undefined" && window !== null ? window.esper : void 0) != null ? _ref5 : typeof self !== "undefined" && self !== null ? self.esper : void 0) != null ? _ref4 : typeof global !== "undefined" && global !== null ? global.esper : void 0) != null ? _ref3 : require('esper.js');
    state = {};
    messWithLoops = false;
    if (aether.options.whileTrueAutoYield || aether.options.simpleLoops) {
      messWithLoops = true;
    }
    if (!aether.esperEngine) {
      aether.esperEngine = new esper.Engine({
        strict: true,
        foreignObjectMode: 'smart'
      });
    }
    engine = aether.esperEngine;
    fxName = aether.options.functionName || 'foo';
    if (aether.language.injectCode != null) {
      engine.evalASTSync(aether.language.injectCode);
    }
    _ref6 = Object.keys(addedGlobals);
    for (_i = 0, _len = _ref6.length; _i < _len; _i++) {
      name = _ref6[_i];
      engine.addGlobal(name, addedGlobals[name]);
    }
    engine.evalASTSync(aether.ast);
    upgradeEvaluator(aether, engine.evaluator);
    x = 0;
    if (aether.options.yieldConditionally) {
      fx = engine.fetchFunction(fxName, makeYieldFilter(aether));
    } else if (aether.options.yieldAutomatically) {
      fx = engine.fetchFunction(fxName, function(engine) {
        return true;
      });
    } else {
      fx = engine.fetchFunctionSync(fxName);
    }
    return fx;
  };
 
  makeYieldFilter = function(aether) {
    return function(engine) {
      var frame_stack, newMark, top;
      frame_stack = engine.evaluator.frames;
      top = frame_stack[0];
      if (top.type === 'loop') {
        if (frame_stack[1].ast.type === 'WhileStatement' && frame_stack[1].ast.test.type === 'Literal') {
          if (!top.marked) {
            top.marked = true;
            Iif (aether.whileLoopMarker) {
              top.mark = aether.whileLoopMarker();
            }
          } else if (top.ast == null) {
            Eif (!top.didYield) {
              Eif (!aether.whileLoopMarker || aether.whileLoopMarker() === top.mark) {
                top.didYield = false;
                return true;
              } else {
                newMark = aether.whileLoopMarker();
                top.mark = newMark;
              }
            }
          }
        }
      }
      return false;
    };
  };
 
  module.exports.createThread = function(aether, fx) {
    var Evaluator, engine, internalFx;
    internalFx = esper.Value.getBookmark(fx);
    engine = new esper.Engine(aether.esperEngine.options);
    Evaluator = aether.esperEngine.evaluator.constructor;
    engine.evaluator = new Evaluator(aether.esperEngine.realm, aether.esperEngine.evaluator.ast, aether.esperEngine.evaluator.frames[0].scope);
    engine.evaluator.frames = [];
    upgradeEvaluator(aether, engine.evaluator);
    return engine.makeFunctionFromClosure(internalFx, function() {
      return false;
    });
  };
 
  module.exports.upgradeEvaluator = upgradeEvaluator = function(aether, evaluator) {
    var executionCount;
    executionCount = 0;
    return evaluator.instrument = function() {
      if (++executionCount > aether.options.executionLimit) {
        throw new TypeError('Statement execution limit reached');
      }
      return updateState(aether, evaluator);
    };
  };
 
}).call(this);