diff --git a/compiler/fn.v b/compiler/fn.v index 7fb50facd7..84fab3610d 100644 --- a/compiler/fn.v +++ b/compiler/fn.v @@ -518,6 +518,13 @@ fn (p mut Parser) fn_call(f Fn, method_ph int, receiver_var, receiver_type strin p.error('function `$f.name` is private') } p.calling_c = f.is_c + if f.is_c && !p.builtin_pkg { + if f.name == 'free' { + p.error('use `free()` instead of `C.free()`') + } else if f.name == 'malloc' { + p.error('use `malloc()` instead of `C.malloc()`') + } + } cgen_name := p.table.cgen_name(f) // if p.pref.is_prof { // p.cur_fn.called_fns << cgen_name diff --git a/vlib/builtin/builtin.v b/vlib/builtin/builtin.v index c5fdfe5824..342c886331 100644 --- a/vlib/builtin/builtin.v +++ b/vlib/builtin/builtin.v @@ -93,11 +93,6 @@ pub fn free(ptr voidptr) { C.free(ptr) } -fn _strlen(s byteptr) int { - return C.strlen(s) -} - - fn memdup(src voidptr, sz int) voidptr { mem := malloc(sz) return C.memcpy(mem, src, sz) diff --git a/vlib/builtin/int.v b/vlib/builtin/int.v index ad08158df9..117737781a 100644 --- a/vlib/builtin/int.v +++ b/vlib/builtin/int.v @@ -7,25 +7,25 @@ module builtin pub fn (d double) str() string { buf := malloc(sizeof(double) * 5 + 1)// TODO C.sprintf(buf, '%f', d) - return tos(buf, _strlen(buf)) + return tos(buf, strlen(buf)) } pub fn (d f64) str() string { buf := malloc(sizeof(double) * 5 + 1)// TODO C.sprintf(buf, '%f', d) - return tos(buf, _strlen(buf)) + return tos(buf, strlen(buf)) } pub fn (d f32) str() string { buf := malloc(sizeof(double) * 5 + 1)// TODO C.sprintf(buf, '%f', d) - return tos(buf, _strlen(buf)) + return tos(buf, strlen(buf)) } pub fn ptr_str(ptr voidptr) string { buf := malloc(sizeof(double) * 5 + 1)// TODO C.sprintf(buf, '%p', ptr) - return tos(buf, _strlen(buf)) + return tos(buf, strlen(buf)) } // fn (nn i32) str() string { diff --git a/vlib/os/os.v b/vlib/os/os.v index 507a444e78..0523c0bbd2 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -105,24 +105,21 @@ fn parse_windows_cmd_line(cmd byteptr) []string { // read_file reads the file in `path` and returns the contents. pub fn read_file(path string) ?string { - mut res := '' mut mode := 'rb' cpath := path.cstr() fp := C.fopen(cpath, mode.cstr()) if isnil(fp) { return error('failed to open file "$path"') - //panic('failed to open file "$path"') } C.fseek(fp, 0, SEEK_END) fsize := C.ftell(fp) - // C.fseek(fp, 0, SEEK_SET) // same as C.rewind(fp) below + // C.fseek(fp, 0, SEEK_SET) // same as `C.rewind(fp)` below C.rewind(fp) mut str := malloc(fsize + 1) C.fread(str, fsize, 1, fp) C.fclose(fp) str[fsize] = 0 - res = tos(str, fsize) - return res + return string(str, fsize) } // file_size returns the size of the file located in `path`. @@ -463,7 +460,7 @@ pub fn get_raw_line() string { if nr_chars == 0 { return '' } - return tos(buf, nr_chars) + return string(buf, nr_chars) } } @@ -480,6 +477,15 @@ pub fn user_os() string { $if freebsd { return 'freebsd' } + $if openbsd { + return 'openbsd' + } + $if netbsd { + return 'netbsd' + } + $if dragonfly { + return 'dragonfly' + } return 'unknown' } @@ -501,7 +507,7 @@ pub fn home_dir() string { return home } -// write_file writes text data to a file in `path`. +// write_file writes `text` data to a file in `path`. pub fn write_file(path, text string) { f := os.create(path) or { return @@ -536,11 +542,11 @@ pub fn executable() string { if count < 0 { panic('error reading /proc/self/exe to get exe path') } - return tos(result, count) + return string(result, count) } $if windows { ret := int(C.GetModuleFileName( 0, result, MAX_PATH )) - return tos( result, ret) + return string( result, ret) } $if mac { pid := C.getpid() @@ -567,14 +573,14 @@ pub fn executable() string { if count < 0 { panic('error reading /proc/curproc/exe to get exe path') } - return tos(result, count) + return string(result, count) } $if dragonfly { count := int(C.readlink('/proc/curproc/file', result, MAX_PATH )) if count < 0 { panic('error reading /proc/curproc/file to get exe path') } - return tos(result, count) + return string(result, count) } return '.' } diff --git a/vlib/strings/builder.v b/vlib/strings/builder.v index 11f34c6127..59a5f32ab8 100644 --- a/vlib/strings/builder.v +++ b/vlib/strings/builder.v @@ -38,5 +38,5 @@ pub fn (b Builder) cut(n int) { } pub fn (b mut Builder) free() { - C.free(b.buf.data) + free(b.buf.data) } diff --git a/vlib/strings/strings.v b/vlib/strings/strings.v index ce32c38aa9..919e831cd7 100644 --- a/vlib/strings/strings.v +++ b/vlib/strings/strings.v @@ -5,9 +5,10 @@ pub fn repeat(c byte, n int) string { return '' } mut arr := malloc(n + 1) + //mut arr := [byte(0); n + 1] for i := 0; i < n; i++ { arr[i] = c } arr[n] = `\0` - return tos(arr, n) + return string(arr, n) }