/* * TinyBlocks - tb_api.c * Implementacao do gestor publico de modulos/libraries (tinyblocks-api). */ #define _POSIX_C_SOURCE 200809L #include #include #include #include #ifdef __linux__ # include #endif #include #include "tb_api.h" typedef const TinyBlocks_LibraryDescriptor* (*GetLibFn)(void); static TinyBlocks* s_rt = NULL; static TbApiModule s_mods[TB_API_MAX_MODULES]; static int s_n = 0; static char s_err[256]; /* -- Helpers --- */ static time_t file_mtime(const char* p) { struct stat st; return (stat(p, &st) == 0) ? st.st_mtime : 0; } static void set_err(const char* fmt, const char* a) { snprintf(s_err, sizeof(s_err), fmt, a ? a : ""); } static int find_by_lib(const TinyBlocks_LibraryBlock* lib) { for (int i = 0; i < s_n; i++) if (s_mods[i].lib == lib) return i; return -1; } static int find_by_name(const char* name) { if (!name) return -1; for (int i = 0; i < s_n; i++) if (strcmp(s_mods[i].name, name) == 0) return i; return -1; } /* -- Ciclo de vida --- */ bool tb_api_init(TinyBlocks* rt) { s_rt = rt; s_n = 0; s_err[0] = 0; return rt != NULL; } void tb_api_shutdown(void) { /* O runtime e o dono dos blocos; aqui so esquecemos os modulos. */ s_n = 0; s_rt = NULL; } TinyBlocks* tb_api_runtime(void) { return s_rt; } const char* tb_api_last_error(void) { return s_err; } /* -- Registro de modulos --- */ int tb_api_adopt_all(void) { if (!s_rt) return 0; for (uint32_t i = 0; i < tb_rt_library_count(s_rt); i++) { TinyBlocks_LibraryBlock* lib = tb_rt_library_at(s_rt, i); if (!lib || find_by_lib(lib) >= 0) continue; if (s_n >= TB_API_MAX_MODULES) break; TbApiModule* m = &s_mods[s_n]; memset(m, 0, sizeof(*m)); const char* nm = tb_library_name(lib); snprintf(m->name, sizeof(m->name), "%s", nm ? nm : "?"); m->lib = lib; m->category = tb_library_category(lib); m->block_count = tb_library_block_count(lib); s_n++; } return s_n; } int tb_api_module_count(void) { return s_n; } const TbApiModule* tb_api_module(int i) { return (i >= 0 && i < s_n) ? &s_mods[i] : NULL; } const TbApiModule* tb_api_module_find(const char* name) { int i = find_by_name(name); return i >= 0 ? &s_mods[i] : NULL; } bool tb_api_is_loaded(const char* name) { return find_by_name(name) >= 0; } /* -- Carregamento dinamico --- */ int tb_api_load(const char* path) { if (!s_rt) { set_err("%s", "tb_api: runtime nao inicializado"); return -1; } if (!path || !path[0]) { set_err("%s", "tb_api: caminho vazio"); return -1; } if (s_n >= TB_API_MAX_MODULES) { set_err("%s", "tb_api: limite de modulos atingido"); return -1; } #ifdef __linux__ void* dl = dlopen(path, RTLD_NOW | RTLD_LOCAL); if (!dl) { set_err("dlopen: %s", dlerror()); return -1; } GetLibFn get_lib = (GetLibFn)(uintptr_t)dlsym(dl, TINYBLOCKS_LIBRARY_SYMBOL); if (!get_lib) { const char* base = strrchr(path, '/'); base = base ? base + 1 : path; if (strncmp(base, "libtinyblocks-", 14) == 0) set_err("'%s' e dependencia = carregue a library (ex: python.so)", base); else set_err("'%s' nao exporta tinyblocks_get_library", base); dlclose(dl); return -1; } const TinyBlocks_LibraryDescriptor* desc = get_lib(); TinyBlocks_LibraryBlock* lib = tb_library_register(s_rt, desc); if (!lib) { set_err("tb_library_register falhou para %s", path); dlclose(dl); return -1; } TbApiModule* m = &s_mods[s_n]; memset(m, 0, sizeof(*m)); snprintf(m->name, sizeof(m->name), "%s", desc->name ? desc->name : "?"); snprintf(m->version, sizeof(m->version), "%s", desc->version ? desc->version : ""); snprintf(m->path, sizeof(m->path), "%s", path); m->handle = dl; m->lib = lib; m->category = tb_library_category(lib); m->block_count = tb_library_block_count(lib); m->mtime = file_mtime(path); s_err[0] = 0; return s_n++; #else (void)path; set_err("%s", "tb_api: dlopen nao suportado nesta plataforma"); return -1; #endif } bool tb_api_unload(int idx) { if (idx < 0 || idx >= s_n) return false; TbApiModule* m = &s_mods[idx]; if (m->lib) tb_library_unload(s_rt, m->lib); #ifdef __linux__ if (m->handle) dlclose(m->handle); #endif for (int i = idx; i < s_n - 1; i++) s_mods[i] = s_mods[i + 1]; s_n--; return true; } bool tb_api_reload(int idx) { if (idx < 0 || idx >= s_n) return false; if (!s_mods[idx].path[0]) return false; /* embutida nao recarrega */ char path[TB_API_PATH_LEN]; snprintf(path, sizeof(path), "%s", s_mods[idx].path); tb_api_unload(idx); return tb_api_load(path) >= 0; } bool tb_api_any_changed(void) { for (int i = 0; i < s_n; i++) { TbApiModule* m = &s_mods[i]; if (!m->path[0]) continue; time_t mt = file_mtime(m->path); if (mt && mt != m->mtime) return true; } return false; } int tb_api_poll_reload(void) { int n = 0; for (int i = 0; i < s_n; i++) { TbApiModule* m = &s_mods[i]; if (!m->path[0]) continue; time_t mt = file_mtime(m->path); if (!mt || mt == m->mtime) continue; char path[TB_API_PATH_LEN]; snprintf(path, sizeof(path), "%s", m->path); tb_api_unload(i); if (tb_api_load(path) >= 0) n++; i = -1; /* a lista mudou; reinicia a varredura */ } return n; } /* -- Introspeccao de tipos --- */ int tb_api_type_count(void) { return (int)tb_rt_type_count(s_rt); } const TinyBlocks_TypeBlock* tb_api_type(int i) { if (i < 0) return NULL; return tb_rt_type_at(s_rt, (uint32_t)i); } const TinyBlocks_TypeBlock* tb_api_type_find(const char* name) { return s_rt ? tb_rt_find_type(s_rt, name) : NULL; }