-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
135 lines (116 loc) · 4.09 KB
/
main.js
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
const { readFileSync } = require("fs");
const execSync = require('child_process').execSync;
const fs = require("fs");
const path = require("path");
/// Recibo como parametro el nombre del archivo a compilar (.wat)
/// Ejemplo: node main.js typecheck.wat
/// El script compila el archivo .wat con wat2wasm y luego ejecuta el modulo
/// Lee como entrada el fichero situado en la carpeta inputs/$name.txt de la misma carpeta que el .wat
/// E imprime el resultado en carpeta outputs/$name.txt
/// Si el archivo no existe, se crea
if (process.argv.length < 3) {
console.log("Usage: node main.js <wat file>");
process.exit();
}
let watFilePath = process.argv[2];
/// input esta en el mismo directorio que el .wat
/// output esta en el mismo directorio que el .wat
const watFile = path.join(__dirname, watFilePath);
const name = path.basename(watFile, ".wat");
const folder = path.dirname(watFile);
const wasmFile = path.join(folder, `${name}.wasm`);
const inputFile = path.join(folder, "input", `${name}.txt`);
const outputFile = path.join(folder, "output", `${name}.txt`);
const expectedOutputFile = path.join(folder, "expected_output", `${name}.txt`);
let entrada = [];
let i = 0;
const disableConsolePrint = process.argv.includes("--disable-console-print");
const importObjects = {
runtime: {
exceptionHandler: function (code) {
let errStr;
if (code == 1) {
errStr = "Error 1. ";
} else if (code == 2) {
errStr = "Error 2. ";
} else if (code == 3) {
errStr = "Not enough memory. ";
} else {
errStr = "Unknown error\n";
}
throw new Error(errStr);
},
print: function (n) {
if (!disableConsolePrint) {
console.log(n);
}
if (outputFile) {
fs.appendFileSync(outputFile, n + "\n");
}
},
scan: function () {
if (i >= entrada.length) {
throw new Error("No hay mas entradas");
}
let val = entrada[i];
i += 1;
return val;
}
}
};
async function start(wasmFile) {
const code = readFileSync(wasmFile);
wasmModule = await WebAssembly.compile(code);
instance = await WebAssembly.instantiate(wasmModule, importObjects);
/// await instance.exports.init();
}
execSync(`./wat2wasm "${watFilePath}" -o "${wasmFile}"`, async (error, stdout, stderr) => {
if (error) {
console.error(`error wat2wasm: ${error.message}`);
process.exit(1);
}
if (stderr) {
console.error(`stderr wat2wasm: ${stderr}`);
process.exit(1);
}
});
const main = async () => {
/// Leer los ficheros de entrada y salida
if (fs.existsSync(inputFile)) {
/// Cargar en memoria el fichero de entrada
/// Partirlo por lineas
const input = fs.readFileSync(inputFile, "utf8");
entrada = input.split("\n");
}
if (!fs.existsSync(outputFile)) {
/// Crear el fichero de salida vacio para que no de error
fs.mkdirSync(path.dirname(outputFile), { recursive: true });
}
/// Vaciar el fichero de salida
fs.writeFileSync(outputFile, "");
try {
await start(watFilePath.replace(".wat", ".wasm"));
}
catch (e) {
console.log(e);
process.exit(1);
}
if (fs.existsSync(expectedOutputFile)) {
/// Comparar la salida con el fichero de salida esperado
const expectedOutput = fs.readFileSync(expectedOutputFile, "utf8").trim();
const output = fs.readFileSync(outputFile, "utf8").trim();
if (output == expectedOutput) {
console.log(`✅ [${name}] Coincide la salida con la esperada`);
} else {
console.log(`❌ [${name}] No coincide la salida con la esperada`);
/// Show diff
console.log("➡ Salida esperada");
console.log(expectedOutput);
console.log("➡ Salida obtenida");
console.log(output);
process.exit(1);
}
}
process.exit(0);
};
main();