tinyblocks-api (gestor publico de modulos/libraries): - registry de modulos (embutidos + .so), load/unload/reload via dlopen, hot reload por mtime e introspeccao de tipos de bloco. - substitui a logica de hotlib duplicada no editor. tinyblocks-libman (gestor/buscador de libraries): - varre library.toml (libraries/ e ~/.tinyblocks/libraries), le metadados de forma RECURSIVA e resolve o caminho do .so/.a. - busca por texto e filtro por categoria; consulta o estado de carga na api. Editor: tela cheia "Escolha uma Extensão" (UI desenhada pelo editor, como no Scratch): botao Bibliotecas / Ctrl+L, busca, chips de categoria que quebram linha, grade de cartoes com faixa colorida e selo "carregada"; clicar carrega o .so via api. Respeito a interfaces publicas entre libraries: - libman deixa de incluir o ast.h (interno) do tinyblocks-toml e passa a usar uma API publica e opaca: tb_toml_open/close/str/int/int_array. - core ganha accessors publicos: tb_rt_type_count/at, tb_rt_library_count/at, tb_library_name/category/block_count; api e editor usam-nos em vez de ler campos de struct do core diretamente. Testes: make test-libman (novo) + step no harness que abre o libman, busca, carrega um .so e confirma a categoria na paleta. test-editor/toml/blueprint/ plugin seguem OK.
212 lines
6.7 KiB
C
212 lines
6.7 KiB
C
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
#include "tinyblocks/tinyblocks.h"
|
|
|
|
#ifdef __linux__
|
|
# include <dlfcn.h>
|
|
#endif
|
|
|
|
/* == Vtable de LibraryBlock === */
|
|
static void lib_eval(TinyBlocks_Block* self, TinyBlocks* rt)
|
|
{
|
|
/* Libraries não avaliam diretamente; servem como namespaces */
|
|
(void)self; (void)rt;
|
|
self->dirty = false;
|
|
}
|
|
|
|
static void lib_destroy(TinyBlocks_Block* self, TinyBlocks* rt)
|
|
{
|
|
TinyBlocks_LibraryBlock* lib = (TinyBlocks_LibraryBlock*)self;
|
|
tb_rt_unregister_library(rt, lib);
|
|
/* Destrói blocos contidos */
|
|
for (uint32_t i = 0; i < lib->block_count; ++i)
|
|
tb_block_destroy(lib->blocks[i], rt);
|
|
tb_free(rt, lib->blocks);
|
|
}
|
|
|
|
static size_t lib_to_string(TinyBlocks_Block* self, TinyBlocks* rt,
|
|
char* buf, size_t sz)
|
|
{
|
|
(void)rt;
|
|
return (size_t)snprintf(buf, sz, "[Library:%s (%u blocks)]",
|
|
self->name ? self->name : "?",
|
|
((TinyBlocks_LibraryBlock*)self)->block_count);
|
|
}
|
|
|
|
static TinyBlocks_Block* lib_clone(const TinyBlocks_Block* self, TinyBlocks* rt)
|
|
{
|
|
/* Clonar uma library cria uma nova library vazia com o mesmo nome */
|
|
return (TinyBlocks_Block*)tb_library_block_create(rt, self->name);
|
|
}
|
|
|
|
static const TinyBlocks_BlockVTable s_lib_vtable = {
|
|
.eval = lib_eval,
|
|
.destroy = lib_destroy,
|
|
.to_string = lib_to_string,
|
|
.clone = lib_clone,
|
|
};
|
|
|
|
/* == Criação === */
|
|
TinyBlocks_LibraryBlock* tb_library_block_create(TinyBlocks* rt, const char* name)
|
|
{
|
|
TinyBlocks_LibraryBlock* lib = (TinyBlocks_LibraryBlock*)tb_block_alloc_base(
|
|
rt, TB_KIND_LIBRARY, sizeof(TinyBlocks_LibraryBlock),
|
|
0, 0, &s_lib_vtable);
|
|
if (!lib) return NULL;
|
|
|
|
lib->base.name = tb_strdup(rt, name);
|
|
lib->block_capacity = 16;
|
|
lib->category = NULL;
|
|
lib->blocks = tb_alloc(rt, lib->block_capacity * sizeof(TinyBlocks_Block*));
|
|
if (!lib->blocks) {
|
|
tb_block_destroy(&lib->base, rt);
|
|
return NULL;
|
|
}
|
|
|
|
tb_rt_register_library(rt, lib);
|
|
return lib;
|
|
}
|
|
|
|
void tb_library_block_destroy(TinyBlocks_LibraryBlock* lib, TinyBlocks* rt)
|
|
{
|
|
tb_block_destroy(&lib->base, rt);
|
|
}
|
|
|
|
/* == Gerenciamento de blocos contidos === */
|
|
bool tb_library_add(TinyBlocks_LibraryBlock* lib, TinyBlocks_Block* block)
|
|
{
|
|
if (!lib || !block) return false;
|
|
if (lib->block_count >= lib->block_capacity) {
|
|
uint32_t new_cap = lib->block_capacity * 2;
|
|
/* A library pertence a um runtime - mas não temos rt aqui.
|
|
Solução: guardar rt no lib_userdata NÃO é a API pública.
|
|
Usamos realloc direto pois este array não usa o alocador customizado
|
|
(trade-off documentado: arrays de library usam realloc). */
|
|
TinyBlocks_Block** arr = realloc(lib->blocks,
|
|
new_cap * sizeof(TinyBlocks_Block*));
|
|
if (!arr) return false;
|
|
lib->blocks = arr;
|
|
lib->block_capacity = new_cap;
|
|
}
|
|
lib->blocks[lib->block_count++] = block;
|
|
block->parent = &lib->base;
|
|
return true;
|
|
}
|
|
|
|
/* == Categoria === */
|
|
void tb_library_set_category(TinyBlocks_LibraryBlock* lib,
|
|
const TinyBlocks_Category* category)
|
|
{
|
|
if (lib) lib->category = category;
|
|
}
|
|
|
|
bool tb_library_remove_at(TinyBlocks_LibraryBlock* lib, uint32_t index)
|
|
{
|
|
if (!lib || index >= lib->block_count) return false;
|
|
lib->blocks[index]->parent = NULL;
|
|
lib->blocks[index] = lib->blocks[--lib->block_count];
|
|
return true;
|
|
}
|
|
|
|
TinyBlocks_Block* tb_library_find(TinyBlocks_LibraryBlock* lib, const char* name)
|
|
{
|
|
if (!lib || !name) return NULL;
|
|
for (uint32_t i = 0; i < lib->block_count; ++i) {
|
|
TinyBlocks_Block* b = lib->blocks[i];
|
|
/* Compara pelo nome do bloco ou pelo type_name se for TypeBlock */
|
|
if (b->name && strcmp(b->name, name) == 0) return b;
|
|
if (b->kind == TB_KIND_TYPE) {
|
|
TinyBlocks_TypeBlock* tb = (TinyBlocks_TypeBlock*)b;
|
|
if (tb->type_name && strcmp(tb->type_name, name) == 0) return b;
|
|
}
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
/* == Acesso publico aos metadados === */
|
|
const char* tb_library_name(const TinyBlocks_LibraryBlock* lib) {
|
|
return lib ? lib->base.name : NULL;
|
|
}
|
|
|
|
const TinyBlocks_Category* tb_library_category(const TinyBlocks_LibraryBlock* lib) {
|
|
return lib ? lib->category : NULL;
|
|
}
|
|
|
|
uint32_t tb_library_block_count(const TinyBlocks_LibraryBlock* lib) {
|
|
return lib ? lib->block_count : 0;
|
|
}
|
|
|
|
/* == Carregamento dinâmico === */
|
|
TinyBlocks_LibraryBlock* tb_library_load(TinyBlocks* rt, const char* path)
|
|
{
|
|
#ifdef __linux__
|
|
void* handle = dlopen(path, RTLD_NOW | RTLD_LOCAL);
|
|
if (!handle) {
|
|
tinyblocks_set_error(rt, "tb_library_load: dlopen failed: %s", dlerror());
|
|
return NULL;
|
|
}
|
|
|
|
typedef const TinyBlocks_LibraryDescriptor* (*GetLibFn)(void);
|
|
GetLibFn get_lib = (GetLibFn)(uintptr_t)dlsym(handle, TINYBLOCKS_LIBRARY_SYMBOL);
|
|
if (!get_lib) {
|
|
tinyblocks_set_error(rt, "tb_library_load: symbol '%s' not found in %s",
|
|
TINYBLOCKS_LIBRARY_SYMBOL, path);
|
|
dlclose(handle);
|
|
return NULL;
|
|
}
|
|
|
|
const TinyBlocks_LibraryDescriptor* desc = get_lib();
|
|
TinyBlocks_LibraryBlock* lib = tb_library_register(rt, desc);
|
|
if (!lib) { dlclose(handle); return NULL; }
|
|
|
|
/* Guarda handle para dlclose no deinit */
|
|
if (rt->dl_handle_count >= rt->dl_handle_capacity) {
|
|
uint32_t nc = rt->dl_handle_capacity * 2;
|
|
void** arr = realloc(rt->dl_handles, nc * sizeof(void*));
|
|
if (!arr) { tb_library_unload(rt, lib); dlclose(handle); return NULL; }
|
|
rt->dl_handles = arr;
|
|
rt->dl_handle_capacity = nc;
|
|
}
|
|
rt->dl_handles[rt->dl_handle_count++] = handle;
|
|
return lib;
|
|
#else
|
|
(void)path;
|
|
tinyblocks_set_error(rt, "tb_library_load: not supported on this platform");
|
|
return NULL;
|
|
#endif
|
|
}
|
|
|
|
void tb_library_unload(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib)
|
|
{
|
|
if (!lib) return;
|
|
if (lib->on_unload) lib->on_unload(lib, rt);
|
|
tb_library_block_destroy(lib, rt);
|
|
}
|
|
|
|
TinyBlocks_LibraryBlock* tb_library_register(TinyBlocks* rt,
|
|
const TinyBlocks_LibraryDescriptor* desc)
|
|
{
|
|
if (!desc) {
|
|
tinyblocks_set_error(rt, "tb_library_register: descriptor is NULL");
|
|
return NULL;
|
|
}
|
|
|
|
TinyBlocks_LibraryBlock* lib = tb_library_block_create(rt, desc->name);
|
|
if (!lib) return NULL;
|
|
|
|
if (desc->init && !desc->init(rt, lib)) {
|
|
tinyblocks_set_error(rt, "tb_library_register: init failed for '%s'",
|
|
desc->name ? desc->name : "?");
|
|
tb_library_block_destroy(lib, rt);
|
|
return NULL;
|
|
}
|
|
|
|
if (lib->on_load && !lib->on_load(lib, rt)) {
|
|
tb_library_block_destroy(lib, rt);
|
|
return NULL;
|
|
}
|
|
|
|
return lib;
|
|
}
|