From: Brendan Hansen Date: Mon, 7 Jun 2021 05:14:12 +0000 (-0500) Subject: changed assert to use #callsite X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=ea5e1f7109c61a6f82017d77450ffd3cbbd8f7f9;p=onyx.git changed assert to use #callsite --- diff --git a/bin/onyx b/bin/onyx index dd29ce75..76241afa 100755 Binary files a/bin/onyx and b/bin/onyx differ diff --git a/core/builtin.onyx b/core/builtin.onyx index 664a617e..254d7d54 100644 --- a/core/builtin.onyx +++ b/core/builtin.onyx @@ -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; diff --git a/core/runtime/common.onyx b/core/runtime/common.onyx index 917a97fa..8a3f719a 100644 --- a/core/runtime/common.onyx +++ b/core/runtime/common.onyx @@ -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");