Skip to content

Commit 00d8e0f

Browse files
author
Rafael Gago
committed
interface: Don't mutate user options
This patch fixes cases where the 'opts' table passed by the user is modified, making the external table unable to be reused in some cases. This was causing problems e.g. when dealing with properties, as the user was having a predefined dbus options table, as the "interface" key was changed internally to 'org.freedesktop.DBus.Properties' without being changed back on return.
1 parent cdef26d commit 00d8e0f

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

init.lua

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,22 @@ else
1717
dbus.exit = dbus.raw.exit
1818
end
1919

20+
local function optscopy(opts)
21+
if type(opts) ~= "table" then return nil end
22+
return {
23+
args = opts.args,
24+
bus = opts.bus,
25+
callback = opts.callback,
26+
destination = opts.destination,
27+
handler = opts.handler,
28+
interface = opts.interface,
29+
origin = opts.origin,
30+
path = opts.path,
31+
sender = opts.sender,
32+
type = opts.type,
33+
}
34+
end
35+
2036
function dbus.signal_handler(signal, ...)
2137
signal.events = ((dbus.signals[signal.bus] or {})[signal.interface] or {}).events
2238
if not signal.events then return end
@@ -58,7 +74,7 @@ function dbus.on(name, callback, opts)
5874
elseif callback and type(callback) ~= 'function' then
5975
callback, opts = nil, callback
6076
end
61-
opts = opts or {}
77+
local opts = optscopy(opts) or {}
6278
opts.type = opts.type or "signal"
6379
opts.bus = opts.bus or "session"
6480
callback = callback or opts.callback
@@ -115,7 +131,7 @@ end
115131

116132

117133
function dbus.off(name, callback, opts)
118-
opts = opts or {}
134+
local opts = optscopy(opts) or {}
119135
opts.type = opts.type or "signal"
120136
opts.bus = opts.bus or "session"
121137
local signal = (dbus.signals[opts.bus] or {})[opts.interface]
@@ -156,7 +172,7 @@ function dbus.call(name, callback, opts)
156172
if callback and type(callback) ~= 'function' then
157173
callback, opts = nil, callback
158174
end
159-
opts = opts or {}
175+
local opts = optscopy(opts) or {}
160176
opts.type = opts.type or "method_call"
161177
opts.bus = opts.bus or "session"
162178
callback = callback or opts.callback
@@ -179,9 +195,8 @@ function dbus.call(name, callback, opts)
179195
return serial
180196
end
181197

182-
183198
function dbus.property.get(name, callback, opts)
184-
opts = opts or {}
199+
local opts = optscopy(opts) or {}
185200
opts.args = {'s', opts.interface, 's', name} -- actual arguments to Get
186201
opts.interface = 'org.freedesktop.DBus.Properties'
187202
callback = callback or opts.callback
@@ -190,7 +205,7 @@ end
190205

191206

192207
function dbus.property.set(name, value, opts)
193-
opts = opts or {}
208+
local opts = optscopy(opts) or {}
194209
opts.args = {'s', opts.interface, 's', name, 'v', value} -- actual arguments to Get
195210
opts.interface = 'org.freedesktop.DBus.Properties'
196211
return dbus.call('Set', opts)

0 commit comments

Comments
 (0)