Strengthening Function.prototype.toString

Yehuda Katz wycats at gmail.com
Sat Sep 27 10:00:39 PDT 2008


I use toString in a test suite that I built (jspec) to simplify the testing
DSL. Effectively, I extract the string contents of the function, and build a
new function with parameters representing some methods I want to make
available inside, and then call the function with those methods as
parameters.
It's a little bit of a hack, but protects the global namespace and allows me
to keep the DSL simple.

I believe Screw.Unit uses the same technique, as it's based on my original
jspec proof-of-concept.

Also, if toString() was guaranteed to return a valid JS representation of
the original function, it would be theoretically possible to mutate
functions (a la lisp macros) with a pure-JS JS parser. For the most part, I
have assumed that a correct toString() is available on implementations I'm
worried about people wanting to run jspec.

-- Yehuda

2008/9/26 Juriy Zaytsev <kangax at gmail.com>

>
> On Sep 26, 2008, at 5:32 PM, Hallvord R. M. Steen wrote:
>
> On Fri, 26 Sep 2008 19:58:27 +0200, liorean <liorean at gmail.com> wrote:
>
> 2008/9/26 Erik Arvidsson <erik.arvidsson at gmail.com>:
>
> I know that Opera on mobile phones used to return a string
>
> representation that did not reflect the original.
>
>
> Yeah. Opera Mobile returned "[ECMAScript code]" or "[ecmascript
>
> code]". This was contrary to the ES3 spec (must be parsable as a
>
> function definition, IIRC) and also breaks the eval roundtripping by
>
> throwing a parse error.
>
> Anybody know if those issues have been fixed in more modern versions?
>
>
> No, not consistently across "modern versions". It's not likely to be
> properly "fixed" for a while yet. The reason is that on many platforms
> where memory is scarce, not enabling JS decompilation helps reduce memory
> requirements.
>
> Unfortunately several major libraries (prototype in particular, jQuery to
> a minor extent) have started (ab)using decompilation for various purposes.
>
> Pages that rely on this tend to malfunction in Opera on low-memory
> devices. Some Opera versions make at least some of these pages work again
> by returning "function(){[ecmascript code]}" instead but that's of course
> a silly hack..
>
> (It would be great if ES-next could have a look at why these libraries
> rely on toString - hacks and make it possible to do equivalent things with
>
> nicer language constructs. One of the reasons toString is used is probably
>
> outside the group's scope and caused by typeOf bugs in IE: prototype's
> isFunction method uses toString in order to reliably detect whether
> something is a function reference. Another usage is apparently - from my
>
>
> That's what jQuery's `isFunction` used to do. Afaik, they reverted it to a
> simple `instanceof` check just recently. In prototype.js, on the other hand,
> we tried to work around cross-frame "issues" that arise when using
> `instanceof`. Doing a `typeof` check (of both an object and its call
> property) seems to be sufficient.
>
>
> 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.
>
>
>
> --
> Hallvord R. M. Steen
> Core QA JavaScript tester, Opera Software
> http://www.opera.com/
> Opera - simply the best Internet experience
> _______________________________________________
> Es-discuss mailing list
> Es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
> --
> *Juriy Zaytsev*
> *
> *
>
> _______________________________________________
> Es-discuss mailing list
> Es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>


-- 
Yehuda Katz
Developer | Engine Yard
(ph) 718.877.1325
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.mozilla.org/pipermail/es-discuss/attachments/20080927/cca94bed/attachment.html 


More information about the Es-discuss mailing list