mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
JavaSript backend (early stage)
This commit is contained in:
22
vlib/os/os.v
22
vlib/os/os.v
@ -69,25 +69,6 @@ fn C.ftell(fp voidptr) int
|
||||
fn C.getenv(byteptr) byteptr
|
||||
fn C.sigaction(int, voidptr, int)
|
||||
|
||||
fn init_os_args(argc int, argv &byteptr) []string {
|
||||
mut args := []string
|
||||
$if windows {
|
||||
mut args_list := &voidptr(0)
|
||||
mut args_count := 0
|
||||
args_list = C.CommandLineToArgvW(C.GetCommandLine(), &args_count)
|
||||
for i := 0; i < args_count; i++ {
|
||||
args << string_from_wide(&u16(args_list[i]))
|
||||
}
|
||||
|
||||
C.LocalFree(args_list)
|
||||
} $else {
|
||||
for i := 0; i < argc; i++ {
|
||||
args << string(argv[i])
|
||||
}
|
||||
}
|
||||
return args
|
||||
}
|
||||
|
||||
fn parse_windows_cmd_line(cmd byteptr) []string {
|
||||
s := string(cmd)
|
||||
return s.split(' ')
|
||||
@ -100,8 +81,7 @@ pub fn read_file(path string) ?string {
|
||||
$if windows {
|
||||
fp = C._wfopen(path.to_wide(), mode.to_wide())
|
||||
} $else {
|
||||
cpath := path.str
|
||||
fp = C.fopen(cpath, mode.str)
|
||||
fp = C.fopen(path.str, mode.str)
|
||||
}
|
||||
if isnil(fp) {
|
||||
return error('failed to open file "$path"')
|
||||
|
@ -7,6 +7,14 @@ const (
|
||||
PathSeparator = '/'
|
||||
)
|
||||
|
||||
fn init_os_args(argc int, argv &byteptr) []string {
|
||||
mut args := []string
|
||||
for i := 0; i < argc; i++ {
|
||||
args << string(argv[i])
|
||||
}
|
||||
return args
|
||||
}
|
||||
|
||||
|
||||
// get_error_msg return error code representation in string.
|
||||
pub fn get_error_msg(code int) string {
|
||||
|
@ -4,8 +4,8 @@ module os
|
||||
#include <winsock2.h>
|
||||
|
||||
const (
|
||||
PathSeparator = '\\'
|
||||
)
|
||||
PathSeparator = '\\'
|
||||
)
|
||||
|
||||
// Ref - https://docs.microsoft.com/en-us/windows/desktop/winprog/windows-data-types
|
||||
// A handle to an object.
|
||||
@ -30,7 +30,7 @@ mut:
|
||||
nFileSizeLow u32
|
||||
dwReserved0 u32
|
||||
dwReserved1 u32
|
||||
cFileName [260]u16 // MAX_PATH = 260
|
||||
cFileName [260]u16 // MAX_PATH = 260
|
||||
cAlternateFileName [14]u16 // 14
|
||||
dwFileType u32
|
||||
dwCreatorType u32
|
||||
@ -38,6 +38,18 @@ mut:
|
||||
}
|
||||
|
||||
|
||||
fn init_os_args(argc int, argv &byteptr) []string {
|
||||
mut args := []string
|
||||
mut args_list := &voidptr(0)
|
||||
mut args_count := 0
|
||||
args_list = C.CommandLineToArgvW(C.GetCommandLine(), &args_count)
|
||||
for i := 0; i < args_count; i++ {
|
||||
args << string_from_wide(&u16(args_list[i]))
|
||||
}
|
||||
C.LocalFree(args_list)
|
||||
return args
|
||||
}
|
||||
|
||||
|
||||
pub fn ls(path string) []string {
|
||||
mut find_file_data := win32finddata{}
|
||||
@ -54,7 +66,7 @@ pub fn ls(path string) []string {
|
||||
}
|
||||
// NOTE: Should eventually have path struct & os dependant path seperator (eg os.PATH_SEPERATOR)
|
||||
// we need to add files to path eg. c:\windows\*.dll or :\windows\*
|
||||
path_files := '$path\\*'
|
||||
path_files := '$path\\*'
|
||||
// NOTE:TODO: once we have a way to convert utf16 wide character to utf8
|
||||
// we should use FindFirstFileW and FindNextFileW
|
||||
h_find_files := C.FindFirstFile(path_files.to_wide(), &find_file_data)
|
||||
@ -70,7 +82,7 @@ pub fn ls(path string) []string {
|
||||
}
|
||||
C.FindClose(h_find_files)
|
||||
return dir_files
|
||||
}
|
||||
}
|
||||
|
||||
pub fn dir_exists(path string) bool {
|
||||
_path := path.replace('/', '\\')
|
||||
@ -82,7 +94,7 @@ pub fn dir_exists(path string) bool {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// mkdir creates a new directory with the specified path.
|
||||
pub fn mkdir(path string) {
|
||||
@ -93,7 +105,7 @@ pub fn mkdir(path string) {
|
||||
mkdir(_path.all_before_last('\\'))
|
||||
}
|
||||
C.CreateDirectory(_path.to_wide(), 0)
|
||||
}
|
||||
}
|
||||
|
||||
// Ref - https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/get-osfhandle?view=vs-2019
|
||||
// get_file_handle retrieves the operating-system file handle that is associated with the specified file descriptor.
|
||||
@ -108,10 +120,10 @@ pub fn get_file_handle(path string) HANDLE {
|
||||
}
|
||||
|
||||
// Ref - https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-getmodulefilenamea
|
||||
// get_module_filename retrieves the fully qualified path for the file that contains the specified module.
|
||||
// get_module_filename retrieves the fully qualified path for the file that contains the specified module.
|
||||
// The module must have been loaded by the current process.
|
||||
pub fn get_module_filename(handle HANDLE) ?string {
|
||||
mut sz := int(4096) // Optimized length
|
||||
mut sz := int(4096) // Optimized length
|
||||
mut buf := &u16(malloc(4096))
|
||||
for {
|
||||
status := C.GetModuleFileName(handle, &buf, sz)
|
||||
@ -142,15 +154,15 @@ const (
|
||||
SUBLANG_NEUTRAL = 0x00
|
||||
SUBLANG_DEFAULT = 0x01
|
||||
LANG_NEUTRAL = (SUBLANG_NEUTRAL)
|
||||
)
|
||||
)
|
||||
|
||||
// Ref - https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes--12000-15999-
|
||||
const (
|
||||
MAX_ERROR_CODE = 15841 // ERROR_API_UNAVAILABLE
|
||||
)
|
||||
|
||||
// ptr_win_get_error_msg return string (voidptr)
|
||||
// representation of error, only for windows.
|
||||
// ptr_win_get_error_msg return string (voidptr)
|
||||
// representation of error, only for windows.
|
||||
fn ptr_win_get_error_msg(code u32) voidptr {
|
||||
mut buf := voidptr(0)
|
||||
// Check for code overflow
|
||||
|
Reference in New Issue
Block a user