Bug 20261

Summary: Support Array and String generic shorthands
Product: WebKit Reporter: Garrett Smith <xk1t>
Component: JavaScriptCoreAssignee: Nobody <webkit-unassigned>
Status: NEW ---    
Severity: Normal CC: ap, arv, barraclough, kangax, mamacdon, m.goleb+bugzilla, mjs, pmuellr, webmaster
Priority: P2    
Version: 528+ (Nightly build)   
Hardware: All   
OS: All   

Description Garrett Smith 2008-08-02 00:30:45 PDT
Array Generics the built-in Array.prototype methods as generic static methods on the built in Array. 

Array Generics accept an Array-like object as the first argument and return the method of the same name, called in context of the Array-like object.

// Non-generic. 
Array.prototype.slice.call(arrayLike);

// Generic.
Array.slice(arrayLike);

The generic approach reduces clutter and property lookups. It is cleaner and faster.

These methods are implemented in Firefox for quite some time now. For details, please see:
http://developer.mozilla.org/en/docs/New_in_JavaScript_1.6#Array_and_String_generics

Still unimplemented in:
Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10_4_11; en) AppleWebKit/527.3+ (KHTML, like Gecko) Version/3.1.1 Safari/525.18
Comment 1 Alexey Proskuryakov 2009-11-10 22:39:43 PST
Re-titling to (hopefully) better match terminology found in aforementioned developer.mozilla.org article. We do support generics, just not the new funny syntax for them.
Comment 2 Patrick Mueller 2009-12-04 08:24:11 PST
(In reply to comment #1)
> Re-titling to (hopefully) better match terminology found in aforementioned
> developer.mozilla.org article. We do support generics, just not the new funny
> syntax for them.

Just to be clear - I had to try it in a console to be sure - you can call the existing Array methods via Array.prototype.[method].call() and pass Strings.

   > Array.prototype.slice.call("123")
   < ["1", "2", "3"]
Comment 3 Erik Arvidsson 2009-12-04 10:02:24 PST
(In reply to comment #2)
> (In reply to comment #1)
> > Re-titling to (hopefully) better match terminology found in aforementioned
> > developer.mozilla.org article. We do support generics, just not the new funny
> > syntax for them.
> 
> Just to be clear - I had to try it in a console to be sure - you can call the
> existing Array methods via Array.prototype.[method].call() and pass Strings.
> 
>    > Array.prototype.slice.call("123")
>    < ["1", "2", "3"]

Yes, but it is a verbose and confusing compared to:

Array.slice('123')

That is exactly why Mozilla added these to JS in Firefox 1.5 (?).
Comment 4 Patrick Mueller 2009-12-04 10:45:22 PST
(In reply to comment #3)
> Yes, but it is a verbose and confusing compared to:

Oh, trust me, I agree!!!

When I first read the bug, I wasn't sure what  Alexey meant by "We do support generics".  Just wanted to clarify that bit, and so added an actual sample.

This shouldn't be too hard to add, should it?  I'd be happy to give it a go ...
Comment 5 Patrick Mueller 2009-12-07 11:31:19 PST
As my first foray into the JSC impl, I've hacked these in by adding the methods to ArrayConstructor.cpp (copying the pattern from DateConstructor.cpp).  I only did one method, just see if it actually worked; it did.  I copied the map() code from ArrayPrototype.cpp, changing as needed.

But clearly that's not The Right Thing To Do.  Wondering what is:

- copy the relevant functions into a separate cpp, making them externs, and then having ArrayPrototype and ArrayConstructor call into them?

- copy the relevant functions into a .h file as inline, using that .h file in the relevant .cpps?

- lazily lookup the existing ArrayPrototype calls out of the lut.h table, and in the ArrayConstructor code, munge the args and call into the original functions.

- something else

Guess I should also ask if I should even bother.  If I get these implemented in an agreeable fashion, would we even add these methods to Array?  Is there some religious or technical reason we wouldn't?
Comment 6 Erik Arvidsson 2009-12-07 11:37:15 PST
(In reply to comment #5)
> - copy the relevant functions into a separate cpp, making them externs, and
> then having ArrayPrototype and ArrayConstructor call into them?

This is what I would do but I would hope someone more senior to JSC could chime in.
Comment 7 Maciej Stachowiak 2009-12-07 13:57:37 PST
(In reply to comment #6)
> (In reply to comment #5)
> > - copy the relevant functions into a separate cpp, making them externs, and
> > then having ArrayPrototype and ArrayConstructor call into them?
> 
> This is what I would do but I would hope someone more senior to JSC could chime
> in.

In general this makes sense, but the extra level of function call overhead could result in a measurable performance regression, so it may be necessary to make the shared code inline.

My main concern about this feature would be whether we want to do something nonstandard - it seems like no one, not even Mozilla, is really pushing this to be in ECMAScript itself. Although we would consider just copying a Firefox extension if it seemed like a really good idea.
Comment 8 Garrett Smith 2010-03-17 17:51:35 PDT
(In reply to comment #7)
> (In reply to comment #6)
> > (In reply to comment #5)

[...]

> My main concern about this feature would be whether we want to do something
> nonstandard - it seems like no one, not even Mozilla, is really pushing this to
> be in ECMAScript itself. Although we would consider just copying a Firefox
> extension if it seemed like a really good idea.

A feature being implemented in multiple implementations and used in applications can provide reason for it to becoming a standard.

Copying this nonstandard feature would be an act of providing reason to for argument of the feature becoming standard. 

If you are seriously considering this route, maybe it would make sense to press the issue es-discuss a little harder beforehand.
Comment 9 kangax 2010-07-28 07:05:06 PDT
FWIW, there's a generics proposal in ES wiki <http://wiki.ecmascript.org/doku.php?id=proposals:static_generics>

Not sure how likely they are to end up in ES harmony (here's a last discussion, which didn't seem to have many nay-votes — https://mail.mozilla.org/pipermail/es-discuss/2009-December/010217.html). It looks like the only holdup is just that no one spec'd them yet.

I'm curious what's the webkit stance on this. Can we have them implemented before harmony?
Comment 10 Gavin Barraclough 2012-03-12 14:30:00 PDT
Looks like an easy fix if we want to do so, but this is currently in the proposal & not harmony:proposal namespace, so it doesn't look like its currently on track to standardization.