/*---------------------------------------------------------------------------
  Copyright 2023-2024, Microsoft Research, Daan Leijen.

  This is free software; you can redistribute it and/or modify it under the
  terms of the Apache License, Version 2.0. A copy of the License can be
  found in the LICENSE file at the root of this distribution.
---------------------------------------------------------------------------*/

// Core debugging functions.
module std/core/debugstd/core/debug

import std/core/typesstd/core/types
import std/core/unsafestd/core/unsafe
import std/core/hndstd/core/hnd
import std/core/stringstd/core/string
import std/core/consolestd/core/console

extern import
  c file "inline/debug.c"

// ------------------------------------------------------------------------------
// File locations
// ------------------------------------------------------------------------------

// Compilation constant that is replaced with the current file's module name
pub val file/kk-modulestd/core/debug/file/kk-module: string : stringstd/core/types/string: V = ""literal: string
count= 0
// Compilation constant that is replaced with the current line number pub val file/kk-linestd/core/debug/file/kk-line: string : stringstd/core/types/string: V = ""literal: string
count= 0
// Compilation constant that is replaced with the current file name pub val file/kk-filestd/core/debug/file/kk-file: string : stringstd/core/types/string: V = ""literal: string
count= 0
pub fun file/kk-file-linestd/core/debug/file/kk-file-line: (@implicit/kk-file : string, @implicit/kk-line : string) -> string( @implicit/kk-file?kk-file: string, @implicit/kk-line?kk-line: string )result: -> total string @implicit/kk-file?kk-file: string ++std/core/types/(++): (x : string, y : string) -> string "("literal: string
count= 1
++std/core/types/(++): (x : string, y : string) -> string @implicit/kk-line?kk-line: string ++std/core/types/(++): (x : string, y : string) -> string ")"literal: string
count= 1
// ---------------------------------------------------------------------------- // Trace, assert, todo // ---------------------------------------------------------------------------- extern xtracestd/core/debug/xtrace: (message : string) -> () : ( message : stringstd/core/types/string: V ) -> (std/core/types/unit: V)std/core/types/unit: V c "kk_trace" cs "Primitive.Trace" js "$std_core_console._trace" extern xtrace-anystd/core/debug/xtrace-any: forall<a> (message : string, x : a) -> () : forall<aa: V> ( message: stringstd/core/types/string: V, x : aa: V ) -> (std/core/types/unit: V)std/core/types/unit: V c "kk_trace_any" cs "Primitive.TraceAny" js "$std_core_console._trace_any" val trace-enabledstd/core/debug/trace-enabled: ref<global,bool> : refstd/core/types/ref: (H, V) -> V<globalstd/core/types/global: H,boolstd/core/types/bool: V> = unsafe-totalstd/core/unsafe/unsafe-total: (action : () -> <alloc<global>|_122> ref<global,bool>) -> ref<global,bool>{ refstd/core/types/ref: (value : bool) -> <alloc<global>|_122> ref<global,bool>(Truestd/core/types/True: bool) } // Trace a message used for debug purposes. // The behaviour is system dependent. On a browser and node it uses // `console.log` by default. // Disabled if `notrace` is called. pub fun tracestd/core/debug/trace: (message : string) -> ()( messagemessage: string : stringstd/core/types/string: V )result: -> total () : (std/core/types/unit: V)std/core/types/unit: V unsafe-totalstd/core/unsafe/unsafe-total: (action : () -> <read<global>|_150> ()) -> () if !std/core/types/ref/(!): (ref : ref<global,bool>) -> <read<global>|_150> booltrace-enabledstd/core/debug/trace-enabled: ref<global,bool> then xtracestd/core/debug/xtrace: (message : string) -> <read<global>|_150> ()(messagemessage: string)std/core/types/Unit: () pub fun trace-infostd/core/debug/trace-info: (message : string, @implicit/kk-file-line : string) -> ()( messagemessage: string : stringstd/core/types/string: V, @implicit/kk-file-line?kk-file-line: string : stringstd/core/types/string: V )result: -> total () : (std/core/types/unit: V)std/core/types/unit: V tracestd/core/debug/trace: (message : string) -> ()(@implicit/kk-file-line?kk-file-line: string ++std/core/types/(++): (x : string, y : string) -> string ": "literal: string
count= 2
++std/core/types/(++): (x : string, y : string) -> string messagemessage: string
) pub fun trace-showstd/core/debug/trace-show: forall<a> (x : a, @implicit/show : (a) -> string, @implicit/kk-file-line : string) -> ()( xx: $185 : aa: V, @implicit/show?show: ($185) -> string : aa: V -> stringstd/core/types/string: V, @implicit/kk-file-line?kk-file-line: string : stringstd/core/types/string: V )result: -> total () : (std/core/types/unit: V)std/core/types/unit: V trace-infostd/core/debug/trace-info: (message : string, @implicit/kk-file-line : string) -> ()
?kk-file-line=?kk-file-line
(xx: $185.show?show: ($185) -> string
) pub fun trace-anystd/core/debug/trace-any: forall<a> (message : string, x : a) -> ()( messagemessage: string : stringstd/core/types/string: V, xx: $211 : aa: V )result: -> total () : (std/core/types/unit: V)std/core/types/unit: V unsafe-totalstd/core/unsafe/unsafe-total: (action : () -> <read<global>|_237> ()) -> () if !std/core/types/ref/(!): (ref : ref<global,bool>) -> <read<global>|_237> booltrace-enabledstd/core/debug/trace-enabled: ref<global,bool> then xtrace-anystd/core/debug/xtrace-any: (message : string, x : $211) -> <read<global>|_237> ()(messagemessage: string,xx: $211)std/core/types/Unit: () // Disable tracing completely. pub noinline fun notracestd/core/debug/notrace: () -> (st<global>) ()()result: -> (st<global>) () : ststd/core/types/st: H -> E<globalstd/core/types/global: H> (std/core/types/unit: V)std/core/types/unit: V trace-enabledstd/core/debug/trace-enabled: ref<global,bool> :=std/core/types/set: (ref : ref<global,bool>, assigned : bool) -> <write<global>,alloc<global>,read<global>> () Falsestd/core/types/False: bool noinline extern unsafe-assert-failstd/core/debug/unsafe-assert-fail: (msg : string) -> ()( msgmsg: string : stringstd/core/types/string: V ) : (std/core/types/unit: V)std/core/types/unit: V c "kk_assert_fail" js inline "function() { throw new Error(\"assertion failed: \" + #1) }()" pub fun assertstd/core/debug/assert: (message : string, condition : bool, @implicit/kk-file-line : string) -> ()( messagemessage: string : stringstd/core/types/string: V, conditioncondition: bool : boolstd/core/types/bool: V, @implicit/kk-file-line?kk-file-line: string : stringstd/core/types/string: V )result: -> total () : (std/core/types/unit: V)std/core/types/unit: V // Compiler removes assert calls in optimized builds if !std/core/types/bool/(!): (b : bool) -> boolconditioncondition: bool then unsafe-assert-failstd/core/debug/unsafe-assert-fail: (msg : string) -> ()(kk-file-line?kk-file-line: string ++std/core/types/(++): (x : string, y : string) -> string ": "literal: string
count= 2
++std/core/types/(++): (x : string, y : string) -> string messagemessage: string
)std/core/types/Unit: () // Explicitly trigger a breakpoint pub extern breakpointstd/core/debug/breakpoint: () -> ndet ()() : ndetstd/core/types/ndet: X (std/core/types/unit: V)std/core/types/unit: V { c "kk_debugger_break" cs "System.Diagnostics.Debugger.Break" js inline "(function(){ debugger; })()" }result: -> ndet ()