From 57c79770b3082febbb8ac3c5a9579cbc19939ab1 Mon Sep 17 00:00:00 2001 From: playX Date: Wed, 20 Oct 2021 16:02:21 +0300 Subject: [PATCH] js: port more methods (os, builtin) (#12238) --- vlib/builtin/js/builtin.js.v | 4 ++++ vlib/builtin/js/builtin.v | 1 + vlib/builtin/js/int.js.v | 32 ++++++++++++++++++++++++++++++++ vlib/builtin/js/string.js.v | 7 +++++++ vlib/os/file.js.v | 30 ++++++++++++++++++++++-------- vlib/os/os.js.v | 17 +++++++++++++++++ 6 files changed, 83 insertions(+), 8 deletions(-) diff --git a/vlib/builtin/js/builtin.js.v b/vlib/builtin/js/builtin.js.v index a676acc19c..b5092ba8aa 100644 --- a/vlib/builtin/js/builtin.js.v +++ b/vlib/builtin/js/builtin.js.v @@ -83,6 +83,10 @@ fn js_stacktrace() string { return stacktrace } +pub fn print_backtrace() { + println(js_stacktrace()) +} + // Check for nil value pub fn isnil(val voidptr) bool { res := false diff --git a/vlib/builtin/js/builtin.v b/vlib/builtin/js/builtin.v index 6f53ef257d..421f092020 100644 --- a/vlib/builtin/js/builtin.v +++ b/vlib/builtin/js/builtin.v @@ -6,6 +6,7 @@ module builtin fn (a any) toString() +[noreturn] pub fn panic(s string) { eprintln('V panic: $s\n$js_stacktrace()') exit(1) diff --git a/vlib/builtin/js/int.js.v b/vlib/builtin/js/int.js.v index 10535d37d4..eae88fcd13 100644 --- a/vlib/builtin/js/int.js.v +++ b/vlib/builtin/js/int.js.v @@ -155,3 +155,35 @@ pub fn (b []byte) hex() string { } return hex } + +pub fn (i int) hex2() string { + return '0x' + i.hex() +} + +pub fn (i i8) hex2() string { + return '0x' + i.hex() +} + +pub fn (i i16) hex2() string { + return '0x' + i.hex() +} + +pub fn (i i64) hex2() string { + return '0x' + i.hex() +} + +pub fn (i byte) hex2() string { + return '0x' + i.hex() +} + +pub fn (i u16) hex2() string { + return '0x' + i.hex() +} + +pub fn (i u32) hex2() string { + return '0x' + i.hex() +} + +pub fn (i u64) hex2() string { + return '0x' + i.hex() +} diff --git a/vlib/builtin/js/string.js.v b/vlib/builtin/js/string.js.v index 126a4ca236..18b11f1ab1 100644 --- a/vlib/builtin/js/string.js.v +++ b/vlib/builtin/js/string.js.v @@ -910,3 +910,10 @@ pub fn (s string) index(search string) ?int { } return res } + +pub fn (_rune string) utf32_code() int { + res := 0 + #res.val = s.str.charCodeAt() + + return res +} diff --git a/vlib/os/file.js.v b/vlib/os/file.js.v index c7ae99cd79..61aa7b940d 100644 --- a/vlib/os/file.js.v +++ b/vlib/os/file.js.v @@ -35,7 +35,7 @@ pub fn open_file(path string, mode string, options ...int) ?File { #try { #res.fd = new int($fs.openSync(''+path,''+mode,permissions)) #} catch (e) { - #return builtin.error('' + e); + #return error(new string('' + e)); #} res.is_opened = true @@ -83,11 +83,11 @@ pub fn (f &File) read(mut buf []byte) ?int { } mut nbytes := 0 #try { - #let buffer = $fs.readFileSync(f.fd.valueOf()); + #let buffer = $fs.readFileSync(f.val.fd.valueOf()); # #for (const val of buffer.values()) { buf.arr[nbytes++] = val; } #} - #catch (e) { return builtin.error('' + e); } + #catch (e) { return error('' + e); } return nbytes } @@ -97,8 +97,9 @@ pub fn (mut f File) write(buf []byte) ?int { return error('file is not opened') } mut nbytes := 0 - #const b = $buffer.Buffer.from(buf.arr.map((x) => x.valueOf())) - #try { $fs.writeSync(f.fd.valueOf(),b,0,buf.len.valueOf(),0); } catch (e) { return builtin.error('' + e); } + #buf.arr.make_copy() + #const b = $buffer.Buffer.from(buf.arr.arr.map((x) => x.valueOf())) + #try { $fs.writeSync(f.val.fd.valueOf(),b,0,buf.len.valueOf(),0); } catch (e) { return error(new string('' + e)); } return nbytes } @@ -116,8 +117,9 @@ pub fn (mut f File) write_to(pos u64, buf []byte) ?int { return error('file is not opened') } mut nbytes := 0 - #const b = $buffer.Buffer.from(buf.arr.map((x) => x.valueOf())) - #try { $fs.writeSync(f.fd.valueOf(),b,0,buf.len.valueOf(),pos.valueOf()); } catch (e) { return builtin.error('' + e); } + #buf.arr.make_copy() + #const b = $buffer.Buffer.from(buf.arr.arr.map((x) => x.valueOf())) + #try { $fs.writeSync(f.val.fd.valueOf(),b,0,buf.len.valueOf(),pos.valueOf()); } catch (e) { return error(new string('' + e)); } return nbytes } @@ -130,7 +132,19 @@ pub fn (mut f File) write_string(s string) ?int { } pub fn (mut f File) close() { - #f.valueOf().fd.close() + #$fs.closeSync(f.valueOf().fd.valueOf()) } pub fn (mut f File) write_full_buffer(s voidptr, buffer_len usize) ? {} + +pub fn (mut f File) write_array(buffer array) ?int { + if !f.is_opened { + return error('file is not opened') + } + mut nbytes := 0 + #buffer.arr.make_copy() + #const b = $buffer.Buffer.from(buffer.arr.arr.map((x) => x.valueOf())) + #try { $fs.writeSync(f.val.fd.valueOf(),b,0,buffer.len.valueOf(),0); } catch (e) { return error(new string('' + e)); } + + return nbytes +} diff --git a/vlib/os/os.js.v b/vlib/os/os.js.v index 1bc19f8e21..bdcfa556dc 100644 --- a/vlib/os/os.js.v +++ b/vlib/os/os.js.v @@ -133,3 +133,20 @@ pub fn glob(patterns ...string) ?[]string { panic('not yet implemented') return none } + +pub fn write_file_array(path string, buffer array) ? { + mut f := create(path) ? + f.write_array(buffer) ? + f.close() +} + +pub fn chdir(s string) ? { + #try { $process.chdir(s.str); } catch (e) { return error(new string('' + s)) } +} + +pub fn file_last_mod_unix(path string) int { + mtime := 0 + #mtime.val = Math.floor($fs.lstatSync(path.str).mtime.getTime() / 1000) + + return mtime +}