Skip to content

Commit

Permalink
JS: Add REPL interactive console
Browse files Browse the repository at this point in the history
  • Loading branch information
Willy-JL committed Mar 17, 2024
1 parent 9d83750 commit 6b21783
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions applications/system/js_app/examples/apps/Scripts/interactive.js
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 6b21783

Please sign in to comment.