From 8686347ea4504907de49914a791f354fb02ba16d Mon Sep 17 00:00:00 2001 From: yuyi Date: Wed, 28 Aug 2024 15:42:28 +0800 Subject: [PATCH] cgen: fix shared object method call --- vlib/v/gen/c/fn.v | 6 +-- .../publish_shared_object_method_call.out | 2 + .../publish_shared_object_method_call.vv | 45 +++++++++++++++++++ 3 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 vlib/v/slow_tests/inout/publish_shared_object_method_call.out create mode 100644 vlib/v/slow_tests/inout/publish_shared_object_method_call.vv diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index cd7e55fbd6674a..a910ec8aaacd53 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -1755,7 +1755,7 @@ fn (mut g Gen) method_call(node ast.CallExpr) { g.write('ADDR(${rec_cc_type}, ') cast_n++ } else if !is_array_method_first_last_repeat && !(left_type.has_flag(.shared_f) - && left_type == node.receiver_type) { + && g.typ(left_type) == g.typ(node.receiver_type)) { g.write('&') } } @@ -1867,8 +1867,8 @@ fn (mut g Gen) method_call(node ast.CallExpr) { g.write(embed_name) } } - if left_type.has_flag(.shared_f) - && (left_type != node.receiver_type || is_array_method_first_last_repeat) { + if left_type.has_flag(.shared_f) && (g.typ(left_type) != g.typ(node.receiver_type) + || is_array_method_first_last_repeat) { g.write('->val') } } diff --git a/vlib/v/slow_tests/inout/publish_shared_object_method_call.out b/vlib/v/slow_tests/inout/publish_shared_object_method_call.out new file mode 100644 index 00000000000000..4b2e3a89a8be7c --- /dev/null +++ b/vlib/v/slow_tests/inout/publish_shared_object_method_call.out @@ -0,0 +1,2 @@ +hello +22 diff --git a/vlib/v/slow_tests/inout/publish_shared_object_method_call.vv b/vlib/v/slow_tests/inout/publish_shared_object_method_call.vv new file mode 100644 index 00000000000000..508db9491ae977 --- /dev/null +++ b/vlib/v/slow_tests/inout/publish_shared_object_method_call.vv @@ -0,0 +1,45 @@ +pub struct EventBus[T] { +mut: + subscribers map[string][]Subscriber[T] +} + +type EventType = string +type Subscriber[T] = fn (T) + +pub fn new_event_bus[T]() &EventBus[T] { + return &EventBus[T]{ + subscribers: map[string][]Subscriber[T]{} + } +} + +fn (shared eb EventBus[T]) subscribe(event_type EventType, subscriber Subscriber[T]) { + lock eb { + if event_type !in eb.subscribers { + eb.subscribers[event_type] = []Subscriber[T]{} + } + + eb.subscribers[event_type] << subscriber + } +} + +fn (shared eb EventBus[T]) publish(event_type EventType, data T) { + lock eb { + for _, subscriber in eb.subscribers[event_type] { + subscriber(data) + } + } +} + +fn main() { + shared eb_string := new_event_bus[string]() + eb_string.subscribe('test', fn (data string) { + println(data) + }) + eb_string.publish('test', 'hello') + + shared eb_int := new_event_bus[int]() + eb_int.subscribe('test', fn (data int) { + println(data) + }) + eb_int.publish('test', 22) +}