1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/builtin/js/builtin.v

86 lines
1.6 KiB
V
Raw Normal View History

// Copyright (c) 2019-2021 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()
[noreturn]
2020-06-20 14:22:49 +03:00
pub fn panic(s string) {
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
}
// IError holds information about an error instance
pub interface IError {
msg string
code int
}
// Error is the default implementation of IError, that is returned by e.g. `error()`
pub struct Error {
pub:
msg string
code int
}
struct None__ {
msg string
code int
}
fn (_ None__) str() string {
return 'none'
}
pub const none__ = IError(None__{'', 0})
pub struct Option {
state byte
err IError = none__
}
pub fn (err IError) str() string {
return match err {
None__ { 'none' }
Error { err.msg }
else { 'Error: $err.msg' }
}
}
pub fn (o Option) str() string {
if o.state == 0 {
return 'Option{ ok }'
}
if o.state == 1 {
return 'Option{ none }'
}
return 'Option{ error: "$o.err" }'
}
fn trace_error(x string) {
eprintln('> ${@FN} | $x')
}
// error returns a default error instance containing the error given in `message`.
// Example: `if ouch { return error('an error occurred') }`
[inline]
pub fn error(message string) IError {
// trace_error(message)
return &Error{
msg: message
}
}
// error_with_code returns a default error instance containing the given `message` and error `code`.
// `if ouch { return error_with_code('an error occurred', 1) }`
[inline]
pub fn error_with_code(message string, code int) IError {
// trace_error('$message | code: $code')
return &Error{
msg: message
code: code
}
}