Add static, runtime and debug assert utilities

This commit is contained in:
2025-08-09 21:36:54 +01:00
parent 75be2316e0
commit b8c548ee4b
29 changed files with 100 additions and 56 deletions

View File

@@ -46,7 +46,10 @@ def make_array(user_datatypes: Dict[CDataType, ArrayData] = {}):
out_dir.mkdir(parents=True, exist_ok=True)
common_includes: List[CInclude] = [
CInclude(header="stdbool.h"),
CInclude(
header=str(convert_to_relative(WAPP_SRC_ROOT / "common" / "aliases" / "aliases.h", out_dir)).replace("\\", "/"),
local=True,
),
CInclude(
header=str(convert_to_relative(WAPP_SRC_ROOT / "primitives" / "mem_allocator" / "mem_allocator.h", out_dir)).replace("\\", "/"),
local=True,
@@ -103,7 +106,10 @@ def make_array(user_datatypes: Dict[CDataType, ArrayData] = {}):
includes=[
CInclude(header, local=True, same_dir=True),
CInclude(header="stddef.h"),
CInclude(header="assert.h"),
CInclude(
header=str(convert_to_relative(WAPP_SRC_ROOT / "common" / "assert" / "assert.h", out_dir)).replace("\\", "/"),
local=True
),
],
internal_funcs=[],
funcs=header.funcs

View File

@@ -1,4 +1,4 @@
assert(allocator != NULL);
wapp_debug_assert(allocator != NULL, "`array` should not be NULL");
u64 allocation_size = sizeof({ArrayType}) + item_size * capacity;
{ArrayType} *array = wapp_mem_allocator_alloc(allocator, allocation_size);

View File

@@ -1,4 +1,4 @@
assert(allocator != NULL && array != NULL);
wapp_debug_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL");
{ArrayType} *output = array;

View File

@@ -1,4 +1,5 @@
assert(array != NULL && array->count < array->capacity);
wapp_debug_assert(array != NULL, "`array` should not be NULL");
wapp_runtime_assert(array->count < array->capacity, "`array` is full");
u64 index = (array->count)++;
wapp_{Tlower}_array_set(array, index, item);

View File

@@ -1,4 +1,5 @@
assert(array != NULL && index < array->count);
wapp_debug_assert(array != NULL, "`array` should not be NULL");
wapp_runtime_assert(index < array->count, "`index` is out of bounds");
u8 *ptr = (u8 *)(array->items) + (array->item_size * index);
return ({T} *)ptr;

View File

@@ -1,2 +1,2 @@
assert(array != NULL);
wapp_debug_assert(array != NULL, "`array` should not be NULL");
array->count = 0;

View File

@@ -1,4 +1,4 @@
assert(allocator != NULL && src != NULL && dst != NULL);
wapp_debug_assert(allocator != NULL && src != NULL && dst != NULL, "`allocator`, `src` and `dst` should not be NULL");
{ArrayType} *output = dst;

View File

@@ -1,4 +1,4 @@
assert(src != NULL && dst != NULL);
wapp_debug_assert(src != NULL && dst != NULL, "`src` and `dst` should not be NULL");
wapp_{Tlower}_array_clear(dst);

View File

@@ -1,4 +1,4 @@
assert(allocator != NULL && array != NULL && other != NULL);
wapp_debug_assert(allocator != NULL && array != NULL && other != NULL, "`allocator`, `array` and `other` should not be NULL");
{ArrayType} *output = array;

View File

@@ -1,7 +1,7 @@
assert(array != NULL && other != NULL);
wapp_debug_assert(array != NULL && other != NULL, "`array` and `other` should not be NULL");
u64 remaining_capacity = array->capacity - array->count;
assert(other->count < remaining_capacity);
wapp_runtime_assert(other->count < remaining_capacity, "`array` does not have enough capacity");
{T} *item;

View File

@@ -2,7 +2,7 @@ from pathlib import Path
from dataclasses import dataclass, field
from typing import List, Dict
from codegen.constants import WAPP_SRC_ROOT
from codegen.utils import load_func_body_from_file
from codegen.utils import load_func_body_from_file, convert_to_relative
from codegen.datatypes import (
CDataType,
CMacro,
@@ -49,7 +49,10 @@ def make_dbl_list(user_datatypes: Dict[CDataType, DblListData] = {}):
out_dir.mkdir(parents=True, exist_ok=True)
common_includes: List[CInclude] = [
CInclude(header="stdbool.h")
CInclude(
header=str(convert_to_relative(WAPP_SRC_ROOT / "common" / "aliases" / "aliases.h", out_dir)).replace("\\", "/"),
local=True,
)
]
common_decl_types: List[CStruct] = []
@@ -95,6 +98,10 @@ def make_dbl_list(user_datatypes: Dict[CDataType, DblListData] = {}):
decl_types=[*common_decl_types],
includes=[
CInclude(header, local=True, same_dir=True),
CInclude(
header=str(convert_to_relative(WAPP_SRC_ROOT / "common" / "assert" / "assert.h", out_dir)).replace("\\", "/"),
local=True
),
CInclude(header="stddef.h"),
CInclude(header="assert.h"),
],

View File

@@ -1,4 +1,4 @@
assert(list != NULL);
wapp_debug_assert(list != NULL, "`list` should not be NULL");
u64 count = list->node_count;
for (u64 i = 0; i < count; ++i) {{

View File

@@ -1,4 +1,4 @@
assert(index < list->node_count);
wapp_runtime_assert(index < list->node_count, "`index` is out of bounds");
{NodeType} *output = NULL;
{NodeType} *current = list->first;

View File

@@ -1,4 +1,4 @@
assert(list != NULL && node != NULL && (node->item) != NULL);
wapp_debug_assert(list != NULL && node != NULL && (node->item) != NULL, "`list`, `node` and `node->item` should not be NULL");
if (index == 0) {{
wapp_{Tlower}_list_push_front(list, node);

View File

@@ -1,4 +1,4 @@
assert(list != NULL);
wapp_debug_assert(list != NULL, "`list` should not be NULL");
{NodeType} *output = NULL;

View File

@@ -1,4 +1,4 @@
assert(list != NULL);
wapp_debug_assert(list != NULL, "`list` should not be NULL");
{NodeType} *output = NULL;

View File

@@ -1,4 +1,4 @@
assert(list != NULL && node != NULL && (node->item) != NULL);
wapp_debug_assert(list != NULL && node != NULL && (node->item) != NULL, "`list`, `node` and `node->item` should not be NULL");
{ListType} node_list = {Tlower}_node_to_list(node);

View File

@@ -1,4 +1,4 @@
assert(list != NULL && node != NULL && (node->item) != NULL);
wapp_debug_assert(list != NULL && node != NULL && (node->item) != NULL, "`list`, `node` and `node->item` should not be NULL");
{ListType} node_list = {Tlower}_node_to_list(node);

View File

@@ -1,4 +1,4 @@
assert(list != NULL);
wapp_debug_assert(list != NULL, "`list` should not be NULL");
{NodeType} *output = NULL;