More string indexing semantics issues
Maciej Stachowiak
mjs at apple.com
Wed Jun 25 18:27:49 PDT 2008
On Jun 25, 2008, at 4:00 PM, Garrett Smith wrote:
>
>> In spec terms, WebKit's behavior can be explained in
>> terms of strings having additional DontDelete ReadOnly properties.
>
> Let me get this straight:
>
> Webkit's behavior can be explained in terms of String objects having
> additional properties with numeric names and the attributes
> {DontDelete ReadOnly}
>
> Is that what you meant?
Yes.
>> The
>> Mozilla behavior can be explained as strings having those same
>> additional
>> properties, but they are not ReadOnly. In both cases, index
>> properties past
>> the last character do not exist ahead of time.
>>
>
> My observations indicate otherwise. Webkit does not appear to create
> additional properties to String objects.
>
> javascript:alert(Object("foo").hasOwnProperty(0));
>
>
> FF2 - true
> Sf3 - false
> Op9 - false
>
> Where does the "0" property exist, Maciej? Is this bug related to
> hasOwnProperty?
I just tried this in Safari 3.1 and Safari alerted true. The same
happens in WebKit trunk. If it alerted false I would say that is a bug.
> It appears to me that Mozilla and Opera and Webkit all implement a
> specialized [[Get]], where Opera and Mozilla do:
>
> 1) Look for property P on String object.
> 2) Look for String instance charAt( P )
> 3) Look in String prototype.
>
> Webkit does:-
> 1) Look for String instance charAt( P )
> 2) Call the[[Get]] method on S with argument P.
You could model it in many ways. I have not looked at Mozilla's or
Opera's actual implementations. What I am saying is that Safari/WebKit
tries to publicly present the logical model that these are ReadOnly
DontDelete properties. How it's actually implemented isn't really
relevant. In WebKit's implementation we implement all sorts of JS
properties internally in ways other than putting them in the generic
object property map.
It is true that in spec-speak you could define it as special [[Get]]
and [[Put]] behavior (and other operations like [[Delete]] and
[[HasOwnProperty]]) instead of special properties.
> javascript:var f = Object("1234567");void(String.prototype[0] = "0");
> void(f[0] = "8"); alert(f[0]);
>
> "8" in Opera9 and FF2.
> "0" in Saf3.
>
> In Opera, the object doesn't have numeric properties, and only appears
> to have special [[Get]]:-
> javascript:alert("0" in Object("foo"));
> javascript:alert(Object("foo")[0]);
>
> Op9 - false and "f"
> FF2 - true and "f"
>
> Mozilla has the properties on the object and Opera doesn't.
>
> (this explains why - Object("foo").hasOwnProperty(0) - is false in
> Opera.
>
>> The reason for the way WebKit does things, for what it's worth, is
>> because
>> index properties of the string are checked first before normal
>> properties
>> (because they can't be overwritten), so "abc"[1] can be as fast as
>> an array
>> access instead of going at the speed of normal property access.
>>
>
> So the [[Put]] method on a String instance is different in Webkit.
What I am talking about above is the equivalent of the spec's [[Get]],
not [[Put]]. The specialization I describe is for performance, and
behaviorally transparent. However, our JavaScript implementation
doesn't have things that correspond exactly to the spec's [[Get]] and
[[Put]] formalisms.
Regards,
Maciej
More information about the Es4-discuss
mailing list