Semantics of "indexed" string access

Allen Wirfs-Brock Allen.Wirfs-Brock at microsoft.com
Tue Jun 24 18:06:19 PDT 2008


A slightly revised (last two steps) version of the pseudo-code:

15.5.5.2 [[GetOwnProperty]] (P)
String objects use a variation of the [[GetOwnProperty]] method used for other native ECMAScript objects (8.6.2.1.2).
Assume S is a String object and P is a string.
When the [[GetOwnProperty]] method of S is called with property name P, the following steps are taken:
1.      Call the default [[GetOwnProperty]] method (8.6.2.1.2) with S as the this value and argument P.
2.      If Result(1) is not undefined return Result(1).
3.      If P is not an array index (15.4) , return undefined.
4.      Call ToString, giving S as its argument.
5.      Call ToInteger(P).
6.      Compute the number of characters in Result(4).
7.      If Result(5) is less than 0 or is not less than Result(4), return undefined.
8.      Create a string of length 1, containing one character from Result(4), namely the character at position Result(5), where the first (leftmost) character in Result(4) is considered to be at position 0, the next one at position 1, and so on.
9.      Return a DDesc with attributes {[[Value]]: Result(8), [[Enumerable]]: false, [[Writeable]]: false, [[Dynamic]]: false]


From: es4-discuss-bounces at mozilla.org [mailto:es4-discuss-bounces at mozilla.org] On Behalf Of Allen Wirfs-Brock
Sent: Tuesday, June 24, 2008 5:46 PM
To: es3.x-discuss at mozilla.org; Pratap Lakshman (VJ#SDK)
Cc: es4-discuss at mozilla.org
Subject: Semantics of "indexed" string access

I've taken a crack at cleaning up Pratap's initial specification for supporting direct indexing of strings, eg "abc"[1] yields "b"

Here are the semantics that seemed to make sense:

s[n]
1)  If s has an own property whose name is the same as the value of n, the value of that property is returned.
2) else If the value of n is convertible to a number that is within the bounds of the string value, return a string containing the corresponding character
3) else if try to resolve n as the name of an inherited property
4) else return undefined.

In other words, indexing into a string with a "valid" index returns that appropriate character unless somebody has explicitly defined a property named by that index on the object.
If the "index" is not within the bounds of the string it is treated like a normal property access - If it isn't defined either locally or on the parent chain the result is undefined.

I've specified this by "over-riding" the definition of [[GetOwnProperty]] for string instances".  Note that literal strings as in the example above will have been transformed into objects by the property access semantics of 11.2.1

15.5.5.2 [[GetOwnProperty]] (P)
String objects use a variation of the [[GetOwnProperty]] method used for other native ECMAScript objects (8.6.2.1.2).
Assume S is a String object and P is a string.
When the [[GetOwnProperty]] method of S is called with property name P, the following steps are taken:
10.   Call the default [[GetOwnProperty]] method (8.6.2.1.2) with S as the this value and argument P.
11.   If Result(1) is not undefined return Result(1).
12.   If P is not an array index (15.4) , return undefined.
13.   Call ToString, giving S as its argument.
14.   Call ToInteger(P).
15.   Compute the number of characters in Result(4).
16.   If Result(5) is less than 0 or is not less than Result(4), return undefined.
17.   Return a string of length 1, containing one character from Result(4), namely the character at position Result(5), where the first (leftmost) character in Result(4) is considered to be at position 0, the next one at position 1, and so on.


Is this a reasonable semantics? Does it match what browsers have already implemented?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.mozilla.org/pipermail/es-discuss/attachments/20080624/de36e200/attachment-0002.html 


More information about the Es4-discuss mailing list