Adiciona libraries toml/fs, metadados .toml e build por library

- tinyblocks-toml: lexer + parser TOML (include/lexeme.h, include/ast.h,
  src/lexer.c, src/parser.c) e bloco Toml.Get.
- tinyblocks-fs: categoria "Arquivos" com FS.Open/Read/Write/Append/Tell/Close.
- library.toml em cada library com metadados (categoria, cor, descricao) e
  receita de build, sem precisar embutir isso no .so.
- Makefiles proprios por library com build/ local; Makefile raiz delega.
- Palco (tb_stage) movido para o core; pen deixa de depender do editor, o
  que torna os .so arrastaveis.
- Editor linka com -rdynamic para resolver simbolos do core em .so via dlopen.
- Testes: test-toml (parser) e test-plugin (dlopen de .so standalone).
This commit is contained in:
mguinhos committed 2026-10-20 19:38:58 -03:00
1 parent 90d47bf8a0
commit dc23b92d15
48 files changed
+2405 -744

No files matched your search

+106
View File
@@ -0,0 +1,106 @@
/*
* Teste de plugin: prova que um library .so e standalone e cargavel via
* dlopen, resolvendo os simbolos do core exportados pelo binario (-rdynamic).
*
* Uso: plugin_test <caminho.so>
*/
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <dlfcn.h>
#include <unistd.h>
#include <tinyblocks/tinyblocks.h>
#include <tinyblocks/tb_block.h>
#include <tinyblocks/tb_type_block.h>
#include <tinyblocks/tb_library_block.h>
typedef const TinyBlocks_LibraryDescriptor* (*GetLibFn)(void);
static int fails = 0;
#define CHECK(cond, msg) do { if (cond) printf("ok %s\n", msg); else { printf("FAIL %s\n", msg); fails++; } } while (0)
static TinyBlocks_Block* mk(TinyBlocks* rt, const char* type) {
TinyBlocks_TypeBlock* t = tb_type_block_find(rt, type);
if (!t) { printf("FAIL tipo %s nao encontrado\n", type); fails++; return NULL; }
return tb_block_create(rt, t);
}
static void set_text(TinyBlocks_Block* b, uint32_t i, const char* s) {
TinyBlocks_Value v = tb_value_text(s);
tb_block_set_default(b, i, v);
tb_value_free(&v);
}
static void set_int(TinyBlocks_Block* b, uint32_t i, int64_t n) {
TinyBlocks_Value v = tb_value_integer(n);
tb_block_set_default(b, i, v);
tb_value_free(&v);
}
int main(int argc, char** argv) {
const char* so = argc > 1 ? argv[1] : "libraries/tinyblocks-fs/build/libtinyblocks-fs.so";
char path[256];
snprintf(path, sizeof(path), "/tmp/tb_plugin_%d.txt", (int)getpid());
unlink(path);
TinyBlocks* rt = NULL;
CHECK(tinyblocks_init(&rt, NULL), "runtime inicializa");
void* dl = dlopen(so, RTLD_NOW | RTLD_LOCAL);
if (!dl) { printf("FAIL dlopen %s: %s\n", so, dlerror()); return 1; }
printf("ok dlopen %s\n", so);
GetLibFn get = (GetLibFn)(uintptr_t)dlsym(dl, "tinyblocks_get_library");
CHECK(get != NULL, "simbolo tinyblocks_get_library presente");
if (!get) return 1;
const TinyBlocks_LibraryDescriptor* desc = get();
CHECK(desc && desc->name && strstr(desc->name, "fs") != NULL, "descriptor da library");
CHECK(tb_library_register(rt, desc) != NULL, "library registra no runtime");
/* Anexa duas linhas e le de volta. */
TinyBlocks_Block* app = mk(rt, "FS.Append");
if (app) {
set_text(app, 0, path);
set_text(app, 1, "linha-1\n");
tb_block_eval(app, rt);
set_text(app, 1, "linha-2\n");
tb_block_eval(app, rt);
}
TinyBlocks_Block* op = mk(rt, "FS.Open");
int64_t handle = 0;
if (op) {
set_text(op, 0, path);
tb_block_eval(op, rt);
handle = op->outputs[0].as.integer;
}
CHECK(handle >= 1, "FS.Open devolve handle");
TinyBlocks_Block* rd = mk(rt, "FS.Read");
if (rd) {
set_int(rd, 0, handle);
set_int(rd, 1, 0);
tb_block_eval(rd, rt);
const char* got = rd->outputs[0].as.text;
CHECK(got && strcmp(got, "linha-1\nlinha-2\n") == 0, "FS.Read le o conteudo");
if (got && strcmp(got, "linha-1\nlinha-2\n") != 0) printf(" obtido=[%s]\n", got);
}
TinyBlocks_Block* tl = mk(rt, "FS.Tell");
if (tl) {
set_int(tl, 0, handle);
tb_block_eval(tl, rt);
CHECK(tl->outputs[0].as.integer == 16, "FS.Tell reporta 16 bytes");
}
TinyBlocks_Block* cl = mk(rt, "FS.Close");
if (cl) { set_int(cl, 0, handle); tb_block_eval(cl, rt); }
unlink(path);
dlclose(dl);
tinyblocks_deinit(&rt);
printf(fails ? "\n%d FALHA(S)\n" : "\nTUDO OK\n", fails);
return fails != 0;
}