| 1 | module builtin |
| 2 | |
| 3 | // ChanState describes the result of an attempted channel transaction. |
| 4 | pub enum ChanState { |
| 5 | success |
| 6 | not_ready // push()/pop() would have to wait, but no_block was requested |
| 7 | closed |
| 8 | } |
| 9 | |
| 10 | /* |
| 11 | The following methods are only stubs. |
| 12 | The real implementation is in `vlib/sync/channels.v` |
| 13 | */ |
| 14 | |
| 15 | // close closes the channel for further push transactions. |
| 16 | // closed channels cannot be pushed to, and after the buffer is drained |
| 17 | // `<-ch or {}` and `<-ch?` use `err` or default to `channel closed`. |
| 18 | pub fn (ch chan) close(err ...IError) {} |
| 19 | |
| 20 | // try_pop returns `ChanState.success` if an object is popped from the channel. |
| 21 | // try_pop effectively pops from the channel without waiting for objects to become available. |
| 22 | // Both the test and pop transaction is done atomically. |
| 23 | // Pass the destination as `mut`: `ch.try_pop(mut value)`, not `ch.try_pop(&value)`. |
| 24 | pub fn (ch chan) try_pop(obj voidptr) ChanState { |
| 25 | return .success |
| 26 | } |
| 27 | |
| 28 | // try_push returns `ChanState.success` if the object is pushed to the channel. |
| 29 | // try_push effectively both push and test if the transaction `ch <- a` succeeded. |
| 30 | // Both the test and push transaction is done atomically. |
| 31 | pub fn (ch chan) try_push(obj voidptr) ChanState { |
| 32 | return .success |
| 33 | } |
| 34 | |
| 35 | // IError holds information about an error instance. |
| 36 | pub interface IError { |
| 37 | msg() string |
| 38 | code() int |
| 39 | } |
| 40 | |
| 41 | struct _result { |
| 42 | is_error bool |
| 43 | err IError = none__ |
| 44 | // Data is trailing after err |
| 45 | // and is not included in here but in the |
| 46 | // derived Result_xxx types |
| 47 | } |
| 48 | |
| 49 | fn _result_ok(data voidptr, mut res _result, size int) { |
| 50 | unsafe { |
| 51 | *res = _result{} |
| 52 | // use err to get the end of ResultBase and then memcpy into it |
| 53 | vmemcpy(&u8(&res.err) + sizeof(IError), data, size) |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | // str returns the message of IError. |
| 58 | pub fn (err IError) str() string { |
| 59 | if err is None__ { |
| 60 | return 'none' |
| 61 | } |
| 62 | c := err.code() |
| 63 | if c > 0 { |
| 64 | return err.msg() + '; code: ' + c.str() |
| 65 | } |
| 66 | return err.msg() |
| 67 | } |
| 68 | |
| 69 | // Error is the empty default implementation of `IError`. |
| 70 | pub struct Error {} |
| 71 | |
| 72 | pub fn (err Error) msg() string { |
| 73 | return '' |
| 74 | } |
| 75 | |
| 76 | pub fn (err Error) code() int { |
| 77 | return 0 |
| 78 | } |
| 79 | |
| 80 | // MessageError is the default implementation of the `IError` interface that is returned by the `error()` function. |
| 81 | struct MessageError { |
| 82 | pub: |
| 83 | msg string |
| 84 | code int |
| 85 | } |
| 86 | |
| 87 | // str returns both the .msg and .code of MessageError, when .code is != 0 . |
| 88 | pub fn (err MessageError) str() string { |
| 89 | if err.code > 0 { |
| 90 | return '${err.msg}; code: ${err.code}' |
| 91 | } |
| 92 | return err.msg |
| 93 | } |
| 94 | |
| 95 | // msg returns only the message of MessageError. |
| 96 | pub fn (err MessageError) msg() string { |
| 97 | return err.msg |
| 98 | } |
| 99 | |
| 100 | // code returns only the code of MessageError. |
| 101 | pub fn (err MessageError) code() int { |
| 102 | return err.code |
| 103 | } |
| 104 | |
| 105 | @[unsafe] |
| 106 | pub fn (err &MessageError) free() { |
| 107 | unsafe { err.msg.free() } |
| 108 | } |
| 109 | |
| 110 | @[if trace_error ?] |
| 111 | fn trace_error(x string) { |
| 112 | eprintln('> ${@FN} | ${x}') |
| 113 | } |
| 114 | |
| 115 | // error returns a default error instance containing the error given in `message`. |
| 116 | // Example: f := fn (ouch bool) ! { if ouch { return error('an error occurred') } }; f(false)! |
| 117 | @[inline] |
| 118 | pub fn error(message string) IError { |
| 119 | trace_error(message) |
| 120 | return &MessageError{ |
| 121 | msg: message |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // error_with_code returns a default error instance containing the given `message` and error `code`. |
| 126 | // Example: f := fn (ouch bool) ! { if ouch { return error_with_code('an error occurred', 1) } }; f(false)! |
| 127 | @[inline] |
| 128 | pub fn error_with_code(message string, code int) IError { |
| 129 | trace_error('${message} | code: ${code}') |
| 130 | return &MessageError{ |
| 131 | msg: message |
| 132 | code: code |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | // Option is the base of V's internal option return system. |
| 137 | struct Option { |
| 138 | state u8 // 0 - ok; 2 - none; 1 - ? |
| 139 | err IError = none__ |
| 140 | // Data is trailing after err |
| 141 | // and is not included in here but in the |
| 142 | // derived Option_xxx types |
| 143 | } |
| 144 | |
| 145 | // option is the base of V's internal option return system. |
| 146 | struct _option { |
| 147 | state u8 |
| 148 | err IError = none__ |
| 149 | // Data is trailing after err |
| 150 | // and is not included in here but in the |
| 151 | // derived _option_xxx types |
| 152 | } |
| 153 | |
| 154 | fn _option_none(data voidptr, mut option _option, size int) { |
| 155 | unsafe { |
| 156 | *option = _option{ |
| 157 | state: 2 |
| 158 | } |
| 159 | // use err to get the end of OptionBase and then memcpy into it |
| 160 | vmemcpy(&u8(&option.err) + sizeof(IError), data, size) |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | fn _option_ok(data voidptr, mut option _option, size int) { |
| 165 | unsafe { |
| 166 | *option = _option{} |
| 167 | // use err to get the end of OptionBase and then memcpy into it |
| 168 | vmemcpy(&u8(&option.err) + sizeof(IError), data, size) |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | fn _option_clone(current &_option, mut option _option, size int) { |
| 173 | unsafe { |
| 174 | *option = _option{ |
| 175 | state: current.state |
| 176 | err: current.err |
| 177 | } |
| 178 | // use err to get the end of OptionBase and then memcpy into it |
| 179 | vmemcpy(&u8(&option.err) + sizeof(IError), &u8(¤t.err) + sizeof(IError), size) |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | // |
| 184 | |
| 185 | const none__ = IError(&None__{}) |
| 186 | |
| 187 | struct None__ { |
| 188 | Error |
| 189 | } |
| 190 | |
| 191 | fn (_ None__) str() string { |
| 192 | return 'none' |
| 193 | } |
| 194 | |
| 195 | // str for none, returns 'none' |
| 196 | pub fn (_ none) str() string { |
| 197 | return 'none' |
| 198 | } |
| 199 | |