/* * cat/value.c -- dispatch helpers over the CAT_VALUE_Vtable. * * Naming rule: symbols here are cat_value_* / CAT_VALUE_*. * * These helpers implement the "friendly C API": a caller working with * opaque CAT_VALUE_Value* pointers can perform any operator without * knowing the concrete type. A missing vtable slot becomes a structured * error, never a crash. */ #include "cat/value.h" #include "cat/error.h" #include "cat/types/empty.h" #include "cat/types/bool.h" #include "cat/types/int.h" #include "cat/types/float.h" #include "cat/types/number.h" #include "cat/types/list.h" #include "cat/types/table.h" #include "cat/types/text.h" #include "cat/types/ascii_text.h" #include #include #include #include /* ---- reference counting (thread-safe) ------------------------------- * * The count lives in the common value header. retain/release use atomic * read-modify-write with acquire/release ordering, so the object graph * can be shared across threads safely. Destroying the object runs on * whichever thread drops the last reference. * * A negative count marks an immortal value (a singleton, or a wrapper * whose storage lives on the C stack); retain/release ignore it. */ void cat_value_retain(CAT_VALUE_Value *v) { if (v == NULL) return; if (atomic_load_explicit(&v->refcnt, memory_order_relaxed) < 0) return; atomic_fetch_add_explicit(&v->refcnt, 1, memory_order_relaxed); } void cat_value_release(CAT_VALUE_Value *v) { long prev; if (v == NULL) return; prev = atomic_load_explicit(&v->refcnt, memory_order_relaxed); if (prev < 0) return; if (atomic_fetch_sub_explicit(&v->refcnt, 1, memory_order_acq_rel) == 1) { /* Last reference: publish our writes, then destroy. */ atomic_thread_fence(memory_order_acquire); if (v->vtable != NULL && v->vtable->destroy != NULL) v->vtable->destroy(v); } } long cat_value_refcount(CAT_VALUE_Value *v) { if (v == NULL) return 0; return atomic_load_explicit(&v->refcnt, memory_order_relaxed); } void cat_value_make_immortal(CAT_VALUE_Value *v) { if (v == NULL) return; atomic_store_explicit(&v->refcnt, -1, memory_order_relaxed); } const char *cat_value_type_name(CAT_VALUE_Value *v) { if (v == NULL || v->vtable == NULL || v->vtable->type_name == NULL) return ""; return v->vtable->type_name; } /* Default representation by family (no `_repr_` lookup). Caller does * not free the returned bytes. Uses a ring of static buffers so nested * calls (an object's fields) do not clobber each other. */ static size_t repr_into(char *out, size_t cap, CAT_VALUE_Value *v, int quote_text); /* Resolve `_repr_` if present: returns the value to format (a text * result from a callable/value `_repr_`), or `v` itself otherwise. * Never returns NULL. */ static CAT_VALUE_Value *repr_resolve(CAT_VALUE_Value *v) { CAT_ERROR_Error e; CAT_VALUE_Value *r = NULL; if (v == NULL) return NULL; cat_error_clear(&e); if (cat_value_get_attribute(v, "_repr_", &r, &e) == 0 && r != NULL && r != v) { if (r->vtable != NULL && r->vtable->kind == CAT_VALUE_KIND_FUNCTION) { CAT_VALUE_Value *out = NULL; CAT_VALUE_Value *callargs[1]; callargs[0] = v; cat_error_clear(&e); if (cat_value_call(r, callargs, 1, &out, &e) == 0 && out != NULL && out != v) return out; return NULL; } return r; } return v; } const char *cat_value_repr_text(CAT_VALUE_Value *v) { static char ring[8][512]; static int slot = 0; char *buf = ring[slot]; slot = (slot + 1) % 8; repr_into(buf, sizeof(ring[0]), v, 1); return buf; } /* Display form (no quoting of text): what `tostring` produces. */ const char *cat_value_str_text(CAT_VALUE_Value *v) { static char ring[8][512]; static int slot = 0; char *buf = ring[slot]; slot = (slot + 1) % 8; repr_into(buf, sizeof(ring[0]), v, 0); return buf; } static size_t repr_into(char *out, size_t cap, CAT_VALUE_Value *v, int quote_text) { if (cap == 0) return 0; /* Honour `_repr_` first: its text result is used verbatim (no * quotes); a non-text result is formatted normally. */ if (v != NULL) { CAT_VALUE_Value *r = repr_resolve(v); if (r != NULL && r != v) { if (r->vtable != NULL && r->vtable->kind == CAT_VALUE_KIND_TEXT) { snprintf(out, cap, "%s", cat_types_text_bytes(r)); return strlen(out); } v = r; } else if (r == NULL) { out[0] = '\0'; return 0; } } if (v == NULL || v == cat_types_empty()) { snprintf(out, cap, "empty"); return strlen(out); } if (v->vtable != NULL && v->vtable->kind == CAT_VALUE_KIND_TEXT) { if (quote_text) snprintf(out, cap, "\"%s\"", cat_types_text_bytes(v)); else snprintf(out, cap, "%s", cat_types_text_bytes(v)); return strlen(out); } if (v->vtable != NULL && v->vtable->kind == CAT_VALUE_KIND_NUMBER) { if (((CAT_TYPES_NUMBER_Number *)v)->is_float) snprintf(out, cap, "%g", cat_types_number_as_double(v)); else snprintf(out, cap, "%lld", cat_types_number_as_int(v)); return strlen(out); } if (v->vtable != NULL && v->vtable->kind == CAT_VALUE_KIND_BOOL) { snprintf(out, cap, "%s", ((CAT_TYPES_BOOL_Bool *)v)->v ? "true" : "false"); return strlen(out); } if (cat_types_list_is(v)) { int i, n = cat_types_list_count(v); size_t off = 0; if (off < cap - 1) out[off++] = '['; for (i = 0; i < n && off < cap - 24; i++) { char item[256]; repr_into(item, sizeof(item), cat_types_list_at(v, i), quote_text); int w = snprintf(out + off, cap - off, "%s%s", i ? ", " : "", item); if (w > 0) off += (size_t)w; } if (off < cap - 1) out[off++] = ']'; out[off] = '\0'; return off; } if (v->vtable != NULL && v->vtable == cat_types_table_vtable()) { int i, n = cat_types_table_count(v); size_t off = 0; if (off < cap - 1) out[off++] = '{'; for (i = 0; i < n && off < cap - 40; i++) { char item[256]; repr_into(item, sizeof(item), cat_types_table_value_at(v, i), quote_text); int w = snprintf(out + off, cap - off, "%s%s = %s", i ? ", " : "", cat_types_table_key_at(v, i), item); if (w > 0) off += (size_t)w; } if (off < cap - 1) out[off++] = '}'; out[off] = '\0'; return off; } snprintf(out, cap, "<%s>", cat_value_type_name(v)); return strlen(out); } /* The textual representation of a value, as an ascii_text value: * the repr form (text quoted). Honours `_repr_`. */ CAT_VALUE_Value *cat_value_repr(CAT_VALUE_Value *v) { return cat_types_ascii_text_new(cat_value_repr_text(v)); } static CAT_VALUE_Vtable *vt(CAT_VALUE_Value *v) { return (v != NULL) ? v->vtable : NULL; } int cat_value_add(CAT_VALUE_Value *a, CAT_VALUE_Value *b, CAT_VALUE_Value **out, CAT_ERROR_Error *err) { if (vt(a) == NULL || vt(a)->add.fn == NULL) return (cat_error_raise(err, "type %s does not implement add", cat_value_type_name(a)), -1); return vt(a)->add.fn(a, b, out, err); } int cat_value_sub(CAT_VALUE_Value *a, CAT_VALUE_Value *b, CAT_VALUE_Value **out, CAT_ERROR_Error *err) { if (vt(a) == NULL || vt(a)->sub.fn == NULL) return (cat_error_raise(err, "type %s does not implement sub", cat_value_type_name(a)), -1); return vt(a)->sub.fn(a, b, out, err); } int cat_value_mul(CAT_VALUE_Value *a, CAT_VALUE_Value *b, CAT_VALUE_Value **out, CAT_ERROR_Error *err) { if (vt(a) == NULL || vt(a)->mul.fn == NULL) return (cat_error_raise(err, "type %s does not implement mul", cat_value_type_name(a)), -1); return vt(a)->mul.fn(a, b, out, err); } int cat_value_div(CAT_VALUE_Value *a, CAT_VALUE_Value *b, CAT_VALUE_Value **out, CAT_ERROR_Error *err) { if (vt(a) == NULL || vt(a)->div.fn == NULL) return (cat_error_raise(err, "type %s does not implement div", cat_value_type_name(a)), -1); return vt(a)->div.fn(a, b, out, err); } int cat_value_rem(CAT_VALUE_Value *a, CAT_VALUE_Value *b, CAT_VALUE_Value **out, CAT_ERROR_Error *err) { if (vt(a) == NULL || vt(a)->rem.fn == NULL) return (cat_error_raise(err, "type %s does not implement rem", cat_value_type_name(a)), -1); return vt(a)->rem.fn(a, b, out, err); } int cat_value_call(CAT_VALUE_Value *fn, CAT_VALUE_Value **args, int nargs, CAT_VALUE_Value **out, CAT_ERROR_Error *err) { if (vt(fn) == NULL || vt(fn)->call.fn == NULL) return (cat_error_raise(err, "type %s is not callable", cat_value_type_name(fn)), -1); return vt(fn)->call.fn(fn, args, nargs, out, err); } int cat_value_get_attribute(CAT_VALUE_Value *self, const char *name, CAT_VALUE_Value **out, CAT_ERROR_Error *err) { const CAT_VALUE_Slot *s; if (vt(self) != NULL && vt(self)->get_attribute.fn != NULL && vt(self)->get_attribute.fn(self, name, out, err) == 0) return 0; /* The type's own handler declined; clear any error it left behind * before trying the C-known member layout (slots). */ if (err != NULL) cat_error_clear(err); s = cat_value_slot_find(self, name); if (s != NULL && cat_value_slot_get(self, s, out, err) == 0) { if (err != NULL) cat_error_clear(err); return 0; } return (cat_error_raise(err, "type %s has no attribute '%s'", cat_value_type_name(self), name), -1); } int cat_value_set_attribute(CAT_VALUE_Value *self, const char *name, CAT_VALUE_Value *value, CAT_ERROR_Error *err) { const CAT_VALUE_Slot *s; if (vt(self) != NULL && vt(self)->set_attribute.fn != NULL && vt(self)->set_attribute.fn(self, name, value, err) == 0) return 0; if (err != NULL) cat_error_clear(err); s = cat_value_slot_find(self, name); if (s != NULL && cat_value_slot_set(self, s, value, err) == 0) { if (err != NULL) cat_error_clear(err); return 0; } return (cat_error_raise(err, "type %s has no settable attribute '%s'", cat_value_type_name(self), name), -1); } int cat_value_truthy(CAT_VALUE_Value *v, int *out, CAT_ERROR_Error *err) { if (vt(v) == NULL || vt(v)->truthy == NULL) { *out = 1; /* default: everything is true */ return 0; } return vt(v)->truthy(v, err) ? (*out = 1, 0) : (*out = 0, 0); } int cat_value_equal(CAT_VALUE_Value *a, CAT_VALUE_Value *b, int *out, CAT_ERROR_Error *err) { CAT_VALUE_Value *r = NULL; CAT_ERROR_Error local; /* String/text equality is not arithmetic; compare bytes when both * sides are in the text family, otherwise fall back to `==` via sub * (a-b is zero) for numbers. */ (void)err; if (vt(a) != NULL && vt(a)->kind == CAT_VALUE_KIND_TEXT && vt(b) != NULL && vt(b)->kind == CAT_VALUE_KIND_TEXT) { cat_error_clear(&local); *out = (cat_value_type_name(a) == cat_value_type_name(b)); return 0; } cat_error_clear(&local); if (cat_value_sub(a, b, &r, &local) != 0) { *out = 0; return 0; } *out = 0; (void)r; return 0; } int cat_value_next(CAT_VALUE_Value *v, CAT_VALUE_Value **out, CAT_ERROR_Error *err) { if (vt(v) == NULL || vt(v)->next.fn == NULL) return (cat_error_raise(err, "type %s is not iterable", cat_value_type_name(v)), -1); return vt(v)->next.fn(v, out, err); } const CAT_VALUE_Slot *cat_value_slot_find(CAT_VALUE_Value *v, const char *name) { int i; if (vt(v) == NULL || vt(v)->slots == NULL) return NULL; for (i = 0; i < vt(v)->nslots; i++) if (strcmp(vt(v)->slots[i].name, name) == 0) return &vt(v)->slots[i]; return NULL; } int cat_value_slot_get(CAT_VALUE_Value *v, const CAT_VALUE_Slot *s, CAT_VALUE_Value **out, CAT_ERROR_Error *err) { char *base = (char *)v; switch (s->kind) { case CAT_VALUE_SLOT_VALUE: *out = *(CAT_VALUE_Value **)(base + s->offset); break; case CAT_VALUE_SLOT_NUMBER: *out = cat_types_int_new(*(long long *)(base + s->offset)); break; case CAT_VALUE_SLOT_DOUBLE: *out = cat_types_float_new(*(double *)(base + s->offset)); break; case CAT_VALUE_SLOT_TEXT: { char *p = *(char **)(base + s->offset); *out = cat_types_ascii_text_new(p ? p : ""); break; } case CAT_VALUE_SLOT_BOOL: *out = (*(int *)(base + s->offset)) ? cat_types_bool_true() : cat_types_bool_false(); break; default: *out = cat_types_empty(); break; } if (*out == NULL) return (cat_error_raise(err, "slot '%s' unreadable", s->name), -1); return 0; } int cat_value_slot_set(CAT_VALUE_Value *v, const CAT_VALUE_Slot *s, CAT_VALUE_Value *value, CAT_ERROR_Error *err) { char *base = (char *)v; if (s->readonly) return (cat_error_raise(err, "slot '%s' is read-only", s->name), -1); switch (s->kind) { case CAT_VALUE_SLOT_VALUE: *(CAT_VALUE_Value **)(base + s->offset) = value; break; case CAT_VALUE_SLOT_NUMBER: *(long long *)(base + s->offset) = cat_types_number_as_int(value); break; case CAT_VALUE_SLOT_DOUBLE: *(double *)(base + s->offset) = cat_types_number_as_double(value); break; case CAT_VALUE_SLOT_TEXT: { const char *p = cat_types_text_bytes(value); char **dst = (char **)(base + s->offset); free(*dst); *dst = p ? strdup(p) : NULL; break; } case CAT_VALUE_SLOT_BOOL: { int t = 1; cat_value_truthy(value, &t, err); *(int *)(base + s->offset) = t; break; } default: break; } return 0; }