2020-11-24 22:52:23 +01:00
|
|
|
module builtin
|
|
|
|
|
|
|
|
// used to generate JS throw statements.
|
2021-09-08 19:30:46 +02:00
|
|
|
|
2021-10-13 08:40:14 +02:00
|
|
|
[noreturn]
|
2020-11-24 22:52:23 +01:00
|
|
|
pub fn js_throw(s any) {
|
2021-08-30 19:47:18 +02:00
|
|
|
#throw s
|
2021-10-13 08:40:14 +02:00
|
|
|
|
|
|
|
for {}
|
2020-11-24 22:52:23 +01:00
|
|
|
}
|
2021-08-18 10:33:37 +02:00
|
|
|
|
2021-09-03 13:13:36 +02:00
|
|
|
#let globalPrint;
|
2021-09-03 12:00:24 +02:00
|
|
|
$if js_freestanding {
|
2021-09-03 13:13:36 +02:00
|
|
|
#globalPrint = globalThis.print
|
2021-09-03 12:00:24 +02:00
|
|
|
}
|
|
|
|
|
2021-08-30 19:47:18 +02:00
|
|
|
pub fn println(s string) {
|
2021-08-18 10:33:37 +02:00
|
|
|
$if js_freestanding {
|
2021-09-03 12:00:24 +02:00
|
|
|
#globalPrint(s.str)
|
2021-08-18 10:33:37 +02:00
|
|
|
} $else {
|
2021-08-30 19:47:18 +02:00
|
|
|
#console.log(s.str)
|
2021-08-18 10:33:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 19:47:18 +02:00
|
|
|
pub fn print(s string) {
|
2021-08-18 10:33:37 +02:00
|
|
|
$if js_node {
|
2021-08-30 19:47:18 +02:00
|
|
|
#$process.stdout.write(s.str)
|
2021-08-18 10:33:37 +02:00
|
|
|
} $else {
|
|
|
|
panic('Cannot `print` in a browser, use `println` instead')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 19:47:18 +02:00
|
|
|
pub fn eprintln(s string) {
|
2021-08-18 10:33:37 +02:00
|
|
|
$if js_freestanding {
|
2021-09-03 12:00:24 +02:00
|
|
|
#globalPrint(s.str)
|
2021-08-18 10:33:37 +02:00
|
|
|
} $else {
|
2021-08-30 19:47:18 +02:00
|
|
|
#console.error(s.str)
|
2021-08-18 10:33:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 19:47:18 +02:00
|
|
|
pub fn eprint(s string) {
|
2021-08-18 10:33:37 +02:00
|
|
|
$if js_node {
|
2021-08-30 19:47:18 +02:00
|
|
|
#$process.stderr.write(s.str)
|
2021-08-18 10:33:37 +02:00
|
|
|
} $else {
|
|
|
|
panic('Cannot `eprint` in a browser, use `println` instead')
|
|
|
|
}
|
|
|
|
}
|
2021-08-26 14:20:54 +02: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.
|
2021-10-13 08:40:14 +02:00
|
|
|
[noreturn]
|
2021-08-26 14:20:54 +02:00
|
|
|
pub fn exit(c int) {
|
|
|
|
JS.process.exit(c)
|
|
|
|
js_throw('exit($c)')
|
|
|
|
}
|
2021-08-29 13:27:17 +02:00
|
|
|
|
|
|
|
fn opt_ok(data voidptr, option Option) {
|
|
|
|
#option.state = 0
|
|
|
|
#option.err = none__
|
|
|
|
#option.data = data
|
|
|
|
}
|
2021-08-30 19:47:18 +02:00
|
|
|
|
|
|
|
pub fn unwrap(opt string) string {
|
|
|
|
mut o := Option{}
|
|
|
|
#o = opt
|
|
|
|
if o.state != 0 {
|
|
|
|
js_throw(o.err)
|
|
|
|
}
|
2021-09-03 11:16:07 +02:00
|
|
|
|
|
|
|
mut res := ''
|
|
|
|
#res = opt.data
|
|
|
|
|
|
|
|
return res
|
2021-08-30 19:47:18 +02:00
|
|
|
}
|
2021-09-08 19:30:46 +02:00
|
|
|
|
2021-09-11 13:24:47 +02:00
|
|
|
fn js_stacktrace() string {
|
|
|
|
stacktrace := ''
|
|
|
|
#let err = new TypeError();
|
|
|
|
#err.name = 'stacktrace: '
|
|
|
|
#stacktrace.str = err.stack
|
|
|
|
|
|
|
|
return stacktrace
|
|
|
|
}
|
2021-10-07 14:55:47 +02:00
|
|
|
|
2021-10-20 15:02:21 +02:00
|
|
|
pub fn print_backtrace() {
|
|
|
|
println(js_stacktrace())
|
|
|
|
}
|
|
|
|
|
2021-10-07 14:55:47 +02:00
|
|
|
// Check for nil value
|
|
|
|
pub fn isnil(val voidptr) bool {
|
|
|
|
res := false
|
|
|
|
// This one is kinda weird. In C and native backend we can cast booleans and integers to pointers
|
|
|
|
// so we just check *for* all possible NULL-like values here.
|
2021-12-21 11:31:29 +01:00
|
|
|
#if (typeof val == 'function') { res.val = false; } else {
|
|
|
|
#val = val instanceof voidptr ? val.valueOf().val : val;
|
|
|
|
#res.val = val === null || val === undefined || val === false || val === 0 || val === BigInt(0) || (val instanceof int ? val.val == 0 : false)
|
|
|
|
#}
|
2021-10-07 14:55:47 +02:00
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (f float_literal) str() string {
|
|
|
|
res := ''
|
|
|
|
#res.str += f.valueOf()
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|