From accd4d83bf43e3677fd6c6154c2ebe844ebd2365 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sun, 4 Apr 2021 20:14:51 +0300 Subject: [PATCH] ci: fix compilation --- .../modules/automaton/automaton.v | 2 +- examples/path_tracing.v | 2 +- vlib/builtin/array.v | 2 +- vlib/builtin/string.v | 2 +- vlib/builtin/string_charptr_byteptr_helpers.v | 104 ++++++++++++++++++ vlib/json/json_primitives.v | 4 +- vlib/os/os_c.v | 10 +- 7 files changed, 115 insertions(+), 11 deletions(-) create mode 100644 vlib/builtin/string_charptr_byteptr_helpers.v diff --git a/examples/game_of_life/modules/automaton/automaton.v b/examples/game_of_life/modules/automaton/automaton.v index 0845541624..0bf346fb15 100644 --- a/examples/game_of_life/modules/automaton/automaton.v +++ b/examples/game_of_life/modules/automaton/automaton.v @@ -13,7 +13,7 @@ fn new_a2d(maxx int, maxy int) &A2D { return &A2D{ maxx: maxx maxy: maxy - data: &int(vcalloc(size)) + data: unsafe { &int(vcalloc(size)) } } } diff --git a/examples/path_tracing.v b/examples/path_tracing.v index 6a7a5a866a..c7a152aceb 100644 --- a/examples/path_tracing.v +++ b/examples/path_tracing.v @@ -91,7 +91,7 @@ fn new_image(w int, h int) Image { return Image{ width: w height: h - data: &Vec(vcalloc(vecsize * w * h)) + data: unsafe { &Vec(vcalloc(vecsize * w * h)) } } } diff --git a/vlib/builtin/array.v b/vlib/builtin/array.v index 67b2db4dc3..96fd45928b 100644 --- a/vlib/builtin/array.v +++ b/vlib/builtin/array.v @@ -653,6 +653,6 @@ pub fn (data voidptr) vbytes(len int) []byte { // byteptr.vbytes() - makes a V []byte structure from a C style memory buffer. NB: the data is reused, NOT copied! [unsafe] -pub fn (data byteptr) vbytes(len int) []byte { +pub fn (data &byte) vbytes(len int) []byte { return unsafe { voidptr(data).vbytes(len) } } diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index 27594e7fc5..cc54075bdc 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -43,7 +43,7 @@ NB: A V string should be/is immutable from the point of view of */ pub struct string { pub: - str &byte // points to a C style 0 terminated string of bytes. + str &byte = 0 // points to a C style 0 terminated string of bytes. len int // the length of the .str field, excluding the ending 0 byte. It is always equal to strlen(.str). mut: is_lit int diff --git a/vlib/builtin/string_charptr_byteptr_helpers.v b/vlib/builtin/string_charptr_byteptr_helpers.v new file mode 100644 index 0000000000..e738ee2111 --- /dev/null +++ b/vlib/builtin/string_charptr_byteptr_helpers.v @@ -0,0 +1,104 @@ +module builtin + +// NB: this file will be removed soon + +// byteptr.vbytes() - makes a V []byte structure from a C style memory buffer. NB: the data is reused, NOT copied! +[unsafe] +pub fn (data byteptr) vbytes(len int) []byte { + return unsafe { voidptr(data).vbytes(len) } +} + +// vstring converts a C style string to a V string. NB: the string data is reused, NOT copied. +// strings returned from this function will be normal V strings beside that (i.e. they would be +// freed by V's -autofree mechanism, when they are no longer used). +[unsafe] +pub fn (bp byteptr) vstring() string { + return string{ + str: bp + len: unsafe { C.strlen(&char(bp)) } + } +} + +// vstring_with_len converts a C style string to a V string. +// NB: the string data is reused, NOT copied. +[unsafe] +pub fn (bp byteptr) vstring_with_len(len int) string { + return string{ + str: bp + len: len + is_lit: 0 + } +} + +// vstring converts C char* to V string. +// NB: the string data is reused, NOT copied. +[unsafe] +pub fn (cp charptr) vstring() string { + return string{ + str: byteptr(cp) + len: unsafe { C.strlen(&char(cp)) } + is_lit: 0 + } +} + +// vstring_with_len converts C char* to V string. +// NB: the string data is reused, NOT copied. +[unsafe] +pub fn (cp charptr) vstring_with_len(len int) string { + return string{ + str: byteptr(cp) + len: len + is_lit: 0 + } +} + +// vstring_literal converts a C style string to a V string. +// NB: the string data is reused, NOT copied. +// NB2: unlike vstring, vstring_literal will mark the string +// as a literal, so it will not be freed by autofree. +// This is suitable for readonly strings, C string literals etc, +// that can be read by the V program, but that should not be +// managed by it, for example `os.args` is implemented using it. +[unsafe] +pub fn (bp byteptr) vstring_literal() string { + return string{ + str: bp + len: unsafe { C.strlen(&char(bp)) } + is_lit: 1 + } +} + +// vstring_with_len converts a C style string to a V string. +// NB: the string data is reused, NOT copied. +[unsafe] +pub fn (bp byteptr) vstring_literal_with_len(len int) string { + return string{ + str: bp + len: len + is_lit: 1 + } +} + +// vstring_literal converts C char* to V string. +// See also vstring_literal defined on byteptr for more details. +// NB: the string data is reused, NOT copied. +[unsafe] +pub fn (cp charptr) vstring_literal() string { + return string{ + str: byteptr(cp) + len: unsafe { C.strlen(&char(cp)) } + is_lit: 1 + } +} + +// vstring_literal_with_len converts C char* to V string. +// See also vstring_literal_with_len defined on byteptr. +// NB: the string data is reused, NOT copied. +[unsafe] +pub fn (cp charptr) vstring_literal_with_len(len int) string { + return string{ + str: byteptr(cp) + len: len + is_lit: 1 + } +} diff --git a/vlib/json/json_primitives.v b/vlib/json/json_primitives.v index 3cfca1e6e9..59d2d4eb97 100644 --- a/vlib/json/json_primitives.v +++ b/vlib/json/json_primitives.v @@ -190,12 +190,12 @@ fn json_parse(s string) &C.cJSON { // json_string := json_print(encode_User(user)) fn json_print(json &C.cJSON) string { s := C.cJSON_PrintUnformatted(json) - return unsafe { tos(byteptr(s), C.strlen(s)) } + return unsafe { tos(byteptr(s), C.strlen(&char(s))) } } fn json_print_pretty(json &C.cJSON) string { s := C.cJSON_Print(json) - return unsafe { tos(byteptr(s), C.strlen(s)) } + return unsafe { tos(byteptr(s), C.strlen(&char(s))) } } // / cjson wrappers diff --git a/vlib/os/os_c.v b/vlib/os/os_c.v index 5f7c827b23..13c7f0d5a8 100644 --- a/vlib/os/os_c.v +++ b/vlib/os/os_c.v @@ -118,7 +118,7 @@ pub fn file_size(path string) u64 { $if x64 { $if windows { mut swin := C.__stat64{} - C._wstat64(path.to_wide(), voidptr(&swin)) + C._wstat64(&char(path.to_wide()), voidptr(&swin)) return swin.st_size } $else { C.stat(&char(path.str), &s) @@ -308,7 +308,7 @@ pub fn system(cmd string) int { unsafe { arg := [c'/bin/sh', c'-c', &byte(cmd.str), 0] pid := 0 - ret = C.posix_spawn(&pid, '/bin/sh', 0, 0, arg.data, 0) + ret = C.posix_spawn(&pid, c'/bin/sh', 0, 0, arg.data, 0) status := 0 ret = C.waitpid(pid, &status, 0) if C.WIFEXITED(status) { @@ -405,7 +405,7 @@ pub fn rm(path string) ? { // rmdir removes a specified directory. pub fn rmdir(path string) ? { $if windows { - rc := C.RemoveDirectory(path.to_wide()) + rc := C.RemoveDirectory(&char(path.to_wide())) if rc == 0 { // https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-removedirectorya - 0 is failure return error('Failed to remove "$path": ' + posix_get_error_msg(C.errno)) @@ -566,7 +566,7 @@ pub fn executable() string { $if windows { max := 512 size := max * 2 // max_path_len * sizeof(wchar_t) - mut result := &u16(vcalloc(size)) + mut result := unsafe { &u16(vcalloc(size)) } len := C.GetModuleFileName(0, result, max) // determine if the file is a windows symlink attrs := C.GetFileAttributesW(result) @@ -575,7 +575,7 @@ pub fn executable() string { // gets handle with GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 file := C.CreateFile(result, 0x80000000, 1, 0, 3, 0x80, 0) if file != voidptr(-1) { - final_path := &u16(vcalloc(size)) + final_path := unsafe { &u16(vcalloc(size)) } // https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getfinalpathnamebyhandlew final_len := C.GetFinalPathNameByHandleW(file, final_path, size, 0) if final_len < size {