Skip to content

Commit

Permalink
Merge pull request #55 from jamisonderek/js-path
Browse files Browse the repository at this point in the history
JS: Add __filepath and __dirpath globals.
  • Loading branch information
HaxSam authored Mar 27, 2024
2 parents 6553f40 + ea9f151 commit ab72ead
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
9 changes: 9 additions & 0 deletions applications/system/js_app/examples/apps/Scripts/path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let storage = require("storage");

print("script has __dirpath of" + __dirpath);
print("script has __filepath of" + __filepath);
if (storage.exists(__dirpath + "/math.js")) {
print("math.js exist here.");
} else {
print("math.js does not exist here.");
}
19 changes: 19 additions & 0 deletions applications/system/js_app/js_thread.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <common/cs_dbg.h>
#include <toolbox/path.h>
#include <toolbox/stream/file_stream.h>
#include <loader/firmware_api/firmware_api.h>
#include <flipper_application/api_hashtable/api_hashtable.h>
Expand Down Expand Up @@ -319,6 +320,24 @@ static int32_t js_thread(void* arg) {
struct mjs* mjs = mjs_create(worker);
worker->modules = js_modules_create(mjs, worker->resolver);
mjs_val_t global = mjs_get_global(mjs);
if(worker->path) {
FuriString* dirpath = furi_string_alloc();
path_extract_dirname(furi_string_get_cstr(worker->path), dirpath);
mjs_set(
mjs,
global,
"__filepath",
~0,
mjs_mk_string(
mjs, furi_string_get_cstr(worker->path), furi_string_size(worker->path), true));
mjs_set(
mjs,
global,
"__dirpath",
~0,
mjs_mk_string(mjs, furi_string_get_cstr(dirpath), furi_string_size(dirpath), true));
furi_string_free(dirpath);
}
mjs_set(mjs, global, "print", ~0, MJS_MK_FN(js_print));
mjs_set(mjs, global, "delay", ~0, MJS_MK_FN(js_delay));
mjs_set(mjs, global, "to_string", ~0, MJS_MK_FN(js_global_to_string));
Expand Down

0 comments on commit ab72ead

Please sign in to comment.