UNPKG

@polygon-hermez/vm

Version:
75 lines 2.64 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var assert_1 = __importDefault(require("assert")); var ceil = function (value, ceiling) { var r = value % ceiling; if (r === 0) { return value; } else { return value + ceiling - r; } }; /** * Memory implements a simple memory model * for the ethereum virtual machine. */ var Memory = /** @class */ (function () { function Memory() { this._store = Buffer.alloc(0); } /** * Extends the memory given an offset and size. Rounds extended * memory to word-size. */ Memory.prototype.extend = function (offset, size) { if (size === 0) { return; } var newSize = ceil(offset + size, 32); var sizeDiff = newSize - this._store.length; if (sizeDiff > 0) { this._store = Buffer.concat([this._store, Buffer.alloc(sizeDiff)]); } }; /** * Writes a byte array with length `size` to memory, starting from `offset`. * @param offset - Starting position * @param size - How many bytes to write * @param value - Value */ Memory.prototype.write = function (offset, size, value) { if (size === 0) { return; } (0, assert_1.default)(value.length === size, 'Invalid value size'); (0, assert_1.default)(offset + size <= this._store.length, 'Value exceeds memory capacity'); (0, assert_1.default)(Buffer.isBuffer(value), 'Invalid value type'); for (var i = 0; i < size; i++) { this._store[offset + i] = value[i]; } }; /** * Reads a slice of memory from `offset` till `offset + size` as a `Buffer`. * It fills up the difference between memory's length and `offset + size` with zeros. * @param offset - Starting position * @param size - How many bytes to read */ Memory.prototype.read = function (offset, size) { var returnBuffer = Buffer.allocUnsafe(size); // Copy the stored "buffer" from memory into the return Buffer var loaded = Buffer.from(this._store.slice(offset, offset + size)); returnBuffer.fill(loaded, 0, loaded.length); if (loaded.length < size) { // fill the remaining part of the Buffer with zeros returnBuffer.fill(0, loaded.length, size); } return returnBuffer; }; return Memory; }()); exports.default = Memory; //# sourceMappingURL=memory.js.map