mirror of
https://github.com/flipperdevices/flipperzero-firmware.git
synced 2025-12-12 12:51:22 +04:00
[FL-3893] JS modules (#3841)
* feat: backport js_gpio from unleashed * feat: backport js_keyboard, TextInputModel::minimum_length from unleashed * fix: api version inconsistency * style: js_gpio * build: fix submodule ._ . * refactor: js_gpio * docs: type declarations for gpio * feat: gpio interrupts * fix: js_gpio freeing, resetting and minor stylistic changes * style: js_gpio * style: mlib array, fixme's * feat: js_gpio adc * feat: js_event_loop * docs: js_event_loop * feat: js_event_loop subscription cancellation * feat: js_event_loop + js_gpio integration * fix: js_event_loop memory leak * feat: stop event loop on back button * test: js: basic, math, event_loop * feat: js_event_loop queue * feat: js linkage to previously loaded plugins * build: fix ci errors * feat: js module ordered teardown * feat: js_gui_defer_free * feat: basic hourglass view * style: JS ASS (Argument Schema for Scripts) * fix: js_event_loop mem leaks and lifetime problems * fix: crashing test and pvs false positives * feat: mjs custom obj destructors, gui submenu view * refactor: yank js_gui_defer_free (yuck) * refactor: maybe_unsubscribe * empty_screen, docs, typing fix-ups * docs: navigation event & demo * feat: submenu setHeader * feat: text_input * feat: text_box * docs: text_box availability * ci: silence irrelevant pvs low priority warning * style: use furistring * style: _get_at -> _safe_get * fix: built-in module name assignment * feat: js_dialog; refactor, optimize: js_gui * docs: js_gui * ci: silence pvs warning: Memory allocation is infallible * style: fix storage spelling * feat: foreign pointer signature checks * feat: js_storage * docs: js_storage * fix: my unit test was breaking other tests ;_; * ci: fix ci? * Make doxygen happy * docs: flipper, math, notification, global * style: review suggestions * style: review fixups * fix: badusb demo script * docs: badusb * ci: add nofl * ci: make linter happy * Bump api version Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
#include <core/common_defines.h>
|
||||
#include "js_modules.h"
|
||||
#include <m-dict.h>
|
||||
#include <m-array.h>
|
||||
|
||||
#include "modules/js_flipper.h"
|
||||
#ifdef FW_CFG_unit_tests
|
||||
#include "modules/js_tests.h"
|
||||
#endif
|
||||
|
||||
#define TAG "JS modules"
|
||||
|
||||
@@ -9,54 +13,72 @@
|
||||
#define MODULES_PATH "/ext/apps_data/js_app/plugins"
|
||||
|
||||
typedef struct {
|
||||
JsModeConstructor create;
|
||||
JsModeDestructor destroy;
|
||||
FuriString* name;
|
||||
const JsModuleConstructor create;
|
||||
const JsModuleDestructor destroy;
|
||||
void* context;
|
||||
} JsModuleData;
|
||||
|
||||
DICT_DEF2(JsModuleDict, FuriString*, FURI_STRING_OPLIST, JsModuleData, M_POD_OPLIST);
|
||||
// not using:
|
||||
// - a dict because ordering is required
|
||||
// - a bptree because it forces a sorted ordering
|
||||
// - an rbtree because i deemed it more tedious to implement, and with the
|
||||
// amount of modules in use (under 10 in the overwhelming majority of cases)
|
||||
// i bet it's going to be slower than a plain array
|
||||
ARRAY_DEF(JsModuleArray, JsModuleData, M_POD_OPLIST);
|
||||
#define M_OPL_JsModuleArray_t() ARRAY_OPLIST(JsModuleArray)
|
||||
|
||||
static const JsModuleDescriptor modules_builtin[] = {
|
||||
{"flipper", js_flipper_create, NULL},
|
||||
{"flipper", js_flipper_create, NULL, NULL},
|
||||
#ifdef FW_CFG_unit_tests
|
||||
{"tests", js_tests_create, NULL, NULL},
|
||||
#endif
|
||||
};
|
||||
|
||||
struct JsModules {
|
||||
struct mjs* mjs;
|
||||
JsModuleDict_t module_dict;
|
||||
JsModuleArray_t modules;
|
||||
PluginManager* plugin_manager;
|
||||
CompositeApiResolver* resolver;
|
||||
};
|
||||
|
||||
JsModules* js_modules_create(struct mjs* mjs, CompositeApiResolver* resolver) {
|
||||
JsModules* modules = malloc(sizeof(JsModules));
|
||||
modules->mjs = mjs;
|
||||
JsModuleDict_init(modules->module_dict);
|
||||
JsModuleArray_init(modules->modules);
|
||||
|
||||
modules->plugin_manager = plugin_manager_alloc(
|
||||
PLUGIN_APP_ID, PLUGIN_API_VERSION, composite_api_resolver_get(resolver));
|
||||
|
||||
modules->resolver = resolver;
|
||||
|
||||
return modules;
|
||||
}
|
||||
|
||||
void js_modules_destroy(JsModules* modules) {
|
||||
JsModuleDict_it_t it;
|
||||
for(JsModuleDict_it(it, modules->module_dict); !JsModuleDict_end_p(it);
|
||||
JsModuleDict_next(it)) {
|
||||
const JsModuleDict_itref_t* module_itref = JsModuleDict_cref(it);
|
||||
if(module_itref->value.destroy) {
|
||||
module_itref->value.destroy(module_itref->value.context);
|
||||
void js_modules_destroy(JsModules* instance) {
|
||||
for
|
||||
M_EACH(module, instance->modules, JsModuleArray_t) {
|
||||
FURI_LOG_T(TAG, "Tearing down %s", furi_string_get_cstr(module->name));
|
||||
if(module->destroy) module->destroy(module->context);
|
||||
furi_string_free(module->name);
|
||||
}
|
||||
}
|
||||
plugin_manager_free(modules->plugin_manager);
|
||||
JsModuleDict_clear(modules->module_dict);
|
||||
free(modules);
|
||||
plugin_manager_free(instance->plugin_manager);
|
||||
JsModuleArray_clear(instance->modules);
|
||||
free(instance);
|
||||
}
|
||||
|
||||
JsModuleData* js_find_loaded_module(JsModules* instance, const char* name) {
|
||||
for
|
||||
M_EACH(module, instance->modules, JsModuleArray_t) {
|
||||
if(furi_string_cmp_str(module->name, name) == 0) return module;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
mjs_val_t js_module_require(JsModules* modules, const char* name, size_t name_len) {
|
||||
FuriString* module_name = furi_string_alloc_set_str(name);
|
||||
// Check if module is already installed
|
||||
JsModuleData* module_inst = JsModuleDict_get(modules->module_dict, module_name);
|
||||
JsModuleData* module_inst = js_find_loaded_module(modules, name);
|
||||
if(module_inst) { //-V547
|
||||
furi_string_free(module_name);
|
||||
mjs_prepend_errorf(
|
||||
modules->mjs, MJS_BAD_ARGS_ERROR, "\"%s\" module is already installed", name);
|
||||
return MJS_UNDEFINED;
|
||||
@@ -73,8 +95,11 @@ mjs_val_t js_module_require(JsModules* modules, const char* name, size_t name_le
|
||||
|
||||
if(strncmp(name, modules_builtin[i].name, name_compare_len) == 0) {
|
||||
JsModuleData module = {
|
||||
.create = modules_builtin[i].create, .destroy = modules_builtin[i].destroy};
|
||||
JsModuleDict_set_at(modules->module_dict, module_name, module);
|
||||
.create = modules_builtin[i].create,
|
||||
.destroy = modules_builtin[i].destroy,
|
||||
.name = furi_string_alloc_set_str(name),
|
||||
};
|
||||
JsModuleArray_push_at(modules->modules, 0, module);
|
||||
module_found = true;
|
||||
FURI_LOG_I(TAG, "Using built-in module %s", name);
|
||||
break;
|
||||
@@ -83,39 +108,57 @@ mjs_val_t js_module_require(JsModules* modules, const char* name, size_t name_le
|
||||
|
||||
// External module load
|
||||
if(!module_found) {
|
||||
FuriString* deslashed_name = furi_string_alloc_set_str(name);
|
||||
furi_string_replace_all_str(deslashed_name, "/", "__");
|
||||
FuriString* module_path = furi_string_alloc();
|
||||
furi_string_printf(module_path, "%s/js_%s.fal", MODULES_PATH, name);
|
||||
FURI_LOG_I(TAG, "Loading external module %s", furi_string_get_cstr(module_path));
|
||||
furi_string_printf(
|
||||
module_path, "%s/js_%s.fal", MODULES_PATH, furi_string_get_cstr(deslashed_name));
|
||||
FURI_LOG_I(
|
||||
TAG, "Loading external module %s from %s", name, furi_string_get_cstr(module_path));
|
||||
do {
|
||||
uint32_t plugin_cnt_last = plugin_manager_get_count(modules->plugin_manager);
|
||||
PluginManagerError load_error = plugin_manager_load_single(
|
||||
modules->plugin_manager, furi_string_get_cstr(module_path));
|
||||
if(load_error != PluginManagerErrorNone) {
|
||||
FURI_LOG_E(
|
||||
TAG,
|
||||
"Module %s load error. It may depend on other modules that are not yet loaded.",
|
||||
name);
|
||||
break;
|
||||
}
|
||||
const JsModuleDescriptor* plugin =
|
||||
plugin_manager_get_ep(modules->plugin_manager, plugin_cnt_last);
|
||||
furi_assert(plugin);
|
||||
|
||||
if(strncmp(name, plugin->name, name_len) != 0) {
|
||||
FURI_LOG_E(TAG, "Module name missmatch %s", plugin->name);
|
||||
if(furi_string_cmp_str(deslashed_name, plugin->name) != 0) {
|
||||
FURI_LOG_E(TAG, "Module name mismatch %s", plugin->name);
|
||||
break;
|
||||
}
|
||||
JsModuleData module = {.create = plugin->create, .destroy = plugin->destroy};
|
||||
JsModuleDict_set_at(modules->module_dict, module_name, module);
|
||||
JsModuleData module = {
|
||||
.create = plugin->create,
|
||||
.destroy = plugin->destroy,
|
||||
.name = furi_string_alloc_set_str(name),
|
||||
};
|
||||
JsModuleArray_push_at(modules->modules, 0, module);
|
||||
|
||||
if(plugin->api_interface) {
|
||||
FURI_LOG_I(TAG, "Added module API to composite resolver: %s", plugin->name);
|
||||
composite_api_resolver_add(modules->resolver, plugin->api_interface);
|
||||
}
|
||||
|
||||
module_found = true;
|
||||
} while(0);
|
||||
furi_string_free(module_path);
|
||||
furi_string_free(deslashed_name);
|
||||
}
|
||||
|
||||
// Run module constructor
|
||||
mjs_val_t module_object = MJS_UNDEFINED;
|
||||
if(module_found) {
|
||||
module_inst = JsModuleDict_get(modules->module_dict, module_name);
|
||||
module_inst = js_find_loaded_module(modules, name);
|
||||
furi_assert(module_inst);
|
||||
if(module_inst->create) { //-V779
|
||||
module_inst->context = module_inst->create(modules->mjs, &module_object);
|
||||
module_inst->context = module_inst->create(modules->mjs, &module_object, modules);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,7 +166,12 @@ mjs_val_t js_module_require(JsModules* modules, const char* name, size_t name_le
|
||||
mjs_prepend_errorf(modules->mjs, MJS_BAD_ARGS_ERROR, "\"%s\" module load fail", name);
|
||||
}
|
||||
|
||||
furi_string_free(module_name);
|
||||
|
||||
return module_object;
|
||||
}
|
||||
|
||||
void* js_module_get(JsModules* modules, const char* name) {
|
||||
FuriString* module_name = furi_string_alloc_set_str(name);
|
||||
JsModuleData* module_inst = js_find_loaded_module(modules, name);
|
||||
furi_string_free(module_name);
|
||||
return module_inst ? module_inst->context : NULL;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user