2021-01-18 15:20:06 +03:00
|
|
|
// 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()
|
|
|
|
|
2020-05-21 23:36:06 +03:00
|
|
|
pub fn println(s any) {
|
2020-12-08 19:49:20 +03:00
|
|
|
// Quickfix to properly print basic types
|
|
|
|
// TODO: Add proper detection code for this
|
|
|
|
JS.console.log(s.toString())
|
2019-09-14 23:48:30 +03:00
|
|
|
}
|
|
|
|
|
2020-05-21 23:36:06 +03:00
|
|
|
pub fn print(s any) {
|
2020-06-20 14:22:49 +03:00
|
|
|
// TODO
|
|
|
|
// $if js.node {
|
2021-03-24 21:39:59 +03:00
|
|
|
JS.process.stdout.write(s.toString())
|
2020-06-20 14:22:49 +03:00
|
|
|
// } $else {
|
|
|
|
// panic('Cannot `print` in a browser, use `println` instead')
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn eprintln(s any) {
|
2020-12-27 13:22:16 +03:00
|
|
|
JS.console.error(s.toString())
|
2020-06-20 14:22:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn eprint(s any) {
|
|
|
|
// TODO
|
|
|
|
// $if js.node {
|
2021-03-24 21:39:59 +03:00
|
|
|
JS.process.stderr.write(s.toString())
|
2020-06-20 14:22:49 +03:00
|
|
|
// } $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-25 00:52:23 +03:00
|
|
|
js_throw('exit($c)')
|
2020-06-20 14:22:49 +03:00
|
|
|
}
|
|
|
|
|
2020-11-24 14:54:26 +03:00
|
|
|
pub fn unwrap(opt any) any {
|
|
|
|
o := &Option(opt)
|
2021-02-28 23:20:21 +03:00
|
|
|
if o.state != 0 {
|
|
|
|
js_throw(o.err)
|
2020-11-24 14:54:26 +03:00
|
|
|
}
|
|
|
|
return opt
|
|
|
|
}
|
|
|
|
|
2020-06-20 14:22:49 +03:00
|
|
|
pub fn panic(s string) {
|
|
|
|
eprintln('V panic: $s')
|
|
|
|
exit(1)
|
2020-06-04 21:26:18 +03:00
|
|
|
}
|
2020-11-24 14:54:26 +03:00
|
|
|
|
2021-02-28 23:20:21 +03:00
|
|
|
struct Option<T> {
|
|
|
|
state byte
|
|
|
|
err Error
|
|
|
|
data T
|
|
|
|
}
|
2020-11-24 14:54:26 +03:00
|
|
|
|
2021-02-28 23:20:21 +03:00
|
|
|
pub struct Error {
|
|
|
|
pub:
|
|
|
|
msg string
|
|
|
|
code int
|
2020-11-24 14:54:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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 }'
|
|
|
|
}
|
|
|
|
return 'Option{ error: "$o.err" }'
|
2020-11-24 14:54:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn error(s string) Option {
|
|
|
|
return Option{
|
2021-02-28 23:20:21 +03:00
|
|
|
state: 2
|
|
|
|
err: {
|
|
|
|
msg: s
|
|
|
|
}
|
2020-11-24 14:54:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn error_with_code(s string, code int) Option {
|
|
|
|
return Option{
|
2021-02-28 23:20:21 +03:00
|
|
|
state: 2
|
|
|
|
err: {
|
|
|
|
msg: s
|
|
|
|
code: code
|
|
|
|
}
|
2020-11-24 14:54:26 +03:00
|
|
|
}
|
|
|
|
}
|