changed assert to use #callsite
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 7 Jun 2021 05:14:12 +0000 (00:14 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 7 Jun 2021 05:14:12 +0000 (00:14 -0500)
bin/onyx
core/builtin.onyx
core/runtime/common.onyx

index dd29ce75741bcf6232bd1591728e77787ba9751b..76241afab918a9a7f027aaf386afbd1f0334a71a 100755 (executable)
Binary files a/bin/onyx and b/bin/onyx differ
index 664a617e317b39f308b5308f12235a2b2b145efa..254d7d5473c531250f6ef2b2fa94fe44bb9b8c87 100644 (file)
@@ -55,7 +55,7 @@ OnyxContext :: struct {
 
     logger         : Logger = .{ default_logger, null };
 
-    assert_handler : (msg: str, file: str) -> void;
+    assert_handler : (msg: str, site: CallSite) -> void;
 }
 
 #if runtime.Runtime != runtime.Runtime_Custom {
@@ -79,8 +79,8 @@ OnyxContext :: struct {
 // first parameter to ALL procedures in a compilation unit.
 context : OnyxContext;
 
-assert :: (cond: bool, msg: str, file: str = null_str) {
-    if !cond do context.assert_handler(msg, file);
+assert :: (cond: bool, msg: str, site := #callsite) {
+    if !cond do context.assert_handler(msg, site);
 }
 
 
@@ -170,6 +170,11 @@ Iterator :: struct (T: type_expr) {
 }
 
 
+
+// This structure represents the result of a '#callsite' expression. Currently,
+// #callsite is only valid (and parsed) as a default value for a procedure parameter.
+// It allows the function to get the address of the calling site, which can be
+// used for error printing, unique hashes, and much more.
 CallSite :: struct {
     file   : str;
     line   : u32;
index 917a97fa0a9bc2d9b83de18e708978acfd814019..8a3f719ac331039ed4a35c1f13c798f84a2ece71 100644 (file)
@@ -5,13 +5,13 @@ use package core.intrinsics.onyx { __initialize }
 
 // The default assert handler. This assumes that __output_string
 // and __exit are defined in the 'runtime' package.
-__assert_handler :: (msg: str, file: str) {
+__assert_handler :: (msg: str, site: CallSite) {
     __output_string("Assert failed: ");
     __output_string(msg);
 
-    if file.data != null {
+    if site.file.data != null {
         __output_string(" in ");
-        __output_string(file);
+        __output_string(site.file);
     }
 
     __output_string("\n");