diff --git a/vlib/builtin/bare/.checks/.gitignore b/vlib/builtin/bare/.checks/.gitignore index 7780991965..4132964f74 100644 --- a/vlib/builtin/bare/.checks/.gitignore +++ b/vlib/builtin/bare/.checks/.gitignore @@ -1 +1,5 @@ checks +linuxsys/linuxsys +string/string +consts/consts +structs/structs diff --git a/vlib/builtin/bare/.checks/checks.v b/vlib/builtin/bare/.checks/checks.v index 0868cf73e5..8fd3575d91 100644 --- a/vlib/builtin/bare/.checks/checks.v +++ b/vlib/builtin/bare/.checks/checks.v @@ -26,6 +26,7 @@ fn main() { vcheck("linuxsys") vcheck("string") vcheck("consts") + vcheck("structs") exit(0) } diff --git a/vlib/builtin/bare/.checks/string/string.v b/vlib/builtin/bare/.checks/string/string.v index 01d4426cf4..864d201b39 100644 --- a/vlib/builtin/bare/.checks/string/string.v +++ b/vlib/builtin/bare/.checks/string/string.v @@ -39,12 +39,24 @@ fn check_str_clone() { assert c == "-6789" } +fn check_string_add_works(){ + abc := 'abc' + combined := 'a' + 'b' + 'c' + assert abc.len == combined.len + assert abc[0] == combined[0] + assert abc[1] == combined[1] + assert abc[2] == combined[2] + assert abc[0] == `a` + assert abc == combined +} + fn main () { mut fails := 0 fails += forkedtest.normal_run(check_string_eq, "check_string_eq") fails += forkedtest.normal_run(check_i64_tos, "check_i64_tos") fails += forkedtest.normal_run(check_i64_str, "check_i64_str") fails += forkedtest.normal_run(check_str_clone, "check_str_clone") + fails += forkedtest.normal_run(check_string_add_works, "check_string_add_works") assert fails == 0 sys_exit(0) } diff --git a/vlib/builtin/bare/.checks/structs/structs.v b/vlib/builtin/bare/.checks/structs/structs.v new file mode 100644 index 0000000000..f63285d7e2 --- /dev/null +++ b/vlib/builtin/bare/.checks/structs/structs.v @@ -0,0 +1,42 @@ +module main +import forkedtest + +struct SimpleEmptyStruct{ +} + +struct NonEmptyStruct{ + x int + y int + z int +} + +fn check_simple_empty_struct(){ + s := SimpleEmptyStruct{} + addr_s := &s + str_addr_s := ptr_str( addr_s ) + assert !isnil(addr_s) + assert str_addr_s.len > 3 + println(str_addr_s) +} + +fn check_non_empty_struct(){ + a := NonEmptyStruct{1,2,3} + b := NonEmptyStruct{4,5,6} + assert sizeof(NonEmptyStruct) > 0 + assert sizeof(SimpleEmptyStruct) < sizeof(NonEmptyStruct) + assert a.x == 1 + assert a.y == 2 + assert a.z == 3 + assert b.x + b.y + b.z == 15 + assert ptr_str(&a) != ptr_str(&b) + println('sizeof SimpleEmptyStruct:' + i64_str( sizeof(SimpleEmptyStruct) , 10 )) + println('sizeof NonEmptyStruct:' + i64_str( sizeof(NonEmptyStruct) , 10 )) +} + +fn main(){ + mut fails := 0 + fails += forkedtest.normal_run(check_simple_empty_struct, "check_simple_empty_struct") + fails += forkedtest.normal_run(check_non_empty_struct, "check_non_empty_struct") + assert fails == 0 + sys_exit(0) +} diff --git a/vlib/builtin/bare/string_bare.v b/vlib/builtin/bare/string_bare.v index 1dbd286b0d..1c6eb423d8 100644 --- a/vlib/builtin/bare/string_bare.v +++ b/vlib/builtin/bare/string_bare.v @@ -22,6 +22,22 @@ pub fn tos(s byteptr, len int) string { } } +fn (s string) add(a string) string { + new_len := a.len + s.len + mut res := string { + len: new_len + str: malloc(new_len + 1) + } + for j := 0; j < s.len; j++ { + res[j] = s[j] + } + for j := 0; j < a.len; j++ { + res[s.len + j] = a[j] + } + res[new_len] = `\0`// V strings are not null terminated, but just in case + return res +} + /* pub fn tos_clone(s byteptr) string { if s == 0 { @@ -100,6 +116,13 @@ pub fn i64_str(n0 i64, base int) string { return i64_tos(buf, 79, n0, base) } +pub fn ptr_str(ptr voidptr) string { + buf := [16]byte + hex := i64_tos(buf, 15, i64(ptr), 16) + res := '0x' + hex + return res +} + pub fn (a string) clone() string { mut b := string { len: a.len @@ -109,3 +132,4 @@ pub fn (a string) clone() string { b[a.len] = `\0` return b } + diff --git a/vlib/compiler/cheaders.v b/vlib/compiler/cheaders.v index aeac952f6b..db86d3b511 100644 --- a/vlib/compiler/cheaders.v +++ b/vlib/compiler/cheaders.v @@ -2,6 +2,28 @@ module compiler const ( +c_common_macros = ' + +#define EMPTY_STRUCT_DECLARATION +#define EMPTY_STRUCT_INITIALIZATION 0 +// Due to a tcc bug, the length of an array needs to be specified, but GCC crashes if it is... +#define EMPTY_ARRAY_OF_ELEMS(x,n) (x[]) +#define TCCSKIP(x) x + +#ifdef __TINYC__ +#undef EMPTY_STRUCT_DECLARATION +#undef EMPTY_STRUCT_INITIALIZATION +#define EMPTY_STRUCT_DECLARATION char _dummy +#define EMPTY_STRUCT_INITIALIZATION 0 +#undef EMPTY_ARRAY_OF_ELEMS +#define EMPTY_ARRAY_OF_ELEMS(x,n) (x[n]) +#undef TCCSKIP +#define TCCSKIP(x) +#endif + +#define OPTION_CAST(x) (x) +' + c_headers = ' //#include // int64_t etc @@ -69,24 +91,7 @@ c_headers = ' #include // os__wait uses wait on nix #endif -#define EMPTY_STRUCT_DECLARATION -#define EMPTY_STRUCT_INITIALIZATION 0 -// Due to a tcc bug, the length of an array needs to be specified, but GCC crashes if it is... -#define EMPTY_ARRAY_OF_ELEMS(x,n) (x[]) -#define TCCSKIP(x) x - -#ifdef __TINYC__ -#undef EMPTY_STRUCT_DECLARATION -#undef EMPTY_STRUCT_INITIALIZATION -#define EMPTY_STRUCT_DECLARATION char _dummy -#define EMPTY_STRUCT_INITIALIZATION 0 -#undef EMPTY_ARRAY_OF_ELEMS -#define EMPTY_ARRAY_OF_ELEMS(x,n) (x[n]) -#undef TCCSKIP -#define TCCSKIP(x) -#endif - -#define OPTION_CAST(x) (x) +$c_common_macros #ifdef _WIN32 #define WINVER 0x0600 @@ -209,19 +214,7 @@ typedef map map_string; bare_c_headers = ' -#define EMPTY_ARRAY_OF_ELEMS(x,n) (x[]) -#define TCCSKIP(x) x - -#ifdef __TINYC__ -#undef EMPTY_ARRAY_OF_ELEMS -#define EMPTY_ARRAY_OF_ELEMS(x,n) (x[n]) -#undef TCCSKIP -#define TCCSKIP(x) -#endif - -#ifndef EMPTY_STRUCT_INITIALIZATION -#define EMPTY_STRUCT_INITIALIZATION 0 -#endif +$c_common_macros #ifndef exit #define exit(rc) sys_exit(rc) @@ -229,3 +222,5 @@ void sys_exit (int); #endif ' ) + +