This commit is contained in:
MX 2024-10-18 20:22:21 +03:00
parent bc73b13b1b
commit fe3228b63e
No known key found for this signature in database
GPG Key ID: 7CCC66B7DBDD1C83
7 changed files with 49 additions and 20 deletions

View File

@ -59,7 +59,7 @@ eventLoop.subscribe(views.dialog.input, function (_sub, button, eventLoop, gui)
notify.error();
}
// Optional, but allows to interchange with usbdisk
// Optional, but allows to unlock usb interface to switch profile
badusb.quit();
eventLoop.stop();

View File

@ -8,6 +8,7 @@ let textInputView = require("gui/text_input");
let byteInputView = require("gui/byte_input");
let textBoxView = require("gui/text_box");
let dialogView = require("gui/dialog");
let filePicker = require("gui/file_picker");
let flipper = require("flipper");
// declare view instances
@ -40,6 +41,7 @@ let views = {
"Text input & Dialog",
"Byte input",
"Text box",
"File picker",
"Exit app",
],
}),
@ -63,12 +65,22 @@ eventLoop.subscribe(views.demos.chosen, function (_sub, index, gui, eventLoop, v
} else if (index === 4) {
gui.viewDispatcher.switchTo(views.longText);
} else if (index === 5) {
let path = filePicker.pickFile("/ext", "*");
if (path) {
views.helloDialog.set("text", "You selected:\n" + path);
} else {
views.helloDialog.set("text", "You didn't select a file");
}
views.helloDialog.set("center", "Nice!");
gui.viewDispatcher.switchTo(views.helloDialog);
} else if (index === 6) {
eventLoop.stop();
}
}, gui, eventLoop, views);
// say hi after keyboard input
eventLoop.subscribe(views.keyboard.input, function (_sub, name, gui, views) {
views.keyboard.set("defaultText", name); // Remember for next usage
views.helloDialog.set("text", "Hi " + name + "! :)");
views.helloDialog.set("center", "Hi Flipper! :)");
gui.viewDispatcher.switchTo(views.helloDialog);

View File

@ -11,8 +11,9 @@ storage.makeDirectory("/ext/.tmp/js");
storage.rmrf("/ext/.tmp/js/repl")
storage.makeDirectory("/ext/.tmp/js/repl")
let ctx = {
tmp_template: "/ext/.tmp/js/repl/",
tmp_number: 0,
tmpTemplate: "/ext/.tmp/js/repl/",
tmpNumber: 0,
persistentScope: {},
};
let views = {
@ -41,28 +42,40 @@ eventLoop.subscribe(views.dialog.input, function (_sub, button, gui, views) {
eventLoop.subscribe(views.textInput.input, function (_sub, text, gui, views, ctx) {
gui.viewDispatcher.switchTo(views.loading);
let path = ctx.tmp_template + toString(ctx.tmp_number++);
let path = ctx.tmpTemplate + toString(ctx.tmpNumber++);
let file = storage.openFile(path, "w", "create_always");
file.write("({run:function(){return " + text + ";},})");
file.write(text);
file.close();
// Hide GUI before running, we want to see console and avoid deadlock if code fails
gui.viewDispatcher.sendTo("back");
let result = load(path).run();
let result = load(path, ctx.persistentScope); // Load runs JS and returns last value on stack
storage.remove(path);
gui.viewDispatcher.sendTo("front");
// Must convert to string explicitly
if (typeof result === "number") {
if (result === null) { // mJS: typeof null === "null", ECMAScript: typeof null === "object", IDE complains when checking "null" type
result = "null";
} else if (typeof result === "string") {
result = "'" + result + "'";
} else if (typeof result === "number") {
result = toString(result);
} else if (typeof result === "undefined") {
result = "undefined";
} else if (typeof result === "bigint") { // mJS doesn't support BigInt() but might aswell check
result = "bigint";
} else if (typeof result === "boolean") {
result = result ? "true" : "false";
} else if (typeof result === "symbol") { // mJS doesn't support Symbol() but might aswell check
result = "symbol";
} else if (typeof result === "undefined") {
result = "undefined";
} else if (typeof result === "object") {
result = JSON.stringify(result);
result = "object"; // JSON.stringify() is not implemented
} else if (typeof result === "function") {
result = "function";
} else {
result = "unknown type: " + typeof result;
}
gui.viewDispatcher.sendTo("front");
views.dialog.set("header", "JS Returned:");
views.dialog.set("text", result);
gui.viewDispatcher.switchTo(views.dialog);
@ -74,4 +87,8 @@ eventLoop.subscribe(gui.viewDispatcher.navigation, function (_sub, _, eventLoop)
}, eventLoop);
gui.viewDispatcher.switchTo(views.dialog);
// Message behind GUI if something breaks
print("If you're stuck here, something went wrong, re-run the script")
eventLoop.run();
print("\n\nFinished correctly :)")

View File

@ -1,3 +1,3 @@
let math = load(__dirpath + "/load_api.js");
let math = load(__dirname + "/load_api.js");
let result = math.add(5, 10);
print(result);

View File

@ -1,8 +1,8 @@
let storage = require("storage");
print("script has __dirpath of" + __dirpath);
print("script has __filepath of" + __filepath);
if (storage.fileExists(__dirpath + "/math.js")) {
print("script has __dirname of" + __dirname);
print("script has __filename of" + __filename);
if (storage.fileExists(__dirname + "/math.js")) {
print("math.js exist here.");
} else {
print("math.js does not exist here.");

View File

@ -25,5 +25,5 @@ storage.remove(path);
print("Done")
// You don't need to close the file after each operation, this is just to show some different ways to use
// There's also many more functions and options, check types docs in firmware repo
// You don't need to close the file after each operation, this is just to show some different ways to use the API
// There's also many more functions and options, check type definitions in firmware repo

View File

@ -12,8 +12,8 @@ let searchStr = "World";
let result2 = toString(sampleText.indexOf(searchStr));
print(result2);
let upperCaseText = "Text in upper case: " + toUpperCase(sampleText);
let upperCaseText = "Text in upper case: " + sampleText.toUpperCase();
print(upperCaseText);
let lowerCaseText = "Text in lower case: " + toLowerCase(sampleText);
let lowerCaseText = "Text in lower case: " + sampleText.toLowerCase();
print(lowerCaseText);