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
2020-11-24 22:52:23 +01:00

89 lines
1.5 KiB
V

// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module builtin
pub fn println(s any) {
JS.console.log(s)
}
pub fn print(s any) {
// TODO
// $if js.node {
JS.process.stdout.write(s)
// } $else {
// panic('Cannot `print` in a browser, use `println` instead')
// }
}
pub fn eprintln(s any) {
JS.console.error(s)
}
pub fn eprint(s any) {
// TODO
// $if js.node {
JS.process.stderr.write(s)
// } $else {
// panic('Cannot `eprint` in a browser, use `eprintln` instead')
// }
}
// Exits the process in node, and halts execution in the browser
// because `process.exit` is undefined. Workaround for not having
// a 'real' way to exit in the browser.
pub fn exit(c int) {
JS.process.exit(c)
js_throw('exit($c)')
}
pub fn unwrap(opt any) any {
o := &Option(opt)
if o.not_ok {
js_throw(o.error)
}
return opt
}
pub fn panic(s string) {
eprintln('V panic: $s')
exit(1)
}
struct Option {
not_ok bool
is_none bool
error string
ecode int
data any
}
pub fn (o Option) str() string {
if !o.not_ok {
return 'Option{ ok }'
}
if o.is_none {
return 'Option{ none }'
}
return 'Option{ error: "${o.error}" }'
}
pub fn error(s string) Option {
return Option{
not_ok: true
is_none: false
error: s
}
}
pub fn error_with_code(s string, code int) Option {
return Option{
not_ok: true
is_none: false
error: s
ecode: code
}
}