Code coverage report for sc/lang/bytecode.js

Statements: 100% (204 / 204)      Branches: 100% (82 / 82)      Functions: 100% (30 / 30)      Lines: 100% (204 / 204)      Ignored: none     

All files » sc/lang/ » bytecode.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 3261     1 1 1   1 1 1   1 2887     1 1731 1731 1731 1731 1731 1731     1 1731 1731 10 10 10   1721 1721   1731 36   1731 1731 1731     1 3437 3437 3437 3437 3437 3437 3437     1 329 24   329     1 1833 1833     1 783 783     1 1262 999 999       1 2442 218 2224 582   1642       1 3629 3629   3629 3629   3629   3629 3629   3629 3639 3635 44 44     3625 4     3625 3625   3625   3625     1 582   582 1729     581     1 1262   1262   1262   1262 502 502 24       1262 784     1262   1262   1262     1 784 784   784 784 784   784 784 784 728 513 513 24 24 24     704 158     704   704 704 627 332   295       704 669       784     1 1262 4 4   1258 892 892   366 131 131   366 271 83   188 188   271       1 710 277 277   710     1 24     1 32 20   12     1 10675 55 10620 6336   10675     1 47 47 47     1 1115 1115 1115 676   1115     1 4 4 4 2   4 4 4 4 4     1 6 6 6 3   6 6 6 6 6     1 445 1 1       1   1 1731     1 440 439     1 3 3     1 2 2     1 4     1 31     1    
(function(sc) {
  "use strict";
 
  require("./lang");
  require("./dollar");
  require("./fn");
 
  var $  = sc.lang.$;
  var fn = sc.lang.fn;
  var current = null;
 
  function insideOfARoutine() {
    return sc.lang.main.getCurrentThread().__tag === sc.TAG_ROUTINE;
  }
 
  function Bytecode(initializer, def) {
    this._initializer = initializer;
    this._def  = def;
    this._code = [];
    this._vals = [];
    this._$owner = null;
    this._init(initializer);
  }
 
  Bytecode.prototype._init = function() {
    var code = this._initializer();
    if (this._def && code[0]) {
      code[0] = fn(code[0], this._def);
      this._argNames = code[0]._argNames;
      this._argVals  = code[0]._argVals;
    } else {
      this._argNames = [];
      this._argVals  = [];
    }
    if (code.length > 1) {
      this._freeFunc = code.pop();
    }
    this._code   = code;
    this._length = code.length;
    return this.reset();
  };
 
  Bytecode.prototype.reset = function() {
    this.state   = sc.STATE_INIT;
    this.result  = null;
    this._index  = 0;
    this._iter   = null;
    this._parent = null;
    this._child  = null;
    return this;
  };
 
  Bytecode.prototype.free = function() {
    if (this._freeFunc) {
      this._freeFunc();
    }
    return this;
  };
 
  Bytecode.prototype.setOwner = function($owner) {
    this._$owner = $owner;
    return this;
  };
 
  Bytecode.prototype.setIterator = function(iter) {
    this._iter = iter;
    return this;
  };
 
  Bytecode.prototype.setParent = function(parent) {
    if (parent && parent !== this) {
      this._parent  = parent;
      parent._child = this;
    }
  };
 
  Bytecode.prototype.run = function(args) {
    if (insideOfARoutine()) {
      return this.runAsRoutine(args);
    } else if (this._iter) {
      return this.runAsFunctionWithIter();
    } else {
      return this.runAsFunction(args);
    }
  };
 
  Bytecode.prototype.runAsFunction = function(args) {
    var result;
    var i, code, length;
 
    code   = this._code;
    length = this._length;
 
    this._parent = current;
 
    current = this;
    this.state = sc.STATE_RUNNING;
 
    for (i = 0; i < length; ++i) {
      result = this.update(code[i].apply(this, args));
      if (this.state === sc.STATE_BREAK) {
        this._iter = null;
        break;
      }
    }
    if (this._freeFunc) {
      this._freeFunc();
    }
 
    current = this._parent;
    this._parent = null;
 
    this.state = sc.STATE_INIT;
 
    return result || $.Nil();
  };
 
  Bytecode.prototype.runAsFunctionWithIter = function() {
    var items;
 
    while (this._iter && (items = this._iter.next()) !== null) {
      this.runAsFunction(items);
    }
 
    this._iter = null;
  };
 
  Bytecode.prototype.runAsRoutine = function(args) {
    var result;
 
    this.setParent(current);
 
    current = this;
 
    if (this._child) {
      result = this._child.runAsRoutine(args);
      if (this.state === sc.STATE_RUNNING) {
        result = null;
      }
    }
 
    if (!result) {
      result = this._runAsRoutine(args);
    }
 
    current = this._parent;
 
    this.advance();
 
    return this.result ? $.Nil() : result;
  };
 
  Bytecode.prototype._runAsRoutine = function(args) {
    var result;
    var code, length, iter;
 
    code   = this._code;
    length = this._length;
    iter   = this._iter;
 
    this.state  = sc.STATE_RUNNING;
    this.result = null;
    while (this._index < length) {
      if (iter && this._index === 0) {
        args = iter.next();
        if (args === null) {
          this.state  = sc.STATE_SUSPENDED;
          this._index = length;
          break;
        }
      }
      if (iter && !iter.hasNext) {
        iter = null;
      }
 
      result = this.update(code[this._index].apply(this, args));
 
      this._index += 1;
      if (this._index >= length) {
        if (iter) {
          this._index = 0;
        } else {
          this.state = sc.STATE_SUSPENDED;
        }
      }
 
      if (this.state !== sc.STATE_RUNNING) {
        break;
      }
    }
 
    return result;
  };
 
  Bytecode.prototype.advance = function() {
    if (this.state === sc.STATE_INIT) {
      this.free();
      return;
    }
    if (this._child || this._index < this._length) {
      this.state = sc.STATE_SUSPENDED;
      return;
    }
    if (!this.result) {
      this.state = sc.STATE_DONE;
      this.free();
    }
    if (this._parent) {
      if (this.state === sc.STATE_DONE) {
        this._parent.state = sc.STATE_RUNNING;
      } else {
        this._parent.state = sc.STATE_SUSPENDED;
        this.free();
      }
      this._parent.purge();
    }
  };
 
  Bytecode.prototype.purge = function() {
    if (this._child) {
      this._child._parent = null;
      this._child = null;
    }
    return this;
  };
 
  Bytecode.prototype.push = function() {
    this._vals.push(null);
  };
 
  Bytecode.prototype.shift = function() {
    if (this._vals.length) {
      return this._vals.shift();
    }
    return this._parent.shift();
  };
 
  Bytecode.prototype.update = function($value) {
    if (this._vals.length) {
      this._vals[this._vals.length - 1] = $value;
    } else if (this._parent) {
      this._parent.update($value);
    }
    return $value;
  };
 
  Bytecode.prototype.break = function() {
    this.state  = sc.STATE_BREAK;
    this._index = this._length;
    return this;
  };
 
  Bytecode.prototype.yield = function($value) {
    this.state  = sc.STATE_SUSPENDED;
    this.result = $value;
    if (this._parent && this._$owner.__tag === sc.TAG_FUNC) {
      this._parent.yield($value);
    }
    return this;
  };
 
  Bytecode.prototype.yieldAndReset = function($value) {
    this.state   = sc.STATE_INIT;
    this.result  = $value;
    if (this._parent && this._$owner.__tag === sc.TAG_FUNC) {
      this._parent.yieldAndReset($value);
    }
    this._index  = 0;
    this._iter   = null;
    this._parent = null;
    this._child  = null;
    return this;
  };
 
  Bytecode.prototype.alwaysYield = function($value) {
    this.state   = sc.STATE_DONE;
    this.result  = $value;
    if (this._parent && this._$owner.__tag === sc.TAG_FUNC) {
      this._parent.alwaysYield($value);
    }
    this._index  = this._length;
    this._iter   = null;
    this._parent = null;
    this._child  = null;
    return this.free();
  };
 
  var throwIfOutsideOfRoutine = function() {
    if (!insideOfARoutine()) {
      current = null;
      throw new Error("yield was called outside of a Routine.");
    }
  };
 
  var bytecode = {};
 
  bytecode.create = function(initializer, def) {
    return new Bytecode(initializer, def);
  };
 
  bytecode.yield = function($value) {
    throwIfOutsideOfRoutine();
    return current.yield($value).purge();
  };
 
  bytecode.alwaysYield = function($value) {
    throwIfOutsideOfRoutine();
    return current.alwaysYield($value);
  };
 
  bytecode.yieldAndReset = function($value) {
    throwIfOutsideOfRoutine();
    return current.yieldAndReset($value);
  };
 
  bytecode.setCurrent = function(bytecode) {
    current = bytecode;
  };
 
  bytecode.getCurrent = function() {
    return current;
  };
 
  sc.lang.bytecode = bytecode;
})(sc);