Erros como valores e library tinyblocks-errors (categoria Erros)
Core:
- Um erro levantado numa entrada impede o bloco de rodar: o erro sobe
como uma exceção até quem trata.
- TypeBlock.lazy_inputs: o bloco avalia as próprias entradas (para ver o
erro de dentro).
- tinyblocks_caught_error / tinyblocks_set_caught_error: o erro em
tratamento ("erro capturado").
tinyblocks-errors:
- Resultados no estilo do Result do Rust ({"ok": v} / {"erro": msg}):
tente obter, Ok, Erro, é ok?, é erro?, valor de ( ) ou ( ),
desembrulhe, mensagem do erro.
- Tipos de "tente / se der erro" e "tente / se der erro / finalmente"
(executados por quem roda os scripts), "lance o erro" e "erro capturado".
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0103f39ffe
commit
1b39c6892d
8 files changed
+378
-5
No files matched your search
@@ -43,6 +43,11 @@ struct TinyBlocks_TypeBlock {
|
|||||||
* Para blocos de cena (ex: Editor.Scene): coordenadas absolutas de tela. */
|
* Para blocos de cena (ex: Editor.Scene): coordenadas absolutas de tela. */
|
||||||
bool (*on_draw)(TinyBlocks_Block* self, TinyBlocks* rt,
|
bool (*on_draw)(TinyBlocks_Block* self, TinyBlocks* rt,
|
||||||
TinyBlocks_RenderableBlock** dst);
|
TinyBlocks_RenderableBlock** dst);
|
||||||
|
|
||||||
|
/* true = as entradas NAO sao avaliadas antes do eval_fn: o bloco as avalia
|
||||||
|
(tb_block_get_input) quando e se quiser. Ex.: "tente obter", que
|
||||||
|
precisa ver o erro de dentro para transforma-lo em valor. */
|
||||||
|
bool lazy_inputs;
|
||||||
};
|
};
|
||||||
|
|
||||||
/* -- Criação de TypeBlock --- */
|
/* -- Criação de TypeBlock --- */
|
||||||
|
|||||||
@@ -70,6 +70,9 @@ struct TinyBlocks {
|
|||||||
|
|
||||||
/* Último erro em texto legível */
|
/* Último erro em texto legível */
|
||||||
char error[512];
|
char error[512];
|
||||||
|
|
||||||
|
/* Erro tratado agora por um "tente / se der erro" (bloco "erro capturado") */
|
||||||
|
char caught[512];
|
||||||
};
|
};
|
||||||
|
|
||||||
/* -- Opções de inicialização --- */
|
/* -- Opções de inicialização --- */
|
||||||
@@ -103,6 +106,9 @@ TB_API char* tb_strdup(TinyBlocks* rt, const char* str);
|
|||||||
TB_API const char* tinyblocks_last_error(TinyBlocks* rt);
|
TB_API const char* tinyblocks_last_error(TinyBlocks* rt);
|
||||||
TB_API void tinyblocks_set_error (TinyBlocks* rt, const char* fmt, ...);
|
TB_API void tinyblocks_set_error (TinyBlocks* rt, const char* fmt, ...);
|
||||||
TB_API void tinyblocks_clear_error(TinyBlocks* rt);
|
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 --- */
|
/* -- Busca no registro --- */
|
||||||
TB_API TinyBlocks_Block* tb_rt_find_block (TinyBlocks* rt, uint64_t id);
|
TB_API TinyBlocks_Block* tb_rt_find_block (TinyBlocks* rt, uint64_t id);
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
# Makefile da library errors (categoria "Erros")
|
||||||
|
# Constroi build/libtinyblocks-errors.a e build/libtinyblocks-errors.so
|
||||||
|
CC := gcc
|
||||||
|
BUILD := build
|
||||||
|
|
||||||
|
CFLAGS := -std=c11 -Wall -Wextra -pedantic -fvisibility=hidden \
|
||||||
|
-I../../include -Iinclude
|
||||||
|
|
||||||
|
OBJ := $(BUILD)/tb_errors.o
|
||||||
|
LIB_A := $(BUILD)/libtinyblocks-errors.a
|
||||||
|
LIB_SO := $(BUILD)/libtinyblocks-errors.so
|
||||||
|
|
||||||
|
.PHONY: all static shared clean
|
||||||
|
all: static shared
|
||||||
|
static: $(LIB_A)
|
||||||
|
shared: $(LIB_SO)
|
||||||
|
|
||||||
|
$(OBJ): src/tb_errors.c include/tb_errors.h
|
||||||
|
@mkdir -p $(BUILD)
|
||||||
|
$(CC) $(CFLAGS) -fPIC -c $< -o $@
|
||||||
|
|
||||||
|
$(LIB_A): $(OBJ)
|
||||||
|
ar rcs $@ $^
|
||||||
|
|
||||||
|
$(LIB_SO): $(OBJ)
|
||||||
|
$(CC) -shared -o $@ $^ -lm
|
||||||
|
|
||||||
|
clean:
|
||||||
|
$(RM) -r $(BUILD)
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
/*
|
||||||
|
* TinyBlocks - tb_errors.h
|
||||||
|
* Library tinyblocks-errors (categoria "Erros").
|
||||||
|
*
|
||||||
|
* Erros como valores, no estilo do Result do Rust: um resultado e um objeto
|
||||||
|
* JSON {"ok": valor} ou {"erro": "mensagem"}. "tente obter (x)" transforma
|
||||||
|
* o erro de um reporter num resultado; "desembrulhe" faz o caminho de volta.
|
||||||
|
*
|
||||||
|
* Tratamento de erros: os blocos "tente / se der erro" e "tente / se der
|
||||||
|
* erro / finalmente" sao interpretados por quem executa (o editor); aqui so
|
||||||
|
* ficam os tipos. "lance o erro" levanta um erro e "erro capturado" le a
|
||||||
|
* mensagem dentro do "se der erro".
|
||||||
|
*/
|
||||||
|
#ifndef TINYBLOCKS_ERRORS_H
|
||||||
|
#define TINYBLOCKS_ERRORS_H
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <tinyblocks/tinyblocks.h>
|
||||||
|
#include <tinyblocks/tb_json_block.h>
|
||||||
|
|
||||||
|
TB_API const TinyBlocks_LibraryDescriptor* tb_errors_get_library(void);
|
||||||
|
|
||||||
|
/* Resultados (ref nova; libere com tb_json_release) */
|
||||||
|
TB_API TinyBlocks_JsonValue* tb_errors_ok(TinyBlocks_Value v);
|
||||||
|
TB_API TinyBlocks_JsonValue* tb_errors_err(const char* msg);
|
||||||
|
|
||||||
|
/* true se v e um resultado de erro; msg (opcional) recebe a mensagem */
|
||||||
|
TB_API bool tb_errors_is_err(TinyBlocks_Value v, const char** msg);
|
||||||
|
/* true se v e um resultado ok */
|
||||||
|
TB_API bool tb_errors_is_ok(TinyBlocks_Value v);
|
||||||
|
|
||||||
|
#endif /* TINYBLOCKS_ERRORS_H */
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
# Metadados da library errors. Lido pela UI/build sem recompilar o .so.
|
||||||
|
[library]
|
||||||
|
name = "tinyblocks-errors"
|
||||||
|
version = "1.0.0"
|
||||||
|
category = "Erros"
|
||||||
|
description = "Erros como valores (Ok/Erro) e tente / se der erro / finalmente"
|
||||||
|
color = [98, 104, 255, 255]
|
||||||
|
dark = [70, 74, 214, 255]
|
||||||
|
|
||||||
|
[build]
|
||||||
|
sources = ["src/tb_errors.c"]
|
||||||
|
includes = ["include"]
|
||||||
|
shared = "build/libtinyblocks-errors.so"
|
||||||
|
static = "build/libtinyblocks-errors.a"
|
||||||
|
depends = []
|
||||||
@@ -0,0 +1,277 @@
|
|||||||
|
/*
|
||||||
|
* TinyBlocks - tb_errors.c
|
||||||
|
* Categoria "Erros": resultados (Ok/Erro, como o Result do Rust) e os tipos
|
||||||
|
* dos blocos de tratamento de erro (ver tb_errors.h).
|
||||||
|
*/
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
#include <tinyblocks/tinyblocks.h>
|
||||||
|
#include <tinyblocks/tb_json_block.h>
|
||||||
|
#include "tb_errors.h"
|
||||||
|
|
||||||
|
/* -- Valores <-> JSON ----------------------------------------------------- */
|
||||||
|
|
||||||
|
static TinyBlocks_JsonValue* json_from_value(TinyBlocks_Value v) {
|
||||||
|
switch (v.kind) {
|
||||||
|
case TB_VALUE_INTEGER: return tb_json_number((double)v.as.integer);
|
||||||
|
case TB_VALUE_REAL: return tb_json_number(v.as.real);
|
||||||
|
case TB_VALUE_TEXT: return tb_json_string(v.as.text ? v.as.text : "");
|
||||||
|
case TB_VALUE_JSON: if (v.as.json) { tb_json_retain(v.as.json); return v.as.json; } return tb_json_null();
|
||||||
|
default: return tb_json_null();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static TinyBlocks_Value value_from_json(const TinyBlocks_JsonValue* j) {
|
||||||
|
if (!j) return tb_value_text("");
|
||||||
|
switch (tb_json_kind(j)) {
|
||||||
|
case TB_JSON_NUMBER: {
|
||||||
|
double d = tb_json_as_number(j, 0);
|
||||||
|
if (d == floor(d) && fabs(d) < 1e15) return tb_value_integer((int64_t)d);
|
||||||
|
return tb_value_real(d);
|
||||||
|
}
|
||||||
|
case TB_JSON_STRING: return tb_value_text(tb_json_as_string(j));
|
||||||
|
case TB_JSON_BOOL: return tb_value_integer(tb_json_as_bool(j, false) ? 1 : 0);
|
||||||
|
case TB_JSON_NULL: return tb_value_text("");
|
||||||
|
default: {
|
||||||
|
TinyBlocks_JsonValue* c = tb_json_clone(j);
|
||||||
|
TinyBlocks_Value v = tb_value_json(c);
|
||||||
|
tb_json_release(c);
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Campo `key` de um resultado (ref nova) ou NULL se v nao e um resultado
|
||||||
|
com esse campo */
|
||||||
|
static TinyBlocks_JsonValue* result_field(TinyBlocks_Value v, const char* key) {
|
||||||
|
if (v.kind != TB_VALUE_JSON || !v.as.json || tb_json_kind(v.as.json) != TB_JSON_OBJECT) return NULL;
|
||||||
|
if (tb_json_object_length(v.as.json) != 1) return NULL;
|
||||||
|
return tb_json_object_get(v.as.json, key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* -- API ------------------------------------------------------------------ */
|
||||||
|
|
||||||
|
TinyBlocks_JsonValue* tb_errors_ok(TinyBlocks_Value v) {
|
||||||
|
TinyBlocks_JsonValue* o = tb_json_object();
|
||||||
|
TinyBlocks_JsonValue* j = json_from_value(v);
|
||||||
|
if (o && j) tb_json_object_set(o, "ok", j);
|
||||||
|
if (j) tb_json_release(j);
|
||||||
|
return o;
|
||||||
|
}
|
||||||
|
|
||||||
|
TinyBlocks_JsonValue* tb_errors_err(const char* msg) {
|
||||||
|
TinyBlocks_JsonValue* o = tb_json_object();
|
||||||
|
TinyBlocks_JsonValue* j = tb_json_string(msg && msg[0] ? msg : "erro");
|
||||||
|
if (o && j) tb_json_object_set(o, "erro", j);
|
||||||
|
if (j) tb_json_release(j);
|
||||||
|
return o;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool tb_errors_is_err(TinyBlocks_Value v, const char** msg) {
|
||||||
|
TinyBlocks_JsonValue* e = result_field(v, "erro");
|
||||||
|
if (!e) return false;
|
||||||
|
/* a string fica viva enquanto o resultado v existir */
|
||||||
|
if (msg) { const char* s = tb_json_as_string(e); *msg = s ? s : "erro"; }
|
||||||
|
tb_json_release(e);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool tb_errors_is_ok(TinyBlocks_Value v) {
|
||||||
|
TinyBlocks_JsonValue* o = result_field(v, "ok");
|
||||||
|
if (o) tb_json_release(o);
|
||||||
|
return o != NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* -- Saidas --------------------------------------------------------------- */
|
||||||
|
|
||||||
|
static void out_json(TinyBlocks_Block* self, TinyBlocks_JsonValue* j) {
|
||||||
|
tb_block_set_output(self, 0, j ? tb_value_json(j) : tb_value_none());
|
||||||
|
if (j) tb_json_release(j);
|
||||||
|
self->dirty = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void out_bool(TinyBlocks_Block* self, bool b) {
|
||||||
|
tb_block_set_output(self, 0, tb_value_integer(b ? 1 : 0));
|
||||||
|
self->dirty = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const char* in_text(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i, char* buf, size_t cap) {
|
||||||
|
TinyBlocks_Value v = tb_block_get_input(self, rt, i);
|
||||||
|
switch (v.kind) {
|
||||||
|
case TB_VALUE_TEXT: return v.as.text ? v.as.text : "";
|
||||||
|
case TB_VALUE_INTEGER: snprintf(buf, cap, "%lld", (long long)v.as.integer); return buf;
|
||||||
|
case TB_VALUE_REAL: snprintf(buf, cap, "%g", v.as.real); return buf;
|
||||||
|
default: return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* -- Blocos: resultados --------------------------------------------------- */
|
||||||
|
|
||||||
|
/* "tente obter (x)": avalia x; erro vira {"erro": msg}, valor vira {"ok": v} */
|
||||||
|
static void eval_attempt(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||||
|
char before[512];
|
||||||
|
snprintf(before, sizeof(before), "%s", tinyblocks_last_error(rt));
|
||||||
|
tinyblocks_clear_error(rt);
|
||||||
|
TinyBlocks_Value v = tb_block_get_input(self, rt, 0);
|
||||||
|
const char* err = tinyblocks_last_error(rt);
|
||||||
|
if (err && err[0]) {
|
||||||
|
TinyBlocks_JsonValue* r = tb_errors_err(err);
|
||||||
|
tinyblocks_clear_error(rt); /* tratado: vira valor */
|
||||||
|
out_json(self, r);
|
||||||
|
} else {
|
||||||
|
out_json(self, tb_errors_ok(v));
|
||||||
|
}
|
||||||
|
if (before[0]) tinyblocks_set_error(rt, "%s", before);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void eval_ok(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||||
|
out_json(self, tb_errors_ok(tb_block_get_input(self, rt, 0)));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void eval_err(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||||
|
char b[64];
|
||||||
|
out_json(self, tb_errors_err(in_text(self, rt, 0, b, sizeof(b))));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void eval_is_ok(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||||
|
out_bool(self, tb_errors_is_ok(tb_block_get_input(self, rt, 0)));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void eval_is_err(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||||
|
out_bool(self, tb_errors_is_err(tb_block_get_input(self, rt, 0), NULL));
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Valor de dentro de um resultado ok; um valor comum passa direto */
|
||||||
|
static TinyBlocks_Value ok_value(TinyBlocks_Value v) {
|
||||||
|
TinyBlocks_JsonValue* o = result_field(v, "ok");
|
||||||
|
if (!o) return tb_value_clone(v);
|
||||||
|
TinyBlocks_Value r = value_from_json(o);
|
||||||
|
tb_json_release(o);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* "valor de (r) ou (padrao)" (unwrap_or) */
|
||||||
|
static void eval_unwrap_or(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||||
|
TinyBlocks_Value v = tb_block_get_input(self, rt, 0);
|
||||||
|
if (tb_errors_is_err(v, NULL)) tb_block_set_output(self, 0, tb_value_clone(tb_block_get_input(self, rt, 1)));
|
||||||
|
else tb_block_set_output(self, 0, ok_value(v));
|
||||||
|
self->dirty = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* "desembrulhe (r)" (unwrap / "?"): erro volta a ser erro de verdade */
|
||||||
|
static void eval_unwrap(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||||
|
TinyBlocks_Value v = tb_block_get_input(self, rt, 0);
|
||||||
|
const char* msg;
|
||||||
|
if (tb_errors_is_err(v, &msg)) {
|
||||||
|
tinyblocks_set_error(rt, "%s", msg);
|
||||||
|
tb_block_set_output(self, 0, tb_value_none());
|
||||||
|
} else {
|
||||||
|
tb_block_set_output(self, 0, ok_value(v));
|
||||||
|
}
|
||||||
|
self->dirty = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* "mensagem do erro (r)": "" se nao for erro */
|
||||||
|
static void eval_message(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||||
|
const char* msg = "";
|
||||||
|
TinyBlocks_Value v = tb_block_get_input(self, rt, 0);
|
||||||
|
tb_errors_is_err(v, &msg);
|
||||||
|
tb_block_set_output(self, 0, tb_value_text(msg));
|
||||||
|
self->dirty = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* -- Blocos: tratamento --------------------------------------------------- */
|
||||||
|
|
||||||
|
/* "lance o erro [msg]" (raise / panic!) */
|
||||||
|
static void eval_raise(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||||
|
char b[64];
|
||||||
|
const char* msg = in_text(self, rt, 0, b, sizeof(b));
|
||||||
|
tinyblocks_set_error(rt, "%s", msg[0] ? msg : "erro");
|
||||||
|
self->dirty = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* "erro capturado": mensagem do erro em tratamento ("se der erro") */
|
||||||
|
static void eval_caught(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||||
|
tb_block_set_output(self, 0, tb_value_text(tinyblocks_caught_error(rt)));
|
||||||
|
self->dirty = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Os blocos C sao executados por quem roda os scripts */
|
||||||
|
static void eval_nop(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||||
|
(void)rt;
|
||||||
|
self->dirty = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* -- Registro ------------------------------------------------------------- */
|
||||||
|
|
||||||
|
static bool reg(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib, const char* name,
|
||||||
|
const TinyBlocks_SlotDesc* in, uint32_t ni,
|
||||||
|
const TinyBlocks_SlotDesc* out, uint32_t no,
|
||||||
|
void (*fn)(TinyBlocks_Block*, TinyBlocks*)) {
|
||||||
|
TinyBlocks_TypeBlock* t = tb_type_block_create(rt, name, in, ni, out, no, fn);
|
||||||
|
return t && tb_library_add(lib, &t->base);
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool errors_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||||
|
static const TinyBlocks_Category cat = {
|
||||||
|
"Erros",
|
||||||
|
{ 98/255.f, 104/255.f, 255/255.f, 1.f},
|
||||||
|
{ 70/255.f, 74/255.f, 214/255.f, 1.f},
|
||||||
|
};
|
||||||
|
tb_library_set_category(lib, &cat);
|
||||||
|
|
||||||
|
TB_BLOCK_META("Errors.Try", "eval_nop",
|
||||||
|
"Roda o primeiro bloco; se der erro, o segundo; e sempre o terceiro no fim");
|
||||||
|
TB_BLOCK_META("Errors.TryCatch", "eval_nop",
|
||||||
|
"Roda o primeiro bloco; se der erro, roda o segundo em vez de parar o script");
|
||||||
|
TB_BLOCK_META("Errors.Raise", "eval_raise", "Levanta um erro (para o script, ou vai para o \"se der erro\")");
|
||||||
|
TB_BLOCK_META("Errors.Caught", "eval_caught", "Mensagem do erro dentro do \"se der erro\"");
|
||||||
|
TB_BLOCK_META("Errors.Attempt", "eval_attempt",
|
||||||
|
"Avalia o valor e devolve um resultado: {\"ok\": valor} ou {\"erro\": mensagem}");
|
||||||
|
TB_BLOCK_META("Errors.Ok", "tb_errors_ok", "Resultado de sucesso com o valor");
|
||||||
|
TB_BLOCK_META("Errors.Err", "tb_errors_err", "Resultado de erro com a mensagem");
|
||||||
|
TB_BLOCK_META("Errors.IsOk", "tb_errors_is_ok", "Verdadeiro se o resultado for ok");
|
||||||
|
TB_BLOCK_META("Errors.IsErr", "tb_errors_is_err", "Verdadeiro se o resultado for erro");
|
||||||
|
TB_BLOCK_META("Errors.UnwrapOr", "eval_unwrap_or", "Valor do resultado, ou o padrao se for erro");
|
||||||
|
TB_BLOCK_META("Errors.Unwrap", "eval_unwrap", "Valor do resultado; se for erro, levanta o erro");
|
||||||
|
TB_BLOCK_META("Errors.Message", "eval_message", "Mensagem de um resultado de erro (vazio se ok)");
|
||||||
|
|
||||||
|
static const TinyBlocks_SlotDesc msg_in[] = {{"mensagem", TB_VALUE_TEXT}};
|
||||||
|
static const TinyBlocks_SlotDesc any_in[] = {{"valor", TB_VALUE_NONE}};
|
||||||
|
static const TinyBlocks_SlotDesc or_in[] = {{"resultado", TB_VALUE_NONE}, {"padrão", TB_VALUE_NONE}};
|
||||||
|
static const TinyBlocks_SlotDesc res_out[] = {{"resultado", TB_VALUE_JSON}};
|
||||||
|
static const TinyBlocks_SlotDesc any_out[] = {{"valor", TB_VALUE_NONE}};
|
||||||
|
static const TinyBlocks_SlotDesc text_out[] = {{"mensagem", TB_VALUE_TEXT}};
|
||||||
|
/* saidas terminadas em '?' = blocos booleanos (hexagonais no editor) */
|
||||||
|
static const TinyBlocks_SlotDesc ok_out[] = {{"é ok?", TB_VALUE_INTEGER}};
|
||||||
|
static const TinyBlocks_SlotDesc err_out[] = {{"é erro?", TB_VALUE_INTEGER}};
|
||||||
|
|
||||||
|
if (!(reg(rt, lib, "Errors.TryCatch", NULL, 0, NULL, 0, eval_nop)
|
||||||
|
&& reg(rt, lib, "Errors.Try", NULL, 0, NULL, 0, eval_nop)
|
||||||
|
&& reg(rt, lib, "Errors.Raise", msg_in, 1, NULL, 0, eval_raise)
|
||||||
|
&& reg(rt, lib, "Errors.Caught", NULL, 0, text_out, 1, eval_caught)
|
||||||
|
&& reg(rt, lib, "Errors.Attempt", any_in, 1, res_out, 1, eval_attempt)))
|
||||||
|
return false;
|
||||||
|
tb_type_block_find(rt, "Errors.Attempt")->lazy_inputs = true; /* ve o erro de dentro */
|
||||||
|
|
||||||
|
return reg(rt, lib, "Errors.Ok", any_in, 1, res_out, 1, eval_ok)
|
||||||
|
&& reg(rt, lib, "Errors.Err", msg_in, 1, res_out, 1, eval_err)
|
||||||
|
&& reg(rt, lib, "Errors.IsOk", any_in, 1, ok_out, 1, eval_is_ok)
|
||||||
|
&& reg(rt, lib, "Errors.IsErr", any_in, 1, err_out, 1, eval_is_err)
|
||||||
|
&& reg(rt, lib, "Errors.UnwrapOr", or_in, 2, any_out, 1, eval_unwrap_or)
|
||||||
|
&& reg(rt, lib, "Errors.Unwrap", any_in, 1, any_out, 1, eval_unwrap)
|
||||||
|
&& reg(rt, lib, "Errors.Message", any_in, 1, text_out, 1, eval_message);
|
||||||
|
}
|
||||||
|
|
||||||
|
TINYBLOCKS_DEFINE_LIBRARY(errors,
|
||||||
|
"tinyblocks-errors",
|
||||||
|
"1.0.0",
|
||||||
|
errors_init,
|
||||||
|
NULL)
|
||||||
|
|
||||||
|
const TinyBlocks_LibraryDescriptor* tb_errors_get_library(void) {
|
||||||
|
return &_tb_lib_desc_errors;
|
||||||
|
}
|
||||||
+10
-5
@@ -151,12 +151,17 @@ void tb_block_eval(TinyBlocks_Block* block, TinyBlocks* rt)
|
|||||||
}
|
}
|
||||||
block->in_eval = true;
|
block->in_eval = true;
|
||||||
|
|
||||||
/* Avalia fontes conectadas primeiro (lazy) */
|
/* Avalia fontes conectadas primeiro. Se uma delas levantar um erro, o
|
||||||
for (uint32_t i = 0; i < block->input_count; ++i) {
|
bloco nao roda: o erro sobe (como uma excecao) ate quem trata. */
|
||||||
TinyBlocks_Connection* conn = &block->inputs[i];
|
bool had_error = rt->error[0] != '\0';
|
||||||
if (conn->source && conn->source->dirty)
|
if (!(block->type && block->type->lazy_inputs)) {
|
||||||
tb_block_eval(conn->source, rt);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
if (!had_error && rt->error[0]) { block->in_eval = false; return; }
|
||||||
|
|
||||||
block->vtable->eval(block, rt);
|
block->vtable->eval(block, rt);
|
||||||
block->in_eval = false;
|
block->in_eval = false;
|
||||||
|
|||||||
@@ -240,6 +240,10 @@ TinyBlocks_LibraryBlock* tb_rt_library_at(const TinyBlocks* rt, uint32_t i) {
|
|||||||
/* -- Erros --- */
|
/* -- Erros --- */
|
||||||
const char* tinyblocks_last_error(TinyBlocks* rt) { return rt->error; }
|
const char* tinyblocks_last_error(TinyBlocks* rt) { return rt->error; }
|
||||||
void tinyblocks_clear_error(TinyBlocks* rt) { rt->error[0] = '\0'; }
|
void tinyblocks_clear_error(TinyBlocks* rt) { rt->error[0] = '\0'; }
|
||||||
|
const char* tinyblocks_caught_error(TinyBlocks* rt) { return rt->caught; }
|
||||||
|
void tinyblocks_set_caught_error(TinyBlocks* rt, const char* msg) {
|
||||||
|
snprintf(rt->caught, sizeof(rt->caught), "%s", msg ? msg : "");
|
||||||
|
}
|
||||||
|
|
||||||
void tinyblocks_set_error(TinyBlocks* rt, const char* fmt, ...)
|
void tinyblocks_set_error(TinyBlocks* rt, const char* fmt, ...)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in new issue
Block a user