UNPKG

microvium

Version:

A compact, embeddable scripting engine for microcontrollers for executing small scripts written in a subset of JavaScript.

90 lines 3.94 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.noContainer = exports.Format = exports.VisualBuffer = exports.BinaryData = exports.Byte = void 0; const smart_buffer_1 = require("smart-buffer"); const _ = __importStar(require("lodash")); const utils_1 = require("./utils"); const runtime_types_1 = require("./runtime-types"); const Byte = (b) => ((0, utils_1.hardAssert)((0, runtime_types_1.isUInt8)(b)), b); exports.Byte = Byte; const BinaryData = (bytes) => Object.freeze(bytes.map(exports.Byte)); exports.BinaryData = BinaryData; class VisualBuffer { constructor(htmlTemplate = exports.noContainer) { this.htmlTemplate = htmlTemplate; this.segments = new Map(); this.totalSize = 0; } get writeOffset() { return this.totalSize; } append(value, format) { const offset = this.totalSize; const binaryData = (0, exports.BinaryData)(format.binaryFormat(value)); this.totalSize += binaryData.length; this.segments.set(offset, { value, binaryData, htmlFormat: format.htmlFormat }); } overwrite(value, format, offset) { const segment = this.segments.get(offset); if (segment === undefined) { return (0, utils_1.invalidOperation)('Can only overwrite a region that exactly matches of the offset of a previous `append`'); } const binaryData = (0, exports.BinaryData)(format.binaryFormat(value)); if (binaryData.length !== segment.binaryData.length) { return (0, utils_1.invalidOperation)('An `overwrite` must have exactly the same size as the original `append`'); } this.segments.set(offset, { value, binaryData, htmlFormat: format.htmlFormat }); } toBuffer() { const buffer = new smart_buffer_1.SmartBuffer(); const segments = _.sortBy([...this.segments.entries()], s => s[0]); for (const [offset, segment] of segments) { segment.binaryData.forEach(b => buffer.writeUInt8(b)); } return buffer.toBuffer(); } toHTML() { const offsets = _.sortBy([...this.segments.keys()], o => o); const content = offsets .map(offset => renderHtmlSegment((0, utils_1.notUndefined)(this.segments.get(offset)), offset)) .join('\n'); return this.htmlTemplate(content, this.totalSize); function renderHtmlSegment({ value, htmlFormat, binaryData }, offset) { return htmlFormat(value, binaryData, offset); } } } exports.VisualBuffer = VisualBuffer; const Format = (binaryFormat, htmlFormat) => ({ binaryFormat, htmlFormat, }); exports.Format = Format; const noContainer = content => content; exports.noContainer = noContainer; //# sourceMappingURL=visual-buffer.js.map