2020-11-25 00:52:23 +03:00
|
|
|
module builtin
|
|
|
|
|
|
|
|
// used to generate JS throw statements.
|
2021-09-08 20:30:46 +03:00
|
|
|
|
2021-10-13 09:40:14 +03:00
|
|
|
[noreturn]
|
2020-11-25 00:52:23 +03:00
|
|
|
pub fn js_throw(s any) {
|
2021-08-30 20:47:18 +03:00
|
|
|
#throw s
|
2021-10-13 09:40:14 +03:00
|
|
|
|
|
|
|
for {}
|
2020-11-25 00:52:23 +03:00
|
|
|
}
|
2021-08-18 11:33:37 +03:00
|
|
|
|
2022-12-20 14:16:06 +03:00
|
|
|
#let globalPrint, globalWrite;
|
2021-09-03 13:00:24 +03:00
|
|
|
$if js_freestanding {
|
2021-09-03 14:13:36 +03:00
|
|
|
#globalPrint = globalThis.print
|
2022-12-20 14:16:06 +03:00
|
|
|
#globalWrite = (typeof globalThis.write === 'function')? write: globalThis.print
|
2021-09-03 13:00:24 +03:00
|
|
|
}
|
|
|
|
|
2022-06-07 12:15:35 +03:00
|
|
|
pub fn flush_stdout() {
|
|
|
|
// needed for parity with builtin.c.v
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn flush_stderr() {
|
|
|
|
// needed for parity with builtin.c.v
|
|
|
|
}
|
|
|
|
|
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)
|
2022-12-20 14:16:06 +03:00
|
|
|
} $else $if js_freestanding {
|
|
|
|
#globalWrite(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.
|
2021-10-13 09:40:14 +03:00
|
|
|
[noreturn]
|
2021-08-26 15:20:54 +03:00
|
|
|
pub fn exit(c int) {
|
|
|
|
JS.process.exit(c)
|
2022-11-15 16:53:13 +03:00
|
|
|
js_throw('exit(${c})')
|
2021-08-26 15:20:54 +03:00
|
|
|
}
|
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
|
|
|
|
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
|
|
|
|
}
|
2021-10-07 15:55:47 +03:00
|
|
|
|
2021-10-20 16:02:21 +03:00
|
|
|
pub fn print_backtrace() {
|
|
|
|
println(js_stacktrace())
|
|
|
|
}
|
|
|
|
|
2021-10-07 15:55:47 +03: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 13:31:29 +03: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 15:55:47 +03:00
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (f float_literal) str() string {
|
|
|
|
res := ''
|
|
|
|
#res.str += f.valueOf()
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|