-
-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
29 additions
and
0 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
applications/system/js_app/examples/apps/Scripts/interactive.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |