Parliament has passed a law legalising gay marriage, 77 votes to 44, amid loud shows of jubilation from the floor and the gallery.