1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright (c) Cole Frederick. All rights reserved.
// The use and distribution terms for this software are covered by the
// Eclipse Public License 1.0 (https://opensource.org/licenses/eclipse-1.0.php)
// which can be found in the file epl-v10.html at the root of this distribution.
// By using this software in any fashion, you are agreeing to be bound by the terms of this license.
// You must not remove this notice, or any other, from this software.


//#[cfg(target_arch = "wasm32")]
mod web_console {
    #[link(wasm_import_module = "console")]
    extern {
        fn _console_log(byte_address: u32, byte_count: u32);
        fn _console_warn(byte_address: u32, byte_count: u32);
        fn _console_error(byte_address: u32, byte_count: u32);
        fn _console_panic_error(byte_address: u32, byte_count: u32);
        fn _console_group(byte_address: u32, byte_count: u32);
        fn _console_group_end();
    }
    pub fn log(s: &str) {
        unsafe { _console_log(s.as_ptr() as usize as u32, s.len() as u32) }
    }
    pub fn warn(s: &str) {
        unsafe { _console_warn(s.as_ptr() as usize as u32, s.len() as u32); }
    }
    pub fn error(s: &str) {
        unsafe { _console_error(s.as_ptr() as usize as u32, s.len() as u32); }
    }
    pub fn panic_error(s: &str) {
        unsafe { _console_panic_error(s.as_ptr() as usize as u32, s.len() as u32); }
    }
    pub fn group(s: &str) {
        unsafe { _console_group(s.as_ptr() as usize as u32, s.len() as u32); }
    }
    pub fn group_end() {
        unsafe { _console_group_end(); }
    }


    #[link(wasm_import_module = "performance")]
    extern {
        fn _performance_mark(byte_address: u32, byte_count: u32);
        //fn _performance_measure(byte_address: u32, byte_count: u32);
        //fn _performance_duration();
        //fn _performance_entries(byte_address: u32, byte_count: u32);
        //fn _performance_clear_marks(byte_address: u32, byte_count: u32);
        //fn _performance_clear_measures(byte_address: u32, byte_count: u32);
    }
    pub fn mark(s: &str) {
        unsafe { _performance_mark(s.as_ptr() as usize as u32, s.len() as u32); }
    }
}
// Console, web impl and eprint
// Performance, web impl and std::time
pub fn log(s: &str) {
    #[cfg(target_arch = "wasm32")]
        { web_console::log(s) }
    #[cfg(not(target_arch = "wasm32"))]
        { eprintln!("{}", s) }
}
pub fn warn(s: &str) {
    #[cfg(target_arch = "wasm32")]
        { web_console::warn(s) }
    #[cfg(not(target_arch = "wasm32"))]
        { eprintln!("{}", s) }
}
pub fn error(s: &str) {
    #[cfg(target_arch = "wasm32")]
        { web_console::error(s) }
    #[cfg(not(target_arch = "wasm32"))]
        { eprintln!("{}", s) }
}
pub fn panic_error(s: &str) {
    #[cfg(target_arch = "wasm32")]
        { web_console::panic_error(s) }
    #[cfg(not(target_arch = "wasm32"))]
        { eprintln!("{}", s) }
}
pub fn group(s: &str) {
    #[cfg(target_arch = "wasm32")]
        { web_console::group(s) }
    #[cfg(not(target_arch = "wasm32"))]
        { eprintln!("{}", s) }
}
pub fn group_end() {
    #[cfg(target_arch = "wasm32")]
        { web_console::group_end() }
}
pub fn mark(s: &str) {
    #[cfg(target_arch = "wasm32")]
        { web_console::mark(s) }
    #[cfg(not(target_arch = "wasm32"))]
        {  }
}

use std::cell::Cell;
thread_local! {
    pub static GROUP_DEPTH: Cell<u32> = Cell::new(0);
    // incrementing id, for timing potentially recursive computations, reentrant
    pub static ID_COUNTER: Cell<u32> = Cell::new(0);
}

pub fn slice_out_src(s: &'static str) -> &'static str {
    unimplemented!()
}

macro_rules! log {
    ($msg:expr) => {
        if cfg!(feature = "trace") {
            let _print = format!(concat!($msg, "%c     www.fress.io/src/fress/{}.html#", line!()),
                                 &file!()[4..]);
            $crate::trace::log(&_print);
        }
    };
    ($msg:expr, $ ($arg:tt) *) => ({
        if cfg!(feature = "trace") {
            let _print = format!(concat!($msg, "%c     www.fress.io/src/fress/{}.html#", line!()),
                                 $ ($arg) *, &file!()[4..]);
            $crate::trace::log(&_print);
        }
    });
}
macro_rules! warn {
    ($msg:expr) => {
        if cfg!(feature = "trace") {
            let _print = format!(concat!($msg, "%c     www.fress.io/src/fress/{}.html#", line!()),
                                 &file!()[4..]);
            $crate::trace::warn(&_print);
        }
    };
    ($msg:expr, $ ($arg:tt) *) => ({
        if cfg!(feature = "trace") {
            let _print = format!(concat!($msg, "%c     www.fress.io/src/fress/{}.html#", line!()),
                                 $ ($arg) *, &file!()[4..]);
            $crate::trace::warn(&_print);
        }
    });
}
macro_rules! error {
    ($msg:expr) => {
        if cfg!(feature = "trace") {
            let _print = format!(concat!($msg, "%c     www.fress.io/src/fress/{}.html#", line!()),
                                 &file!()[4..]);
            $crate::trace::error(&_print);
        }
    };
    ($msg:expr, $ ($arg:tt) *) => ({
        if cfg!(feature = "trace") {
            let _print = format!(concat!($msg, "%c     www.fress.io/src/fress/{}.html#", line!()),
                                 $ ($arg) *, &file!()[4..]);
            $crate::trace::error(&_print);
        }
    });
}
macro_rules! group {
    ($msg:expr) => {
        if cfg!(feature = "trace") {
            let _print = format!(concat!($msg, "%c     www.fress.io/src/fress/{}.html#", line!()),
                                 &file!()[4..]);
            $crate::trace::group(&_print);
        }
    };
    ($msg:expr, $ ($arg:tt) *) => ({
        if cfg!(feature = "trace") {
            let _print = format!(concat!($msg, "%c     www.fress.io/src/fress/{}.html#", line!()),
                                 $ ($arg) *, &file!()[4..]);
            $crate::trace::group(&_print);
        }
    });
}
macro_rules! group_end {
    () => {
        if cfg!(feature = "trace") {
            $crate::trace::group_end();
        }
    };
}