Strengthening Function.prototype.toString

Yuh-Ruey Chen maian330 at gmail.com
Fri Oct 10 20:02:40 PDT 2008


Juriy Zaytsev wrote:
> On Sep 26, 2008, at 5:32 PM, Hallvord R. M. Steen wrote:
>>  
>> memory - to look at the names of the named arguments and do something  
>> magic if the first argument is $super. I'm not up-to-date on whether
>> the  
>> next iteration has an elegant solution for this apparent requirement.)
>
> Latest build does the same thing. That "something magic" is actually
> just creating a reference to a "super" method (same-named method of a
> parent class). Function decompilation, while non-standard, seems to be
> the best solution when implementing such inheritance mechanism. We
> would gladly consider alternative (more compliant) solutions, if any
> exist.

So as far as I can tell, what we need to discourage usage of
func.toString() is:

1) An API for function currying/partial evaluation (specializing/binding
certain arguments).
2) A read-only property on functions that contains the list of parameter
names.

For any other purpose, I would think that you might as well parse the
whole source file rather than just the function, since you would need to
keep track of closures. For example, consider this:

function foo() {
    var x = 10;
    return function() {
       return x;
    };
}

function bar(f) {
    var x = 20;
    print(f());
    print(eval(f.toString())());
}

bar(foo());

That would print 10, then 20. So as you can see, even in the same scope,
eval(f.toString())(...) is not necessarily equal to f(), even if
f.toString() is a correct decompilation of f.


More information about the Es-discuss mailing list