From 1de299ad2280b61b59b8a11c5da03390b92663d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Uwe=20Kr=C3=BCger?= <45282134+UweKrueger@users.noreply.github.com> Date: Tue, 2 Feb 2021 13:13:13 +0100 Subject: [PATCH] cgen: allow `shared` initialization from return values of functions (#8512) --- vlib/v/gen/cgen.v | 16 ++++++++++++++++ vlib/v/tests/shared_array_test.v | 13 +++++++++++-- vlib/v/tests/shared_map_test.v | 13 +++++++++++-- 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index ecfa9852f5..4371b7924d 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -2530,6 +2530,19 @@ fn (mut g Gen) expr(node ast.Expr) { // if g.fileis('1.strings') { // println('\ncall_expr()()') // } + mut shared_styp := '' + if g.is_shared { + ret_sym := g.table.get_type_symbol(node.return_type) + shared_typ := node.return_type.set_flag(.shared_f) + shared_styp = g.typ(shared_typ) + if ret_sym.kind == .array { + g.writeln('($shared_styp*)__dup_shared_array(&($shared_styp){.val = ') + } else if ret_sym.kind == .map { + g.writeln('($shared_styp*)__dup_shared_map(&($shared_styp){.val = ') + } else { + g.writeln('($shared_styp*)__dup${shared_styp}(&($shared_styp){.val = ') + } + } g.call_expr(node) // if g.fileis('1.strings') { // println('before:' + node.autofree_pregen) @@ -2546,6 +2559,9 @@ fn (mut g Gen) expr(node ast.Expr) { g.strs_to_free0 = [] // println('pos=$node.pos.pos') } + if g.is_shared { + g.writeln('}, sizeof($shared_styp))') + } // if g.autofree && node.autofree_pregen != '' { // g.strs_to_free0.len != 0 { /* if g.autofree { diff --git a/vlib/v/tests/shared_array_test.v b/vlib/v/tests/shared_array_test.v index 185c5e7c10..41b6a2d32d 100644 --- a/vlib/v/tests/shared_array_test.v +++ b/vlib/v/tests/shared_array_test.v @@ -46,20 +46,29 @@ fn test_shared_init_syntax() { shared bar := [-12.5, 23.125, 6.0625, 12.5] shared baz := &[]int{len: 5, cap: 12} shared qux := []f64{len: 7} + shared quux := new_array() lock foo { foo[2] = 20 } lock bar { bar[3] = 12.5 } - lock baz, qux { + lock baz, qux, quux { baz[3] = 12 qux[6] = -17.0625 + quux[2] = 7.0625 } - rlock foo, bar, baz, qux { + rlock foo, bar, baz, qux, quux { assert foo[2] == 20 assert bar[3] == 12.5 assert baz[3] == 12 assert qux[6] == -17.0625 + assert quux[1] == 6.25 + assert quux[2] == 7.0625 } } + +fn new_array() []f64 { + a := [12.5, 6.25, -3.125, 1.75] + return a +} diff --git a/vlib/v/tests/shared_map_test.v b/vlib/v/tests/shared_map_test.v index 22cd55b0ba..2ed8a01b15 100644 --- a/vlib/v/tests/shared_map_test.v +++ b/vlib/v/tests/shared_map_test.v @@ -41,20 +41,29 @@ fn test_shared_init_syntax() { shared bar := {'wer': 13.75, 'cvbn': -7.25, 'asd': -0.0625} shared baz := &map[string]int{} shared qux := map[string]f64{} + shared quux := new_map() lock foo { foo['q'] = 20 } lock bar { bar['asd'] = 12.5 } - lock baz, qux { + lock baz, qux, quux { baz['wer'] = 12 qux['abc'] = -17.0625 + quux['tzu'] = 1.125 } - rlock foo, bar, baz, qux { + rlock foo, bar, baz, qux, quux { assert foo['q'] == 20 assert bar['asd'] == 12.5 assert baz['wer'] == 12 assert qux['abc'] == -17.0625 + assert quux['tzu'] == 1.125 + assert quux['yxc'] == 9.125 } } + +fn new_map() map[string]f64 { + m := { 'qwe': 34.25, 'yxc': 9.125, 'tzu': -7.5 } + return m +}