-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JS: Backport³ and more additions & fixes #3961
Open
Willy-JL
wants to merge
34
commits into
flipperdevices:dev
Choose a base branch
from
Willy-JL:js-backport3-and-more
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+996
−70
Open
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
223336f
JS: Fix file select for fbt launch js_app
Willy-JL 68d31dd
JS: badusb: Add numpad keys
Willy-JL 60e96e6
JS: badusb: Layout support
Willy-JL 4cae1e9
JS: badusb: altPrint() and altPrintln()
Willy-JL efc2494
JS: badusb: quit()
Willy-JL e8079f8
JS: serial: readAny()
Willy-JL efca524
JS: serial: end()
Willy-JL 5476395
JS: serial: Auto disable expansion service
Willy-JL 229c77b
JS: storage: Add example script
Willy-JL d50b062
JS: gui: text_input: Fix NULL ptr when no prop given
Willy-JL 4bbbd29
JS: gui: text_input: Default text props
Willy-JL f559c2f
JS: gui: byte_input
Willy-JL 78a0544
JS: gui: file_picker
Willy-JL d05c20b
JS: gui: viewDispatcher.currentView
Willy-JL 1967ec0
JS: gui: view.hasProperty()
Willy-JL ab49604
JS: gui: Add some missing typedefs comments
Willy-JL 0e75674
JS: globals: Fix toString() with negative numbers
Willy-JL 56e45c8
JS: globals: parseInt()
Willy-JL 1e9fb43
JS: globals: toUpperCase() and toLowerCase()
Willy-JL 76f3a2e
JS: globals: Add some missing typedefs
Willy-JL 0aa3528
JS: Add example for string functions
Willy-JL 00fd305
JS: globals: __dirpath and __filepath
Willy-JL fe9f428
JS: globals: load() typedef missing scope param
Willy-JL a694e80
JS: Add interactive REPL script example
Willy-JL 6e599c3
Merge branch 'dev' into js-backport3-and-more
hedger d2a3c16
JS: Add missing icon for file picker
Willy-JL d1808e7
JS: Rename to __filename and __dirname
Willy-JL 998fb06
JS: Move toUpperCase() and toLowerCase() to string class
Willy-JL c95cfcb
JS: parseInt() refactor
Willy-JL 26643d3
JS: Typedef base param for parseInt()
Willy-JL 5c46835
Revert "JS: gui: view.hasProperty()"
Willy-JL f0a6b6f
JS: Move toString() to Number class
Willy-JL 32f8346
JS: Fix duplicate plugin files
Willy-JL f8852e4
Merge branch 'dev' into js-backport3-and-more
skotopes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
94 changes: 94 additions & 0 deletions
94
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,94 @@ | ||
let eventLoop = require("event_loop"); | ||
let gui = require("gui"); | ||
let dialog = require("gui/dialog"); | ||
let textInput = require("gui/text_input"); | ||
let loading = require("gui/loading"); | ||
let storage = require("storage"); | ||
|
||
// No eval() or exec() so need to run code from file, and filename must be unique | ||
storage.makeDirectory("/ext/.tmp"); | ||
storage.makeDirectory("/ext/.tmp/js"); | ||
storage.rmrf("/ext/.tmp/js/repl") | ||
storage.makeDirectory("/ext/.tmp/js/repl") | ||
let ctx = { | ||
tmpTemplate: "/ext/.tmp/js/repl/", | ||
tmpNumber: 0, | ||
persistentScope: {}, | ||
}; | ||
|
||
let views = { | ||
dialog: dialog.makeWith({ | ||
header: "Interactive Console", | ||
text: "Press OK to Start", | ||
center: "Run Some JS" | ||
}), | ||
textInput: textInput.makeWith({ | ||
header: "Type JavaScript Code:", | ||
defaultText: "2+2", | ||
defaultTextClear: true, | ||
// Props for makeWith() are passed in reverse order, so maxLength must be after defaultText | ||
minLength: 0, | ||
maxLength: 256, | ||
}), | ||
loading: loading.make(), | ||
}; | ||
|
||
eventLoop.subscribe(views.dialog.input, function (_sub, button, gui, views) { | ||
if (button === "center") { | ||
gui.viewDispatcher.switchTo(views.textInput); | ||
} | ||
}, gui, views); | ||
|
||
eventLoop.subscribe(views.textInput.input, function (_sub, text, gui, views, ctx) { | ||
gui.viewDispatcher.switchTo(views.loading); | ||
|
||
let path = ctx.tmpTemplate + (ctx.tmpNumber++).toString(); | ||
let file = storage.openFile(path, "w", "create_always"); | ||
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, ctx.persistentScope); // Load runs JS and returns last value on stack | ||
storage.remove(path); | ||
|
||
// Must convert to string explicitly | ||
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 = result.toString(); | ||
} 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 = "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); | ||
views.textInput.set("defaultText", text); | ||
}, gui, views, ctx); | ||
|
||
eventLoop.subscribe(gui.viewDispatcher.navigation, function (_sub, _, eventLoop) { | ||
eventLoop.stop(); | ||
}, 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 :)") |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
let math = load("/ext/apps/Scripts/load_api.js"); | ||
let math = load(__dirname + "/load_api.js"); | ||
let result = math.add(5, 10); | ||
print(result); |
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,9 @@ | ||
let storage = require("storage"); | ||
|
||
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."); | ||
} |
29 changes: 29 additions & 0 deletions
29
applications/system/js_app/examples/apps/Scripts/storage.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 storage = require("storage"); | ||
let path = "/ext/storage.test"; | ||
|
||
print("File exists:", storage.fileExists(path)); | ||
|
||
print("Writing..."); | ||
let file = storage.openFile(path, "w", "create_always"); | ||
file.write("Hello "); | ||
file.close(); | ||
|
||
print("File exists:", storage.fileExists(path)); | ||
|
||
file = storage.openFile(path, "w", "open_append"); | ||
file.write("World!"); | ||
file.close(); | ||
|
||
print("Reading..."); | ||
file = storage.openFile(path, "r", "open_existing"); | ||
let text = file.read("ascii", 128); | ||
file.close(); | ||
print(text); | ||
|
||
print("Removing...") | ||
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 the API | ||
// There's also many more functions and options, check type definitions in firmware repo |
19 changes: 19 additions & 0 deletions
19
applications/system/js_app/examples/apps/Scripts/stringutils.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,19 @@ | ||
let sampleText = "Hello, World!"; | ||
|
||
let lengthOfText = "Length of text: " + sampleText.length.toString(); | ||
print(lengthOfText); | ||
|
||
let start = 7; | ||
let end = 12; | ||
let substringResult = sampleText.slice(start, end); | ||
print(substringResult); | ||
|
||
let searchStr = "World"; | ||
let result2 = sampleText.indexOf(searchStr).toString(); | ||
print(result2); | ||
|
||
let upperCaseText = "Text in upper case: " + sampleText.toUpperCase(); | ||
print(upperCaseText); | ||
|
||
let lowerCaseText = "Text in lower case: " + sampleText.toLowerCase(); | ||
print(lowerCaseText); |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm curious whether it's at all possible to integrate the file picker with the rest of the GUI system as an ordinary view and make its API asynchronous. Gonna look into it.