Estado atual do TinyBlocks: editor estilo Scratch + graphics como interface
Snapshot do projeto antes de implementar o diálogo "Criar um Bloco": - runtime C block-based (src/, include/) - libraries: graphics (interface), sdl2 (backend), editor (estilo Scratch), python, mcp - exemplos (hello, sdl2, editor, vector, json, customappearance, python, mcp) - Makefile com alvo help e build do editor Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
commit
4950ad6006
49 files changed
+10596
No files matched your search
@@ -0,0 +1,190 @@
|
||||
#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->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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/* ── 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;
|
||||
}
|
||||
Reference in new issue
Block a user