2021-01-17 02:12:13 +01:00
|
|
|
const { writeFileSync } = require('fs');
|
2021-01-16 13:14:17 +01:00
|
|
|
const { spawn } = require('child_process');
|
|
|
|
|
2021-01-17 02:12:13 +01:00
|
|
|
function execute(language, source, stdin = '', args = []) {
|
|
|
|
return new Promise(resolve => {
|
2021-01-16 13:14:17 +01:00
|
|
|
const stamp = new Date().getTime();
|
|
|
|
const sourceFile = `/tmp/${stamp}.code`;
|
|
|
|
|
2021-01-17 02:12:13 +01:00
|
|
|
writeFileSync(sourceFile, source);
|
2021-01-16 13:14:17 +01:00
|
|
|
|
|
|
|
const process = spawn(__dirname + '/../lxc/execute', [
|
|
|
|
language.name,
|
|
|
|
sourceFile,
|
2021-01-17 02:12:13 +01:00
|
|
|
stdin,
|
|
|
|
args.join('\n'),
|
2021-01-16 13:14:17 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
let stdout = '';
|
|
|
|
let stderr = '';
|
|
|
|
let output = '';
|
|
|
|
|
|
|
|
process.stderr.on('data', chunk => {
|
|
|
|
stderr += chunk;
|
|
|
|
output += chunk;
|
|
|
|
});
|
|
|
|
|
|
|
|
process.stdout.on('data', chunk => {
|
|
|
|
stdout += chunk;
|
|
|
|
output += chunk;
|
|
|
|
});
|
|
|
|
|
2021-01-16 14:19:29 +01:00
|
|
|
process.on('exit', code => {
|
2021-01-16 13:14:17 +01:00
|
|
|
stderr = stderr.trim().substring(0, 65535);
|
|
|
|
stdout = stdout.trim().substring(0, 65535);
|
|
|
|
output = output.trim().substring(0, 65535);
|
|
|
|
|
2021-01-16 14:19:29 +01:00
|
|
|
resolve({
|
|
|
|
stdout,
|
|
|
|
stderr,
|
|
|
|
output,
|
|
|
|
ran: code === 0,
|
|
|
|
});
|
2021-01-16 13:14:17 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
execute,
|
|
|
|
};
|