@@ -439,23 +439,36 @@ global.eval = new LocalProxy(localEval, EvalHandler);
439439 * Promise sanitization
440440 */
441441
442- if ( localPromise && ! allowAsync ) {
442+ if ( localPromise ) {
443443
444444 const PromisePrototype = localPromise . prototype ;
445445
446- overrideWithProxy ( PromisePrototype , 'then' , PromisePrototype . then , AsyncErrorHandler ) ;
447- // This seems not to work, and will produce
448- // UnhandledPromiseRejectionWarning: TypeError: Method Promise.prototype.then called on incompatible receiver [object Object].
449- // This is likely caused since the host.Promise.prototype.then cannot use the VM Proxy object.
450- // Contextify.connect(host.Promise.prototype.then, Promise.prototype.then);
446+ if ( ! allowAsync ) {
447+
448+ overrideWithProxy ( PromisePrototype , 'then' , PromisePrototype . then , AsyncErrorHandler ) ;
449+ // This seems not to work, and will produce
450+ // UnhandledPromiseRejectionWarning: TypeError: Method Promise.prototype.then called on incompatible receiver [object Object].
451+ // This is likely caused since the host.Promise.prototype.then cannot use the VM Proxy object.
452+ // Contextify.connect(host.Promise.prototype.then, Promise.prototype.then);
453+
454+ } else {
455+
456+ overrideWithProxy ( PromisePrototype , 'then' , PromisePrototype . then , {
457+ __proto__ : null ,
458+ apply ( target , thiz , args ) {
459+ if ( args . length > 1 ) {
460+ const onRejected = args [ 1 ] ;
461+ if ( typeof onRejected === 'function' ) {
462+ args [ 1 ] = function wrapper ( error ) {
463+ error = ensureThis ( error ) ;
464+ return localReflectApply ( onRejected , this , [ error ] ) ;
465+ } ;
466+ }
467+ }
468+ return localReflectApply ( target , thiz , args ) ;
469+ }
470+ } ) ;
451471
452- if ( PromisePrototype . finally ) {
453- overrideWithProxy ( PromisePrototype , 'finally' , PromisePrototype . finally , AsyncErrorHandler ) ;
454- // Contextify.connect(host.Promise.prototype.finally, Promise.prototype.finally);
455- }
456- if ( Promise . prototype . catch ) {
457- overrideWithProxy ( PromisePrototype , 'catch' , PromisePrototype . catch , AsyncErrorHandler ) ;
458- // Contextify.connect(host.Promise.prototype.catch, Promise.prototype.catch);
459472 }
460473
461474}
0 commit comments