From 355f46f47567a02b4f19fd8d83fef17e2319f5a0 Mon Sep 17 00:00:00 2001 From: shadowninja55 <49539636+shadowninja55@users.noreply.github.com> Date: Sat, 17 Jul 2021 04:19:28 -0400 Subject: [PATCH] gen: implement `thread.str()` (#10820) --- vlib/v/gen/c/auto_str_methods.v | 9 +++++++++ vlib/v/tests/thread_to_string_test.v | 8 ++++++++ 2 files changed, 17 insertions(+) create mode 100644 vlib/v/tests/thread_to_string_test.v diff --git a/vlib/v/gen/c/auto_str_methods.v b/vlib/v/gen/c/auto_str_methods.v index 4a7a51d964..ad1fd6b436 100644 --- a/vlib/v/gen/c/auto_str_methods.v +++ b/vlib/v/gen/c/auto_str_methods.v @@ -172,6 +172,9 @@ fn (mut g Gen) gen_str_for_type(typ ast.Type) string { ast.Chan { g.gen_str_for_chan(sym.info, styp, str_fn_name) } + ast.Thread { + g.gen_str_for_thread(sym.info, styp, str_fn_name) + } else { verror("could not generate string method $str_fn_name for type '$styp'") } @@ -467,6 +470,12 @@ fn (mut g Gen) gen_str_for_chan(info ast.Chan, styp string, str_fn_name string) g.auto_str_funcs.writeln('static string ${str_fn_name}($styp x) { return sync__Channel_auto_str(x, _SLIT("$elem_type_name")); }') } +fn (mut g Gen) gen_str_for_thread(info ast.Thread, styp string, str_fn_name string) { + ret_type_name := util.strip_main_name(g.table.get_type_name(info.return_type)) + g.type_definitions.writeln('static string ${str_fn_name}($styp _); // auto}') + g.auto_str_funcs.writeln('static string ${str_fn_name}($styp _) { return _SLIT("thread($ret_type_name)");}') +} + [inline] fn styp_to_str_fn_name(styp string) string { return styp.replace_each(['*', '', '.', '__', ' ', '__']) + '_str' diff --git a/vlib/v/tests/thread_to_string_test.v b/vlib/v/tests/thread_to_string_test.v new file mode 100644 index 0000000000..944efccc40 --- /dev/null +++ b/vlib/v/tests/thread_to_string_test.v @@ -0,0 +1,8 @@ +fn ret_ten() int { + return 10 +} + +fn test_thread_str() { + th := go ret_ten() + assert th.str() == 'thread(int)' +}