diff --git a/docs/system.md b/docs/system.md index d74ae29d..08676005 100644 --- a/docs/system.md +++ b/docs/system.md @@ -108,9 +108,10 @@ Namespace of various internal functions. May change at any time. | `•internal.Variation` | Convert `𝕩` to the variation specified in `𝕨` | | `•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.EEqual` | exactly equal (NaN equals NaN, 0 equals ¯0) | -| `•internal.Temp` | place to test new features or temporarily expose some internal function | -| `•internal.Properties` | various build properties | +| `•internal.EEqual` | Exactly equal (NaN equals NaN, 0 equals ¯0) | +| `•internal.Temp` | Place to test new features or temporarily expose some internal function | +| `•internal.Properties` | Various build properties | +| `•internal.Validate` | Validate that `𝕩` has correct flags set | # FFI diff --git a/src/builtins.h b/src/builtins.h index f0be2ed1..7d301637 100644 --- a/src/builtins.h +++ b/src/builtins.h @@ -20,7 +20,7 @@ /* 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(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*/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˜⁼") \ diff --git a/src/builtins/internal.c b/src/builtins/internal.c index 93ba1f7f..1bc6a7ef 100644 --- a/src/builtins/internal.c +++ b/src/builtins/internal.c @@ -3,6 +3,8 @@ #include "../builtins.h" #include "../ns.h" #include "../utils/cstr.h" +#include "../utils/calls.h" +#include B itype_c1(B t, B x) { B r; @@ -412,6 +414,44 @@ B iProperties_c2(B t, B w, B x) { 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) { if (!isArr(x)) thrM("•internal.Unshare 𝕩: 𝕩 must be an array"); B r = unshare(x); @@ -433,8 +473,8 @@ B getInternalNS(void) { #undef F #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"); - 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)); + 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)F(iValidate)); #undef F gc_add(internalNS); } diff --git a/src/builtins/sysfn.c b/src/builtins/sysfn.c index 2ff9e2cc..fcc32bf8 100644 --- a/src/builtins/sysfn.c +++ b/src/builtins/sysfn.c @@ -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.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"•ns.Get",U"•ns.Has",U"•ns.Keys", diff --git a/src/core/stuff.h b/src/core/stuff.h index 1efd859a..ae4b745a 100644 --- a/src/core/stuff.h +++ b/src/core/stuff.h @@ -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 i32 num_fmt(char buf[30], f64 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 make_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 C2(F,W,X) F##_c2(m_f64(0),W,X) +bool validate_flags(bool crash, B x); char* type_repr(u8 u); char* pfn_repr(u8 u); char* pm1_repr(u8 u); diff --git a/test/cases/system.bqn b/test/cases/system.bqn index 79ae5830..a368ce70 100644 --- a/test/cases/system.bqn +++ b/test/cases/system.bqn @@ -416,3 +416,6 @@ a←⋈"Ai32"•internal.Variation ↕10 ⋄ •internal.Type ⊑•internal.Dee ⟨a⟩‿b‿c ← •internal.Unshare ⟨{a⇐↕2},"hi",3⟩ ⋄ a‿b‿c %% ⟨0‿1, "hi", 3⟩ # •internal.EEqual %USE nan ⋄ a←1⌽nans∾•ParseFloat¨"0"‿"1.2"‿"-0" ⋄ a •internal.EEqual ⌽a %% 1 +# •internal.Validate +! •internal.Validate⊸≡ ↕10 +! •internal.Validate⊸≡ ∨↕1000