Skip to content

Commit

Permalink
cgen: fix shared object method call
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Aug 28, 2024
1 parent 426205e commit 8686347
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
6 changes: 3 additions & 3 deletions vlib/v/gen/c/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -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('&')
}
}
Expand Down Expand Up @@ -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')
}
}
Expand Down
2 changes: 2 additions & 0 deletions vlib/v/slow_tests/inout/publish_shared_object_method_call.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
hello
22
45 changes: 45 additions & 0 deletions vlib/v/slow_tests/inout/publish_shared_object_method_call.vv
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 8686347

Please sign in to comment.