UNPKG

microvium

Version:

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

40 lines 1.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const lib_1 = require("../lib"); const chai_1 = require("chai"); suite('hello-world', function () { test('create', () => { const logs = []; const vm = lib_1.Microvium.create(); vm.globalThis.print = (s) => logs.push(s); vm.evaluateModule({ sourceText: 'print("Hello, World!");' }); chai_1.assert.deepEqual(logs, ['Hello, World!']); }); test('restore', () => { // These IDs are shared knowledge between the two epochs const PRINT = 1; const SAY_HELLO = 42; const logs = []; const print = (s) => void logs.push(s); const importMap = { [PRINT]: print }; const vm1 = lib_1.Microvium.create(importMap); vm1.globalThis.print = vm1.vmImport(PRINT); vm1.globalThis.vmExport = vm1.vmExport; vm1.evaluateModule({ sourceText: ` vmExport(${SAY_HELLO}, sayHello); function sayHello() { print('Hello, World!'); } ` }); const snapshot = vm1.createSnapshot(); // Restore the VM with access to the same set of imports by ID const vm2 = lib_1.Microvium.restore(snapshot, importMap); const sayHello = vm2.resolveExport(SAY_HELLO); chai_1.assert.deepEqual(logs, []); sayHello(); chai_1.assert.deepEqual(logs, ['Hello, World!']); }); }); //# sourceMappingURL=hello-world.test.js.map