mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
js: os
now compiles to the JS backend, more builtins & minor codegen fixes (#11302)
This commit is contained in:
136
vlib/os/file.js.v
Normal file
136
vlib/os/file.js.v
Normal file
@@ -0,0 +1,136 @@
|
||||
module os
|
||||
|
||||
pub struct File {
|
||||
pub:
|
||||
fd int
|
||||
pub mut:
|
||||
is_opened bool
|
||||
}
|
||||
|
||||
#const $buffer = require('buffer');
|
||||
|
||||
// todo(playX): __as_cast is broken here
|
||||
/*
|
||||
pub struct ErrFileNotOpened {
|
||||
msg string = 'os: file not opened'
|
||||
code int
|
||||
}
|
||||
pub struct ErrSizeOfTypeIs0 {
|
||||
msg string = 'os: size of type is 0'
|
||||
code int
|
||||
}
|
||||
fn error_file_not_opened() IError {
|
||||
return IError(&ErrFileNotOpened{})
|
||||
}
|
||||
fn error_size_of_type_0() IError {
|
||||
return IError(&ErrSizeOfTypeIs0{})
|
||||
}
|
||||
*/
|
||||
pub fn open_file(path string, mode string, options ...int) ?File {
|
||||
mut res := File{}
|
||||
$if js_node {
|
||||
#if (!options) { options = new array([]); }
|
||||
#let permissions = 0o666
|
||||
#if (options.arr.length > 0) { permissions = options.arr[0]; }
|
||||
#try {
|
||||
#res.fd = new int($fs.openSync(''+path,''+mode,permissions))
|
||||
#} catch (e) {
|
||||
#return builtin.error('' + e);
|
||||
#}
|
||||
|
||||
res.is_opened = true
|
||||
} $else {
|
||||
error('cannot open file on non NodeJS runtime')
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
// open tries to open a file for reading and returns back a read-only `File` object.
|
||||
pub fn open(path string) ?File {
|
||||
f := open_file(path, 'r') ?
|
||||
return f
|
||||
}
|
||||
|
||||
pub fn create(path string) ?File {
|
||||
f := open_file(path, 'w') ?
|
||||
return f
|
||||
}
|
||||
|
||||
pub fn stdin() File {
|
||||
return File{
|
||||
fd: 0
|
||||
is_opened: true
|
||||
}
|
||||
}
|
||||
|
||||
pub fn stdout() File {
|
||||
return File{
|
||||
fd: 1
|
||||
is_opened: true
|
||||
}
|
||||
}
|
||||
|
||||
pub fn stderr() File {
|
||||
return File{
|
||||
fd: 2
|
||||
is_opened: true
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (f &File) read(mut buf []byte) ?int {
|
||||
if buf.len == 0 {
|
||||
return 0
|
||||
}
|
||||
mut nbytes := 0
|
||||
#try {
|
||||
#let buffer = $fs.readFileSync(f.fd.valueOf());
|
||||
#
|
||||
#for (const val of buffer.values()) { buf.arr[nbytes++] = val; }
|
||||
#}
|
||||
#catch (e) { return builtin.error('' + e); }
|
||||
|
||||
return nbytes
|
||||
}
|
||||
|
||||
pub fn (mut f File) write(buf []byte) ?int {
|
||||
if !f.is_opened {
|
||||
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); }
|
||||
|
||||
return nbytes
|
||||
}
|
||||
|
||||
// writeln writes the string `s` into the file, and appends a \n character.
|
||||
// It returns how many bytes were written, including the \n character.
|
||||
pub fn (mut f File) writeln(s string) ?int {
|
||||
mut nbytes := f.write(s.bytes()) ?
|
||||
nbytes += f.write('\n'.bytes()) ?
|
||||
return nbytes
|
||||
}
|
||||
|
||||
pub fn (mut f File) write_to(pos u64, buf []byte) ?int {
|
||||
if !f.is_opened {
|
||||
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); }
|
||||
|
||||
return nbytes
|
||||
}
|
||||
|
||||
// write_string writes the string `s` into the file
|
||||
// It returns how many bytes were actually written.
|
||||
pub fn (mut f File) write_string(s string) ?int {
|
||||
nbytes := f.write(s.bytes()) ?
|
||||
return nbytes
|
||||
}
|
||||
|
||||
pub fn (mut f File) close() {
|
||||
#f.valueOf().fd.close()
|
||||
}
|
||||
|
||||
pub fn (mut f File) write_full_buffer(s voidptr, buffer_len size_t) ? {}
|
21
vlib/os/os.c.v
Normal file
21
vlib/os/os.c.v
Normal file
@@ -0,0 +1,21 @@
|
||||
module os
|
||||
|
||||
// write_file_array writes the data in `buffer` to a file in `path`.
|
||||
pub fn write_file_array(path string, buffer array) ? {
|
||||
mut f := create(path) ?
|
||||
unsafe { f.write_full_buffer(buffer.data, size_t(buffer.len * buffer.element_size)) ? }
|
||||
f.close()
|
||||
}
|
||||
|
||||
pub fn glob(patterns ...string) ?[]string {
|
||||
mut matches := []string{}
|
||||
for pattern in patterns {
|
||||
native_glob_pattern(pattern, mut matches) ?
|
||||
}
|
||||
matches.sort()
|
||||
return matches
|
||||
}
|
||||
|
||||
pub const (
|
||||
args = []string{}
|
||||
)
|
@@ -4,9 +4,9 @@ module os
|
||||
#const $path = require('path');
|
||||
|
||||
pub const (
|
||||
args = []string{}
|
||||
path_delimiter = '/'
|
||||
path_separator = '/'
|
||||
args = []string{}
|
||||
)
|
||||
|
||||
$if js_node {
|
||||
|
21
vlib/os/os.v
21
vlib/os/os.v
@@ -4,7 +4,6 @@
|
||||
module os
|
||||
|
||||
pub const (
|
||||
args = []string{}
|
||||
max_path_len = 4096
|
||||
wd_at_startup = getwd()
|
||||
)
|
||||
@@ -344,22 +343,15 @@ pub fn write_file(path string, text string) ? {
|
||||
f.close()
|
||||
}
|
||||
|
||||
// write_file_array writes the data in `buffer` to a file in `path`.
|
||||
pub fn write_file_array(path string, buffer array) ? {
|
||||
mut f := create(path) ?
|
||||
unsafe { f.write_full_buffer(buffer.data, size_t(buffer.len * buffer.element_size)) ? }
|
||||
f.close()
|
||||
}
|
||||
|
||||
// executable_fallback is used when there is not a more platform specific and accurate implementation.
|
||||
// It relies on path manipulation of os.args[0] and os.wd_at_startup, so it may not work properly in
|
||||
// all cases, but it should be better, than just using os.args[0] directly.
|
||||
fn executable_fallback() string {
|
||||
if os.args.len == 0 {
|
||||
if args.len == 0 {
|
||||
// we are early in the bootstrap, os.args has not been initialized yet :-|
|
||||
return ''
|
||||
}
|
||||
mut exepath := os.args[0]
|
||||
mut exepath := args[0]
|
||||
$if windows {
|
||||
if !exepath.contains('.exe') {
|
||||
exepath += '.exe'
|
||||
@@ -639,12 +631,3 @@ pub fn execute_or_exit(cmd string) Result {
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
pub fn glob(patterns ...string) ?[]string {
|
||||
mut matches := []string{}
|
||||
for pattern in patterns {
|
||||
native_glob_pattern(pattern, mut matches) ?
|
||||
}
|
||||
matches.sort()
|
||||
return matches
|
||||
}
|
||||
|
@@ -15,22 +15,25 @@ pub fn mkdir(path string) ?bool {
|
||||
|
||||
pub fn is_dir(path string) bool {
|
||||
res := false
|
||||
#res.val = $fs.existsSync(path,str) && $fs.lstatSync(path.str).isDirectory()
|
||||
|
||||
$if js_node {
|
||||
#res.val = $fs.existsSync(path,str) && $fs.lstatSync(path.str).isDirectory()
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
pub fn is_link(path string) bool {
|
||||
res := false
|
||||
#res.val = $fs.existsSync(path.str) && $fs.lstatSync(path.str).isSymbolicLink()
|
||||
|
||||
$if js_node {
|
||||
#res.val = $fs.existsSync(path.str) && $fs.lstatSync(path.str).isSymbolicLink()
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
pub fn exists(path string) bool {
|
||||
res := false
|
||||
#res.val = $fs.existsSync(path.str)
|
||||
|
||||
$if js_node {
|
||||
#res.val = $fs.existsSync(path.str)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
@@ -40,8 +43,85 @@ pub fn ls(path string) ?[]string {
|
||||
}
|
||||
|
||||
result := []string{}
|
||||
#let i = 0
|
||||
#$fs.readdirSync(path.str).forEach((path) => result.arr[i++] = new builtin.string(path))
|
||||
|
||||
$if js_node {
|
||||
#let i = 0
|
||||
#$fs.readdirSync(path.str).forEach((path) => result.arr[i++] = new builtin.string(path))
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
pub fn get_raw_line() string {
|
||||
return ''
|
||||
}
|
||||
|
||||
pub fn executable() string {
|
||||
return ''
|
||||
}
|
||||
|
||||
pub fn is_executable(path string) bool {
|
||||
eprintln('TODO: There is no isExecutable on fs.stats')
|
||||
return false
|
||||
}
|
||||
|
||||
pub fn rmdir(path string) ? {
|
||||
$if js_node {
|
||||
err := ''
|
||||
#try {
|
||||
#$fs.rmdirSync(path.str)
|
||||
#return;
|
||||
#} catch (e) {
|
||||
#err.str = 'Failed to remove "' + path.str + '": ' + e.toString()
|
||||
#}
|
||||
|
||||
return error(err)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn rm(path string) ? {
|
||||
$if js_node {
|
||||
err := ''
|
||||
#try {
|
||||
#$fs.rmSync(path.str)
|
||||
#return;
|
||||
#} catch (e) {
|
||||
#err.str = 'Failed to remove "' + path.str + '": ' + e.toString()
|
||||
#}
|
||||
|
||||
return error(err)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn cp(src string, dst string) ? {
|
||||
$if js_node {
|
||||
err := ''
|
||||
#try {
|
||||
#$fs.cpSync(src.str,dst.str);
|
||||
#return;
|
||||
#} catch (e) {
|
||||
#err.str = 'failed to copy ' + src.str + ' to ' + dst.str + ': ' + e.toString();
|
||||
#}
|
||||
|
||||
return error(err)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn read_file(s string) ?string {
|
||||
mut err := ''
|
||||
err = err
|
||||
res := ''
|
||||
#try {
|
||||
#res.str = $fs.readFileSync(s.str).toString()
|
||||
#} catch (e) {
|
||||
#err.str = 'Failed to read file: ' + e.toString()
|
||||
#return error(err)
|
||||
#}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
pub fn getwd() string {
|
||||
res := ''
|
||||
#res.str = $process.cwd()
|
||||
|
||||
return res
|
||||
}
|
||||
|
Reference in New Issue
Block a user