•internal.Validate

This commit is contained in:
dzaima 2025-05-11 20:30:20 +03:00
parent 09aa7b285f
commit 62366d10fa
6 changed files with 53 additions and 7 deletions

View File

@ -108,9 +108,10 @@ Namespace of various internal functions. May change at any time.
| `•internal.Variation` | Convert `𝕩` to the variation specified in `𝕨` | | `•internal.Variation` | Convert `𝕩` to the variation specified in `𝕨` |
| `•internal.ClearRefs` | Clear references `•internal.Variation` made for `*Inc` variations | | `•internal.ClearRefs` | Clear references `•internal.Variation` made for `*Inc` variations |
| `•internal.Unshare` | Get a unique, reference count 1 version of the argument; recursively unshares array items, doesn't touch namespaces | | `•internal.Unshare` | Get a unique, reference count 1 version of the argument; recursively unshares array items, doesn't touch namespaces |
| `•internal.EEqual` | exactly equal (NaN equals NaN, 0 equals ¯0) | | `•internal.EEqual` | Exactly equal (NaN equals NaN, 0 equals ¯0) |
| `•internal.Temp` | place to test new features or temporarily expose some internal function | | `•internal.Temp` | Place to test new features or temporarily expose some internal function |
| `•internal.Properties` | various build properties | | `•internal.Properties` | Various build properties |
| `•internal.Validate` | Validate that `𝕩` has correct flags set |
# FFI # FFI

View File

@ -20,7 +20,7 @@
/* inverse.c*/M(setInvReg,"(SetInvReg)") M(setInvSwap,"(SetInvSwap)") M(nativeInvReg,"(NativeInvReg)") M(nativeInvSwap,"(NativeInvSwap)") \ /* inverse.c*/M(setInvReg,"(SetInvReg)") M(setInvSwap,"(SetInvSwap)") M(nativeInvReg,"(NativeInvReg)") M(nativeInvSwap,"(NativeInvSwap)") \
/*internal.c*/M(itype,"•internal.Type") M(elType,"•internal.ElType") M(refc,"•internal.Refc") M(isPure,"•internal.IsPure") A(info,"•internal.Info") \ /*internal.c*/M(itype,"•internal.Type") M(elType,"•internal.ElType") M(refc,"•internal.Refc") M(isPure,"•internal.IsPure") A(info,"•internal.Info") \
/*internal.c*/M(heapDump,"•internal.HeapDump") M(internalGC,"•internal.GC") M(heapStats,"•internal.HeapStats") A(iObjFlags,"•internal.ObjFlags") \ /*internal.c*/M(heapDump,"•internal.HeapDump") M(internalGC,"•internal.GC") M(heapStats,"•internal.HeapStats") A(iObjFlags,"•internal.ObjFlags") \
/*internal.c*/D(eequal,"•internal.EEqual") M(squeeze,"•internal.Squeeze") M(deepSqueeze,"•internal.DeepSqueeze") \ /*internal.c*/D(eequal,"•internal.EEqual") M(squeeze,"•internal.Squeeze") M(deepSqueeze,"•internal.DeepSqueeze") M(iValidate,"•internal.Validate") \
/*internal.c*/A(internalTemp,"•internal.Temp") M(iHasFill,"•internal.HasFill") M(iKeep,"•internal.Keep") D(iProperties,"•internal.Properties") \ /*internal.c*/A(internalTemp,"•internal.Temp") M(iHasFill,"•internal.HasFill") M(iKeep,"•internal.Keep") D(iProperties,"•internal.Properties") \
/*internal.c*/D(variation,"•internal.Variation") A(listVariations,"•internal.ListVariations") M(clearRefs,"•internal.ClearRefs") M(unshare,"•internal.Unshare") \ /*internal.c*/D(variation,"•internal.Variation") A(listVariations,"•internal.ListVariations") M(clearRefs,"•internal.ClearRefs") M(unshare,"•internal.Unshare") \
/* arithd.c*/D(hypot,"•math.Hypot") D(comb,"•math.Comb") D(gcd,"•math.GCD") D(lcm,"•math.LCM") D(atan2,"•math.Atan2") D(atan2ix,"•math.Atan2⁼") D(atan2iw,"•math.Atan2˜") \ /* arithd.c*/D(hypot,"•math.Hypot") D(comb,"•math.Comb") D(gcd,"•math.GCD") D(lcm,"•math.LCM") D(atan2,"•math.Atan2") D(atan2ix,"•math.Atan2⁼") D(atan2iw,"•math.Atan2˜") \

View File

@ -3,6 +3,8 @@
#include "../builtins.h" #include "../builtins.h"
#include "../ns.h" #include "../ns.h"
#include "../utils/cstr.h" #include "../utils/cstr.h"
#include "../utils/calls.h"
#include <stdarg.h>
B itype_c1(B t, B x) { B itype_c1(B t, B x) {
B r; B r;
@ -412,6 +414,44 @@ B iProperties_c2(B t, B w, B x) {
return r; return r;
} }
static NOINLINE NORETURN void validate_fail(bool crash, char* p, ...) {
va_list a;
va_start(a, p);
B msg = do_fmt(emptyCVec(), p, a);
va_end(a);
if (!crash) thr(msg);
printsB(msg);
fatal("object failed validation");
}
bool validate_flags(bool crash, B x) {
if (isArr(x) && IA(x)!=0) {
if (FL_HAS(x, fl_squoze)) {
B t = unshare(x);
t = any_squeeze(t);
if (TI(t,elType) != TI(x,elType)) validate_fail(crash, "Validate: Wrongly squeezed: array has eltype %S, expected %S", eltype_repr(TI(x,elType)), eltype_repr(TI(t,elType)));
decG(t);
}
#define DSC_ASC(FLAG, CMP) \
if (FL_HAS(x, FLAG)) { \
if (RNK(x)==1) { \
SGetU(x) \
usz ia = IA(x); \
for (ux i = 0; i < ia-1; i++) if (compare(GetU(x,i),GetU(x,i+1)) CMP) validate_fail(crash, "Validate: Incorrectly marked " #FLAG " between indices %z+0‿1", i); \
} \
}
DSC_ASC(fl_dsc, < 0)
DSC_ASC(fl_asc, > 0)
}
return true;
}
B iValidate_c1(B t, B x) {
validate_flags(false, x);
return x;
}
B unshare_c1(B t, B x) { B unshare_c1(B t, B x) {
if (!isArr(x)) thrM("•internal.Unshare 𝕩: 𝕩 must be an array"); if (!isArr(x)) thrM("•internal.Unshare 𝕩: 𝕩 must be an array");
B r = unshare(x); B r = unshare(x);
@ -433,8 +473,8 @@ B getInternalNS(void) {
#undef F #undef F
#define F(X) incG(bi_##X), #define F(X) incG(bi_##X),
Body* d = m_nnsDesc("type","eltype","refc","squeeze","ispure","info", "keep", "purekeep","listvariations","variation","clearrefs", "hasfill","unshare","deepsqueeze","heapdump","eequal", "gc", "temp","heapstats", "objflags", "properties"); Body* d = m_nnsDesc("type","eltype","refc","squeeze","ispure","info", "keep", "purekeep","listvariations","variation","clearrefs", "hasfill","unshare","deepsqueeze","heapdump","eequal", "gc", "temp","heapstats", "objflags", "properties", "validate");
internalNS = m_nns(d,F(itype)F(elType)F(refc)F(squeeze)F(isPure)F(info)F(iKeep)F(iPureKeep)F(listVariations)F(variation)F(clearRefs)F(iHasFill)F(unshare)F(deepSqueeze)F(heapDump)F(eequal)F(internalGC)F(internalTemp)F(heapStats)F(iObjFlags)F(iProperties)); internalNS = m_nns(d,F(itype)F(elType)F(refc)F(squeeze)F(isPure)F(info)F(iKeep)F(iPureKeep)F(listVariations)F(variation)F(clearRefs)F(iHasFill)F(unshare)F(deepSqueeze)F(heapDump)F(eequal)F(internalGC)F(internalTemp)F(heapStats)F(iObjFlags)F(iProperties)F(iValidate));
#undef F #undef F
gc_add(internalNS); gc_add(internalNS);
} }

View File

@ -1988,7 +1988,7 @@ u32* const dsv_text[] = {
U"•file.Accessed",U"•file.At",U"•file.Bytes",U"•file.Chars",U"•file.Created",U"•file.CreateDir",U"•file.Exists",U"•file.Lines",U"•file.List", U"•file.Accessed",U"•file.At",U"•file.Bytes",U"•file.Chars",U"•file.Created",U"•file.CreateDir",U"•file.Exists",U"•file.Lines",U"•file.List",
U"•file.MapBytes",U"•file.Modified",U"•file.Name",U"•file.Parent",U"•file.path",U"•file.RealPath",U"•file.Remove",U"•file.Rename",U"•file.Size",U"•file.Type", U"•file.MapBytes",U"•file.Modified",U"•file.Name",U"•file.Parent",U"•file.path",U"•file.RealPath",U"•file.Remove",U"•file.Rename",U"•file.Size",U"•file.Type",
U"•internal.ClearRefs",U"•internal.DeepSqueeze",U"•internal.EEqual",U"•internal.ElType",U"•internal.GC",U"•internal.HasFill",U"•internal.HeapDump",U"•internal.HeapStats",U"•internal.Info",U"•internal.IsPure",U"•internal.Keep",U"•internal.ListVariations",U"•internal.ObjFlags",U"•internal.PureKeep",U"•internal.Refc",U"•internal.Squeeze",U"•internal.Temp",U"•internal.Type",U"•internal.Unshare",U"•internal.Variation", U"•internal.ClearRefs",U"•internal.DeepSqueeze",U"•internal.EEqual",U"•internal.ElType",U"•internal.GC",U"•internal.HasFill",U"•internal.HeapDump",U"•internal.HeapStats",U"•internal.Info",U"•internal.IsPure",U"•internal.Keep",U"•internal.ListVariations",U"•internal.ObjFlags",U"•internal.PureKeep",U"•internal.Refc",U"•internal.Squeeze",U"•internal.Temp",U"•internal.Type",U"•internal.Unshare",U"•internal.Validate",U"•internal.Variation",
U"•math.Acos",U"•math.Acosh",U"•math.Asin",U"•math.Asinh",U"•math.Atan",U"•math.Atan2",U"•math.Atanh",U"•math.Cbrt",U"•math.Comb",U"•math.Cos",U"•math.Cosh",U"•math.Erf",U"•math.ErfC",U"•math.Expm1",U"•math.Fact",U"•math.GCD",U"•math.Hypot",U"•math.LCM",U"•math.Log10",U"•math.Log1p",U"•math.Log2",U"•math.LogFact",U"•math.Sin",U"•math.Sinh",U"•math.Sum",U"•math.Tan",U"•math.Tanh", U"•math.Acos",U"•math.Acosh",U"•math.Asin",U"•math.Asinh",U"•math.Atan",U"•math.Atan2",U"•math.Atanh",U"•math.Cbrt",U"•math.Comb",U"•math.Cos",U"•math.Cosh",U"•math.Erf",U"•math.ErfC",U"•math.Expm1",U"•math.Fact",U"•math.GCD",U"•math.Hypot",U"•math.LCM",U"•math.Log10",U"•math.Log1p",U"•math.Log2",U"•math.LogFact",U"•math.Sin",U"•math.Sinh",U"•math.Sum",U"•math.Tan",U"•math.Tanh",
U"•ns.Get",U"•ns.Has",U"•ns.Keys", U"•ns.Get",U"•ns.Has",U"•ns.Keys",

View File

@ -245,6 +245,7 @@ B vec_addN(B w, B x); // consumes both; fills may be wrong
B vec_join(B w, B x); // consumes both B vec_join(B w, B x); // consumes both
i32 num_fmt(char buf[30], f64 x); i32 num_fmt(char buf[30], f64 x);
#define NUM_FMT_BUF(N,X) char N[30]; num_fmt(N, X); #define NUM_FMT_BUF(N,X) char N[30]; num_fmt(N, X);
B do_fmt(B s, char* p, va_list a);
B append_fmt(B s, char* p, ...); B append_fmt(B s, char* p, ...);
B make_fmt(char* p, ...); B make_fmt(char* p, ...);
void print_fmt(char* p, ...); void print_fmt(char* p, ...);
@ -261,6 +262,7 @@ void fprint_fmt(FILE* f, char* p, ...);
#define C1(F, X) F##_c1(m_f64(0), X) #define C1(F, X) F##_c1(m_f64(0), X)
#define C2(F,W,X) F##_c2(m_f64(0),W,X) #define C2(F,W,X) F##_c2(m_f64(0),W,X)
bool validate_flags(bool crash, B x);
char* type_repr(u8 u); char* type_repr(u8 u);
char* pfn_repr(u8 u); char* pfn_repr(u8 u);
char* pm1_repr(u8 u); char* pm1_repr(u8 u);

View File

@ -416,3 +416,6 @@ a←⋈"Ai32"•internal.Variation ↕10 ⋄ •internal.Type ⊑•internal.Dee
abc •internal.Unshare {a2},"hi",3 abc %% 01, "hi", 3 abc •internal.Unshare {a2},"hi",3 abc %% 01, "hi", 3
# •internal.EEqual # •internal.EEqual
%USE nan a1nans•ParseFloat¨"0""1.2""-0" a •internal.EEqual a %% 1 %USE nan a1nans•ParseFloat¨"0""1.2""-0" a •internal.EEqual a %% 1
# •internal.Validate
! •internal.Validate 10
! •internal.Validate 1000