microvium
Version:
A compact, embeddable scripting engine for microcontrollers for executing small scripts written in a subset of JavaScript.
132 lines • 6.94 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.preformatted3 = exports.preformatted2 = exports.preformatted1 = exports.preformatted = exports.paddingWithDeletedRow = exports.paddingRow = exports.bufferRow = exports.stringUtf8NTRow = exports.doubleLERow = exports.sInt32LERow = exports.uInt32LERow = exports.uHex32LERow = exports.sInt16LERow = exports.uInt16LERow = exports.uHex16LERow = exports.sInt8Row = exports.uInt8Row = exports.uHex8Row = exports.htmlFormats = exports.binaryFormats = exports.tableRow = exports.tableContainer = void 0;
const visual_buffer_1 = require("./visual-buffer");
const escape_html_1 = __importDefault(require("escape-html"));
const utils_1 = require("./utils");
const runtime_types_1 = require("./runtime-types");
const lodash_1 = __importDefault(require("lodash"));
const tableContainer = (content, totalSize) => `
<table class="visual-buffer">
<colgroup>
<col>
<col>
<col>
<col>
</colgroup>
<tbody>
${content}
${
// Final row to show the trailing address
(0, exports.tableRow)(() => '')({ value: 0 }, [], totalSize)}
</tbody>
</table>`;
exports.tableContainer = tableContainer;
const tableRow = (formatValue) => (value, binary, offset) => {
const addressID = `address${offset.toString(16).padStart(4, '0').toUpperCase()}`;
return `
<tr>
<td class="address">
<a class="address-text" id="${addressID}" href="#${addressID}">
${offset.toString(16).padStart(4, '0').toUpperCase()}
</a>
</td>
<td class="data">
${value.value !== undefined
? binary
.map(b => b.toString(16).padStart(2, '0').toUpperCase())
.map(s => `<span class="byte">${s}</span>`)
.join('<wbr>')
: binary
.map(() => `<span class="byte pending"></span>`)
.join('<wbr>')}
</td>
<td class="label">
${value.label ? value.label + ': ' : ''}
</td>
${value.value !== undefined
? `<td class="value">${formatValue(value.value)}</td>`
: '<td class="value pending-value"></td>'}
</tr>`;
};
exports.tableRow = tableRow;
class HTMLFormats {
constructor() {
this.hexRow = (digits, add0x = true, addBaseSubscript = false) => (0, exports.tableRow)(value => (add0x ? '0x' : '') +
(value.toString(16).padStart(digits, '0').toUpperCase()) +
(addBaseSubscript ? '<sub>16</sub>' : ''));
this.intRow = (0, exports.tableRow)(s => s.toFixed(0));
this.doubleRow = (0, exports.tableRow)(s => s.toString());
this.stringRow = (0, exports.tableRow)(s => (0, escape_html_1.default)((0, utils_1.stringifyStringLiteral)(s)));
}
}
class BinaryFormats {
constructor() {
// 8-bit unsigned integer
this.uInt8 = v => [v];
// 8-bit signed integer
this.sInt8 = v => [(0, runtime_types_1.SInt8)(v) & 0xFF];
// Little-endian 16-bit unsigned integer
this.uInt16LE = v => ((0, runtime_types_1.UInt16)(v), [v & 0xFF, (v >> 8) & 0xFF]);
// Little-endian 16-bit signed integer
this.sInt16LE = v => ((0, runtime_types_1.SInt16)(v), [v & 0xFF, (v >> 8) & 0xFF]);
// Little-endian 32-bit unsigned integer
this.uInt32LE = v => ((0, runtime_types_1.UInt32)(v), [v & 0xFF, (v >> 8) & 0xFF, (v >> 16) & 0xFF, (v >> 24) & 0xFF]);
// Little-endian 32-bit signed integer
this.sInt32LE = v => ((0, runtime_types_1.SInt32)(v), [v & 0xFF, (v >> 8) & 0xFF, (v >> 16) & 0xFF, (v >> 24) & 0xFF]);
// Little-endian 64-bit floating point
this.doubleLE = v => { const b = Buffer.allocUnsafe(8); b.writeDoubleLE(v); return [...b]; };
// UTF8, null-terminated string
this.stringUtf8NT = v => [...Buffer.from(v, 'utf8'), 0];
}
}
const renderInt = (s) => s.toFixed(0);
const renderHex = (digits) => (value) => `0x${value.toString(16).padStart(digits, '0').toUpperCase()}`;
const renderDouble = (value) => value.toString();
const renderString = (value) => (0, escape_html_1.default)((0, utils_1.stringifyStringLiteral)(value));
const renderBuffer = (value) => '<Buffer>';
exports.binaryFormats = new BinaryFormats();
exports.htmlFormats = new HTMLFormats();
exports.uHex8Row = rowFormat(exports.binaryFormats.uInt8, 1, renderHex(2));
exports.uInt8Row = rowFormat(exports.binaryFormats.uInt8, 1, renderInt);
exports.sInt8Row = rowFormat(exports.binaryFormats.sInt8, 1, renderInt);
exports.uHex16LERow = rowFormat(exports.binaryFormats.uInt16LE, 2, renderHex(4));
exports.uInt16LERow = rowFormat(exports.binaryFormats.uInt16LE, 2, renderInt);
exports.sInt16LERow = rowFormat(exports.binaryFormats.sInt16LE, 2, renderInt);
exports.uHex32LERow = rowFormat(exports.binaryFormats.uInt32LE, 4, renderHex(8));
exports.uInt32LERow = rowFormat(exports.binaryFormats.uInt32LE, 4, renderInt);
exports.sInt32LERow = rowFormat(exports.binaryFormats.sInt32LE, 4, renderInt);
exports.doubleLERow = rowFormat(exports.binaryFormats.doubleLE, 8, renderDouble);
exports.stringUtf8NTRow = rowFormat(exports.binaryFormats.stringUtf8NT, 1, renderString);
exports.bufferRow = rowFormat(b => (0, visual_buffer_1.BinaryData)([...b]), 0, renderBuffer);
exports.paddingRow = (0, visual_buffer_1.Format)(count => lodash_1.default.range(0, count.value).map(_ => 0), (0, exports.tableRow)(_ => '<padding>'));
// Pad but use VM_VALUE_NULL. This is for padding in a space that the GC might
// be looking at
exports.paddingWithDeletedRow = (0, visual_buffer_1.Format)(count => {
const arr = lodash_1.default.range(0, count.value).map(_ => 0);
(0, utils_1.hardAssert)(arr.length === 2);
for (let i = 0; i < arr.length; i += 2) {
arr[i] = runtime_types_1.vm_TeWellKnownValues.VM_VALUE_DELETED & 0xFF;
arr[i + 1] = (runtime_types_1.vm_TeWellKnownValues.VM_VALUE_DELETED >> 8) & 0xFF;
}
return arr;
}, (0, exports.tableRow)(_ => '<pad with deleted>'));
const preformatted = (byteCount) => rowFormat(v => v.binary, byteCount, v => v.html);
exports.preformatted = preformatted;
exports.preformatted1 = (0, exports.preformatted)(1);
exports.preformatted2 = (0, exports.preformatted)(2);
exports.preformatted3 = (0, exports.preformatted)(3);
function rowFormat(renderBin, byteCount, renderValue) {
return (0, visual_buffer_1.Format)(v => v.value !== undefined ? renderBin(v.value) : zeros(byteCount), (0, exports.tableRow)(renderValue));
}
function zeros(length) {
const result = [];
while (length--) {
result.push(0);
}
return result;
}
//# sourceMappingURL=snapshot-binary-html-formats.js.map