From 8ab183f4c8a6e638982b6c903490cd3fbe14d1a5 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Fri, 2 May 2014 21:26:49 -0400 Subject: [PATCH] Setting super method on override wrapper --- lib/MethodWrappers.js | 5 ++++- test/MethodWrappersTest.js | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/lib/MethodWrappers.js b/lib/MethodWrappers.js index 9dc6eea..6f7d198 100644 --- a/lib/MethodWrappers.js +++ b/lib/MethodWrappers.js @@ -26,7 +26,7 @@ exports.standard = { wrapOverride: function( method, super_method, cid, getInst ) { - return function() + var retf = function() { var context = getInst( this, cid ) || this, retval = undefined @@ -55,6 +55,9 @@ exports.standard = { return retval; }; + + retf.super = super_method; + return retf; }, diff --git a/test/MethodWrappersTest.js b/test/MethodWrappersTest.js index 64aee69..00fa6e3 100644 --- a/test/MethodWrappersTest.js +++ b/test/MethodWrappersTest.js @@ -201,6 +201,39 @@ require( 'common' ).testCase( }, + /** + * While __super is convenient and concise, it is not general-purpose + * and does not solve the problem of invoking any arbitrary method on + * the supertype. In particular, we may override some method foo, but + * wish to call the parent foo in another method; we cannot do that with + * __super. + * + * Note, however, that this will require foo.super.call( this ) to + * provide the proper context. + */ + 'Can invoke super method by calling override.super': function() + { + var expected = {}, + getInst = function() { return {}; }, + + // super method to be overridden + method = this._sut.standard.wrapNew( + function() { return expected; }, + null, 0, getInst + ), + + // the overriding method (we don't care what this does) + override = this._sut.standard.wrapOverride( + function() {}, method, 0, getInst + ) + ; + + // we should be able to invoke the super method by override.super, + // which is added atop of the wrapper + this.assertStrictEqual( override.super(), expected ); + }, + + /** * The proxy wrapper should forward all arguments to the provided object's * appropriate method. The return value should also be proxied back to the