The XWalk internal extensions have a mechanism very similar to this. There we call this function `wrapCallback`.