Browse Source

Fix order inputs are pulled off the runtimeInput array

Inputs are supposed to be pulled off in FIFO order, but were LIFO
instead.
ApisNecros 1 year ago
parent
commit
4e6995b63b
1 changed files with 8 additions and 6 deletions
  1. 8 6
      IntComp/Computer.js

+ 8 - 6
IntComp/Computer.js

@@ -68,15 +68,15 @@ module.exports = class Computer {
         this.outputComputer = null;
 
         /**
-         * An input value provided at runtime
+         * An array of input values provided at runtime
          *
          * This value will be passed to the first INPUT opcode made by the computer,
          * and will then be cleared. If the program being ran tries to make another
          * call to INPUT, and `options.inputFromConsole` is false, then an error
          * will be thrown.
-         * @type {number}
+         * @type {number[]}
          */
-        this.runtimeInput = null;
+        this.runtimeInput = [];
 
         /**
          * An array of outputs from the computer
@@ -115,7 +115,7 @@ module.exports = class Computer {
      * Runs opcodes on the stack until either the a HALT command is
      * encountered, or an error is thrown. Stores the input to be used
      * by the first INPUT opcode encountered.
-     * @param {number} input The input value to initialize the comptuer with.
+     * @param {number[]} input An array of input values to initialize the comptuer with
      * @returns {void}
      */
     async RunWithInput(input) {
@@ -280,8 +280,10 @@ module.exports = class Computer {
         // Get the input based on the input mode
         switch (inputMode) {
             case InputModes.INPUT_FROM_RUNTIME_STACK: {
-                userInput = this.runtimeInput;
-                this.runtimeInput = null;
+                userInput = this.runtimeInput.shift();
+                if (userInput === undefined) {
+                    throw new Error("runtime Input array empty");
+                }
                 break;
             }
             case InputModes.INPUT_FROM_CONSOLE: {