diff --git a/applications/system/js_app/examples/apps/Scripts/path.js b/applications/system/js_app/examples/apps/Scripts/path.js new file mode 100644 index 000000000..0381150d2 --- /dev/null +++ b/applications/system/js_app/examples/apps/Scripts/path.js @@ -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."); +} \ No newline at end of file diff --git a/applications/system/js_app/js_thread.c b/applications/system/js_app/js_thread.c index ceaad5e36..a06ca0c37 100644 --- a/applications/system/js_app/js_thread.c +++ b/applications/system/js_app/js_thread.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -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));