Skip to content

Commit

Permalink
chore: execute js/py code
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden committed Oct 26, 2024
1 parent e1a26cb commit 602ca05
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
6 changes: 3 additions & 3 deletions tools/execute_js_code.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
6 changes: 3 additions & 3 deletions tools/execute_py_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 602ca05

Please sign in to comment.