-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcode_exec.js
53 lines (46 loc) · 1.77 KB
/
code_exec.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
/**********************************************************************
*
* code_exec.js: recebe um código em BIRL, uma entrada padrão e um valor
* de resposta, cria o arquivo .c equivalente ao código BIRL, compila e
* o executa.
*
***********************************************************************/
var crypto = require('crypto');
// Cria um valor hexadecimal com tamanho len
function randomValueHex (len) {
return crypto.randomBytes(Math.ceil(len/2))
.toString('hex')
.slice(0,len);
};
module.exports = function (bCode, stdin, res) {
const fs = require('fs');
const comp = require('./compiler.js');
const code = require('./birlToC.js')(bCode);
var rName = randomValueHex(15).toString();
// Escrevendo a stdin
fs.writeFile(rName + ".txt", stdin, function (error) {
// Se ocorrer erro, retorna a resposta
if (error) {
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({ error: "ERRO INTERNO PAI!\n",
stdout: null,
}));
}
// Se não, escreve o código em um .c com nome aleatorio
//e chama compiler
fs.writeFile(rName + ".c", code, function (err) {
// se ocorrer erro, retorna JSON
if (err) {
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({ error: "ERRO INTERNO PAI!\n",
stdout: null,
}));
return;
}
// caso contrário, compila e executa
process.nextTick(function () {
comp(rName, res);
});
});
});
};