2022-01-04 12:21:08 +03:00
|
|
|
// Copyright (c) 2019-2022 Alexander Medvednikov. All rights reserved.
|
2019-09-14 23:48:30 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
module builtin
|
|
|
|
|
2020-12-08 19:49:20 +03:00
|
|
|
fn (a any) toString()
|
|
|
|
|
2021-10-20 16:02:21 +03:00
|
|
|
[noreturn]
|
2020-06-20 14:22:49 +03:00
|
|
|
pub fn panic(s string) {
|
2022-11-15 16:53:13 +03:00
|
|
|
eprintln('V panic: ${s}\n${js_stacktrace()}')
|
2020-06-20 14:22:49 +03:00
|
|
|
exit(1)
|
2020-06-04 21:26:18 +03:00
|
|
|
}
|
2020-11-24 14:54:26 +03:00
|
|
|
|
2021-08-25 14:40:53 +03:00
|
|
|
// IError holds information about an error instance
|
|
|
|
pub interface IError {
|
2022-02-11 16:52:33 +03:00
|
|
|
// >> Hack to allow old style custom error implementations
|
|
|
|
// TODO: remove once deprecation period for `IError` methods has ended
|
2021-08-25 14:40:53 +03:00
|
|
|
msg string
|
2022-02-11 16:52:33 +03:00
|
|
|
code int // <<
|
|
|
|
msg() string
|
|
|
|
code() int
|
2021-08-25 14:40:53 +03:00
|
|
|
}
|
|
|
|
|
2022-04-30 12:31:23 +03:00
|
|
|
// str returns the message of IError
|
2022-02-11 16:52:33 +03:00
|
|
|
pub fn (err IError) str() string {
|
|
|
|
return match err {
|
|
|
|
None__ {
|
|
|
|
'none'
|
|
|
|
}
|
|
|
|
Error {
|
|
|
|
err.msg()
|
|
|
|
}
|
|
|
|
MessageError {
|
|
|
|
err.msg()
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// >> Hack to allow old style custom error implementations
|
2022-02-12 12:54:10 +03:00
|
|
|
// TODO: remove once deprecation period for `IError` methods has ended
|
|
|
|
old_error_style := unsafe { voidptr(&err.msg) != voidptr(&err.code) } // if fields are not defined (new style) they don't have an offset between them
|
|
|
|
if old_error_style {
|
2022-11-15 16:53:13 +03:00
|
|
|
'${err.type_name()}: ${err.msg}'
|
2022-02-11 16:52:33 +03:00
|
|
|
} else {
|
|
|
|
// <<
|
2022-11-15 16:53:13 +03:00
|
|
|
'${err.type_name()}: ${err.msg()}'
|
2022-02-11 16:52:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error is the empty default implementation of `IError`.
|
2022-02-12 12:54:10 +03:00
|
|
|
pub struct Error {}
|
2020-11-24 14:54:26 +03:00
|
|
|
|
2022-04-30 12:31:23 +03:00
|
|
|
// msg returns the message of Error
|
2022-02-11 16:52:33 +03:00
|
|
|
pub fn (err Error) msg() string {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
|
2022-04-30 12:31:23 +03:00
|
|
|
// code returns the code of Error
|
2022-02-11 16:52:33 +03:00
|
|
|
pub fn (err Error) code() int {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// MessageError is the default implementation of the `IError` interface that is returned by the `error()` function
|
|
|
|
struct MessageError {
|
|
|
|
pub:
|
2021-08-25 14:40:53 +03:00
|
|
|
msg string
|
|
|
|
code int
|
|
|
|
}
|
|
|
|
|
2022-04-30 12:31:23 +03:00
|
|
|
// msg returns the message of the MessageError
|
2022-02-11 16:52:33 +03:00
|
|
|
pub fn (err MessageError) msg() string {
|
|
|
|
return err.msg
|
|
|
|
}
|
|
|
|
|
2022-04-30 12:31:23 +03:00
|
|
|
// code returns the code of MessageError
|
2022-02-11 16:52:33 +03:00
|
|
|
pub fn (err MessageError) code() int {
|
|
|
|
return err.code
|
|
|
|
}
|
|
|
|
|
|
|
|
pub const none__ = IError(&None__{})
|
|
|
|
|
|
|
|
struct None__ {
|
|
|
|
Error
|
|
|
|
}
|
|
|
|
|
2021-08-25 14:40:53 +03:00
|
|
|
fn (_ None__) str() string {
|
|
|
|
return 'none'
|
|
|
|
}
|
|
|
|
|
2021-08-30 20:47:18 +03:00
|
|
|
pub struct Option {
|
2022-04-15 20:31:10 +03:00
|
|
|
state u8
|
2021-08-30 20:47:18 +03:00
|
|
|
err IError = none__
|
|
|
|
}
|
|
|
|
|
2022-04-30 12:31:23 +03:00
|
|
|
// str returns the Option type: ok, none, or error
|
2020-11-24 14:54:26 +03:00
|
|
|
pub fn (o Option) str() string {
|
2022-05-06 19:25:54 +03:00
|
|
|
if o.state == 0 {
|
|
|
|
return 'Option{ ok }'
|
|
|
|
}
|
|
|
|
if o.state == 1 {
|
|
|
|
return 'Option{ none }'
|
|
|
|
}
|
2022-11-15 16:53:13 +03:00
|
|
|
return 'Option{ error: "${o.err}" }'
|
2022-05-06 19:25:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct _option {
|
|
|
|
state u8
|
|
|
|
err IError = none__
|
|
|
|
}
|
|
|
|
|
|
|
|
// str returns the Option type: ok, none, or error
|
|
|
|
pub fn (o _option) str() string {
|
2021-03-24 21:39:59 +03:00
|
|
|
if o.state == 0 {
|
|
|
|
return 'Option{ ok }'
|
|
|
|
}
|
|
|
|
if o.state == 1 {
|
|
|
|
return 'Option{ none }'
|
|
|
|
}
|
2022-11-15 16:53:13 +03:00
|
|
|
return 'Option{ error: "${o.err}" }'
|
2020-11-24 14:54:26 +03:00
|
|
|
}
|
|
|
|
|
2022-01-09 17:12:33 +03:00
|
|
|
// trace_error prints to stderr a string and a backtrace of the error
|
2021-08-25 14:40:53 +03:00
|
|
|
fn trace_error(x string) {
|
2022-11-15 16:53:13 +03:00
|
|
|
eprintln('> ${@FN} | ${x}')
|
2021-08-25 14:40:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// error returns a default error instance containing the error given in `message`.
|
2022-01-09 17:12:33 +03:00
|
|
|
// Example: if ouch { return error('an error occurred') }
|
2021-08-25 14:40:53 +03:00
|
|
|
[inline]
|
|
|
|
pub fn error(message string) IError {
|
2021-08-29 14:27:17 +03:00
|
|
|
// trace_error(message)
|
2022-02-11 16:52:33 +03:00
|
|
|
return &MessageError{
|
2021-08-25 14:40:53 +03:00
|
|
|
msg: message
|
2020-11-24 14:54:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-25 14:40:53 +03:00
|
|
|
// error_with_code returns a default error instance containing the given `message` and error `code`.
|
2022-01-09 17:12:33 +03:00
|
|
|
// Example: if ouch { return error_with_code('an error occurred', 1) }
|
2021-08-25 14:40:53 +03:00
|
|
|
[inline]
|
|
|
|
pub fn error_with_code(message string, code int) IError {
|
|
|
|
// trace_error('$message | code: $code')
|
2022-02-11 16:52:33 +03:00
|
|
|
return &MessageError{
|
2021-08-25 14:40:53 +03:00
|
|
|
msg: message
|
|
|
|
code: code
|
2020-11-24 14:54:26 +03:00
|
|
|
}
|
|
|
|
}
|
2021-12-03 13:25:36 +03:00
|
|
|
|
2022-01-09 17:12:33 +03:00
|
|
|
// free allows for manually freeing memory allocated at the address `ptr`.
|
|
|
|
// However, this is a no-op on JS backend
|
2021-12-03 13:25:36 +03:00
|
|
|
[unsafe]
|
|
|
|
pub fn free(ptr voidptr) {
|
|
|
|
_ := ptr
|
|
|
|
}
|