1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

js: fix printing, make builtins for result and option types behave correctly (#11336)

This commit is contained in:
playX
2021-08-30 20:47:18 +03:00
committed by GitHub
parent f33f216698
commit a9b705bfd8
6 changed files with 382 additions and 244 deletions

View File

@@ -2,36 +2,36 @@ module builtin
// used to generate JS throw statements.
pub fn js_throw(s any) {
#throw (s instanceof Error ? s : new Error(s))
#throw s
}
pub fn println(s any) {
pub fn println(s string) {
$if js_freestanding {
#print(s.toString())
#print(s.str)
} $else {
#console.log(s.toString())
#console.log(s.str)
}
}
pub fn print(s any) {
pub fn print(s string) {
$if js_node {
#$process.stdout.write(s.toString())
#$process.stdout.write(s.str)
} $else {
panic('Cannot `print` in a browser, use `println` instead')
}
}
pub fn eprintln(s any) {
pub fn eprintln(s string) {
$if js_freestanding {
#print(s.toString())
#print(s.str)
} $else {
#console.error(s.toString())
#console.error(s.str)
}
}
pub fn eprint(s any) {
pub fn eprint(s string) {
$if js_node {
#$process.stderr.write(s.toString())
#$process.stderr.write(s.str)
} $else {
panic('Cannot `eprint` in a browser, use `println` instead')
}
@@ -50,3 +50,12 @@ fn opt_ok(data voidptr, option Option) {
#option.err = none__
#option.data = data
}
pub fn unwrap(opt string) string {
mut o := Option{}
#o = opt
if o.state != 0 {
js_throw(o.err)
}
return opt
}