this.callSuper() throws an exception

28 views
Skip to first unread message

Javier Peletier

unread,
Jan 17, 2012, 8:31:33 PM1/17/12
to jsclas...@googlegroups.com

Hi!

 

I have found out this.callSuper() throws an uncontrolled exception if the base class does not implement the method.

 

While I think it makes sense that this call throws some exception if the base class does not implement the method, when coming to modules, well, the module implementer may be does not know if the base class the module will be included in will implement that method.

 

Therefore, I have modified core.js this way:

 

 

JS.Method.keyword('callSuper', function (method, env, receiver, args)

{

       var methods = env.lookup(method.name),

      stackIndex = methods.length - 1,

      params = JS.array(args);

 

       return function ()

       {

              var i = arguments.length;

              while (i--) params[i] = arguments[i];

 

              if (stackIndex==0)

                     return null;

 

              stackIndex -= 1;

              var returnValue = methods[stackIndex].apply(receiver, params);

              stackIndex += 1;

 

              return returnValue;

       };

});

 

This way, if someone attempts to use this.callSuper() when there is no function upwards, it just returns null and that’s it.

 

Anyway, the error thrown was cryptic, since it would try to call methods[-1].apply(), and the array would return undefined, thus crashing.

 

Cheers,

 

/j

 

James Coglan

unread,
Jan 18, 2012, 5:10:39 AM1/18/12
to jsclas...@googlegroups.com
On 18 January 2012 01:31, Javier Peletier <j...@peletier.com> wrote:

Hi!

 

I have found out this.callSuper() throws an uncontrolled exception if the base class does not implement the method.

 

While I think it makes sense that this call throws some exception if the base class does not implement the method, when coming to modules, well, the module implementer may be does not know if the base class the module will be included in will implement that method.


Ruby's approach to this is to make defined?(super) work so modules can check if they need to. It might be possible to remove the callSuper method when we reach the end of the stack, so you could do a similar thing in JavaScript. 

j...@peletier.com

unread,
Jan 20, 2012, 4:32:07 AM1/20/12
to jsclas...@googlegroups.com
I think I will stick to my modification. If I am always checking to see if can call a method, then I'd rather have the check inside the method and simplify my code. For me, callSuper means "do your stuff", either you call it before your code and then follow up, or you call it after to intercept, so I am not worried about it silently returning a null or 'undefined'.

Nevertheless, I think core.js should not throw an Index out of bounds exception, but rather a "no parent method to call" exception.

Cheers,
J
---
Enviado con el móvil
Sent via mobile

From: James Coglan <jco...@gmail.com>
Date: Wed, 18 Jan 2012 10:10:39 +0000
Subject: Re: this.callSuper() throws an exception

James Coglan

unread,
Jan 29, 2012, 7:36:17 AM1/29/12
to jsclas...@googlegroups.com
On 20 January 2012 09:32, <j...@peletier.com> wrote:
Nevertheless, I think core.js should not throw an Index out of bounds exception, but rather a "no parent method to call" exception.


The effect of this is that callSuper is not defined if there is no parent method available. So in the following program:

var A = new JS.Class({
  foo: function() { this.callSuper() }
})

new A().foo()

The errors thrown by Node are:

3.0.x: TypeError: Cannot call method 'apply' of undefined
safe-super: TypeError: Property 'callSuper' of object #<:13529748362> is not a function 
Reply all
Reply to author
Forward
0 new messages