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.
|
|
|
|
#val = val.valueOf()
|
|
|
|
#res.val = val === null || val === undefined || val === false || val === 0 || val === BigInt(0)
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (f float_literal) str() string {
|
|
|
|
res := ''
|
|
|
|
#res.str += f.valueOf()
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
2021-11-06 14:52:24 +01:00
|
|
|
|
|
|
|
pub fn tof64(n JS.Number) f64 {
|
|
|
|
res := f64(0.0)
|
|
|
|
#res.val = n;
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn tof32(n JS.Number) f32 {
|
|
|
|
res := f32(0.0)
|
|
|
|
#res.val = n;
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn toi(n JS.Number) int {
|
|
|
|
res := int(0)
|
|
|
|
#res.val = Math.floor(n);
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn f64tonum(n f64) JS.Number {
|
|
|
|
mut res := JS.Number{}
|
|
|
|
#res = n.val;
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn itonum(n int) JS.Number {
|
|
|
|
mut res := JS.Number{}
|
|
|
|
#res = n.val;
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn i64tobigint(n i64) JS.BigInt {
|
|
|
|
mut res := JS.BigInt{}
|
|
|
|
#res = n.val;
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn u64tobigint(n u64) JS.BigInt {
|
|
|
|
mut res := JS.BigInt{}
|
|
|
|
#res = n.val;
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn tobool(b JS.Boolean) bool {
|
|
|
|
res := false
|
|
|
|
#res.val = b;
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn booltojs(b bool) JS.Boolean {
|
|
|
|
mut res := JS.Boolean{}
|
|
|
|
#res = b.val;
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|