Browse Source

Add SetPointerAddress to the Stack class

ApisNecros 1 year ago
parent
commit
71a418a03d
1 changed files with 20 additions and 0 deletions
  1. 20 0
      IntComp/Stack.js

+ 20 - 0
IntComp/Stack.js

@@ -111,6 +111,26 @@ module.exports = class Stack {
         this._stack[index] = value;
     }
 
+    /**
+     * Sets the pointer to a new address in memory
+     *
+     * If the address is out of the upper bounds of the current memory stack,
+     * the stack will be expanded, filling with 0's as needed.
+     *
+     * @param {number} newAddress The new pointer address
+     * @returns {void}
+     */
+    SetPointerAddress(newAddress) {
+        if (newAddress > this._stack.length) {
+            // Expand the stack if needed
+            const oldLength = this._stack.length;
+            this._stack[newAddress] = 0;
+            this._stack.fill(0, oldLength, newAddress);
+        }
+
+        this.pointer = newAddress;
+    }
+
     /**
      * Return the whole stack
      *