mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
js: os
now compiles to the JS backend, more builtins & minor codegen fixes (#11302)
This commit is contained in:
@ -32,12 +32,38 @@ struct Option {
|
||||
err Error
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
const none__ = IError(&None__{})
|
||||
|
||||
struct None__ {
|
||||
msg string
|
||||
code int
|
||||
}
|
||||
|
||||
fn (_ None__) str() string {
|
||||
return 'none'
|
||||
}
|
||||
|
||||
pub fn (err IError) str() string {
|
||||
return match err {
|
||||
None__ { 'none' }
|
||||
Error { err.msg }
|
||||
else { '$err.type_name(): $err.msg' }
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (o Option) str() string {
|
||||
if o.state == 0 {
|
||||
return 'Option{ ok }'
|
||||
@ -48,21 +74,28 @@ pub fn (o Option) str() string {
|
||||
return 'Option{ error: "$o.err" }'
|
||||
}
|
||||
|
||||
pub fn error(s string) Option {
|
||||
return Option{
|
||||
state: 2
|
||||
err: Error{
|
||||
msg: s
|
||||
}
|
||||
[if trace_error ?]
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
pub fn error_with_code(s string, code int) Option {
|
||||
return Option{
|
||||
state: 2
|
||||
err: Error{
|
||||
msg: s
|
||||
code: code
|
||||
}
|
||||
// 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
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user