Blocos que abrem arquivo (carregue o PNG, Image.Load, FS.*) passam a resolver caminhos relativos contra o diretorio-base do projeto, definido pelo editor ao abrir um .sb. Assim exemplos acham atlas.png etc. mesmo quando o editor roda de outro cwd (ex.: AppImage aberto pelo menu), sem mudar o cwd global (que quebrava "Ver codigo-fonte" e o harness). - tb_path_set_base / tb_path_resolve, na API publica (src/tinyblocks.c) - editor: project_set_dir no project_load (sem chdir) - tb_png, tb_fs e tb_imglib usam o resolvedor - MAX_USER_BLOCKS 16 -> 8192 (documentado em docs/editor.md) Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
139 lines
5.3 KiB
C
139 lines
5.3 KiB
C
/*
|
|
* TinyBlocks - tinyblocks.h
|
|
* Cabeçalho principal do runtime. Inclua apenas este arquivo.
|
|
*
|
|
* O struct TinyBlocks é o runtime da linguagem. Ele é completamente separado
|
|
* de qualquer interface gráfica - GUIs são implementadas como LibraryBlocks.
|
|
*
|
|
* Fluxo de uso mínimo:
|
|
*
|
|
* TinyBlocks* rt = NULL;
|
|
* tinyblocks_init(&rt, NULL); // inicializa com alocador padrão
|
|
*
|
|
* // carrega uma library (estática ou dinâmica)
|
|
* tb_library_register(rt, my_library_descriptor());
|
|
*
|
|
* // cria blocos, conecta-os, avalia
|
|
* TinyBlocks_TypeBlock* t = tb_type_block_find(rt, "Math.Add");
|
|
* TinyBlocks_Block* b = tb_block_create(rt, t);
|
|
* tb_block_connect(b, 0, lit_a, 0);
|
|
* tb_block_connect(b, 1, lit_b, 0);
|
|
* tb_block_eval(b, rt);
|
|
* TinyBlocks_Value result = b->outputs[0];
|
|
*
|
|
* tinyblocks_deinit(&rt);
|
|
*/
|
|
#ifndef TINYBLOCKS_H
|
|
#define TINYBLOCKS_H
|
|
|
|
#include "tb_defs.h"
|
|
#include "tb_object.h"
|
|
#include "tb_block.h"
|
|
#include "tb_type_block.h"
|
|
#include "tb_library_block.h"
|
|
#include "tb_text_block.h"
|
|
#include "tb_integer_block.h"
|
|
#include "tb_real_block.h"
|
|
#include "tb_vector_block.h"
|
|
#include "tb_json_block.h"
|
|
#include "tb_image.h"
|
|
#include "tb_result.h"
|
|
#include "tb_value_class.h"
|
|
#include "tb_meta.h"
|
|
|
|
/* -- Runtime --- */
|
|
struct TinyBlocks {
|
|
/* Alocador (padrão: malloc/free) */
|
|
TinyBlocks_AllocFn alloc;
|
|
TinyBlocks_FreeFn free;
|
|
void* alloc_userdata;
|
|
|
|
/* Registro global de blocos vivos */
|
|
TinyBlocks_Block** blocks;
|
|
uint32_t block_count;
|
|
uint32_t block_capacity;
|
|
|
|
/* Registro de tipos */
|
|
TinyBlocks_TypeBlock** types;
|
|
uint32_t type_count;
|
|
uint32_t type_capacity;
|
|
|
|
/* Registro de libraries */
|
|
TinyBlocks_LibraryBlock** libraries;
|
|
uint32_t library_count;
|
|
uint32_t library_capacity;
|
|
|
|
/* Handles de bibliotecas carregadas dinamicamente (dlopen) */
|
|
void** dl_handles;
|
|
uint32_t dl_handle_count;
|
|
uint32_t dl_handle_capacity;
|
|
|
|
/* Gerador de IDs únicos */
|
|
uint64_t next_id;
|
|
|
|
/* Último erro em texto legível */
|
|
char error[512];
|
|
|
|
/* Erro tratado agora por um "tente / se der erro" (bloco "erro capturado") */
|
|
char caught[512];
|
|
};
|
|
|
|
/* -- Opções de inicialização --- */
|
|
typedef struct TinyBlocks_InitOptions {
|
|
TinyBlocks_AllocFn alloc; /* NULL = malloc padrão */
|
|
TinyBlocks_FreeFn free_fn; /* NULL = free padrão */
|
|
void* alloc_userdata;
|
|
uint32_t initial_block_cap; /* 0 = 64 */
|
|
uint32_t initial_type_cap; /* 0 = 32 */
|
|
uint32_t initial_library_cap; /* 0 = 16 */
|
|
} TinyBlocks_InitOptions;
|
|
|
|
/* -- Ciclo de vida do runtime --- */
|
|
TB_API bool tinyblocks_init (TinyBlocks** out_rt, const TinyBlocks_InitOptions* opts);
|
|
TB_API void tinyblocks_deinit(TinyBlocks** rt);
|
|
|
|
/* -- Caminhos de arquivo --- */
|
|
/* O aplicativo (ex.: o editor) define o diretorio-base do projeto aberto;
|
|
os blocos que abrem arquivos resolvem caminhos relativos contra ele, para
|
|
que um caminho como "atlas.png" ache o arquivo na pasta do projeto mesmo
|
|
quando o processo roda de outro diretorio (ex.: AppImage aberto pelo menu). */
|
|
TB_API void tb_path_set_base(const char* dir);
|
|
TB_API const char* tb_path_resolve(const char* path, char* out, size_t out_n);
|
|
|
|
/* -- Registro interno (usado pelas implementações de bloco) --- */
|
|
TB_API bool tb_rt_register_block (TinyBlocks* rt, TinyBlocks_Block* block);
|
|
TB_API void tb_rt_unregister_block(TinyBlocks* rt, TinyBlocks_Block* block);
|
|
TB_API bool tb_rt_register_type (TinyBlocks* rt, TinyBlocks_TypeBlock* type);
|
|
TB_API void tb_rt_unregister_type (TinyBlocks* rt, TinyBlocks_TypeBlock* type);
|
|
TB_API bool tb_rt_register_library(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib);
|
|
TB_API void tb_rt_unregister_library(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib);
|
|
|
|
/* -- Alocação com o alocador do runtime --- */
|
|
TB_API void* tb_alloc(TinyBlocks* rt, size_t size);
|
|
TB_API void tb_free (TinyBlocks* rt, void* ptr);
|
|
TB_API char* tb_strdup(TinyBlocks* rt, const char* str);
|
|
|
|
/* -- Diagnósticos --- */
|
|
TB_API const char* tinyblocks_last_error(TinyBlocks* rt);
|
|
TB_API void tinyblocks_set_error (TinyBlocks* rt, const char* fmt, ...);
|
|
TB_API void tinyblocks_clear_error(TinyBlocks* rt);
|
|
/* Erro em tratamento (quem executa o "se der erro" define; "" = nenhum) */
|
|
TB_API const char* tinyblocks_caught_error(TinyBlocks* rt);
|
|
TB_API void tinyblocks_set_caught_error(TinyBlocks* rt, const char* msg);
|
|
|
|
/* -- Busca no registro --- */
|
|
TB_API TinyBlocks_Block* tb_rt_find_block (TinyBlocks* rt, uint64_t id);
|
|
TB_API TinyBlocks_TypeBlock* tb_rt_find_type (TinyBlocks* rt, const char* name);
|
|
TB_API TinyBlocks_LibraryBlock* tb_rt_find_library(TinyBlocks* rt, const char* name);
|
|
|
|
/* -- Enumeracao publica dos registros (sem expor os campos do struct) --- */
|
|
TB_API uint32_t tb_rt_type_count (const TinyBlocks* rt);
|
|
TB_API TinyBlocks_TypeBlock* tb_rt_type_at (const TinyBlocks* rt, uint32_t i);
|
|
TB_API uint32_t tb_rt_library_count(const TinyBlocks* rt);
|
|
TB_API TinyBlocks_LibraryBlock* tb_rt_library_at (const TinyBlocks* rt, uint32_t i);
|
|
|
|
/* -- Geração de ID --- */
|
|
TB_API uint64_t tb_rt_next_id(TinyBlocks* rt);
|
|
|
|
#endif /* TINYBLOCKS_H */
|