Feature Walkthrough

This page describes some of the key Microvium features. This assumes that you have already gone through the Getting Started guide and are familiar with the basic concepts of snapshot creation and how to use the runtime engine.

Language Features

Statements and Expressions

If statementif (x) {}
if (x) {} else {}
Loopswhile (x) {}
do {} while (x);
for (let i = 0; i < x; i++) {}
Variabe declarations
(let and const)
let x;
let x = y;
const x = y;
Functionsfunction foo(x, y) { return x + y; }
Modules
(import and export)
import { x } from './another-file';
export const x = 5;
export function foo() { }
Literalsnull
true
false
42
42.5
NaN
Infinity
'Hello, World!'
"Hello, World!"
Object literallet obj = { a: 1, b: 2 };
let obj = { 'a': 1, 'b': 2 };
let obj = { ['a']: 1, ['b']: 2 };
let obj = { a, b };
Arrayslet arr = [1, 2];
Array and object accessarr.push(3); (append element)
let x = arr[0];
let x = arr.length;
let x = obj['a'];
let x = obj.a;
Bitwise operatorsx << y
x >> y
x >>> y
x | y
x & y
x ^ y
~x
Boolean operatorsx && y
x || y
!x
Numeric operatorsx + y
x - y
x * y
x / y (floating point division)
x / y | 0 (integer division)
x % y
x ** y (power)
x < y
x > y
x <= y
x >= y
x++
x--
++x
--x
String operatorsx + y (concatenation)
`Hi ${name}` (template literal)

Built-in Functions

isNaN(x)

Array.push(x) (append to the end of an array)

console.log(x) (compile-time only)

Node.js API at Compile Time

Microvium provides some level of access to the Node.js API at compile-time, as illustrated by the following example.

import fs from 'fs';

const print = vmImport(1);
const text = fs.readFileSync('my-file.txt', 'utf8');

function sayHello() {
  print(text);
}

vmExport(1, sayHello);

This capability is only available at compile-time, and should be seen as a form of inter-process communication (IPC), since the node modules reside in the host and not the VM.

This feature comes with some limitations. In particular, functions, objects, and arrays coming from a Node.js module remain part of the host and are thus lost when the snapshot is taken (their form in the virtual machine is just a proxy of the actual object or array in the host). You cannot, for example, open a file stream at compile-time and read the stream at runtime.