js: port more methods (os, builtin) (#12238)

pull/12241/head weekly.2021.42.1
playX 2021-10-20 16:02:21 +03:00 committed by GitHub
parent 7c1fff3495
commit 57c79770b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 83 additions and 8 deletions

View File

@ -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

View File

@ -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)

View File

@ -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()
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}