aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/vlib/builtin/js/builtin.js.v
diff options
context:
space:
mode:
Diffstat (limited to 'v_windows/v/vlib/builtin/js/builtin.js.v')
-rw-r--r--v_windows/v/vlib/builtin/js/builtin.js.v61
1 files changed, 61 insertions, 0 deletions
diff --git a/v_windows/v/vlib/builtin/js/builtin.js.v b/v_windows/v/vlib/builtin/js/builtin.js.v
new file mode 100644
index 0000000..f64da00
--- /dev/null
+++ b/v_windows/v/vlib/builtin/js/builtin.js.v
@@ -0,0 +1,61 @@
+module builtin
+
+// used to generate JS throw statements.
+pub fn js_throw(s any) {
+ #throw s
+}
+
+pub fn println(s string) {
+ $if js_freestanding {
+ #print(s.str)
+ } $else {
+ #console.log(s.str)
+ }
+}
+
+pub fn print(s string) {
+ $if js_node {
+ #$process.stdout.write(s.str)
+ } $else {
+ panic('Cannot `print` in a browser, use `println` instead')
+ }
+}
+
+pub fn eprintln(s string) {
+ $if js_freestanding {
+ #print(s.str)
+ } $else {
+ #console.error(s.str)
+ }
+}
+
+pub fn eprint(s string) {
+ $if js_node {
+ #$process.stderr.write(s.str)
+ } $else {
+ panic('Cannot `eprint` in a browser, use `println` instead')
+ }
+}
+
+// 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)')
+}
+
+fn opt_ok(data voidptr, option Option) {
+ #option.state = 0
+ #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
+}