add check_bounds to [meta]

This commit is contained in:
Judah Caruso 2025-05-27 00:24:58 -06:00
parent a1df989324
commit e52a9b0275
4 changed files with 21 additions and 17 deletions

View file

@ -23,19 +23,19 @@ m4d :: (diag: float) -> Mat4 #expand {
} }
operator [] :: inline (m: Mat4, idx: int) -> Vec4 #expand { operator [] :: inline (m: Mat4, idx: int) -> Vec4 #expand {
bounds_check_index(idx * 4, m.N); meta.check_bounds(idx * 4, m.N);
return (m.components[idx * 4]).(*Vec4).*; return (m.components[idx * 4]).(*Vec4).*;
} }
operator *[] :: inline (m: *Mat4, $$idx: int) -> *Vec4 #no_abc { operator *[] :: inline (m: *Mat4, $$idx: int) -> *Vec4 #no_abc {
N :: Mat4.{}.N; // @note(judah): dumb workaround for not being able to access pointer type field constants N :: Mat4.{}.N; // @note(judah): dumb workaround for not being able to access pointer type field constants
bounds_check_index(idx * 4, N); meta.check_bounds(idx * 4, N);
return (*m.components[idx * 4]).(*Vec4); return (*m.components[idx * 4]).(*Vec4);
} }
operator []= :: inline (m: *Mat4, $$idx: int, value: Vec4) #no_abc { operator []= :: inline (m: *Mat4, $$idx: int, value: Vec4) #no_abc {
N :: Mat4.{}.N; N :: Mat4.{}.N;
bounds_check_index(idx * 4, N); meta.check_bounds(idx * 4, N);
ptr := (*m.components[idx * 4]).(*Vec4); ptr := (*m.components[idx * 4]).(*Vec4);
ptr.*= value; ptr.*= value;

View file

@ -22,18 +22,8 @@ F64_Min, F64_Max :: #run meta.lo_for(float64), #run meta.hi_for(float64);
#load "mat.jai"; #load "mat.jai";
#load "ease.jai"; #load "ease.jai";
#scope_module; #scope_module;
bounds_check_index :: ($$idx: int, $count: int, loc := #caller_location) #expand {
#if is_constant(idx) {
#assert (idx >= 0 && idx < count) "bounds check failed";
}
else {
basic.assert(idx >= 0 && idx < count, "bounds check failed! index: % (min: 0, max: %)", idx, count, loc = loc);
}
}
#if RUN_TESTS { #if RUN_TESTS {
test :: #import "jc/test"; test :: #import "jc/test";
} }

View file

@ -71,17 +71,17 @@ Vec :: struct(N: int, T: Type)
} }
operator [] :: (v: Vec, $$idx: int) -> v.T #no_abc { operator [] :: (v: Vec, $$idx: int) -> v.T #no_abc {
bounds_check_index(idx, v.N); meta.check_bounds(idx, v.N);
return v.components[idx]; return v.components[idx];
} }
operator *[] :: (v: *Vec, $$idx: int) -> *v.T #no_abc { operator *[] :: (v: *Vec, $$idx: int) -> *v.T #no_abc {
bounds_check_index(idx, v.N); meta.check_bounds(idx, v.N);
return *v.components[idx]; return *v.components[idx];
} }
operator []= :: (v: *Vec, $$idx: int, value: v.T) #no_abc { operator []= :: (v: *Vec, $$idx: int, value: v.T) #no_abc {
bounds_check_index(idx, v.N); meta.check_bounds(idx, v.N);
v.components[idx] = value; v.components[idx] = value;
} }

View file

@ -19,6 +19,18 @@ get_stack_trace_caller_location :: (loc := #caller_location) -> Source_Code_Loca
return cur.info.location; return cur.info.location;
} }
check_bounds :: ($$index: $T, $$count: T, loc := #caller_location) #expand {
#if is_constant(index) && is_constant(count) {
if index < 0 || index >= count {
message := basic.tprint("bounds check failed! index was % (max %)", index, count - 1);
compiler.compiler_report(message, mode = .ERROR, loc = loc);
}
}
else {
basic.assert(index >= 0 && index < count, "bounds check failed! index was % (max %)", index, count -1, loc = loc);
}
}
// Can be passed directly to using,map // Can be passed directly to using,map
remap_snake_to_pascal :: (names: []string) { remap_snake_to_pascal :: (names: []string) {
for names { for names {
@ -27,7 +39,6 @@ remap_snake_to_pascal :: (names: []string) {
} }
snake_to_pascal :: (name: string) -> string { snake_to_pascal :: (name: string) -> string {
basic :: #import "Basic"; // @future
b: basic.String_Builder; b: basic.String_Builder;
upper := true; upper := true;
@ -57,6 +68,9 @@ snake_to_pascal :: (name: string) -> string {
mem :: #import "jc/memory"; mem :: #import "jc/memory";
basic :: #import "Basic"; // @future
compiler :: #import "Compiler"; // @future
// ---------------------------------------------------------- // ----------------------------------------------------------
// TESTS // TESTS