chore: execute js/py code
This commit is contained in:
@@ -5,15 +5,15 @@
|
||||
* @param {Args} args
|
||||
*/
|
||||
exports.run = function run({ code }) {
|
||||
let log = "";
|
||||
let output = "";
|
||||
const oldStdoutWrite = process.stdout.write.bind(process.stdout);
|
||||
process.stdout.write = (chunk, _encoding, callback) => {
|
||||
log += chunk;
|
||||
output += chunk;
|
||||
if (callback) callback();
|
||||
};
|
||||
|
||||
eval(code);
|
||||
|
||||
process.stdout.write = oldStdoutWrite;
|
||||
return log;
|
||||
return output;
|
||||
}
|
||||
|
||||
@@ -7,10 +7,10 @@ def run(code: str):
|
||||
code: Python code to execute, such as `print("hello world")`
|
||||
"""
|
||||
old_stdout = sys.stdout
|
||||
new_stdout = io.StringIO()
|
||||
sys.stdout = new_stdout
|
||||
output = io.StringIO()
|
||||
sys.stdout = output
|
||||
|
||||
exec(code)
|
||||
|
||||
sys.stdout = old_stdout
|
||||
return new_stdout.getvalue()
|
||||
return output.getvalue()
|
||||
Reference in New Issue
Block a user