File size: 1,025 Bytes
30c32c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const fs = require('fs');
const VirtualMachine = require('../index');

/* eslint-env node */
/* eslint-disable no-console */

const file = process.argv[2];
if (!file) {
    throw new Error('Invalid file');
}

const runProject = async buffer => {
    const vm = new VirtualMachine();
    vm.runtime.on('SAY', (target, type, text) => {
        console.log(text);
    });
    vm.setCompatibilityMode(true);
    vm.clear();
    await vm.loadProject(buffer);
    vm.start();
    vm.greenFlag();
    await new Promise(resolve => {
        const interval = setInterval(() => {
            let active = 0;
            const threads = vm.runtime.threads;
            for (let i = 0; i < threads.length; i++) {
                if (!threads[i].updateMonitor) {
                    active += 1;
                }
            }
            if (active === 0) {
                clearInterval(interval);
                resolve();
            }
        }, 50);
    });
    vm.stopAll();
    vm.stop();
};

runProject(fs.readFileSync(file));