2020-11-25 00:52:23 +03:00
|
|
|
module builtin
|
|
|
|
|
2021-09-08 20:30:46 +03:00
|
|
|
import strings
|
2020-11-25 00:52:23 +03:00
|
|
|
// used to generate JS throw statements.
|
2021-09-08 20:30:46 +03:00
|
|
|
|
2020-11-25 00:52:23 +03:00
|
|
|
pub fn js_throw(s any) {
|
2021-08-30 20:47:18 +03:00
|
|
|
#throw s
|
2020-11-25 00:52:23 +03:00
|
|
|
}
|
2021-08-18 11:33:37 +03:00
|
|
|
|
2021-09-03 14:13:36 +03:00
|
|
|
#let globalPrint;
|
2021-09-03 13:00:24 +03:00
|
|
|
$if js_freestanding {
|
2021-09-03 14:13:36 +03:00
|
|
|
#globalPrint = globalThis.print
|
2021-09-03 13:00:24 +03:00
|
|
|
}
|
|
|
|
|
2021-08-30 20:47:18 +03:00
|
|
|
pub fn println(s string) {
|
2021-08-18 11:33:37 +03:00
|
|
|
$if js_freestanding {
|
2021-09-03 13:00:24 +03:00
|
|
|
#globalPrint(s.str)
|
2021-08-18 11:33:37 +03:00
|
|
|
} $else {
|
2021-08-30 20:47:18 +03:00
|
|
|
#console.log(s.str)
|
2021-08-18 11:33:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 20:47:18 +03:00
|
|
|
pub fn print(s string) {
|
2021-08-18 11:33:37 +03:00
|
|
|
$if js_node {
|
2021-08-30 20:47:18 +03:00
|
|
|
#$process.stdout.write(s.str)
|
2021-08-18 11:33:37 +03:00
|
|
|
} $else {
|
|
|
|
panic('Cannot `print` in a browser, use `println` instead')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 20:47:18 +03:00
|
|
|
pub fn eprintln(s string) {
|
2021-08-18 11:33:37 +03:00
|
|
|
$if js_freestanding {
|
2021-09-03 13:00:24 +03:00
|
|
|
#globalPrint(s.str)
|
2021-08-18 11:33:37 +03:00
|
|
|
} $else {
|
2021-08-30 20:47:18 +03:00
|
|
|
#console.error(s.str)
|
2021-08-18 11:33:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 20:47:18 +03:00
|
|
|
pub fn eprint(s string) {
|
2021-08-18 11:33:37 +03:00
|
|
|
$if js_node {
|
2021-08-30 20:47:18 +03:00
|
|
|
#$process.stderr.write(s.str)
|
2021-08-18 11:33:37 +03:00
|
|
|
} $else {
|
|
|
|
panic('Cannot `eprint` in a browser, use `println` instead')
|
|
|
|
}
|
|
|
|
}
|
2021-08-26 15:20:54 +03:00
|
|
|
|
|
|
|
// 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)')
|
|
|
|
}
|
2021-08-29 14:27:17 +03:00
|
|
|
|
|
|
|
fn opt_ok(data voidptr, option Option) {
|
|
|
|
#option.state = 0
|
|
|
|
#option.err = none__
|
|
|
|
#option.data = data
|
|
|
|
}
|
2021-08-30 20:47:18 +03:00
|
|
|
|
|
|
|
pub fn unwrap(opt string) string {
|
|
|
|
mut o := Option{}
|
|
|
|
#o = opt
|
|
|
|
if o.state != 0 {
|
|
|
|
js_throw(o.err)
|
|
|
|
}
|
2021-09-03 12:16:07 +03:00
|
|
|
|
|
|
|
mut res := ''
|
|
|
|
#res = opt.data
|
|
|
|
|
|
|
|
return res
|
2021-08-30 20:47:18 +03:00
|
|
|
}
|
2021-09-08 20:30:46 +03:00
|
|
|
|
|
|
|
pub fn (r rune) str() string {
|
|
|
|
res := ''
|
|
|
|
mut sb := strings.new_builder(5)
|
|
|
|
#res.str = r.valueOf().toString()
|
|
|
|
sb.write_string(res)
|
|
|
|
|
|
|
|
return sb.str()
|
|
|
|
}
|
2021-09-11 14:24:47 +03:00
|
|
|
|
|
|
|
fn js_stacktrace() string {
|
|
|
|
stacktrace := ''
|
|
|
|
#let err = new TypeError();
|
|
|
|
#err.name = 'stacktrace: '
|
|
|
|
#stacktrace.str = err.stack
|
|
|
|
|
|
|
|
return stacktrace
|
|
|
|
}
|