aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--README.md8
-rw-r--r--tools/execute_js_code.js16
-rw-r--r--tools/execute_py_code.py12
3 files changed, 26 insertions, 10 deletions
diff --git a/README.md b/README.md
index fff5aa7..df3f0f2 100644
--- a/README.md
+++ b/README.md
@@ -130,8 +130,8 @@ Create a new javascript in the [./tools/](./tools/) directory (.e.g. `execute_js
* @property {string} code - Javascript code to execute, such as `console.log("hello world")`
* @param {Args} args
*/
-exports.main = function main({ code }) {
- return eval(code);
+exports.run = function ({ code }) {
+ eval(code);
}
```
@@ -141,12 +141,12 @@ exports.main = function main({ code }) {
Create a new python script in the [./tools/](./tools/) directory (e.g. `execute_py_code.py`).
```py
-def main(code: str):
+def run(code: str):
"""Execute the python code.
Args:
code: Python code to execute, such as `print("hello world")`
"""
- return exec(code)
+ exec(code)
```
diff --git a/tools/execute_js_code.js b/tools/execute_js_code.js
index 6bad67a..3ca0401 100644
--- a/tools/execute_js_code.js
+++ b/tools/execute_js_code.js
@@ -1,5 +1,3 @@
-const vm = require('vm');
-
/**
* Execute the javascript code in node.js.
* @typedef {Object} Args
@@ -7,7 +5,15 @@ const vm = require('vm');
* @param {Args} args
*/
exports.run = function run({ code }) {
- const context = vm.createContext({});
- const script = new vm.Script(code);
- return script.runInContext(context);
+ let log = "";
+ const oldStdoutWrite = process.stdout.write.bind(process.stdout);
+ process.stdout.write = (chunk, _encoding, callback) => {
+ log += chunk;
+ if (callback) callback();
+ };
+
+ eval(code);
+
+ process.stdout.write = oldStdoutWrite;
+ return log;
}
diff --git a/tools/execute_py_code.py b/tools/execute_py_code.py
index 3c6beb4..ad9310c 100644
--- a/tools/execute_py_code.py
+++ b/tools/execute_py_code.py
@@ -1,6 +1,16 @@
+import io
+import sys
+
def run(code: str):
"""Execute the python code.
Args:
code: Python code to execute, such as `print("hello world")`
"""
- return eval(code) \ No newline at end of file
+ old_stdout = sys.stdout
+ new_stdout = io.StringIO()
+ sys.stdout = new_stdout
+
+ exec(code)
+
+ sys.stdout = old_stdout
+ return new_stdout.getvalue() \ No newline at end of file