If anyone wants to review my changes, they’re here:
I plan to integrate these changes into the next dev release of swipl.