From fbeaa9cb2c5b7a8e0d9114a62c270c6fe8024c31 Mon Sep 17 00:00:00 2001 From: sigoden Date: Wed, 23 Oct 2024 17:22:11 +0800 Subject: feat: improve execute js/py code and collect results (#117) --- tools/execute_js_code.js | 6 +++++- tools/execute_py_code.py | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) (limited to 'tools') diff --git a/tools/execute_js_code.js b/tools/execute_js_code.js index 853ccab..6bad67a 100644 --- a/tools/execute_js_code.js +++ b/tools/execute_js_code.js @@ -1,3 +1,5 @@ +const vm = require('vm'); + /** * Execute the javascript code in node.js. * @typedef {Object} Args @@ -5,5 +7,7 @@ * @param {Args} args */ exports.run = function run({ code }) { - return eval(code); + const context = vm.createContext({}); + const script = new vm.Script(code); + return script.runInContext(context); } diff --git a/tools/execute_py_code.py b/tools/execute_py_code.py index a774e82..3c6beb4 100644 --- a/tools/execute_py_code.py +++ b/tools/execute_py_code.py @@ -3,4 +3,4 @@ def run(code: str): Args: code: Python code to execute, such as `print("hello world")` """ - return exec(code) + return eval(code) \ No newline at end of file -- cgit v1.2.3