v/vlib/builtin/js/builtin.v

89 lines
1.5 KiB
V
Raw Normal View History

2020-02-03 05:00:36 +01:00
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
2019-09-14 22:48:30 +02:00
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module builtin
2020-05-21 22:36:06 +02:00
pub fn println(s any) {
2020-05-21 15:17:16 +02:00
JS.console.log(s)
2019-09-14 22:48:30 +02:00
}
2020-05-21 22:36:06 +02:00
pub fn print(s any) {
2020-06-20 13:22:49 +02:00
// 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)
2020-11-24 22:52:23 +01:00
js_throw('exit($c)')
2020-06-20 13:22:49 +02:00
}
pub fn unwrap(opt any) any {
o := &Option(opt)
if o.not_ok {
2020-11-24 22:52:23 +01:00
js_throw(o.error)
}
return opt
}
2020-06-20 13:22:49 +02:00
pub fn panic(s string) {
eprintln('V panic: $s')
exit(1)
2020-06-04 20:26:18 +02:00
}
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
}
}