/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */ 'use strict'; function Runtime(vm) { this.vm = vm; this.status = 1; // NEW this.waiting = []; this.threadCount = 0; this.initialized = {}; this.pending = {}; this.staticFields = {}; this.classObjects = {}; } Runtime.prototype.waitStatus = function(callback) { this.waiting.push(callback); throw VM.Pause; } Runtime.prototype.updateStatus = function(status) { this.status = status; var waiting = this.waiting; this.waiting = []; waiting.forEach(function(callback) { try { callback(); } catch(ex if ex instanceof VM.Pause) { // If the callback calls Runtime.prototype.waitStatus to continue waiting, // then calling it will throw VM.Pause, which shouldn't propagate up to // the caller of Runtime.prototype.updateStatus. } }); } Runtime.prototype.addContext = function(ctx) { ++this.threadCount; } Runtime.prototype.removeContext = function(ctx) { if (!--this.threadCount) this.updateStatus(4); // STOPPED } Runtime.prototype.newPrimitiveArray = function(type, size) { var constructor = ARRAYS[type]; if (!constructor.prototype.class) CLASSES.initPrimitiveArrayType(type, constructor); return new constructor(size); } Runtime.prototype.newArray = function(typeName, size) { return new (CLASSES.getClass(typeName).constructor)(size); } Runtime.prototype.newMultiArray = function(typeName, lengths) { var length = lengths[0]; var array = this.newArray(typeName, length); if (lengths.length > 1) { lengths = lengths.slice(1); for (var i=0; i