As I found out, relying on the module slot count is NOT a reliable way to detect if a beacon is allowed to affect a machine or not. What determines that is the allowed effects field of the machine prototype. A machine CAN have zero module slots, AND still be allowed to be affected by a beacon.
Therefore, instead of this code.
local module_inventory = machine.get_module_inventory()
if module_inventory == nil or #module_inventory == 0 then return end
should be
local module_inventory = machine.get_module_inventory()
local allowed_effects = machine.effects
if (module_inventory == nil or #module_inventory == 0) and allowed_effects == nil then return end
When allowed_effects is not nil, it is a table structured like this.
["pollution", ["bonus", value],
"productivity", ["bonus", value],
"effectivity", ["bonus", value],
"speed", ["bonus", value]], minus any effects not allowed.