From 6b21783d763c9e4e8760484951cf79ff61bbf32f Mon Sep 17 00:00:00 2001 From: Willy-JL <49810075+Willy-JL@users.noreply.github.com> Date: Sun, 17 Mar 2024 06:56:18 +0000 Subject: [PATCH] JS: Add REPL interactive console --- .../examples/apps/Scripts/interactive.js | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 applications/system/js_app/examples/apps/Scripts/interactive.js diff --git a/applications/system/js_app/examples/apps/Scripts/interactive.js b/applications/system/js_app/examples/apps/Scripts/interactive.js new file mode 100644 index 0000000000..98473bd89e --- /dev/null +++ b/applications/system/js_app/examples/apps/Scripts/interactive.js @@ -0,0 +1,29 @@ +let dialog = require("dialog"); +let keyboard = require("keyboard"); +let storage = require("storage"); + +// Need to run code from file, and filename must be unique +let tmp_template = "/ext/apps_data/js_app/.interactive.tmp."; +let tmp_number = 0; + +let result = "Run JavaScript Code"; +while (dialog.message("Interactive Console", result)) { + let input = keyboard.text(256); + if (!input) break; + + let path = tmp_template + to_string(tmp_number++); + storage.write(path, "({run:function(){return " + input + ";},})"); + result = load(path).run(); + storage.remove(path); + + // Must convert to string explicitly + if (typeof result === "number") { + result = to_string(result); + } else if (typeof result === "undefined") { + result = "undefined"; + } else if (typeof result === "boolean") { + result = result ? "true" : "false"; + } else if (typeof result === "object") { + result = JSON.stringify(result); + } +}