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
+252
@@ -0,0 +1,252 @@
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include "tinyblocks/tinyblocks.h"
|
||||
|
||||
/* ── Alocação da estrutura base ──────────────────────────────────────────── */
|
||||
TinyBlocks_Block* tb_block_alloc_base(TinyBlocks* rt,
|
||||
TinyBlocks_BlockKind kind,
|
||||
size_t struct_size,
|
||||
uint32_t input_count,
|
||||
uint32_t output_count,
|
||||
const TinyBlocks_BlockVTable* vtable)
|
||||
{
|
||||
TinyBlocks_Block* b = tb_alloc(rt, struct_size);
|
||||
if (!b) return NULL;
|
||||
|
||||
b->kind = kind;
|
||||
b->vtable = vtable;
|
||||
b->id = tb_rt_next_id(rt);
|
||||
b->input_count = input_count;
|
||||
b->output_count = output_count;
|
||||
b->dirty = true;
|
||||
|
||||
if (input_count > 0) {
|
||||
b->inputs = tb_alloc(rt, input_count * sizeof(TinyBlocks_Connection));
|
||||
if (!b->inputs) { tb_free(rt, b); return NULL; }
|
||||
}
|
||||
if (output_count > 0) {
|
||||
b->outputs = tb_alloc(rt, output_count * sizeof(TinyBlocks_Value));
|
||||
if (!b->outputs) { tb_free(rt, b->inputs); tb_free(rt, b); return NULL; }
|
||||
}
|
||||
|
||||
tb_rt_register_block(rt, b);
|
||||
return b;
|
||||
}
|
||||
|
||||
/* ── Vtable padrão para blocos definidos por TypeBlock ───────────────────── */
|
||||
static void default_eval(TinyBlocks_Block* self, TinyBlocks* rt)
|
||||
{
|
||||
if (self->type && self->type->eval_fn)
|
||||
self->type->eval_fn(self, rt);
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static void default_destroy(TinyBlocks_Block* self, TinyBlocks* rt)
|
||||
{
|
||||
if (self->type && self->type->instance_deinit)
|
||||
self->type->instance_deinit(self, rt);
|
||||
tb_free(rt, self->userdata);
|
||||
}
|
||||
|
||||
static size_t default_to_string(TinyBlocks_Block* self, TinyBlocks* rt,
|
||||
char* buf, size_t buf_size)
|
||||
{
|
||||
(void)rt;
|
||||
const char* tname = self->type ? self->type->type_name : "Block";
|
||||
const char* bname = self->name ? self->name : "";
|
||||
return (size_t)snprintf(buf, buf_size,
|
||||
"[%s#%llu%s%s]",
|
||||
tname, (unsigned long long)self->id,
|
||||
bname[0] ? " " : "", bname);
|
||||
}
|
||||
|
||||
static TinyBlocks_Block* default_clone(const TinyBlocks_Block* self, TinyBlocks* rt)
|
||||
{
|
||||
TinyBlocks_Block* c = tb_block_create(rt, self->type);
|
||||
if (!c) return NULL;
|
||||
if (self->name) c->name = tb_strdup(rt, self->name);
|
||||
/* copia conexões */
|
||||
memcpy(c->inputs, self->inputs, self->input_count * sizeof(TinyBlocks_Connection));
|
||||
/* copia outputs */
|
||||
for (uint32_t i = 0; i < self->output_count; ++i)
|
||||
c->outputs[i] = tb_value_clone(self->outputs[i]);
|
||||
return c;
|
||||
}
|
||||
|
||||
static const TinyBlocks_BlockVTable s_default_vtable = {
|
||||
.eval = default_eval,
|
||||
.destroy = default_destroy,
|
||||
.to_string = default_to_string,
|
||||
.clone = default_clone,
|
||||
};
|
||||
|
||||
/* ── Criação via TypeBlock ───────────────────────────────────────────────── */
|
||||
TinyBlocks_Block* tb_block_create(TinyBlocks* rt, TinyBlocks_TypeBlock* type)
|
||||
{
|
||||
if (!type) {
|
||||
tinyblocks_set_error(rt, "tb_block_create: type is NULL");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
TinyBlocks_Block* b = tb_block_alloc_base(rt,
|
||||
TB_KIND_BLOCK,
|
||||
sizeof(TinyBlocks_Block),
|
||||
type->input_desc_count,
|
||||
type->output_desc_count,
|
||||
&s_default_vtable);
|
||||
if (!b) return NULL;
|
||||
|
||||
b->type = type;
|
||||
|
||||
/* Inicializa userdata por instância */
|
||||
if (type->instance_userdata_size > 0) {
|
||||
b->userdata = tb_alloc(rt, type->instance_userdata_size);
|
||||
if (!b->userdata) {
|
||||
tb_block_destroy(b, rt);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if (type->instance_init && !type->instance_init(b, rt)) {
|
||||
tb_block_destroy(b, rt);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return b;
|
||||
}
|
||||
|
||||
/* ── Destruição ──────────────────────────────────────────────────────────── */
|
||||
void tb_block_destroy(TinyBlocks_Block* block, TinyBlocks* rt)
|
||||
{
|
||||
if (!block) return;
|
||||
tb_rt_unregister_block(rt, block);
|
||||
|
||||
if (block->vtable && block->vtable->destroy)
|
||||
block->vtable->destroy(block, rt);
|
||||
|
||||
tb_free(rt, block->name);
|
||||
|
||||
/* Libera valores de default dos inputs */
|
||||
for (uint32_t i = 0; i < block->input_count; ++i)
|
||||
tb_value_free(&block->inputs[i].default_value);
|
||||
tb_free(rt, block->inputs);
|
||||
|
||||
for (uint32_t i = 0; i < block->output_count; ++i)
|
||||
tb_value_free(&block->outputs[i]);
|
||||
tb_free(rt, block->outputs);
|
||||
|
||||
tb_free(rt, block);
|
||||
}
|
||||
|
||||
/* ── Avaliação ───────────────────────────────────────────────────────────── */
|
||||
void tb_block_eval(TinyBlocks_Block* block, TinyBlocks* rt)
|
||||
{
|
||||
if (!block || !block->vtable || !block->vtable->eval) return;
|
||||
|
||||
/* Detecta ciclo: bloco já está sendo avaliado nesta chamada recursiva */
|
||||
if (block->in_eval) {
|
||||
tinyblocks_set_error(rt, "ciclo detectado em '%s' — conexão ignorada",
|
||||
block->type ? block->type->type_name : "?");
|
||||
return;
|
||||
}
|
||||
block->in_eval = true;
|
||||
|
||||
/* Avalia fontes conectadas primeiro (lazy) */
|
||||
for (uint32_t i = 0; i < block->input_count; ++i) {
|
||||
TinyBlocks_Connection* conn = &block->inputs[i];
|
||||
if (conn->source && conn->source->dirty)
|
||||
tb_block_eval(conn->source, rt);
|
||||
}
|
||||
|
||||
block->vtable->eval(block, rt);
|
||||
block->in_eval = false;
|
||||
}
|
||||
|
||||
/* ── Clone ───────────────────────────────────────────────────────────────── */
|
||||
TinyBlocks_Block* tb_block_clone(const TinyBlocks_Block* block, TinyBlocks* rt)
|
||||
{
|
||||
if (!block || !block->vtable || !block->vtable->clone) return NULL;
|
||||
return block->vtable->clone(block, rt);
|
||||
}
|
||||
|
||||
/* ── Representação textual ───────────────────────────────────────────────── */
|
||||
size_t tb_block_to_string(TinyBlocks_Block* block, TinyBlocks* rt,
|
||||
char* buf, size_t buf_size)
|
||||
{
|
||||
if (!block || !buf || buf_size == 0) return 0;
|
||||
if (block->vtable && block->vtable->to_string)
|
||||
return block->vtable->to_string(block, rt, buf, buf_size);
|
||||
return (size_t)snprintf(buf, buf_size, "[Block#%llu]",
|
||||
(unsigned long long)block->id);
|
||||
}
|
||||
|
||||
/* ── Conexões ────────────────────────────────────────────────────────────── */
|
||||
bool tb_block_connect(TinyBlocks_Block* target, uint32_t input_idx,
|
||||
TinyBlocks_Block* source, uint32_t output_idx)
|
||||
{
|
||||
if (!target || input_idx >= target->input_count) return false;
|
||||
if (!source || output_idx >= source->output_count) return false;
|
||||
|
||||
TinyBlocks_Connection* conn = &target->inputs[input_idx];
|
||||
conn->source = source;
|
||||
conn->output_idx = output_idx;
|
||||
target->dirty = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void tb_block_disconnect(TinyBlocks_Block* target, uint32_t input_idx)
|
||||
{
|
||||
if (!target || input_idx >= target->input_count) return;
|
||||
target->inputs[input_idx].source = NULL;
|
||||
target->dirty = true;
|
||||
}
|
||||
|
||||
bool tb_block_set_default(TinyBlocks_Block* target, uint32_t input_idx,
|
||||
TinyBlocks_Value default_value)
|
||||
{
|
||||
if (!target || input_idx >= target->input_count) return false;
|
||||
tb_value_free(&target->inputs[input_idx].default_value);
|
||||
target->inputs[input_idx].default_value = tb_value_clone(default_value);
|
||||
return true;
|
||||
}
|
||||
|
||||
/* ── Acesso a valores em eval_fn ─────────────────────────────────────────── */
|
||||
TinyBlocks_Value tb_block_get_input(TinyBlocks_Block* self, TinyBlocks* rt,
|
||||
uint32_t input_idx)
|
||||
{
|
||||
if (!self || input_idx >= self->input_count) return tb_value_none();
|
||||
|
||||
TinyBlocks_Connection* conn = &self->inputs[input_idx];
|
||||
if (conn->source) {
|
||||
if (conn->source->dirty) tb_block_eval(conn->source, rt);
|
||||
if (conn->output_idx < conn->source->output_count)
|
||||
return conn->source->outputs[conn->output_idx];
|
||||
}
|
||||
return conn->default_value;
|
||||
}
|
||||
|
||||
/* Toma ownership de v — o caller NÃO deve liberar v depois desta chamada.
|
||||
Se precisar manter v, passe tb_value_clone(v) em vez de v. */
|
||||
void tb_block_set_output(TinyBlocks_Block* self, uint32_t output_idx,
|
||||
TinyBlocks_Value v)
|
||||
{
|
||||
if (!self || output_idx >= self->output_count) return;
|
||||
tb_value_free(&self->outputs[output_idx]);
|
||||
self->outputs[output_idx] = v;
|
||||
}
|
||||
|
||||
/* ── Downcasting seguro ──────────────────────────────────────────────────── */
|
||||
TinyBlocks_TypeBlock* tb_block_as_type (TinyBlocks_Block* b)
|
||||
{ return b && b->kind == TB_KIND_TYPE ? (TinyBlocks_TypeBlock*)b : NULL; }
|
||||
|
||||
TinyBlocks_LibraryBlock* tb_block_as_library(TinyBlocks_Block* b)
|
||||
{ return b && b->kind == TB_KIND_LIBRARY ? (TinyBlocks_LibraryBlock*)b : NULL; }
|
||||
|
||||
TinyBlocks_TextBlock* tb_block_as_text (TinyBlocks_Block* b)
|
||||
{ return b && b->kind == TB_KIND_TEXT ? (TinyBlocks_TextBlock*)b : NULL; }
|
||||
|
||||
TinyBlocks_IntegerBlock* tb_block_as_integer(TinyBlocks_Block* b)
|
||||
{ return b && b->kind == TB_KIND_INTEGER ? (TinyBlocks_IntegerBlock*)b : NULL; }
|
||||
|
||||
TinyBlocks_RealBlock* tb_block_as_real (TinyBlocks_Block* b)
|
||||
{ return b && b->kind == TB_KIND_REAL ? (TinyBlocks_RealBlock*)b : NULL; }
|
||||
@@ -0,0 +1,68 @@
|
||||
#include <stdio.h>
|
||||
#include "tinyblocks/tinyblocks.h"
|
||||
|
||||
/* ── Vtable ──────────────────────────────────────────────────────────────── */
|
||||
static void integer_eval(TinyBlocks_Block* self, TinyBlocks* rt)
|
||||
{
|
||||
(void)rt;
|
||||
TinyBlocks_IntegerBlock* ib = (TinyBlocks_IntegerBlock*)self;
|
||||
tb_block_set_output(self, 0, tb_value_integer(ib->value));
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static void integer_destroy(TinyBlocks_Block* self, TinyBlocks* rt)
|
||||
{
|
||||
(void)self; (void)rt; /* nada a liberar além do base */
|
||||
}
|
||||
|
||||
static size_t integer_to_string(TinyBlocks_Block* self, TinyBlocks* rt,
|
||||
char* buf, size_t sz)
|
||||
{
|
||||
(void)rt;
|
||||
TinyBlocks_IntegerBlock* ib = (TinyBlocks_IntegerBlock*)self;
|
||||
return (size_t)snprintf(buf, sz, "%lld", (long long)ib->value);
|
||||
}
|
||||
|
||||
static TinyBlocks_Block* integer_clone(const TinyBlocks_Block* self, TinyBlocks* rt)
|
||||
{
|
||||
const TinyBlocks_IntegerBlock* ib = (const TinyBlocks_IntegerBlock*)self;
|
||||
TinyBlocks_IntegerBlock* clone = tb_integer_block_create(rt, ib->value);
|
||||
if (clone && self->name) clone->base.name = tb_strdup(rt, self->name);
|
||||
return (TinyBlocks_Block*)clone;
|
||||
}
|
||||
|
||||
static const TinyBlocks_BlockVTable s_integer_vtable = {
|
||||
.eval = integer_eval,
|
||||
.destroy = integer_destroy,
|
||||
.to_string = integer_to_string,
|
||||
.clone = integer_clone,
|
||||
};
|
||||
|
||||
/* ── Ciclo de vida ───────────────────────────────────────────────────────── */
|
||||
TinyBlocks_IntegerBlock* tb_integer_block_create(TinyBlocks* rt, int64_t value)
|
||||
{
|
||||
TinyBlocks_IntegerBlock* ib = (TinyBlocks_IntegerBlock*)tb_block_alloc_base(
|
||||
rt, TB_KIND_INTEGER, sizeof(TinyBlocks_IntegerBlock),
|
||||
0, 1, &s_integer_vtable);
|
||||
if (!ib) return NULL;
|
||||
ib->value = value;
|
||||
return ib;
|
||||
}
|
||||
|
||||
void tb_integer_block_destroy(TinyBlocks_IntegerBlock* ib, TinyBlocks* rt)
|
||||
{
|
||||
tb_block_destroy(&ib->base, rt);
|
||||
}
|
||||
|
||||
/* ── Acesso ──────────────────────────────────────────────────────────────── */
|
||||
int64_t tb_integer_block_get(const TinyBlocks_IntegerBlock* ib)
|
||||
{
|
||||
return ib ? ib->value : 0;
|
||||
}
|
||||
|
||||
void tb_integer_block_set(TinyBlocks_IntegerBlock* ib, int64_t value)
|
||||
{
|
||||
if (!ib) return;
|
||||
ib->value = value;
|
||||
ib->base.dirty = true;
|
||||
}
|
||||
@@ -0,0 +1,526 @@
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
#include <math.h>
|
||||
|
||||
#include "tinyblocks/tinyblocks.h"
|
||||
|
||||
struct TinyBlocks_JsonValue {
|
||||
TinyBlocks_JsonKind kind;
|
||||
int32_t ref;
|
||||
union {
|
||||
bool boolean;
|
||||
double number;
|
||||
char* string;
|
||||
struct {
|
||||
TinyBlocks_JsonValue** items;
|
||||
uint32_t count, capacity;
|
||||
} array;
|
||||
struct {
|
||||
char** keys;
|
||||
TinyBlocks_JsonValue** values;
|
||||
uint32_t count, capacity;
|
||||
} object;
|
||||
} as;
|
||||
};
|
||||
|
||||
/* ══════════════════════════════════════════════════════════════════════════
|
||||
* Construtores / ciclo de vida
|
||||
* ══════════════════════════════════════════════════════════════════════════ */
|
||||
static TinyBlocks_JsonValue* json_alloc(TinyBlocks_JsonKind kind) {
|
||||
TinyBlocks_JsonValue* j = calloc(1, sizeof(TinyBlocks_JsonValue));
|
||||
if (!j) return NULL;
|
||||
j->kind = kind;
|
||||
j->ref = 1;
|
||||
return j;
|
||||
}
|
||||
|
||||
TinyBlocks_JsonValue* tb_json_null(void) { return json_alloc(TB_JSON_NULL); }
|
||||
|
||||
TinyBlocks_JsonValue* tb_json_bool(bool b) {
|
||||
TinyBlocks_JsonValue* j = json_alloc(TB_JSON_BOOL);
|
||||
if (j) j->as.boolean = b;
|
||||
return j;
|
||||
}
|
||||
|
||||
TinyBlocks_JsonValue* tb_json_number(double d) {
|
||||
TinyBlocks_JsonValue* j = json_alloc(TB_JSON_NUMBER);
|
||||
if (j) j->as.number = d;
|
||||
return j;
|
||||
}
|
||||
|
||||
TinyBlocks_JsonValue* tb_json_string(const char* s) {
|
||||
TinyBlocks_JsonValue* j = json_alloc(TB_JSON_STRING);
|
||||
if (!j) return NULL;
|
||||
j->as.string = strdup(s ? s : "");
|
||||
return j;
|
||||
}
|
||||
|
||||
TinyBlocks_JsonValue* tb_json_array(void) { return json_alloc(TB_JSON_ARRAY); }
|
||||
TinyBlocks_JsonValue* tb_json_object(void) { return json_alloc(TB_JSON_OBJECT); }
|
||||
|
||||
static void json_destroy(TinyBlocks_JsonValue* j) {
|
||||
switch (j->kind) {
|
||||
case TB_JSON_STRING:
|
||||
free(j->as.string);
|
||||
break;
|
||||
case TB_JSON_ARRAY:
|
||||
for (uint32_t i = 0; i < j->as.array.count; i++)
|
||||
tb_json_release(j->as.array.items[i]);
|
||||
free(j->as.array.items);
|
||||
break;
|
||||
case TB_JSON_OBJECT:
|
||||
for (uint32_t i = 0; i < j->as.object.count; i++) {
|
||||
free(j->as.object.keys[i]);
|
||||
tb_json_release(j->as.object.values[i]);
|
||||
}
|
||||
free(j->as.object.keys);
|
||||
free(j->as.object.values);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
free(j);
|
||||
}
|
||||
|
||||
void tb_json_retain(TinyBlocks_JsonValue* j) {
|
||||
if (j) j->ref++;
|
||||
}
|
||||
|
||||
void tb_json_release(TinyBlocks_JsonValue* j) {
|
||||
if (!j) return;
|
||||
if (--j->ref <= 0) json_destroy(j);
|
||||
}
|
||||
|
||||
TinyBlocks_JsonValue* tb_json_clone(const TinyBlocks_JsonValue* j) {
|
||||
if (!j) return NULL;
|
||||
switch (j->kind) {
|
||||
case TB_JSON_NULL: return tb_json_null();
|
||||
case TB_JSON_BOOL: return tb_json_bool(j->as.boolean);
|
||||
case TB_JSON_NUMBER: return tb_json_number(j->as.number);
|
||||
case TB_JSON_STRING: return tb_json_string(j->as.string);
|
||||
case TB_JSON_ARRAY: {
|
||||
TinyBlocks_JsonValue* out = tb_json_array();
|
||||
if (!out) return NULL;
|
||||
for (uint32_t i = 0; i < j->as.array.count; i++) {
|
||||
TinyBlocks_JsonValue* item = tb_json_clone(j->as.array.items[i]);
|
||||
tb_json_array_push(out, item);
|
||||
tb_json_release(item);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
case TB_JSON_OBJECT: {
|
||||
TinyBlocks_JsonValue* out = tb_json_object();
|
||||
if (!out) return NULL;
|
||||
for (uint32_t i = 0; i < j->as.object.count; i++) {
|
||||
TinyBlocks_JsonValue* val = tb_json_clone(j->as.object.values[i]);
|
||||
tb_json_object_set(out, j->as.object.keys[i], val);
|
||||
tb_json_release(val);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
}
|
||||
return tb_json_null();
|
||||
}
|
||||
|
||||
/* ══════════════════════════════════════════════════════════════════════════
|
||||
* Inspeção
|
||||
* ══════════════════════════════════════════════════════════════════════════ */
|
||||
TinyBlocks_JsonKind tb_json_kind(const TinyBlocks_JsonValue* j) {
|
||||
return j ? j->kind : TB_JSON_NULL;
|
||||
}
|
||||
|
||||
const char* tb_json_kind_name(const TinyBlocks_JsonValue* j) {
|
||||
switch (tb_json_kind(j)) {
|
||||
case TB_JSON_NULL: return "null";
|
||||
case TB_JSON_BOOL: return "bool";
|
||||
case TB_JSON_NUMBER: return "number";
|
||||
case TB_JSON_STRING: return "string";
|
||||
case TB_JSON_ARRAY: return "array";
|
||||
case TB_JSON_OBJECT: return "object";
|
||||
}
|
||||
return "null";
|
||||
}
|
||||
|
||||
bool tb_json_as_bool(const TinyBlocks_JsonValue* j, bool def) {
|
||||
if (!j) return def;
|
||||
if (j->kind == TB_JSON_BOOL) return j->as.boolean;
|
||||
if (j->kind == TB_JSON_NUMBER) return j->as.number != 0.0;
|
||||
return def;
|
||||
}
|
||||
|
||||
double tb_json_as_number(const TinyBlocks_JsonValue* j, double def) {
|
||||
if (!j) return def;
|
||||
if (j->kind == TB_JSON_NUMBER) return j->as.number;
|
||||
if (j->kind == TB_JSON_BOOL) return j->as.boolean ? 1.0 : 0.0;
|
||||
return def;
|
||||
}
|
||||
|
||||
const char* tb_json_as_string(const TinyBlocks_JsonValue* j) {
|
||||
if (!j || j->kind != TB_JSON_STRING) return NULL;
|
||||
return j->as.string;
|
||||
}
|
||||
|
||||
/* ══════════════════════════════════════════════════════════════════════════
|
||||
* Arrays
|
||||
* ══════════════════════════════════════════════════════════════════════════ */
|
||||
bool tb_json_array_push(TinyBlocks_JsonValue* arr, TinyBlocks_JsonValue* item) {
|
||||
if (!arr || arr->kind != TB_JSON_ARRAY || !item) return false;
|
||||
if (arr->as.array.count == arr->as.array.capacity) {
|
||||
uint32_t cap = arr->as.array.capacity ? arr->as.array.capacity * 2 : 8;
|
||||
TinyBlocks_JsonValue** items = realloc(arr->as.array.items, cap * sizeof(*items));
|
||||
if (!items) return false;
|
||||
arr->as.array.items = items;
|
||||
arr->as.array.capacity = cap;
|
||||
}
|
||||
tb_json_retain(item);
|
||||
arr->as.array.items[arr->as.array.count++] = item;
|
||||
return true;
|
||||
}
|
||||
|
||||
uint32_t tb_json_array_length(const TinyBlocks_JsonValue* arr) {
|
||||
return (arr && arr->kind == TB_JSON_ARRAY) ? arr->as.array.count : 0;
|
||||
}
|
||||
|
||||
TinyBlocks_JsonValue* tb_json_array_get(const TinyBlocks_JsonValue* arr, uint32_t idx) {
|
||||
if (!arr || arr->kind != TB_JSON_ARRAY || idx >= arr->as.array.count) return NULL;
|
||||
TinyBlocks_JsonValue* v = arr->as.array.items[idx];
|
||||
tb_json_retain(v);
|
||||
return v;
|
||||
}
|
||||
|
||||
/* ══════════════════════════════════════════════════════════════════════════
|
||||
* Objetos
|
||||
* ══════════════════════════════════════════════════════════════════════════ */
|
||||
bool tb_json_object_set(TinyBlocks_JsonValue* obj, const char* key, TinyBlocks_JsonValue* val) {
|
||||
if (!obj || obj->kind != TB_JSON_OBJECT || !key || !val) return false;
|
||||
for (uint32_t i = 0; i < obj->as.object.count; i++) {
|
||||
if (strcmp(obj->as.object.keys[i], key) == 0) {
|
||||
tb_json_release(obj->as.object.values[i]);
|
||||
tb_json_retain(val);
|
||||
obj->as.object.values[i] = val;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (obj->as.object.count == obj->as.object.capacity) {
|
||||
uint32_t cap = obj->as.object.capacity ? obj->as.object.capacity * 2 : 8;
|
||||
char** keys = realloc(obj->as.object.keys, cap * sizeof(*keys));
|
||||
TinyBlocks_JsonValue** values = realloc(obj->as.object.values, cap * sizeof(*values));
|
||||
if (!keys || !values) { free(keys); free(values); return false; }
|
||||
obj->as.object.keys = keys;
|
||||
obj->as.object.values = values;
|
||||
obj->as.object.capacity = cap;
|
||||
}
|
||||
obj->as.object.keys[obj->as.object.count] = strdup(key);
|
||||
tb_json_retain(val);
|
||||
obj->as.object.values[obj->as.object.count] = val;
|
||||
obj->as.object.count++;
|
||||
return true;
|
||||
}
|
||||
|
||||
uint32_t tb_json_object_length(const TinyBlocks_JsonValue* obj) {
|
||||
return (obj && obj->kind == TB_JSON_OBJECT) ? obj->as.object.count : 0;
|
||||
}
|
||||
|
||||
const char* tb_json_object_key_at(const TinyBlocks_JsonValue* obj, uint32_t idx) {
|
||||
if (!obj || obj->kind != TB_JSON_OBJECT || idx >= obj->as.object.count) return NULL;
|
||||
return obj->as.object.keys[idx];
|
||||
}
|
||||
|
||||
TinyBlocks_JsonValue* tb_json_object_get(const TinyBlocks_JsonValue* obj, const char* key) {
|
||||
if (!obj || obj->kind != TB_JSON_OBJECT || !key) return NULL;
|
||||
for (uint32_t i = 0; i < obj->as.object.count; i++) {
|
||||
if (strcmp(obj->as.object.keys[i], key) == 0) {
|
||||
tb_json_retain(obj->as.object.values[i]);
|
||||
return obj->as.object.values[i];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* ══════════════════════════════════════════════════════════════════════════
|
||||
* Parser (recursive-descent, RFC 8259 simplificado)
|
||||
* ══════════════════════════════════════════════════════════════════════════ */
|
||||
typedef struct { const char* p; const char* end; char* err; size_t errcap; } JsonParser;
|
||||
|
||||
static void parser_fail(JsonParser* ps, const char* msg) {
|
||||
if (ps->err && ps->errcap) snprintf(ps->err, ps->errcap, "%s", msg);
|
||||
}
|
||||
|
||||
static void skip_ws(JsonParser* ps) {
|
||||
while (ps->p < ps->end && isspace((unsigned char)*ps->p)) ps->p++;
|
||||
}
|
||||
|
||||
static TinyBlocks_JsonValue* parse_value(JsonParser* ps);
|
||||
|
||||
static bool parse_literal(JsonParser* ps, const char* lit) {
|
||||
size_t n = strlen(lit);
|
||||
if ((size_t)(ps->end - ps->p) < n) return false;
|
||||
if (memcmp(ps->p, lit, n) != 0) return false;
|
||||
ps->p += n;
|
||||
return true;
|
||||
}
|
||||
|
||||
static char* parse_string_raw(JsonParser* ps) {
|
||||
if (ps->p >= ps->end || *ps->p != '"') { parser_fail(ps, "esperado '\"'"); return NULL; }
|
||||
ps->p++;
|
||||
size_t cap = 32, len = 0;
|
||||
char* buf = malloc(cap);
|
||||
if (!buf) return NULL;
|
||||
while (ps->p < ps->end && *ps->p != '"') {
|
||||
unsigned char c = (unsigned char)*ps->p++;
|
||||
char out = (char)c;
|
||||
if (c == '\\') {
|
||||
if (ps->p >= ps->end) { free(buf); parser_fail(ps, "string truncada"); return NULL; }
|
||||
char esc = *ps->p++;
|
||||
switch (esc) {
|
||||
case '"': out = '"'; break;
|
||||
case '\\': out = '\\'; break;
|
||||
case '/': out = '/'; break;
|
||||
case 'b': out = '\b'; break;
|
||||
case 'f': out = '\f'; break;
|
||||
case 'n': out = '\n'; break;
|
||||
case 'r': out = '\r'; break;
|
||||
case 't': out = '\t'; break;
|
||||
case 'u': {
|
||||
if (ps->end - ps->p < 4) { free(buf); parser_fail(ps, "\\u incompleto"); return NULL; }
|
||||
unsigned cp = 0;
|
||||
for (int i = 0; i < 4; i++) {
|
||||
char h = ps->p[i];
|
||||
cp <<= 4;
|
||||
if (h >= '0' && h <= '9') cp |= (unsigned)(h - '0');
|
||||
else if (h >= 'a' && h <= 'f') cp |= (unsigned)(h - 'a' + 10);
|
||||
else if (h >= 'A' && h <= 'F') cp |= (unsigned)(h - 'A' + 10);
|
||||
else { free(buf); parser_fail(ps, "\\u inválido"); return NULL; }
|
||||
}
|
||||
ps->p += 4;
|
||||
/* codifica em UTF-8 (apenas BMP, sem pares substitutos) */
|
||||
if (len + 4 >= cap) { cap *= 2; buf = realloc(buf, cap); if (!buf) return NULL; }
|
||||
if (cp < 0x80) {
|
||||
buf[len++] = (char)cp;
|
||||
} else if (cp < 0x800) {
|
||||
buf[len++] = (char)(0xC0 | (cp >> 6));
|
||||
buf[len++] = (char)(0x80 | (cp & 0x3F));
|
||||
} else {
|
||||
buf[len++] = (char)(0xE0 | (cp >> 12));
|
||||
buf[len++] = (char)(0x80 | ((cp >> 6) & 0x3F));
|
||||
buf[len++] = (char)(0x80 | (cp & 0x3F));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
default:
|
||||
free(buf); parser_fail(ps, "escape inválido"); return NULL;
|
||||
}
|
||||
}
|
||||
if (len + 1 >= cap) { cap *= 2; buf = realloc(buf, cap); if (!buf) return NULL; }
|
||||
buf[len++] = out;
|
||||
}
|
||||
if (ps->p >= ps->end) { free(buf); parser_fail(ps, "string não terminada"); return NULL; }
|
||||
ps->p++; /* fecha '"' */
|
||||
buf[len] = '\0';
|
||||
return buf;
|
||||
}
|
||||
|
||||
static TinyBlocks_JsonValue* parse_number(JsonParser* ps) {
|
||||
const char* start = ps->p;
|
||||
if (ps->p < ps->end && *ps->p == '-') ps->p++;
|
||||
while (ps->p < ps->end && isdigit((unsigned char)*ps->p)) ps->p++;
|
||||
if (ps->p < ps->end && *ps->p == '.') {
|
||||
ps->p++;
|
||||
while (ps->p < ps->end && isdigit((unsigned char)*ps->p)) ps->p++;
|
||||
}
|
||||
if (ps->p < ps->end && (*ps->p == 'e' || *ps->p == 'E')) {
|
||||
ps->p++;
|
||||
if (ps->p < ps->end && (*ps->p == '+' || *ps->p == '-')) ps->p++;
|
||||
while (ps->p < ps->end && isdigit((unsigned char)*ps->p)) ps->p++;
|
||||
}
|
||||
if (ps->p == start) { parser_fail(ps, "número inválido"); return NULL; }
|
||||
char tmp[64];
|
||||
size_t n = (size_t)(ps->p - start);
|
||||
if (n >= sizeof(tmp)) n = sizeof(tmp) - 1;
|
||||
memcpy(tmp, start, n);
|
||||
tmp[n] = '\0';
|
||||
return tb_json_number(strtod(tmp, NULL));
|
||||
}
|
||||
|
||||
static TinyBlocks_JsonValue* parse_array(JsonParser* ps) {
|
||||
ps->p++; /* '[' */
|
||||
TinyBlocks_JsonValue* arr = tb_json_array();
|
||||
skip_ws(ps);
|
||||
if (ps->p < ps->end && *ps->p == ']') { ps->p++; return arr; }
|
||||
for (;;) {
|
||||
skip_ws(ps);
|
||||
TinyBlocks_JsonValue* item = parse_value(ps);
|
||||
if (!item) { tb_json_release(arr); return NULL; }
|
||||
tb_json_array_push(arr, item);
|
||||
tb_json_release(item);
|
||||
skip_ws(ps);
|
||||
if (ps->p < ps->end && *ps->p == ',') { ps->p++; continue; }
|
||||
if (ps->p < ps->end && *ps->p == ']') { ps->p++; break; }
|
||||
tb_json_release(arr); parser_fail(ps, "esperado ',' ou ']'"); return NULL;
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
static TinyBlocks_JsonValue* parse_object(JsonParser* ps) {
|
||||
ps->p++; /* '{' */
|
||||
TinyBlocks_JsonValue* obj = tb_json_object();
|
||||
skip_ws(ps);
|
||||
if (ps->p < ps->end && *ps->p == '}') { ps->p++; return obj; }
|
||||
for (;;) {
|
||||
skip_ws(ps);
|
||||
char* key = parse_string_raw(ps);
|
||||
if (!key) { tb_json_release(obj); return NULL; }
|
||||
skip_ws(ps);
|
||||
if (ps->p >= ps->end || *ps->p != ':') { free(key); tb_json_release(obj); parser_fail(ps, "esperado ':'"); return NULL; }
|
||||
ps->p++;
|
||||
skip_ws(ps);
|
||||
TinyBlocks_JsonValue* val = parse_value(ps);
|
||||
if (!val) { free(key); tb_json_release(obj); return NULL; }
|
||||
tb_json_object_set(obj, key, val);
|
||||
free(key);
|
||||
tb_json_release(val);
|
||||
skip_ws(ps);
|
||||
if (ps->p < ps->end && *ps->p == ',') { ps->p++; continue; }
|
||||
if (ps->p < ps->end && *ps->p == '}') { ps->p++; break; }
|
||||
tb_json_release(obj); parser_fail(ps, "esperado ',' ou '}'"); return NULL;
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
static TinyBlocks_JsonValue* parse_value(JsonParser* ps) {
|
||||
skip_ws(ps);
|
||||
if (ps->p >= ps->end) { parser_fail(ps, "fim inesperado"); return NULL; }
|
||||
char c = *ps->p;
|
||||
if (c == '{') return parse_object(ps);
|
||||
if (c == '[') return parse_array(ps);
|
||||
if (c == '"') {
|
||||
char* s = parse_string_raw(ps);
|
||||
if (!s) return NULL;
|
||||
TinyBlocks_JsonValue* j = tb_json_string(s);
|
||||
free(s);
|
||||
return j;
|
||||
}
|
||||
if (c == '-' || isdigit((unsigned char)c)) return parse_number(ps);
|
||||
if (parse_literal(ps, "true")) return tb_json_bool(true);
|
||||
if (parse_literal(ps, "false")) return tb_json_bool(false);
|
||||
if (parse_literal(ps, "null")) return tb_json_null();
|
||||
parser_fail(ps, "token inesperado");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
TinyBlocks_JsonValue* tb_json_parse(const char* text, char* errbuf, size_t errbuf_size) {
|
||||
if (!text) { if (errbuf && errbuf_size) snprintf(errbuf, errbuf_size, "texto nulo"); return NULL; }
|
||||
JsonParser ps = { .p = text, .end = text + strlen(text), .err = errbuf, .errcap = errbuf_size };
|
||||
TinyBlocks_JsonValue* v = parse_value(&ps);
|
||||
if (!v) return NULL;
|
||||
skip_ws(&ps);
|
||||
if (ps.p != ps.end) {
|
||||
tb_json_release(v);
|
||||
if (errbuf && errbuf_size) snprintf(errbuf, errbuf_size, "lixo após valor JSON");
|
||||
return NULL;
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
/* ══════════════════════════════════════════════════════════════════════════
|
||||
* Stringify
|
||||
* ══════════════════════════════════════════════════════════════════════════ */
|
||||
typedef struct { char* buf; size_t len, cap; } StrBuf;
|
||||
|
||||
static void sb_ensure(StrBuf* sb, size_t extra) {
|
||||
if (sb->len + extra + 1 <= sb->cap) return;
|
||||
size_t cap = sb->cap ? sb->cap * 2 : 64;
|
||||
while (cap < sb->len + extra + 1) cap *= 2;
|
||||
sb->buf = realloc(sb->buf, cap);
|
||||
sb->cap = cap;
|
||||
}
|
||||
|
||||
static void sb_append(StrBuf* sb, const char* s) {
|
||||
size_t n = strlen(s);
|
||||
sb_ensure(sb, n);
|
||||
memcpy(sb->buf + sb->len, s, n);
|
||||
sb->len += n;
|
||||
sb->buf[sb->len] = '\0';
|
||||
}
|
||||
|
||||
static void sb_append_quoted(StrBuf* sb, const char* s) {
|
||||
sb_append(sb, "\"");
|
||||
for (const unsigned char* p = (const unsigned char*)s; *p; p++) {
|
||||
char tmp[8];
|
||||
switch (*p) {
|
||||
case '"': sb_append(sb, "\\\""); break;
|
||||
case '\\': sb_append(sb, "\\\\"); break;
|
||||
case '\n': sb_append(sb, "\\n"); break;
|
||||
case '\r': sb_append(sb, "\\r"); break;
|
||||
case '\t': sb_append(sb, "\\t"); break;
|
||||
default:
|
||||
if (*p < 0x20) { snprintf(tmp, sizeof(tmp), "\\u%04x", *p); sb_append(sb, tmp); }
|
||||
else { tmp[0] = (char)*p; tmp[1] = '\0'; sb_append(sb, tmp); }
|
||||
}
|
||||
}
|
||||
sb_append(sb, "\"");
|
||||
}
|
||||
|
||||
static void stringify_rec(StrBuf* sb, const TinyBlocks_JsonValue* j) {
|
||||
if (!j) { sb_append(sb, "null"); return; }
|
||||
char tmp[64];
|
||||
switch (j->kind) {
|
||||
case TB_JSON_NULL:
|
||||
sb_append(sb, "null");
|
||||
break;
|
||||
case TB_JSON_BOOL:
|
||||
sb_append(sb, j->as.boolean ? "true" : "false");
|
||||
break;
|
||||
case TB_JSON_NUMBER:
|
||||
if (isfinite(j->as.number) && j->as.number == (double)(int64_t)j->as.number)
|
||||
snprintf(tmp, sizeof(tmp), "%lld", (long long)j->as.number);
|
||||
else
|
||||
snprintf(tmp, sizeof(tmp), "%g", j->as.number);
|
||||
sb_append(sb, tmp);
|
||||
break;
|
||||
case TB_JSON_STRING:
|
||||
sb_append_quoted(sb, j->as.string);
|
||||
break;
|
||||
case TB_JSON_ARRAY:
|
||||
sb_append(sb, "[");
|
||||
for (uint32_t i = 0; i < j->as.array.count; i++) {
|
||||
if (i) sb_append(sb, ",");
|
||||
stringify_rec(sb, j->as.array.items[i]);
|
||||
}
|
||||
sb_append(sb, "]");
|
||||
break;
|
||||
case TB_JSON_OBJECT:
|
||||
sb_append(sb, "{");
|
||||
for (uint32_t i = 0; i < j->as.object.count; i++) {
|
||||
if (i) sb_append(sb, ",");
|
||||
sb_append_quoted(sb, j->as.object.keys[i]);
|
||||
sb_append(sb, ":");
|
||||
stringify_rec(sb, j->as.object.values[i]);
|
||||
}
|
||||
sb_append(sb, "}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
char* tb_json_stringify(const TinyBlocks_JsonValue* j) {
|
||||
StrBuf sb = {0};
|
||||
stringify_rec(&sb, j);
|
||||
if (!sb.buf) sb.buf = strdup("null");
|
||||
return sb.buf;
|
||||
}
|
||||
|
||||
/* ══════════════════════════════════════════════════════════════════════════
|
||||
* Helpers para TinyBlocks_Value
|
||||
* ══════════════════════════════════════════════════════════════════════════ */
|
||||
TinyBlocks_Value tb_value_json(TinyBlocks_JsonValue* j) {
|
||||
TinyBlocks_Value v = { .kind = TB_VALUE_JSON, .as.json = j };
|
||||
if (j) tb_json_retain(j);
|
||||
return v;
|
||||
}
|
||||
|
||||
TinyBlocks_JsonValue* tb_value_as_json(TinyBlocks_Value v) {
|
||||
return v.kind == TB_VALUE_JSON ? v.as.json : NULL;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
#include <stdio.h>
|
||||
#include "tinyblocks/tinyblocks.h"
|
||||
|
||||
/* ── Vtable ──────────────────────────────────────────────────────────────── */
|
||||
static void real_eval(TinyBlocks_Block* self, TinyBlocks* rt)
|
||||
{
|
||||
(void)rt;
|
||||
TinyBlocks_RealBlock* rb = (TinyBlocks_RealBlock*)self;
|
||||
tb_block_set_output(self, 0, tb_value_real(rb->value));
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static void real_destroy(TinyBlocks_Block* self, TinyBlocks* rt)
|
||||
{
|
||||
(void)self; (void)rt;
|
||||
}
|
||||
|
||||
static size_t real_to_string(TinyBlocks_Block* self, TinyBlocks* rt,
|
||||
char* buf, size_t sz)
|
||||
{
|
||||
(void)rt;
|
||||
TinyBlocks_RealBlock* rb = (TinyBlocks_RealBlock*)self;
|
||||
return (size_t)snprintf(buf, sz, "%g", rb->value);
|
||||
}
|
||||
|
||||
static TinyBlocks_Block* real_clone(const TinyBlocks_Block* self, TinyBlocks* rt)
|
||||
{
|
||||
const TinyBlocks_RealBlock* rb = (const TinyBlocks_RealBlock*)self;
|
||||
TinyBlocks_RealBlock* clone = tb_real_block_create(rt, rb->value);
|
||||
if (clone && self->name) clone->base.name = tb_strdup(rt, self->name);
|
||||
return (TinyBlocks_Block*)clone;
|
||||
}
|
||||
|
||||
static const TinyBlocks_BlockVTable s_real_vtable = {
|
||||
.eval = real_eval,
|
||||
.destroy = real_destroy,
|
||||
.to_string = real_to_string,
|
||||
.clone = real_clone,
|
||||
};
|
||||
|
||||
/* ── Ciclo de vida ───────────────────────────────────────────────────────── */
|
||||
TinyBlocks_RealBlock* tb_real_block_create(TinyBlocks* rt, double value)
|
||||
{
|
||||
TinyBlocks_RealBlock* rb = (TinyBlocks_RealBlock*)tb_block_alloc_base(
|
||||
rt, TB_KIND_REAL, sizeof(TinyBlocks_RealBlock),
|
||||
0, 1, &s_real_vtable);
|
||||
if (!rb) return NULL;
|
||||
rb->value = value;
|
||||
return rb;
|
||||
}
|
||||
|
||||
void tb_real_block_destroy(TinyBlocks_RealBlock* rb, TinyBlocks* rt)
|
||||
{
|
||||
tb_block_destroy(&rb->base, rt);
|
||||
}
|
||||
|
||||
/* ── Acesso ──────────────────────────────────────────────────────────────── */
|
||||
double tb_real_block_get(const TinyBlocks_RealBlock* rb)
|
||||
{
|
||||
return rb ? rb->value : 0.0;
|
||||
}
|
||||
|
||||
void tb_real_block_set(TinyBlocks_RealBlock* rb, double value)
|
||||
{
|
||||
if (!rb) return;
|
||||
rb->value = value;
|
||||
rb->base.dirty = true;
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include <string.h>
|
||||
#include <stddef.h>
|
||||
#include "tinyblocks/tb_renderable_block.h"
|
||||
|
||||
/* ── Pool estático — 4 frames em paralelo é suficiente para qualquer backend ─ */
|
||||
#define POOL_CAP 4
|
||||
|
||||
static TinyBlocks_RenderableBlock s_pool[POOL_CAP];
|
||||
static bool s_used[POOL_CAP];
|
||||
|
||||
TinyBlocks_RenderableBlock* tb_renderable_acquire(void) {
|
||||
for (int i = 0; i < POOL_CAP; i++) {
|
||||
if (!s_used[i]) {
|
||||
s_used[i] = true;
|
||||
s_pool[i].count = 0;
|
||||
return &s_pool[i];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void tb_renderable_release(TinyBlocks_RenderableBlock* rb) {
|
||||
for (int i = 0; i < POOL_CAP; i++) {
|
||||
if (&s_pool[i] == rb) { s_used[i] = false; return; }
|
||||
}
|
||||
}
|
||||
|
||||
void tb_renderable_release_all(void) {
|
||||
for (int i = 0; i < POOL_CAP; i++)
|
||||
s_used[i] = false;
|
||||
}
|
||||
|
||||
/* ── Append interno ─────────────────────────────────────────────────────────── */
|
||||
static TinyBlocks_DrawCmd* append(TinyBlocks_RenderableBlock* rb) {
|
||||
if (!rb || rb->count >= TB_DRAW_CMD_CAP) return NULL;
|
||||
TinyBlocks_DrawCmd* c = &rb->cmds[rb->count++];
|
||||
memset(c, 0, sizeof(*c));
|
||||
return c;
|
||||
}
|
||||
|
||||
/* ── Helpers públicos ────────────────────────────────────────────────────────── */
|
||||
void tb_draw_clear(TinyBlocks_RenderableBlock* rb,
|
||||
float r, float g, float b, float a) {
|
||||
TinyBlocks_DrawCmd* c = append(rb); if (!c) return;
|
||||
c->kind = TB_DRAW_CLEAR;
|
||||
c->r=r; c->g=g; c->b=b; c->a=a;
|
||||
}
|
||||
|
||||
void tb_draw_fill_rect(TinyBlocks_RenderableBlock* rb,
|
||||
float x, float y, float w, float h,
|
||||
float r, float g, float b, float a) {
|
||||
TinyBlocks_DrawCmd* c = append(rb); if (!c) return;
|
||||
c->kind = TB_DRAW_FILL_RECT;
|
||||
c->x=x; c->y=y; c->w=w; c->h=h;
|
||||
c->r=r; c->g=g; c->b=b; c->a=a;
|
||||
}
|
||||
|
||||
void tb_draw_fill_rrect(TinyBlocks_RenderableBlock* rb,
|
||||
float x, float y, float w, float h, float radius,
|
||||
float r, float g, float b, float a) {
|
||||
TinyBlocks_DrawCmd* c = append(rb); if (!c) return;
|
||||
c->kind = TB_DRAW_FILL_RRECT;
|
||||
c->x=x; c->y=y; c->w=w; c->h=h; c->param=radius;
|
||||
c->r=r; c->g=g; c->b=b; c->a=a;
|
||||
}
|
||||
|
||||
void tb_draw_fill_circle(TinyBlocks_RenderableBlock* rb,
|
||||
float cx, float cy, float radius,
|
||||
float r, float g, float b, float a) {
|
||||
TinyBlocks_DrawCmd* c = append(rb); if (!c) return;
|
||||
c->kind = TB_DRAW_FILL_CIRCLE;
|
||||
c->x=cx; c->y=cy; c->param=radius;
|
||||
c->r=r; c->g=g; c->b=b; c->a=a;
|
||||
}
|
||||
|
||||
void tb_draw_stroke_line(TinyBlocks_RenderableBlock* rb,
|
||||
float x1, float y1, float x2, float y2, float stroke_w,
|
||||
float r, float g, float b, float a) {
|
||||
TinyBlocks_DrawCmd* c = append(rb); if (!c) return;
|
||||
c->kind = TB_DRAW_STROKE_LINE;
|
||||
c->x=x1; c->y=y1; c->w=x2; c->h=y2; c->param=stroke_w;
|
||||
c->r=r; c->g=g; c->b=b; c->a=a;
|
||||
}
|
||||
|
||||
void tb_draw_stroke_circle(TinyBlocks_RenderableBlock* rb,
|
||||
float cx, float cy, float radius, float stroke_w,
|
||||
float r, float g, float b, float a) {
|
||||
TinyBlocks_DrawCmd* c = append(rb); if (!c) return;
|
||||
c->kind = TB_DRAW_STROKE_CIRCLE;
|
||||
c->x=cx; c->y=cy; c->param=radius; c->w=stroke_w;
|
||||
c->r=r; c->g=g; c->b=b; c->a=a;
|
||||
}
|
||||
|
||||
void tb_draw_stroke_rect(TinyBlocks_RenderableBlock* rb,
|
||||
float x, float y, float w, float h, float stroke_w,
|
||||
float r, float g, float b, float a) {
|
||||
TinyBlocks_DrawCmd* c = append(rb); if (!c) return;
|
||||
c->kind = TB_DRAW_STROKE_RECT;
|
||||
c->x=x; c->y=y; c->w=w; c->h=h; c->param=stroke_w;
|
||||
c->r=r; c->g=g; c->b=b; c->a=a;
|
||||
}
|
||||
|
||||
void tb_draw_set_clip(TinyBlocks_RenderableBlock* rb,
|
||||
float x, float y, float w, float h) {
|
||||
TinyBlocks_DrawCmd* c = append(rb); if (!c) return;
|
||||
c->kind = TB_DRAW_SET_CLIP;
|
||||
c->x=x; c->y=y; c->w=w; c->h=h;
|
||||
}
|
||||
|
||||
void tb_draw_clear_clip(TinyBlocks_RenderableBlock* rb) {
|
||||
TinyBlocks_DrawCmd* c = append(rb); if (!c) return;
|
||||
c->kind = TB_DRAW_CLEAR_CLIP;
|
||||
}
|
||||
|
||||
void tb_draw_text(TinyBlocks_RenderableBlock* rb,
|
||||
float x, float y, const char* text, float font_size,
|
||||
float r, float g, float b, float a) {
|
||||
if (!text || !text[0]) return;
|
||||
TinyBlocks_DrawCmd* c = append(rb); if (!c) return;
|
||||
c->kind = TB_DRAW_TEXT;
|
||||
c->x=x; c->y=y;
|
||||
c->font_size = font_size;
|
||||
c->r=r; c->g=g; c->b=b; c->a=a;
|
||||
strncpy(c->text, text, sizeof(c->text) - 1);
|
||||
}
|
||||
|
||||
void tb_draw_merge_offset(TinyBlocks_RenderableBlock* dst,
|
||||
const TinyBlocks_RenderableBlock* src,
|
||||
float dx, float dy) {
|
||||
if (!dst || !src) return;
|
||||
for (uint32_t i = 0; i < src->count && dst->count < TB_DRAW_CMD_CAP; i++) {
|
||||
TinyBlocks_DrawCmd cmd = src->cmds[i];
|
||||
switch (cmd.kind) {
|
||||
case TB_DRAW_FILL_RECT:
|
||||
case TB_DRAW_FILL_RRECT:
|
||||
case TB_DRAW_STROKE_RECT:
|
||||
case TB_DRAW_SET_CLIP:
|
||||
case TB_DRAW_TEXT:
|
||||
cmd.x += dx; cmd.y += dy;
|
||||
break;
|
||||
case TB_DRAW_FILL_CIRCLE:
|
||||
case TB_DRAW_STROKE_CIRCLE:
|
||||
cmd.x += dx; cmd.y += dy;
|
||||
break;
|
||||
case TB_DRAW_STROKE_LINE:
|
||||
cmd.x += dx; cmd.y += dy;
|
||||
cmd.w += dx; cmd.h += dy;
|
||||
break;
|
||||
case TB_DRAW_CLEAR:
|
||||
case TB_DRAW_CLEAR_CLIP:
|
||||
break;
|
||||
}
|
||||
dst->cmds[dst->count++] = cmd;
|
||||
}
|
||||
}
|
||||
|
||||
float tb_measure_text(const char* text, float font_size) {
|
||||
if (!text) return 0.f;
|
||||
size_t len = 0;
|
||||
for (const char* p = text; *p; p++) {
|
||||
if ((*p & 0xC0) != 0x80) len++; /* conta codepoints UTF-8 */
|
||||
}
|
||||
return (float)len * font_size * 0.55f;
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
#include <string.h>
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "tinyblocks/tinyblocks.h"
|
||||
|
||||
/* ── Vtable ──────────────────────────────────────────────────────────────── */
|
||||
static void text_eval(TinyBlocks_Block* self, TinyBlocks* rt)
|
||||
{
|
||||
(void)rt;
|
||||
TinyBlocks_TextBlock* tb = (TinyBlocks_TextBlock*)self;
|
||||
tb_block_set_output(self, 0, tb_value_text(tb->value));
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static void text_destroy(TinyBlocks_Block* self, TinyBlocks* rt)
|
||||
{
|
||||
TinyBlocks_TextBlock* tb = (TinyBlocks_TextBlock*)self;
|
||||
tb_free(rt, tb->value);
|
||||
}
|
||||
|
||||
static size_t text_to_string(TinyBlocks_Block* self, TinyBlocks* rt,
|
||||
char* buf, size_t sz)
|
||||
{
|
||||
(void)rt;
|
||||
TinyBlocks_TextBlock* tb = (TinyBlocks_TextBlock*)self;
|
||||
return (size_t)snprintf(buf, sz, "\"%s\"", tb->value ? tb->value : "");
|
||||
}
|
||||
|
||||
static TinyBlocks_Block* text_clone(const TinyBlocks_Block* self, TinyBlocks* rt)
|
||||
{
|
||||
const TinyBlocks_TextBlock* tb = (const TinyBlocks_TextBlock*)self;
|
||||
TinyBlocks_TextBlock* clone = tb_text_block_create(rt, tb->value);
|
||||
if (clone && self->name) clone->base.name = tb_strdup(rt, self->name);
|
||||
return (TinyBlocks_Block*)clone;
|
||||
}
|
||||
|
||||
static const TinyBlocks_BlockVTable s_text_vtable = {
|
||||
.eval = text_eval,
|
||||
.destroy = text_destroy,
|
||||
.to_string = text_to_string,
|
||||
.clone = text_clone,
|
||||
};
|
||||
|
||||
/* ── Ciclo de vida ───────────────────────────────────────────────────────── */
|
||||
TinyBlocks_TextBlock* tb_text_block_create(TinyBlocks* rt, const char* value)
|
||||
{
|
||||
TinyBlocks_TextBlock* tb = (TinyBlocks_TextBlock*)tb_block_alloc_base(
|
||||
rt, TB_KIND_TEXT, sizeof(TinyBlocks_TextBlock),
|
||||
0, 1, &s_text_vtable);
|
||||
if (!tb) return NULL;
|
||||
tb->value = tb_strdup(rt, value ? value : "");
|
||||
return tb;
|
||||
}
|
||||
|
||||
void tb_text_block_destroy(TinyBlocks_TextBlock* tb, TinyBlocks* rt)
|
||||
{
|
||||
tb_block_destroy(&tb->base, rt);
|
||||
}
|
||||
|
||||
/* ── Acesso ──────────────────────────────────────────────────────────────── */
|
||||
const char* tb_text_block_get(const TinyBlocks_TextBlock* tb)
|
||||
{
|
||||
return tb ? tb->value : NULL;
|
||||
}
|
||||
|
||||
void tb_text_block_set(TinyBlocks_TextBlock* tb, const char* value)
|
||||
{
|
||||
if (!tb) return;
|
||||
/* Usa realloc para evitar precisar de rt aqui */
|
||||
char* copy = value ? strdup(value) : strdup("");
|
||||
free(tb->value);
|
||||
tb->value = copy;
|
||||
tb->base.dirty = true;
|
||||
}
|
||||
|
||||
void tb_text_block_append(TinyBlocks_TextBlock* tb, const char* suffix)
|
||||
{
|
||||
if (!tb || !suffix) return;
|
||||
size_t old_len = tb->value ? strlen(tb->value) : 0;
|
||||
size_t suf_len = strlen(suffix);
|
||||
char* buf = realloc(tb->value, old_len + suf_len + 1);
|
||||
if (!buf) return;
|
||||
memcpy(buf + old_len, suffix, suf_len + 1);
|
||||
tb->value = buf;
|
||||
tb->base.dirty = true;
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include "tinyblocks/tinyblocks.h"
|
||||
|
||||
/* ── Vtable de TypeBlock ─────────────────────────────────────────────────── */
|
||||
static void type_eval(TinyBlocks_Block* self, TinyBlocks* rt)
|
||||
{
|
||||
/* Um TypeBlock em si não avalia; serve como fábrica/descritor */
|
||||
(void)self; (void)rt;
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static void type_destroy(TinyBlocks_Block* self, TinyBlocks* rt)
|
||||
{
|
||||
TinyBlocks_TypeBlock* tb = (TinyBlocks_TypeBlock*)self;
|
||||
tb_rt_unregister_type(rt, tb);
|
||||
tb_free(rt, tb->type_name);
|
||||
for (uint32_t i = 0; i < tb->input_desc_count; ++i)
|
||||
tb_free(rt, (void*)tb->input_descs[i].name);
|
||||
tb_free(rt, tb->input_descs);
|
||||
for (uint32_t i = 0; i < tb->output_desc_count; ++i)
|
||||
tb_free(rt, (void*)tb->output_descs[i].name);
|
||||
tb_free(rt, tb->output_descs);
|
||||
}
|
||||
|
||||
static size_t type_to_string(TinyBlocks_Block* self, TinyBlocks* rt,
|
||||
char* buf, size_t sz)
|
||||
{
|
||||
(void)rt;
|
||||
TinyBlocks_TypeBlock* tb = (TinyBlocks_TypeBlock*)self;
|
||||
return (size_t)snprintf(buf, sz, "[Type:%s]",
|
||||
tb->type_name ? tb->type_name : "?");
|
||||
}
|
||||
|
||||
static TinyBlocks_Block* type_clone(const TinyBlocks_Block* self, TinyBlocks* rt)
|
||||
{
|
||||
const TinyBlocks_TypeBlock* src = (const TinyBlocks_TypeBlock*)self;
|
||||
return (TinyBlocks_Block*)tb_type_block_create(rt, src->type_name,
|
||||
src->input_descs, src->input_desc_count,
|
||||
src->output_descs, src->output_desc_count,
|
||||
src->eval_fn);
|
||||
}
|
||||
|
||||
static const TinyBlocks_BlockVTable s_type_vtable = {
|
||||
.eval = type_eval,
|
||||
.destroy = type_destroy,
|
||||
.to_string = type_to_string,
|
||||
.clone = type_clone,
|
||||
};
|
||||
|
||||
/* ── Criação ─────────────────────────────────────────────────────────────── */
|
||||
TinyBlocks_TypeBlock* tb_type_block_create(
|
||||
TinyBlocks* rt,
|
||||
const char* type_name,
|
||||
const TinyBlocks_SlotDesc* input_descs, uint32_t input_count,
|
||||
const TinyBlocks_SlotDesc* output_descs, uint32_t output_count,
|
||||
void (*eval_fn)(TinyBlocks_Block* self, TinyBlocks* rt))
|
||||
{
|
||||
TinyBlocks_TypeBlock* tb = (TinyBlocks_TypeBlock*)tb_block_alloc_base(
|
||||
rt, TB_KIND_TYPE, sizeof(TinyBlocks_TypeBlock),
|
||||
0, 0, &s_type_vtable);
|
||||
if (!tb) return NULL;
|
||||
|
||||
tb->type_name = tb_strdup(rt, type_name);
|
||||
tb->eval_fn = eval_fn;
|
||||
tb->input_desc_count = input_count;
|
||||
tb->output_desc_count = output_count;
|
||||
|
||||
if (input_count > 0) {
|
||||
tb->input_descs = tb_alloc(rt, input_count * sizeof(TinyBlocks_SlotDesc));
|
||||
if (!tb->input_descs) goto fail;
|
||||
memcpy(tb->input_descs, input_descs, input_count * sizeof(TinyBlocks_SlotDesc));
|
||||
/* Duplica os nomes dos slots */
|
||||
for (uint32_t i = 0; i < input_count; ++i)
|
||||
tb->input_descs[i].name = tb_strdup(rt, input_descs[i].name);
|
||||
}
|
||||
if (output_count > 0) {
|
||||
tb->output_descs = tb_alloc(rt, output_count * sizeof(TinyBlocks_SlotDesc));
|
||||
if (!tb->output_descs) goto fail;
|
||||
memcpy(tb->output_descs, output_descs, output_count * sizeof(TinyBlocks_SlotDesc));
|
||||
for (uint32_t i = 0; i < output_count; ++i)
|
||||
tb->output_descs[i].name = tb_strdup(rt, output_descs[i].name);
|
||||
}
|
||||
|
||||
tb_rt_register_type(rt, tb);
|
||||
return tb;
|
||||
|
||||
fail:
|
||||
tb_block_destroy(&tb->base, rt);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void tb_type_block_destroy(TinyBlocks_TypeBlock* tb, TinyBlocks* rt)
|
||||
{
|
||||
tb_block_destroy(&tb->base, rt);
|
||||
}
|
||||
|
||||
void tb_type_block_set_instance_hooks(
|
||||
TinyBlocks_TypeBlock* tb,
|
||||
size_t instance_userdata_size,
|
||||
bool (*init )(TinyBlocks_Block* self, TinyBlocks* rt),
|
||||
void (*deinit)(TinyBlocks_Block* self, TinyBlocks* rt))
|
||||
{
|
||||
if (!tb) return;
|
||||
tb->instance_userdata_size = instance_userdata_size;
|
||||
tb->instance_init = init;
|
||||
tb->instance_deinit = deinit;
|
||||
}
|
||||
|
||||
TinyBlocks_TypeBlock* tb_type_block_find(TinyBlocks* rt, const char* type_name)
|
||||
{
|
||||
return tb_rt_find_type(rt, type_name);
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "tinyblocks/tinyblocks.h"
|
||||
|
||||
TinyBlocks_Value tb_value_none(void)
|
||||
{
|
||||
TinyBlocks_Value v = { .kind = TB_VALUE_NONE };
|
||||
return v;
|
||||
}
|
||||
|
||||
TinyBlocks_Value tb_value_integer(int64_t n)
|
||||
{
|
||||
TinyBlocks_Value v = { .kind = TB_VALUE_INTEGER, .as.integer = n };
|
||||
return v;
|
||||
}
|
||||
|
||||
TinyBlocks_Value tb_value_real(double d)
|
||||
{
|
||||
TinyBlocks_Value v = { .kind = TB_VALUE_REAL, .as.real = d };
|
||||
return v;
|
||||
}
|
||||
|
||||
TinyBlocks_Value tb_value_text(const char* str)
|
||||
{
|
||||
TinyBlocks_Value v = { .kind = TB_VALUE_TEXT };
|
||||
v.as.text = str ? strdup(str) : NULL;
|
||||
return v;
|
||||
}
|
||||
|
||||
TinyBlocks_Value tb_value_block(TinyBlocks_Block* block)
|
||||
{
|
||||
TinyBlocks_Value v = { .kind = TB_VALUE_BLOCK, .as.block = block };
|
||||
return v;
|
||||
}
|
||||
|
||||
TinyBlocks_Value tb_value_clone(TinyBlocks_Value v)
|
||||
{
|
||||
if (v.kind == TB_VALUE_TEXT && v.as.text)
|
||||
return tb_value_text(v.as.text);
|
||||
if (v.kind == TB_VALUE_VECTOR && v.as.vector)
|
||||
tb_vector_data_retain(v.as.vector);
|
||||
if (v.kind == TB_VALUE_JSON && v.as.json)
|
||||
tb_json_retain(v.as.json);
|
||||
return v;
|
||||
}
|
||||
|
||||
void tb_value_free(TinyBlocks_Value* v)
|
||||
{
|
||||
if (!v) return;
|
||||
if (v->kind == TB_VALUE_TEXT) {
|
||||
free(v->as.text);
|
||||
v->as.text = NULL;
|
||||
} else if (v->kind == TB_VALUE_VECTOR) {
|
||||
tb_vector_data_release(v->as.vector);
|
||||
v->as.vector = NULL;
|
||||
} else if (v->kind == TB_VALUE_JSON) {
|
||||
tb_json_release(v->as.json);
|
||||
v->as.json = NULL;
|
||||
}
|
||||
v->kind = TB_VALUE_NONE;
|
||||
}
|
||||
@@ -0,0 +1,190 @@
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include "tinyblocks/tinyblocks.h"
|
||||
|
||||
/* ══════════════════════════════════════════════════════════════════════════
|
||||
* TinyBlocks_VectorData — buffer de comandos ref-counted
|
||||
* ══════════════════════════════════════════════════════════════════════════ */
|
||||
TinyBlocks_VectorData* tb_vector_data_create(void) {
|
||||
TinyBlocks_VectorData* v = malloc(sizeof(TinyBlocks_VectorData));
|
||||
if (!v) return NULL;
|
||||
v->capacity = 16;
|
||||
v->count = 0;
|
||||
v->ref = 1;
|
||||
v->cmds = malloc(v->capacity * sizeof(TinyBlocks_VCmd));
|
||||
if (!v->cmds) { free(v); return NULL; }
|
||||
return v;
|
||||
}
|
||||
|
||||
void tb_vector_data_retain(TinyBlocks_VectorData* v) {
|
||||
if (v) v->ref++;
|
||||
}
|
||||
|
||||
void tb_vector_data_release(TinyBlocks_VectorData* v) {
|
||||
if (!v) return;
|
||||
if (--v->ref <= 0) { free(v->cmds); free(v); }
|
||||
}
|
||||
|
||||
TinyBlocks_VectorData* tb_vector_data_clone(const TinyBlocks_VectorData* src) {
|
||||
if (!src) return NULL;
|
||||
TinyBlocks_VectorData* v = tb_vector_data_create();
|
||||
if (!v) return NULL;
|
||||
for (uint32_t i = 0; i < src->count; i++)
|
||||
tb_vector_data_push(v, src->cmds[i]);
|
||||
return v;
|
||||
}
|
||||
|
||||
bool tb_vector_data_push(TinyBlocks_VectorData* v, TinyBlocks_VCmd cmd) {
|
||||
if (!v) return false;
|
||||
if (v->count >= v->capacity) {
|
||||
uint32_t nc = v->capacity * 2;
|
||||
TinyBlocks_VCmd* a = realloc(v->cmds, nc * sizeof(TinyBlocks_VCmd));
|
||||
if (!a) return false;
|
||||
v->cmds = a; v->capacity = nc;
|
||||
}
|
||||
v->cmds[v->count++] = cmd;
|
||||
return true;
|
||||
}
|
||||
|
||||
/* ── Construtores de conveniência ────────────────────────────────────────── */
|
||||
#define PUSH0(v,k) tb_vector_data_push(v, (TinyBlocks_VCmd){(k), {0}})
|
||||
#define PUSH2(v,k,a,b) tb_vector_data_push(v, (TinyBlocks_VCmd){(k), {(a),(b)}})
|
||||
#define PUSH4(v,k,a,b,c,d) tb_vector_data_push(v,(TinyBlocks_VCmd){(k),{(a),(b),(c),(d)}})
|
||||
#define PUSH5(v,k,a,b,c,d,e) tb_vector_data_push(v,(TinyBlocks_VCmd){(k),{(a),(b),(c),(d),(e)}})
|
||||
|
||||
bool tb_vector_begin_path(TinyBlocks_VectorData* v) { return PUSH0(v, TB_VCMD_BEGIN_PATH); }
|
||||
bool tb_vector_move_to (TinyBlocks_VectorData* v, float x, float y) { return PUSH2(v, TB_VCMD_MOVE_TO, x,y); }
|
||||
bool tb_vector_line_to (TinyBlocks_VectorData* v, float x, float y) { return PUSH2(v, TB_VCMD_LINE_TO, x,y); }
|
||||
bool tb_vector_close_path(TinyBlocks_VectorData* v) { return PUSH0(v, TB_VCMD_CLOSE_PATH); }
|
||||
|
||||
bool tb_vector_stroke(TinyBlocks_VectorData* v, float r, float g, float b, float a, float w) {
|
||||
return PUSH5(v, TB_VCMD_STROKE, r, g, b, a, w);
|
||||
}
|
||||
bool tb_vector_fill(TinyBlocks_VectorData* v, float r, float g, float b, float a) {
|
||||
return PUSH4(v, TB_VCMD_FILL, r, g, b, a);
|
||||
}
|
||||
bool tb_vector_rect(TinyBlocks_VectorData* v, float x, float y, float w, float h) {
|
||||
return PUSH4(v, TB_VCMD_RECT, x, y, w, h);
|
||||
}
|
||||
bool tb_vector_circle(TinyBlocks_VectorData* v, float cx, float cy, float radius) {
|
||||
return PUSH5(v, TB_VCMD_CIRCLE, cx, cy, radius, 0, 0);
|
||||
}
|
||||
|
||||
#undef PUSH0
|
||||
#undef PUSH2
|
||||
#undef PUSH4
|
||||
#undef PUSH5
|
||||
|
||||
/* ── Value helpers ───────────────────────────────────────────────────────── */
|
||||
TinyBlocks_Value tb_value_vector(TinyBlocks_VectorData* v) {
|
||||
TinyBlocks_Value val;
|
||||
val.kind = TB_VALUE_VECTOR;
|
||||
val.as.vector = v;
|
||||
if (v) v->ref++; /* tb_value retém a referência */
|
||||
return val;
|
||||
}
|
||||
|
||||
TinyBlocks_VectorData* tb_value_as_vector(TinyBlocks_Value v) {
|
||||
return v.kind == TB_VALUE_VECTOR ? v.as.vector : NULL;
|
||||
}
|
||||
|
||||
/* ── RenderableBlock play ────────────────────────────────────────────────── */
|
||||
void tb_draw_ctx_play(TinyBlocks_RenderableBlock* rb, TinyBlocks_VectorData* v) {
|
||||
if (!rb || !v) return;
|
||||
float px = 0.f, py = 0.f; /* ponto atual do path */
|
||||
float sx = 0.f, sy = 0.f; /* início do path (close_path) */
|
||||
float fr = 1.f, fg = 1.f, fb = 1.f, fa = 1.f; /* cor de fill/stroke atual */
|
||||
for (uint32_t i = 0; i < v->count; i++) {
|
||||
TinyBlocks_VCmd* c = &v->cmds[i];
|
||||
switch (c->kind) {
|
||||
case TB_VCMD_BEGIN_PATH:
|
||||
break;
|
||||
case TB_VCMD_MOVE_TO:
|
||||
px = sx = c->args[0]; py = sy = c->args[1]; break;
|
||||
case TB_VCMD_LINE_TO:
|
||||
tb_draw_stroke_line(rb, px, py, c->args[0], c->args[1], 1.f, fr, fg, fb, fa);
|
||||
px = c->args[0]; py = c->args[1];
|
||||
break;
|
||||
case TB_VCMD_CLOSE_PATH:
|
||||
tb_draw_stroke_line(rb, px, py, sx, sy, 1.f, fr, fg, fb, fa);
|
||||
px = sx; py = sy;
|
||||
break;
|
||||
case TB_VCMD_STROKE:
|
||||
fr = c->args[0]; fg = c->args[1]; fb = c->args[2]; fa = c->args[3];
|
||||
break;
|
||||
case TB_VCMD_FILL:
|
||||
fr = c->args[0]; fg = c->args[1]; fb = c->args[2]; fa = c->args[3];
|
||||
break;
|
||||
case TB_VCMD_RECT:
|
||||
tb_draw_fill_rect(rb, c->args[0], c->args[1], c->args[2], c->args[3],
|
||||
fr, fg, fb, fa);
|
||||
break;
|
||||
case TB_VCMD_CIRCLE:
|
||||
tb_draw_fill_circle(rb, c->args[0], c->args[1], c->args[2], fr, fg, fb, fa);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ══════════════════════════════════════════════════════════════════════════
|
||||
* TinyBlocks_VectorBlock — block kind TB_KIND_VECTOR
|
||||
* ══════════════════════════════════════════════════════════════════════════ */
|
||||
static void vec_eval(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
(void)rt;
|
||||
TinyBlocks_VectorBlock* vb = (TinyBlocks_VectorBlock*)self;
|
||||
/* Coloca o VectorData no output[0] como valor (com retain) */
|
||||
if (self->output_count > 0)
|
||||
tb_block_set_output(self, 0, tb_value_vector(vb->data));
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static void vec_destroy(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
(void)rt;
|
||||
TinyBlocks_VectorBlock* vb = (TinyBlocks_VectorBlock*)self;
|
||||
tb_vector_data_release(vb->data);
|
||||
vb->data = NULL;
|
||||
}
|
||||
|
||||
static size_t vec_to_string(TinyBlocks_Block* self, TinyBlocks* rt,
|
||||
char* buf, size_t sz) {
|
||||
(void)rt;
|
||||
TinyBlocks_VectorBlock* vb = (TinyBlocks_VectorBlock*)self;
|
||||
return (size_t)snprintf(buf, sz, "[Vector %u cmds]",
|
||||
vb->data ? vb->data->count : 0u);
|
||||
}
|
||||
|
||||
static TinyBlocks_Block* vec_clone(const TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
TinyBlocks_VectorBlock* src = (TinyBlocks_VectorBlock*)self;
|
||||
TinyBlocks_VectorBlock* dst = tb_vector_block_create(rt);
|
||||
if (!dst) return NULL;
|
||||
if (src->data) {
|
||||
dst->data = tb_vector_data_clone(src->data);
|
||||
}
|
||||
return &dst->base;
|
||||
}
|
||||
|
||||
static const TinyBlocks_BlockVTable s_vec_vtable = {
|
||||
.eval = vec_eval,
|
||||
.destroy = vec_destroy,
|
||||
.to_string = vec_to_string,
|
||||
.clone = vec_clone,
|
||||
};
|
||||
|
||||
TinyBlocks_VectorBlock* tb_vector_block_create(TinyBlocks* rt) {
|
||||
TinyBlocks_VectorBlock* b = (TinyBlocks_VectorBlock*)tb_block_alloc_base(
|
||||
rt, TB_KIND_VECTOR, sizeof(TinyBlocks_VectorBlock),
|
||||
0, 1, &s_vec_vtable);
|
||||
if (!b) return NULL;
|
||||
b->data = tb_vector_data_create();
|
||||
return b;
|
||||
}
|
||||
|
||||
void tb_vector_block_set_data(TinyBlocks_VectorBlock* b, TinyBlocks_VectorData* v) {
|
||||
if (!b) return;
|
||||
tb_vector_data_release(b->data);
|
||||
b->data = v;
|
||||
if (v) v->ref++;
|
||||
b->base.dirty = true;
|
||||
}
|
||||
@@ -0,0 +1,240 @@
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include "tinyblocks/tinyblocks.h"
|
||||
|
||||
/* ── Alocador padrão ─────────────────────────────────────────────────────── */
|
||||
static void* default_alloc(size_t size, void* ud) { (void)ud; return malloc(size); }
|
||||
static void default_free (void* ptr, void* ud) { (void)ud; free(ptr); }
|
||||
|
||||
/* ── Inicialização ───────────────────────────────────────────────────────── */
|
||||
bool tinyblocks_init(TinyBlocks** out_rt, const TinyBlocks_InitOptions* opts)
|
||||
{
|
||||
if (!out_rt) return false;
|
||||
|
||||
TinyBlocks_AllocFn alloc_fn = opts && opts->alloc ? opts->alloc : default_alloc;
|
||||
TinyBlocks_FreeFn free_fn = opts && opts->free_fn ? opts->free_fn : default_free;
|
||||
void* alloc_ud = opts ? opts->alloc_userdata : NULL;
|
||||
|
||||
TinyBlocks* rt = alloc_fn(sizeof(TinyBlocks), alloc_ud);
|
||||
if (!rt) return false;
|
||||
memset(rt, 0, sizeof(TinyBlocks));
|
||||
|
||||
rt->alloc = alloc_fn;
|
||||
rt->free = free_fn;
|
||||
rt->alloc_userdata = alloc_ud;
|
||||
rt->next_id = 1;
|
||||
|
||||
uint32_t bcap = (opts && opts->initial_block_cap) ? opts->initial_block_cap : 64;
|
||||
uint32_t tcap = (opts && opts->initial_type_cap) ? opts->initial_type_cap : 32;
|
||||
uint32_t lcap = (opts && opts->initial_library_cap) ? opts->initial_library_cap : 16;
|
||||
|
||||
rt->blocks = alloc_fn(bcap * sizeof(TinyBlocks_Block*), alloc_ud);
|
||||
rt->types = alloc_fn(tcap * sizeof(TinyBlocks_TypeBlock*), alloc_ud);
|
||||
rt->libraries = alloc_fn(lcap * sizeof(TinyBlocks_LibraryBlock*), alloc_ud);
|
||||
rt->dl_handles= alloc_fn(lcap * sizeof(void*), alloc_ud);
|
||||
|
||||
if (!rt->blocks || !rt->types || !rt->libraries || !rt->dl_handles) {
|
||||
if (rt->blocks) free_fn(rt->blocks, alloc_ud);
|
||||
if (rt->types) free_fn(rt->types, alloc_ud);
|
||||
if (rt->libraries) free_fn(rt->libraries, alloc_ud);
|
||||
if (rt->dl_handles)free_fn(rt->dl_handles,alloc_ud);
|
||||
free_fn(rt, alloc_ud);
|
||||
return false;
|
||||
}
|
||||
|
||||
rt->block_capacity = bcap;
|
||||
rt->type_capacity = tcap;
|
||||
rt->library_capacity = lcap;
|
||||
rt->dl_handle_capacity = lcap;
|
||||
|
||||
*out_rt = rt;
|
||||
return true;
|
||||
}
|
||||
|
||||
/* ── Desinicialização ────────────────────────────────────────────────────── */
|
||||
void tinyblocks_deinit(TinyBlocks** rt_ptr)
|
||||
{
|
||||
if (!rt_ptr || !*rt_ptr) return;
|
||||
TinyBlocks* rt = *rt_ptr;
|
||||
|
||||
/* Fase 1: destruir instâncias (KB_KIND_BLOCK) primeiro.
|
||||
Elas referenciam TypeBlocks via ->type; devem ser liberadas antes
|
||||
das libraries que contêm esses TypeBlocks. */
|
||||
{
|
||||
uint32_t i = rt->block_count;
|
||||
while (i > 0) {
|
||||
TinyBlocks_Block* b = rt->blocks[i - 1];
|
||||
if (b->kind == TB_KIND_BLOCK) {
|
||||
tb_block_destroy(b, rt); /* remove de rt->blocks e libera */
|
||||
i = rt->block_count; /* reinicia: o array mudou de tamanho */
|
||||
} else {
|
||||
--i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Fase 2: descarregar libraries (invoca on_unload e destrói TypeBlocks). */
|
||||
while (rt->library_count > 0) {
|
||||
TinyBlocks_LibraryBlock* lib = rt->libraries[rt->library_count - 1];
|
||||
if (lib->on_unload) lib->on_unload(lib, rt);
|
||||
tb_library_block_destroy(lib, rt);
|
||||
}
|
||||
|
||||
/* Fase 3: TypeBlocks e primitivos órfãos (não pertencentes a nenhuma library). */
|
||||
while (rt->block_count > 0) {
|
||||
TinyBlocks_Block* b = rt->blocks[rt->block_count - 1];
|
||||
tb_block_destroy(b, rt);
|
||||
}
|
||||
|
||||
#ifdef __linux__
|
||||
/* Fecha dl handles (apenas no Linux por enquanto) */
|
||||
for (uint32_t i = 0; i < rt->dl_handle_count; ++i) {
|
||||
extern int dlclose(void*);
|
||||
dlclose(rt->dl_handles[i]);
|
||||
}
|
||||
#endif
|
||||
|
||||
TinyBlocks_FreeFn free_fn = rt->free;
|
||||
void* alloc_ud = rt->alloc_userdata;
|
||||
|
||||
free_fn(rt->blocks, alloc_ud);
|
||||
free_fn(rt->types, alloc_ud);
|
||||
free_fn(rt->libraries, alloc_ud);
|
||||
free_fn(rt->dl_handles, alloc_ud);
|
||||
free_fn(rt, alloc_ud);
|
||||
|
||||
*rt_ptr = NULL;
|
||||
}
|
||||
|
||||
/* ── Alocação ────────────────────────────────────────────────────────────── */
|
||||
void* tb_alloc(TinyBlocks* rt, size_t size)
|
||||
{
|
||||
void* p = rt->alloc(size, rt->alloc_userdata);
|
||||
if (p) memset(p, 0, size);
|
||||
return p;
|
||||
}
|
||||
|
||||
void tb_free(TinyBlocks* rt, void* ptr)
|
||||
{
|
||||
if (ptr) rt->free(ptr, rt->alloc_userdata);
|
||||
}
|
||||
|
||||
char* tb_strdup(TinyBlocks* rt, const char* str)
|
||||
{
|
||||
if (!str) return NULL;
|
||||
size_t n = strlen(str) + 1;
|
||||
char* p = rt->alloc(n, rt->alloc_userdata);
|
||||
if (p) memcpy(p, str, n);
|
||||
return p;
|
||||
}
|
||||
|
||||
/* ── Geração de ID ───────────────────────────────────────────────────────── */
|
||||
uint64_t tb_rt_next_id(TinyBlocks* rt) { return rt->next_id++; }
|
||||
|
||||
/* ── Registro de blocos ──────────────────────────────────────────────────── */
|
||||
static bool grow_array(TinyBlocks* rt, void*** arr, uint32_t* cap, uint32_t count)
|
||||
{
|
||||
if (count < *cap) return true;
|
||||
uint32_t new_cap = (*cap) * 2;
|
||||
void** new_arr = rt->alloc(new_cap * sizeof(void*), rt->alloc_userdata);
|
||||
if (!new_arr) return false;
|
||||
memcpy(new_arr, *arr, count * sizeof(void*));
|
||||
rt->free(*arr, rt->alloc_userdata);
|
||||
*arr = new_arr;
|
||||
*cap = new_cap;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool tb_rt_register_block(TinyBlocks* rt, TinyBlocks_Block* block)
|
||||
{
|
||||
if (!grow_array(rt, (void***)&rt->blocks, &rt->block_capacity, rt->block_count))
|
||||
return false;
|
||||
rt->blocks[rt->block_count++] = block;
|
||||
return true;
|
||||
}
|
||||
|
||||
void tb_rt_unregister_block(TinyBlocks* rt, TinyBlocks_Block* block)
|
||||
{
|
||||
for (uint32_t i = 0; i < rt->block_count; ++i) {
|
||||
if (rt->blocks[i] == block) {
|
||||
rt->blocks[i] = rt->blocks[--rt->block_count];
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool tb_rt_register_type(TinyBlocks* rt, TinyBlocks_TypeBlock* type)
|
||||
{
|
||||
if (!grow_array(rt, (void***)&rt->types, &rt->type_capacity, rt->type_count))
|
||||
return false;
|
||||
rt->types[rt->type_count++] = type;
|
||||
return true;
|
||||
}
|
||||
|
||||
void tb_rt_unregister_type(TinyBlocks* rt, TinyBlocks_TypeBlock* type)
|
||||
{
|
||||
for (uint32_t i = 0; i < rt->type_count; ++i) {
|
||||
if (rt->types[i] == type) {
|
||||
rt->types[i] = rt->types[--rt->type_count];
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool tb_rt_register_library(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib)
|
||||
{
|
||||
if (!grow_array(rt, (void***)&rt->libraries, &rt->library_capacity, rt->library_count))
|
||||
return false;
|
||||
rt->libraries[rt->library_count++] = lib;
|
||||
return true;
|
||||
}
|
||||
|
||||
void tb_rt_unregister_library(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib)
|
||||
{
|
||||
for (uint32_t i = 0; i < rt->library_count; ++i) {
|
||||
if (rt->libraries[i] == lib) {
|
||||
rt->libraries[i] = rt->libraries[--rt->library_count];
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ── Busca ───────────────────────────────────────────────────────────────── */
|
||||
TinyBlocks_Block* tb_rt_find_block(TinyBlocks* rt, uint64_t id)
|
||||
{
|
||||
for (uint32_t i = 0; i < rt->block_count; ++i)
|
||||
if (rt->blocks[i]->id == id) return rt->blocks[i];
|
||||
return NULL;
|
||||
}
|
||||
|
||||
TinyBlocks_TypeBlock* tb_rt_find_type(TinyBlocks* rt, const char* name)
|
||||
{
|
||||
for (uint32_t i = 0; i < rt->type_count; ++i)
|
||||
if (rt->types[i]->type_name && strcmp(rt->types[i]->type_name, name) == 0)
|
||||
return rt->types[i];
|
||||
return NULL;
|
||||
}
|
||||
|
||||
TinyBlocks_LibraryBlock* tb_rt_find_library(TinyBlocks* rt, const char* name)
|
||||
{
|
||||
for (uint32_t i = 0; i < rt->library_count; ++i)
|
||||
if (rt->libraries[i]->base.name &&
|
||||
strcmp(rt->libraries[i]->base.name, name) == 0)
|
||||
return rt->libraries[i];
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* ── Erros ───────────────────────────────────────────────────────────────── */
|
||||
const char* tinyblocks_last_error(TinyBlocks* rt) { return rt->error; }
|
||||
void tinyblocks_clear_error(TinyBlocks* rt) { rt->error[0] = '\0'; }
|
||||
|
||||
void tinyblocks_set_error(TinyBlocks* rt, const char* fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
vsnprintf(rt->error, sizeof(rt->error), fmt, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
Reference in new issue
Block a user