author | attila |
Mon, 02 Mar 2015 14:33:55 +0100 | |
changeset 29282 | a8523237b66c |
parent 27365 | 6dd6e324d1c7 |
child 33343 | 23abd10384a5 |
permissions | -rw-r--r-- |
16147 | 1 |
/* |
16151 | 2 |
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
16147 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package jdk.nashorn.internal.objects; |
|
27 |
||
17249
a2014831ae7a
8013325: function named 'arguments' should set DEFINES_ARGUMENTS flag in its parent, not itself
attila
parents:
17245
diff
changeset
|
28 |
import static jdk.nashorn.internal.lookup.Lookup.MH; |
16147 | 29 |
import static jdk.nashorn.internal.runtime.ECMAErrors.typeError; |
30 |
import static jdk.nashorn.internal.runtime.JSType.isRepresentableAsInt; |
|
31 |
import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED; |
|
27365
6dd6e324d1c7
8057825: Bug in apply specialization - if an apply specialization that is available doesn't fit, a new one wouldn't be installed, if the new code generated as a specialization didn't manage to do the apply specialization. Basically changing a conditional to an unconditional.
lagergren
parents:
27362
diff
changeset
|
32 |
|
16147 | 33 |
import java.lang.invoke.MethodHandle; |
34 |
import java.lang.invoke.MethodHandles; |
|
23372
09707b3e5fb0
8021350: Share script classes between threads/globals within context
hannesw
parents:
23084
diff
changeset
|
35 |
import java.lang.invoke.MethodType; |
16147 | 36 |
import java.text.Collator; |
37 |
import java.util.ArrayList; |
|
38 |
import java.util.Arrays; |
|
16192 | 39 |
import java.util.LinkedList; |
16147 | 40 |
import java.util.List; |
17776
59fb7b69846d
8015352: "i".toUpperCase() => currently returns "İ", but should be "I" (with Turkish locale)
sundar
parents:
17249
diff
changeset
|
41 |
import java.util.Locale; |
26058
17acdbe290b2
8054503: test/script/external/test262/test/suite/ch12/12.6/12.6.4/12.6.4-2.js fails with tip
sundar
parents:
24778
diff
changeset
|
42 |
import java.util.Set; |
16234
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16226
diff
changeset
|
43 |
import jdk.internal.dynalink.CallSiteDescriptor; |
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16226
diff
changeset
|
44 |
import jdk.internal.dynalink.linker.GuardedInvocation; |
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16226
diff
changeset
|
45 |
import jdk.internal.dynalink.linker.LinkRequest; |
18851
bdb92c95f886
8019947: inherited property invalidation does not work with two globals in same context
sundar
parents:
18618
diff
changeset
|
46 |
import jdk.nashorn.internal.lookup.MethodHandleFactory.LookupException; |
16147 | 47 |
import jdk.nashorn.internal.objects.annotations.Attribute; |
48 |
import jdk.nashorn.internal.objects.annotations.Constructor; |
|
49 |
import jdk.nashorn.internal.objects.annotations.Function; |
|
50 |
import jdk.nashorn.internal.objects.annotations.Getter; |
|
51 |
import jdk.nashorn.internal.objects.annotations.ScriptClass; |
|
52 |
import jdk.nashorn.internal.objects.annotations.SpecializedFunction; |
|
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
53 |
import jdk.nashorn.internal.objects.annotations.SpecializedFunction.LinkLogic; |
16147 | 54 |
import jdk.nashorn.internal.objects.annotations.Where; |
55 |
import jdk.nashorn.internal.runtime.ConsString; |
|
56 |
import jdk.nashorn.internal.runtime.JSType; |
|
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
57 |
import jdk.nashorn.internal.runtime.OptimisticBuiltins; |
18618
136279c4cbe6
8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents:
18318
diff
changeset
|
58 |
import jdk.nashorn.internal.runtime.PropertyMap; |
16147 | 59 |
import jdk.nashorn.internal.runtime.ScriptFunction; |
60 |
import jdk.nashorn.internal.runtime.ScriptObject; |
|
61 |
import jdk.nashorn.internal.runtime.ScriptRuntime; |
|
62 |
import jdk.nashorn.internal.runtime.arrays.ArrayIndex; |
|
63 |
import jdk.nashorn.internal.runtime.linker.NashornGuards; |
|
64 |
import jdk.nashorn.internal.runtime.linker.PrimitiveLookup; |
|
65 |
||
66 |
||
67 |
/** |
|
68 |
* ECMA 15.5 String Objects. |
|
69 |
*/ |
|
70 |
@ScriptClass("String") |
|
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
71 |
public final class NativeString extends ScriptObject implements OptimisticBuiltins { |
16147 | 72 |
|
73 |
private final CharSequence value; |
|
74 |
||
26508
b40ef4386b01
8057021: UserAccessorProperty guards fail with multiple globals
hannesw
parents:
26068
diff
changeset
|
75 |
/** Method handle to create an object wrapper for a primitive string */ |
b40ef4386b01
8057021: UserAccessorProperty guards fail with multiple globals
hannesw
parents:
26068
diff
changeset
|
76 |
static final MethodHandle WRAPFILTER = findOwnMH("wrapFilter", MH.type(NativeString.class, Object.class)); |
b40ef4386b01
8057021: UserAccessorProperty guards fail with multiple globals
hannesw
parents:
26068
diff
changeset
|
77 |
/** Method handle to retrieve the String prototype object */ |
23372
09707b3e5fb0
8021350: Share script classes between threads/globals within context
hannesw
parents:
23084
diff
changeset
|
78 |
private static final MethodHandle PROTOFILTER = findOwnMH("protoFilter", MH.type(Object.class, Object.class)); |
16147 | 79 |
|
18618
136279c4cbe6
8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents:
18318
diff
changeset
|
80 |
// initialized by nasgen |
136279c4cbe6
8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents:
18318
diff
changeset
|
81 |
private static PropertyMap $nasgenmap$; |
136279c4cbe6
8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents:
18318
diff
changeset
|
82 |
|
18851
bdb92c95f886
8019947: inherited property invalidation does not work with two globals in same context
sundar
parents:
18618
diff
changeset
|
83 |
private NativeString(final CharSequence value) { |
bdb92c95f886
8019947: inherited property invalidation does not work with two globals in same context
sundar
parents:
18618
diff
changeset
|
84 |
this(value, Global.instance()); |
bdb92c95f886
8019947: inherited property invalidation does not work with two globals in same context
sundar
parents:
18618
diff
changeset
|
85 |
} |
bdb92c95f886
8019947: inherited property invalidation does not work with two globals in same context
sundar
parents:
18618
diff
changeset
|
86 |
|
bdb92c95f886
8019947: inherited property invalidation does not work with two globals in same context
sundar
parents:
18618
diff
changeset
|
87 |
NativeString(final CharSequence value, final Global global) { |
23375
a1110f2cbe75
8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents:
23372
diff
changeset
|
88 |
this(value, global.getStringPrototype(), $nasgenmap$); |
18851
bdb92c95f886
8019947: inherited property invalidation does not work with two globals in same context
sundar
parents:
18618
diff
changeset
|
89 |
} |
bdb92c95f886
8019947: inherited property invalidation does not work with two globals in same context
sundar
parents:
18618
diff
changeset
|
90 |
|
bdb92c95f886
8019947: inherited property invalidation does not work with two globals in same context
sundar
parents:
18618
diff
changeset
|
91 |
private NativeString(final CharSequence value, final ScriptObject proto, final PropertyMap map) { |
bdb92c95f886
8019947: inherited property invalidation does not work with two globals in same context
sundar
parents:
18618
diff
changeset
|
92 |
super(proto, map); |
29282 | 93 |
assert JSType.isString(value); |
16147 | 94 |
this.value = value; |
95 |
} |
|
96 |
||
97 |
@Override |
|
98 |
public String safeToString() { |
|
99 |
return "[String " + toString() + "]"; |
|
100 |
} |
|
101 |
||
102 |
@Override |
|
103 |
public String toString() { |
|
104 |
return getStringValue(); |
|
105 |
} |
|
106 |
||
107 |
@Override |
|
108 |
public boolean equals(final Object other) { |
|
109 |
if (other instanceof NativeString) { |
|
110 |
return getStringValue().equals(((NativeString) other).getStringValue()); |
|
111 |
} |
|
112 |
||
113 |
return false; |
|
114 |
} |
|
115 |
||
116 |
@Override |
|
117 |
public int hashCode() { |
|
118 |
return getStringValue().hashCode(); |
|
119 |
} |
|
120 |
||
121 |
private String getStringValue() { |
|
122 |
return value instanceof String ? (String) value : value.toString(); |
|
123 |
} |
|
124 |
||
125 |
private CharSequence getValue() { |
|
126 |
return value; |
|
127 |
} |
|
128 |
||
129 |
@Override |
|
130 |
public String getClassName() { |
|
131 |
return "String"; |
|
132 |
} |
|
133 |
||
134 |
@Override |
|
135 |
public Object getLength() { |
|
136 |
return value.length(); |
|
137 |
} |
|
138 |
||
16534
6993b3541b28
8010720: Linkage problem with java.lang.String.length()
sundar
parents:
16277
diff
changeset
|
139 |
// This is to support length as method call as well. |
6993b3541b28
8010720: Linkage problem with java.lang.String.length()
sundar
parents:
16277
diff
changeset
|
140 |
@Override |
6993b3541b28
8010720: Linkage problem with java.lang.String.length()
sundar
parents:
16277
diff
changeset
|
141 |
protected GuardedInvocation findGetMethod(final CallSiteDescriptor desc, final LinkRequest request, final String operator) { |
6993b3541b28
8010720: Linkage problem with java.lang.String.length()
sundar
parents:
16277
diff
changeset
|
142 |
final String name = desc.getNameToken(2); |
6993b3541b28
8010720: Linkage problem with java.lang.String.length()
sundar
parents:
16277
diff
changeset
|
143 |
|
6993b3541b28
8010720: Linkage problem with java.lang.String.length()
sundar
parents:
16277
diff
changeset
|
144 |
// if str.length(), then let the bean linker handle it |
6993b3541b28
8010720: Linkage problem with java.lang.String.length()
sundar
parents:
16277
diff
changeset
|
145 |
if ("length".equals(name) && "getMethod".equals(operator)) { |
6993b3541b28
8010720: Linkage problem with java.lang.String.length()
sundar
parents:
16277
diff
changeset
|
146 |
return null; |
6993b3541b28
8010720: Linkage problem with java.lang.String.length()
sundar
parents:
16277
diff
changeset
|
147 |
} |
6993b3541b28
8010720: Linkage problem with java.lang.String.length()
sundar
parents:
16277
diff
changeset
|
148 |
|
6993b3541b28
8010720: Linkage problem with java.lang.String.length()
sundar
parents:
16277
diff
changeset
|
149 |
return super.findGetMethod(desc, request, operator); |
6993b3541b28
8010720: Linkage problem with java.lang.String.length()
sundar
parents:
16277
diff
changeset
|
150 |
} |
6993b3541b28
8010720: Linkage problem with java.lang.String.length()
sundar
parents:
16277
diff
changeset
|
151 |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
152 |
// This is to provide array-like access to string characters without creating a NativeString wrapper. |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
153 |
@Override |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
154 |
protected GuardedInvocation findGetIndexMethod(final CallSiteDescriptor desc, final LinkRequest request) { |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
155 |
final Object self = request.getReceiver(); |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
156 |
final Class<?> returnType = desc.getMethodType().returnType(); |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
157 |
|
29282 | 158 |
if (returnType == Object.class && JSType.isString(self)) { |
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
159 |
try { |
24719 | 160 |
return new GuardedInvocation(MH.findStatic(MethodHandles.lookup(), NativeString.class, "get", desc.getMethodType()), NashornGuards.getInstanceOf2Guard(String.class, ConsString.class)); |
18851
bdb92c95f886
8019947: inherited property invalidation does not work with two globals in same context
sundar
parents:
18618
diff
changeset
|
161 |
} catch (final LookupException e) { |
24719 | 162 |
//empty. Shouldn't happen. Fall back to super |
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
163 |
} |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
164 |
} |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
165 |
return super.findGetIndexMethod(desc, request); |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
166 |
} |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
167 |
|
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
168 |
@SuppressWarnings("unused") |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
169 |
private static Object get(final Object self, final Object key) { |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
170 |
final CharSequence cs = JSType.toCharSequence(self); |
21867
4e5ee0aeb468
8028210: Missing conversions on array index expression
hannesw
parents:
21447
diff
changeset
|
171 |
final Object primitiveKey = JSType.toPrimitive(key, String.class); |
4e5ee0aeb468
8028210: Missing conversions on array index expression
hannesw
parents:
21447
diff
changeset
|
172 |
final int index = ArrayIndex.getArrayIndex(primitiveKey); |
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
173 |
if (index >= 0 && index < cs.length()) { |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
174 |
return String.valueOf(cs.charAt(index)); |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
175 |
} |
21867
4e5ee0aeb468
8028210: Missing conversions on array index expression
hannesw
parents:
21447
diff
changeset
|
176 |
return ((ScriptObject) Global.toObject(self)).get(primitiveKey); |
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
177 |
} |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
178 |
|
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
179 |
@SuppressWarnings("unused") |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
180 |
private static Object get(final Object self, final double key) { |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
181 |
if (isRepresentableAsInt(key)) { |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
182 |
return get(self, (int)key); |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
183 |
} |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
184 |
return ((ScriptObject) Global.toObject(self)).get(key); |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
185 |
} |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
186 |
|
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
187 |
@SuppressWarnings("unused") |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
188 |
private static Object get(final Object self, final long key) { |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
189 |
final CharSequence cs = JSType.toCharSequence(self); |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
190 |
if (key >= 0 && key < cs.length()) { |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
191 |
return String.valueOf(cs.charAt((int)key)); |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
192 |
} |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
193 |
return ((ScriptObject) Global.toObject(self)).get(key); |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
194 |
} |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
195 |
|
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
196 |
private static Object get(final Object self, final int key) { |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
197 |
final CharSequence cs = JSType.toCharSequence(self); |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
198 |
if (key >= 0 && key < cs.length()) { |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
199 |
return String.valueOf(cs.charAt(key)); |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
200 |
} |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
201 |
return ((ScriptObject) Global.toObject(self)).get(key); |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
202 |
} |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
203 |
|
16147 | 204 |
// String characters can be accessed with array-like indexing.. |
205 |
@Override |
|
206 |
public Object get(final Object key) { |
|
21867
4e5ee0aeb468
8028210: Missing conversions on array index expression
hannesw
parents:
21447
diff
changeset
|
207 |
final Object primitiveKey = JSType.toPrimitive(key, String.class); |
4e5ee0aeb468
8028210: Missing conversions on array index expression
hannesw
parents:
21447
diff
changeset
|
208 |
final int index = ArrayIndex.getArrayIndex(primitiveKey); |
16147 | 209 |
if (index >= 0 && index < value.length()) { |
210 |
return String.valueOf(value.charAt(index)); |
|
211 |
} |
|
21867
4e5ee0aeb468
8028210: Missing conversions on array index expression
hannesw
parents:
21447
diff
changeset
|
212 |
return super.get(primitiveKey); |
16147 | 213 |
} |
214 |
||
215 |
@Override |
|
216 |
public Object get(final double key) { |
|
217 |
if (isRepresentableAsInt(key)) { |
|
218 |
return get((int)key); |
|
219 |
} |
|
220 |
return super.get(key); |
|
221 |
} |
|
222 |
||
223 |
@Override |
|
224 |
public Object get(final long key) { |
|
225 |
if (key >= 0 && key < value.length()) { |
|
226 |
return String.valueOf(value.charAt((int)key)); |
|
227 |
} |
|
228 |
return super.get(key); |
|
229 |
} |
|
230 |
||
231 |
@Override |
|
232 |
public Object get(final int key) { |
|
233 |
if (key >= 0 && key < value.length()) { |
|
234 |
return String.valueOf(value.charAt(key)); |
|
235 |
} |
|
236 |
return super.get(key); |
|
237 |
} |
|
238 |
||
239 |
@Override |
|
24719 | 240 |
public int getInt(final Object key, final int programPoint) { |
241 |
return JSType.toInt32MaybeOptimistic(get(key), programPoint); |
|
16147 | 242 |
} |
243 |
||
244 |
@Override |
|
24719 | 245 |
public int getInt(final double key, final int programPoint) { |
246 |
return JSType.toInt32MaybeOptimistic(get(key), programPoint); |
|
16147 | 247 |
} |
248 |
||
249 |
@Override |
|
24719 | 250 |
public int getInt(final long key, final int programPoint) { |
251 |
return JSType.toInt32MaybeOptimistic(get(key), programPoint); |
|
16147 | 252 |
} |
253 |
||
254 |
@Override |
|
24719 | 255 |
public int getInt(final int key, final int programPoint) { |
256 |
return JSType.toInt32MaybeOptimistic(get(key), programPoint); |
|
16147 | 257 |
} |
258 |
||
259 |
@Override |
|
24719 | 260 |
public long getLong(final Object key, final int programPoint) { |
261 |
return JSType.toLongMaybeOptimistic(get(key), programPoint); |
|
16147 | 262 |
} |
263 |
||
264 |
@Override |
|
24719 | 265 |
public long getLong(final double key, final int programPoint) { |
266 |
return JSType.toLongMaybeOptimistic(get(key), programPoint); |
|
16147 | 267 |
} |
268 |
||
269 |
@Override |
|
24719 | 270 |
public long getLong(final long key, final int programPoint) { |
271 |
return JSType.toLongMaybeOptimistic(get(key), programPoint); |
|
16147 | 272 |
} |
273 |
||
274 |
@Override |
|
24719 | 275 |
public long getLong(final int key, final int programPoint) { |
276 |
return JSType.toLongMaybeOptimistic(get(key), programPoint); |
|
16147 | 277 |
} |
278 |
||
279 |
@Override |
|
24719 | 280 |
public double getDouble(final Object key, final int programPoint) { |
281 |
return JSType.toNumberMaybeOptimistic(get(key), programPoint); |
|
16147 | 282 |
} |
283 |
||
284 |
@Override |
|
24719 | 285 |
public double getDouble(final double key, final int programPoint) { |
286 |
return JSType.toNumberMaybeOptimistic(get(key), programPoint); |
|
16147 | 287 |
} |
288 |
||
289 |
@Override |
|
24719 | 290 |
public double getDouble(final long key, final int programPoint) { |
291 |
return JSType.toNumberMaybeOptimistic(get(key), programPoint); |
|
16147 | 292 |
} |
293 |
||
294 |
@Override |
|
24719 | 295 |
public double getDouble(final int key, final int programPoint) { |
296 |
return JSType.toNumberMaybeOptimistic(get(key), programPoint); |
|
16147 | 297 |
} |
298 |
||
299 |
@Override |
|
300 |
public boolean has(final Object key) { |
|
21867
4e5ee0aeb468
8028210: Missing conversions on array index expression
hannesw
parents:
21447
diff
changeset
|
301 |
final Object primitiveKey = JSType.toPrimitive(key, String.class); |
4e5ee0aeb468
8028210: Missing conversions on array index expression
hannesw
parents:
21447
diff
changeset
|
302 |
final int index = ArrayIndex.getArrayIndex(primitiveKey); |
24719 | 303 |
return isValidStringIndex(index) || super.has(primitiveKey); |
16147 | 304 |
} |
305 |
||
306 |
@Override |
|
307 |
public boolean has(final int key) { |
|
24719 | 308 |
return isValidStringIndex(key) || super.has(key); |
16147 | 309 |
} |
310 |
||
311 |
@Override |
|
312 |
public boolean has(final long key) { |
|
18318
5e4244619d79
8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
17781
diff
changeset
|
313 |
final int index = ArrayIndex.getArrayIndex(key); |
24719 | 314 |
return isValidStringIndex(index) || super.has(key); |
16147 | 315 |
} |
316 |
||
317 |
@Override |
|
318 |
public boolean has(final double key) { |
|
18318
5e4244619d79
8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
17781
diff
changeset
|
319 |
final int index = ArrayIndex.getArrayIndex(key); |
24719 | 320 |
return isValidStringIndex(index) || super.has(key); |
16147 | 321 |
} |
322 |
||
323 |
@Override |
|
324 |
public boolean hasOwnProperty(final Object key) { |
|
21867
4e5ee0aeb468
8028210: Missing conversions on array index expression
hannesw
parents:
21447
diff
changeset
|
325 |
final Object primitiveKey = JSType.toPrimitive(key, String.class); |
4e5ee0aeb468
8028210: Missing conversions on array index expression
hannesw
parents:
21447
diff
changeset
|
326 |
final int index = ArrayIndex.getArrayIndex(primitiveKey); |
24719 | 327 |
return isValidStringIndex(index) || super.hasOwnProperty(primitiveKey); |
16147 | 328 |
} |
329 |
||
330 |
@Override |
|
331 |
public boolean hasOwnProperty(final int key) { |
|
24719 | 332 |
return isValidStringIndex(key) || super.hasOwnProperty(key); |
16147 | 333 |
} |
334 |
||
335 |
@Override |
|
336 |
public boolean hasOwnProperty(final long key) { |
|
18318
5e4244619d79
8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
17781
diff
changeset
|
337 |
final int index = ArrayIndex.getArrayIndex(key); |
24719 | 338 |
return isValidStringIndex(index) || super.hasOwnProperty(key); |
16147 | 339 |
} |
340 |
||
341 |
@Override |
|
342 |
public boolean hasOwnProperty(final double key) { |
|
18318
5e4244619d79
8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
17781
diff
changeset
|
343 |
final int index = ArrayIndex.getArrayIndex(key); |
24719 | 344 |
return isValidStringIndex(index) || super.hasOwnProperty(key); |
16147 | 345 |
} |
346 |
||
347 |
@Override |
|
348 |
public boolean delete(final int key, final boolean strict) { |
|
349 |
return checkDeleteIndex(key, strict)? false : super.delete(key, strict); |
|
350 |
} |
|
351 |
||
352 |
@Override |
|
353 |
public boolean delete(final long key, final boolean strict) { |
|
18318
5e4244619d79
8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
17781
diff
changeset
|
354 |
final int index = ArrayIndex.getArrayIndex(key); |
16147 | 355 |
return checkDeleteIndex(index, strict)? false : super.delete(key, strict); |
356 |
} |
|
357 |
||
358 |
@Override |
|
359 |
public boolean delete(final double key, final boolean strict) { |
|
18318
5e4244619d79
8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
17781
diff
changeset
|
360 |
final int index = ArrayIndex.getArrayIndex(key); |
16147 | 361 |
return checkDeleteIndex(index, strict)? false : super.delete(key, strict); |
362 |
} |
|
363 |
||
364 |
@Override |
|
365 |
public boolean delete(final Object key, final boolean strict) { |
|
21867
4e5ee0aeb468
8028210: Missing conversions on array index expression
hannesw
parents:
21447
diff
changeset
|
366 |
final Object primitiveKey = JSType.toPrimitive(key, String.class); |
4e5ee0aeb468
8028210: Missing conversions on array index expression
hannesw
parents:
21447
diff
changeset
|
367 |
final int index = ArrayIndex.getArrayIndex(primitiveKey); |
4e5ee0aeb468
8028210: Missing conversions on array index expression
hannesw
parents:
21447
diff
changeset
|
368 |
return checkDeleteIndex(index, strict)? false : super.delete(primitiveKey, strict); |
16147 | 369 |
} |
370 |
||
371 |
private boolean checkDeleteIndex(final int index, final boolean strict) { |
|
24719 | 372 |
if (isValidStringIndex(index)) { |
16147 | 373 |
if (strict) { |
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16234
diff
changeset
|
374 |
throw typeError("cant.delete.property", Integer.toString(index), ScriptRuntime.safeToString(this)); |
16147 | 375 |
} |
376 |
return true; |
|
377 |
} |
|
378 |
||
379 |
return false; |
|
380 |
} |
|
381 |
||
382 |
@Override |
|
383 |
public Object getOwnPropertyDescriptor(final String key) { |
|
18318
5e4244619d79
8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
17781
diff
changeset
|
384 |
final int index = ArrayIndex.getArrayIndex(key); |
16147 | 385 |
if (index >= 0 && index < value.length()) { |
386 |
final Global global = Global.instance(); |
|
387 |
return global.newDataDescriptor(String.valueOf(value.charAt(index)), false, true, false); |
|
388 |
} |
|
389 |
||
390 |
return super.getOwnPropertyDescriptor(key); |
|
391 |
} |
|
392 |
||
393 |
/** |
|
394 |
* return a List of own keys associated with the object. |
|
395 |
* @param all True if to include non-enumerable keys. |
|
26058
17acdbe290b2
8054503: test/script/external/test262/test/suite/ch12/12.6/12.6.4/12.6.4-2.js fails with tip
sundar
parents:
24778
diff
changeset
|
396 |
* @param nonEnumerable set of non-enumerable properties seen already.Used |
17acdbe290b2
8054503: test/script/external/test262/test/suite/ch12/12.6/12.6.4/12.6.4-2.js fails with tip
sundar
parents:
24778
diff
changeset
|
397 |
* to filter out shadowed, but enumerable properties from proto children. |
16147 | 398 |
* @return Array of keys. |
399 |
*/ |
|
400 |
@Override |
|
26058
17acdbe290b2
8054503: test/script/external/test262/test/suite/ch12/12.6/12.6.4/12.6.4-2.js fails with tip
sundar
parents:
24778
diff
changeset
|
401 |
protected String[] getOwnKeys(final boolean all, final Set<String> nonEnumerable) { |
16147 | 402 |
final List<Object> keys = new ArrayList<>(); |
403 |
||
404 |
// add string index keys |
|
405 |
for (int i = 0; i < value.length(); i++) { |
|
406 |
keys.add(JSType.toString(i)); |
|
407 |
} |
|
408 |
||
409 |
// add super class properties |
|
26058
17acdbe290b2
8054503: test/script/external/test262/test/suite/ch12/12.6/12.6.4/12.6.4-2.js fails with tip
sundar
parents:
24778
diff
changeset
|
410 |
keys.addAll(Arrays.asList(super.getOwnKeys(all, nonEnumerable))); |
16147 | 411 |
return keys.toArray(new String[keys.size()]); |
412 |
} |
|
413 |
||
414 |
/** |
|
415 |
* ECMA 15.5.3 String.length |
|
416 |
* @param self self reference |
|
417 |
* @return value of length property for string |
|
418 |
*/ |
|
419 |
@Getter(attributes = Attribute.NOT_ENUMERABLE | Attribute.NOT_WRITABLE | Attribute.NOT_CONFIGURABLE) |
|
420 |
public static Object length(final Object self) { |
|
421 |
return getCharSequence(self).length(); |
|
422 |
} |
|
423 |
||
424 |
/** |
|
425 |
* ECMA 15.5.3.2 String.fromCharCode ( [ char0 [ , char1 [ , ... ] ] ] ) |
|
426 |
* @param self self reference |
|
427 |
* @param args array of arguments to be interpreted as char |
|
428 |
* @return string with arguments translated to charcodes |
|
429 |
*/ |
|
430 |
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1, where = Where.CONSTRUCTOR) |
|
23763
950d8bc0554f
8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents:
23375
diff
changeset
|
431 |
public static String fromCharCode(final Object self, final Object... args) { |
16147 | 432 |
final char[] buf = new char[args.length]; |
433 |
int index = 0; |
|
434 |
for (final Object arg : args) { |
|
435 |
buf[index++] = (char)JSType.toUint16(arg); |
|
436 |
} |
|
437 |
return new String(buf); |
|
438 |
} |
|
439 |
||
440 |
/** |
|
441 |
* ECMA 15.5.3.2 - specialization for one char |
|
442 |
* @param self self reference |
|
443 |
* @param value one argument to be interpreted as char |
|
444 |
* @return string with one charcode |
|
445 |
*/ |
|
446 |
@SpecializedFunction |
|
447 |
public static Object fromCharCode(final Object self, final Object value) { |
|
24719 | 448 |
if (value instanceof Integer) { |
449 |
return fromCharCode(self, (int)value); |
|
16147 | 450 |
} |
24720 | 451 |
return Character.toString((char)JSType.toUint16(value)); |
16147 | 452 |
} |
453 |
||
454 |
/** |
|
455 |
* ECMA 15.5.3.2 - specialization for one char of int type |
|
456 |
* @param self self reference |
|
457 |
* @param value one argument to be interpreted as char |
|
458 |
* @return string with one charcode |
|
459 |
*/ |
|
460 |
@SpecializedFunction |
|
23763
950d8bc0554f
8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents:
23375
diff
changeset
|
461 |
public static String fromCharCode(final Object self, final int value) { |
24720 | 462 |
return Character.toString((char)(value & 0xffff)); |
463 |
} |
|
464 |
||
465 |
/** |
|
466 |
* ECMA 15.5.3.2 - specialization for two chars of int type |
|
467 |
* @param self self reference |
|
468 |
* @param ch1 first char |
|
469 |
* @param ch2 second char |
|
470 |
* @return string with one charcode |
|
471 |
*/ |
|
472 |
@SpecializedFunction |
|
473 |
public static Object fromCharCode(final Object self, final int ch1, final int ch2) { |
|
474 |
return Character.toString((char)(ch1 & 0xffff)) + Character.toString((char)(ch2 & 0xffff)); |
|
16147 | 475 |
} |
476 |
||
477 |
/** |
|
24720 | 478 |
* ECMA 15.5.3.2 - specialization for three chars of int type |
16147 | 479 |
* @param self self reference |
24720 | 480 |
* @param ch1 first char |
481 |
* @param ch2 second char |
|
482 |
* @param ch3 third char |
|
16147 | 483 |
* @return string with one charcode |
484 |
*/ |
|
485 |
@SpecializedFunction |
|
24720 | 486 |
public static Object fromCharCode(final Object self, final int ch1, final int ch2, final int ch3) { |
487 |
return Character.toString((char)(ch1 & 0xffff)) + Character.toString((char)(ch2 & 0xffff)) + Character.toString((char)(ch3 & 0xffff)); |
|
488 |
} |
|
489 |
||
490 |
/** |
|
491 |
* ECMA 15.5.3.2 - specialization for four chars of int type |
|
492 |
* @param self self reference |
|
493 |
* @param ch1 first char |
|
494 |
* @param ch2 second char |
|
495 |
* @param ch3 third char |
|
496 |
* @param ch4 fourth char |
|
497 |
* @return string with one charcode |
|
498 |
*/ |
|
499 |
@SpecializedFunction |
|
24769 | 500 |
public static String fromCharCode(final Object self, final int ch1, final int ch2, final int ch3, final int ch4) { |
24720 | 501 |
return Character.toString((char)(ch1 & 0xffff)) + Character.toString((char)(ch2 & 0xffff)) + Character.toString((char)(ch3 & 0xffff)) + Character.toString((char)(ch4 & 0xffff)); |
16147 | 502 |
} |
503 |
||
504 |
/** |
|
505 |
* ECMA 15.5.3.2 - specialization for one char of double type |
|
506 |
* @param self self reference |
|
507 |
* @param value one argument to be interpreted as char |
|
508 |
* @return string with one charcode |
|
509 |
*/ |
|
510 |
@SpecializedFunction |
|
23763
950d8bc0554f
8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents:
23375
diff
changeset
|
511 |
public static String fromCharCode(final Object self, final double value) { |
24720 | 512 |
return Character.toString((char)JSType.toUint16(value)); |
16147 | 513 |
} |
514 |
||
515 |
/** |
|
516 |
* ECMA 15.5.4.2 String.prototype.toString ( ) |
|
517 |
* @param self self reference |
|
518 |
* @return self as string |
|
519 |
*/ |
|
520 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
23763
950d8bc0554f
8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents:
23375
diff
changeset
|
521 |
public static String toString(final Object self) { |
16147 | 522 |
return getString(self); |
523 |
} |
|
524 |
||
525 |
/** |
|
526 |
* ECMA 15.5.4.3 String.prototype.valueOf ( ) |
|
527 |
* @param self self reference |
|
528 |
* @return self as string |
|
529 |
*/ |
|
530 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
23763
950d8bc0554f
8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents:
23375
diff
changeset
|
531 |
public static String valueOf(final Object self) { |
16147 | 532 |
return getString(self); |
533 |
} |
|
534 |
||
535 |
/** |
|
536 |
* ECMA 15.5.4.4 String.prototype.charAt (pos) |
|
537 |
* @param self self reference |
|
538 |
* @param pos position in string |
|
539 |
* @return string representing the char at the given position |
|
540 |
*/ |
|
541 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
23763
950d8bc0554f
8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents:
23375
diff
changeset
|
542 |
public static String charAt(final Object self, final Object pos) { |
21447
5efe5ca7b352
8027024: String.prototype.charAt and charCodeAt do not evaluate 'self' and 'pos' arguments in right order
sundar
parents:
19633
diff
changeset
|
543 |
return charAtImpl(checkObjectToString(self), JSType.toInteger(pos)); |
16192 | 544 |
} |
545 |
||
546 |
/** |
|
547 |
* ECMA 15.5.4.4 String.prototype.charAt (pos) - specialized version for double position |
|
548 |
* @param self self reference |
|
549 |
* @param pos position in string |
|
550 |
* @return string representing the char at the given position |
|
551 |
*/ |
|
552 |
@SpecializedFunction |
|
553 |
public static String charAt(final Object self, final double pos) { |
|
554 |
return charAt(self, (int)pos); |
|
555 |
} |
|
556 |
||
557 |
/** |
|
558 |
* ECMA 15.5.4.4 String.prototype.charAt (pos) - specialized version for int position |
|
559 |
* @param self self reference |
|
560 |
* @param pos position in string |
|
561 |
* @return string representing the char at the given position |
|
562 |
*/ |
|
563 |
@SpecializedFunction |
|
564 |
public static String charAt(final Object self, final int pos) { |
|
21447
5efe5ca7b352
8027024: String.prototype.charAt and charCodeAt do not evaluate 'self' and 'pos' arguments in right order
sundar
parents:
19633
diff
changeset
|
565 |
return charAtImpl(checkObjectToString(self), pos); |
5efe5ca7b352
8027024: String.prototype.charAt and charCodeAt do not evaluate 'self' and 'pos' arguments in right order
sundar
parents:
19633
diff
changeset
|
566 |
} |
5efe5ca7b352
8027024: String.prototype.charAt and charCodeAt do not evaluate 'self' and 'pos' arguments in right order
sundar
parents:
19633
diff
changeset
|
567 |
|
5efe5ca7b352
8027024: String.prototype.charAt and charCodeAt do not evaluate 'self' and 'pos' arguments in right order
sundar
parents:
19633
diff
changeset
|
568 |
private static String charAtImpl(final String str, final int pos) { |
24720 | 569 |
return pos < 0 || pos >= str.length() ? "" : String.valueOf(str.charAt(pos)); |
16147 | 570 |
} |
571 |
||
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
572 |
private static int getValidChar(final Object self, final int pos) { |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
573 |
try { |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
574 |
return ((CharSequence)self).charAt(pos); |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
575 |
} catch (final IndexOutOfBoundsException e) { |
27362
142fc25a0baf
8062381: Wrong index was used for linking charCodeAt specializations
lagergren
parents:
26768
diff
changeset
|
576 |
throw new ClassCastException(); //invalid char, out of bounds, force relink |
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
577 |
} |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
578 |
} |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
579 |
|
16147 | 580 |
/** |
581 |
* ECMA 15.5.4.5 String.prototype.charCodeAt (pos) |
|
582 |
* @param self self reference |
|
583 |
* @param pos position in string |
|
584 |
* @return number representing charcode at position |
|
585 |
*/ |
|
586 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
23763
950d8bc0554f
8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents:
23375
diff
changeset
|
587 |
public static double charCodeAt(final Object self, final Object pos) { |
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
588 |
final String str = checkObjectToString(self); |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
589 |
final int idx = JSType.toInteger(pos); |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
590 |
return idx < 0 || idx >= str.length() ? Double.NaN : str.charAt(idx); |
16192 | 591 |
} |
16147 | 592 |
|
16192 | 593 |
/** |
594 |
* ECMA 15.5.4.5 String.prototype.charCodeAt (pos) - specialized version for double position |
|
595 |
* @param self self reference |
|
596 |
* @param pos position in string |
|
597 |
* @return number representing charcode at position |
|
598 |
*/ |
|
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
599 |
@SpecializedFunction(linkLogic=CharCodeAtLinkLogic.class) |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
600 |
public static int charCodeAt(final Object self, final double pos) { |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
601 |
return charCodeAt(self, (int)pos); //toInt pos is ok |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
602 |
} |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
603 |
|
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
604 |
/** |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
605 |
* ECMA 15.5.4.5 String.prototype.charCodeAt (pos) - specialized version for long position |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
606 |
* @param self self reference |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
607 |
* @param pos position in string |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
608 |
* @return number representing charcode at position |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
609 |
*/ |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
610 |
@SpecializedFunction(linkLogic=CharCodeAtLinkLogic.class) |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
611 |
public static int charCodeAt(final Object self, final long pos) { |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
612 |
return charCodeAt(self, (int)pos); |
16192 | 613 |
} |
614 |
||
615 |
/** |
|
616 |
* ECMA 15.5.4.5 String.prototype.charCodeAt (pos) - specialized version for int position |
|
617 |
* @param self self reference |
|
618 |
* @param pos position in string |
|
619 |
* @return number representing charcode at position |
|
620 |
*/ |
|
21447
5efe5ca7b352
8027024: String.prototype.charAt and charCodeAt do not evaluate 'self' and 'pos' arguments in right order
sundar
parents:
19633
diff
changeset
|
621 |
|
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
622 |
@SpecializedFunction(linkLogic=CharCodeAtLinkLogic.class) |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
623 |
public static int charCodeAt(final Object self, final int pos) { |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
624 |
return getValidChar(self, pos); |
16147 | 625 |
} |
626 |
||
627 |
/** |
|
628 |
* ECMA 15.5.4.6 String.prototype.concat ( [ string1 [ , string2 [ , ... ] ] ] ) |
|
629 |
* @param self self reference |
|
630 |
* @param args list of string to concatenate |
|
631 |
* @return concatenated string |
|
632 |
*/ |
|
633 |
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1) |
|
634 |
public static Object concat(final Object self, final Object... args) { |
|
16192 | 635 |
CharSequence cs = checkObjectToString(self); |
16147 | 636 |
if (args != null) { |
637 |
for (final Object obj : args) { |
|
16192 | 638 |
cs = new ConsString(cs, JSType.toCharSequence(obj)); |
16147 | 639 |
} |
640 |
} |
|
16192 | 641 |
return cs; |
16147 | 642 |
} |
643 |
||
644 |
/** |
|
645 |
* ECMA 15.5.4.7 String.prototype.indexOf (searchString, position) |
|
646 |
* @param self self reference |
|
647 |
* @param search string to search for |
|
648 |
* @param pos position to start search |
|
649 |
* @return position of first match or -1 |
|
650 |
*/ |
|
651 |
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1) |
|
23763
950d8bc0554f
8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents:
23375
diff
changeset
|
652 |
public static int indexOf(final Object self, final Object search, final Object pos) { |
16192 | 653 |
final String str = checkObjectToString(self); |
654 |
return str.indexOf(JSType.toString(search), JSType.toInteger(pos)); |
|
655 |
} |
|
656 |
||
657 |
/** |
|
658 |
* ECMA 15.5.4.7 String.prototype.indexOf (searchString, position) specialized for no position parameter |
|
659 |
* @param self self reference |
|
660 |
* @param search string to search for |
|
661 |
* @return position of first match or -1 |
|
662 |
*/ |
|
663 |
@SpecializedFunction |
|
664 |
public static int indexOf(final Object self, final Object search) { |
|
665 |
return indexOf(self, search, 0); |
|
666 |
} |
|
667 |
||
668 |
/** |
|
669 |
* ECMA 15.5.4.7 String.prototype.indexOf (searchString, position) specialized for double position parameter |
|
670 |
* @param self self reference |
|
671 |
* @param search string to search for |
|
672 |
* @param pos position to start search |
|
673 |
* @return position of first match or -1 |
|
674 |
*/ |
|
675 |
@SpecializedFunction |
|
676 |
public static int indexOf(final Object self, final Object search, final double pos) { |
|
677 |
return indexOf(self, search, (int) pos); |
|
678 |
} |
|
679 |
||
680 |
/** |
|
681 |
* ECMA 15.5.4.7 String.prototype.indexOf (searchString, position) specialized for int position parameter |
|
682 |
* @param self self reference |
|
683 |
* @param search string to search for |
|
684 |
* @param pos position to start search |
|
685 |
* @return position of first match or -1 |
|
686 |
*/ |
|
687 |
@SpecializedFunction |
|
688 |
public static int indexOf(final Object self, final Object search, final int pos) { |
|
689 |
return checkObjectToString(self).indexOf(JSType.toString(search), pos); |
|
16147 | 690 |
} |
691 |
||
692 |
/** |
|
693 |
* ECMA 15.5.4.8 String.prototype.lastIndexOf (searchString, position) |
|
694 |
* @param self self reference |
|
695 |
* @param search string to search for |
|
696 |
* @param pos position to start search |
|
697 |
* @return last position of match or -1 |
|
698 |
*/ |
|
699 |
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1) |
|
23763
950d8bc0554f
8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents:
23375
diff
changeset
|
700 |
public static int lastIndexOf(final Object self, final Object search, final Object pos) { |
16147 | 701 |
|
16192 | 702 |
final String str = checkObjectToString(self); |
16147 | 703 |
final String searchStr = JSType.toString(search); |
17781
1d40bf1412e0
8015349: "abc".lastIndexOf("a",-1) should evaluate to 0 and not -1
sundar
parents:
17776
diff
changeset
|
704 |
final int length = str.length(); |
16147 | 705 |
|
17781
1d40bf1412e0
8015349: "abc".lastIndexOf("a",-1) should evaluate to 0 and not -1
sundar
parents:
17776
diff
changeset
|
706 |
int end; |
16147 | 707 |
|
708 |
if (pos == UNDEFINED) { |
|
17781
1d40bf1412e0
8015349: "abc".lastIndexOf("a",-1) should evaluate to 0 and not -1
sundar
parents:
17776
diff
changeset
|
709 |
end = length; |
16147 | 710 |
} else { |
711 |
final double numPos = JSType.toNumber(pos); |
|
17781
1d40bf1412e0
8015349: "abc".lastIndexOf("a",-1) should evaluate to 0 and not -1
sundar
parents:
17776
diff
changeset
|
712 |
end = Double.isNaN(numPos) ? length : (int)numPos; |
1d40bf1412e0
8015349: "abc".lastIndexOf("a",-1) should evaluate to 0 and not -1
sundar
parents:
17776
diff
changeset
|
713 |
if (end < 0) { |
1d40bf1412e0
8015349: "abc".lastIndexOf("a",-1) should evaluate to 0 and not -1
sundar
parents:
17776
diff
changeset
|
714 |
end = 0; |
1d40bf1412e0
8015349: "abc".lastIndexOf("a",-1) should evaluate to 0 and not -1
sundar
parents:
17776
diff
changeset
|
715 |
} else if (end > length) { |
1d40bf1412e0
8015349: "abc".lastIndexOf("a",-1) should evaluate to 0 and not -1
sundar
parents:
17776
diff
changeset
|
716 |
end = length; |
1d40bf1412e0
8015349: "abc".lastIndexOf("a",-1) should evaluate to 0 and not -1
sundar
parents:
17776
diff
changeset
|
717 |
} |
16147 | 718 |
} |
719 |
||
17781
1d40bf1412e0
8015349: "abc".lastIndexOf("a",-1) should evaluate to 0 and not -1
sundar
parents:
17776
diff
changeset
|
720 |
|
1d40bf1412e0
8015349: "abc".lastIndexOf("a",-1) should evaluate to 0 and not -1
sundar
parents:
17776
diff
changeset
|
721 |
return str.lastIndexOf(searchStr, end); |
16147 | 722 |
} |
723 |
||
724 |
/** |
|
725 |
* ECMA 15.5.4.9 String.prototype.localeCompare (that) |
|
726 |
* @param self self reference |
|
727 |
* @param that comparison object |
|
728 |
* @return result of locale sensitive comparison operation between {@code self} and {@code that} |
|
729 |
*/ |
|
730 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
23763
950d8bc0554f
8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents:
23375
diff
changeset
|
731 |
public static double localeCompare(final Object self, final Object that) { |
16147 | 732 |
|
16192 | 733 |
final String str = checkObjectToString(self); |
16262
75513555e603
8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents:
16258
diff
changeset
|
734 |
final Collator collator = Collator.getInstance(Global.getEnv()._locale); |
16147 | 735 |
|
736 |
collator.setStrength(Collator.IDENTICAL); |
|
737 |
collator.setDecomposition(Collator.CANONICAL_DECOMPOSITION); |
|
738 |
||
24769 | 739 |
return collator.compare(str, JSType.toString(that)); |
16147 | 740 |
} |
741 |
||
742 |
/** |
|
743 |
* ECMA 15.5.4.10 String.prototype.match (regexp) |
|
744 |
* @param self self reference |
|
745 |
* @param regexp regexp expression |
|
746 |
* @return array of regexp matches |
|
747 |
*/ |
|
748 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
23763
950d8bc0554f
8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents:
23375
diff
changeset
|
749 |
public static ScriptObject match(final Object self, final Object regexp) { |
16147 | 750 |
|
16192 | 751 |
final String str = checkObjectToString(self); |
16147 | 752 |
|
753 |
NativeRegExp nativeRegExp; |
|
754 |
if (regexp == UNDEFINED) { |
|
755 |
nativeRegExp = new NativeRegExp(""); |
|
756 |
} else { |
|
757 |
nativeRegExp = Global.toRegExp(regexp); |
|
758 |
} |
|
759 |
||
760 |
if (!nativeRegExp.getGlobal()) { |
|
761 |
return nativeRegExp.exec(str); |
|
762 |
} |
|
763 |
||
764 |
nativeRegExp.setLastIndex(0); |
|
765 |
||
766 |
int previousLastIndex = 0; |
|
767 |
final List<Object> matches = new ArrayList<>(); |
|
768 |
||
769 |
Object result; |
|
770 |
while ((result = nativeRegExp.exec(str)) != null) { |
|
771 |
final int thisIndex = nativeRegExp.getLastIndex(); |
|
772 |
if (thisIndex == previousLastIndex) { |
|
773 |
nativeRegExp.setLastIndex(thisIndex + 1); |
|
774 |
previousLastIndex = thisIndex + 1; |
|
775 |
} else { |
|
776 |
previousLastIndex = thisIndex; |
|
777 |
} |
|
778 |
matches.add(((ScriptObject)result).get(0)); |
|
779 |
} |
|
780 |
||
781 |
if (matches.isEmpty()) { |
|
782 |
return null; |
|
783 |
} |
|
784 |
||
785 |
return new NativeArray(matches.toArray()); |
|
786 |
} |
|
787 |
||
788 |
/** |
|
789 |
* ECMA 15.5.4.11 String.prototype.replace (searchValue, replaceValue) |
|
790 |
* @param self self reference |
|
791 |
* @param string item to replace |
|
792 |
* @param replacement item to replace it with |
|
793 |
* @return string after replacement |
|
24719 | 794 |
* @throws Throwable if replacement fails |
16147 | 795 |
*/ |
796 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
24769 | 797 |
public static String replace(final Object self, final Object string, final Object replacement) throws Throwable { |
16147 | 798 |
|
16192 | 799 |
final String str = checkObjectToString(self); |
16147 | 800 |
|
801 |
final NativeRegExp nativeRegExp; |
|
802 |
if (string instanceof NativeRegExp) { |
|
803 |
nativeRegExp = (NativeRegExp) string; |
|
804 |
} else { |
|
16258 | 805 |
nativeRegExp = NativeRegExp.flatRegExp(JSType.toString(string)); |
16147 | 806 |
} |
807 |
||
808 |
if (replacement instanceof ScriptFunction) { |
|
809 |
return nativeRegExp.replace(str, "", (ScriptFunction)replacement); |
|
810 |
} |
|
811 |
||
812 |
return nativeRegExp.replace(str, JSType.toString(replacement), null); |
|
813 |
} |
|
814 |
||
815 |
/** |
|
816 |
* ECMA 15.5.4.12 String.prototype.search (regexp) |
|
817 |
* |
|
818 |
* @param self self reference |
|
819 |
* @param string string to search for |
|
820 |
* @return offset where match occurred |
|
821 |
*/ |
|
822 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
23763
950d8bc0554f
8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents:
23375
diff
changeset
|
823 |
public static int search(final Object self, final Object string) { |
16147 | 824 |
|
16192 | 825 |
final String str = checkObjectToString(self); |
16147 | 826 |
final NativeRegExp nativeRegExp = Global.toRegExp(string == UNDEFINED ? "" : string); |
827 |
||
828 |
return nativeRegExp.search(str); |
|
829 |
} |
|
830 |
||
831 |
/** |
|
832 |
* ECMA 15.5.4.13 String.prototype.slice (start, end) |
|
833 |
* |
|
834 |
* @param self self reference |
|
835 |
* @param start start position for slice |
|
836 |
* @param end end position for slice |
|
837 |
* @return sliced out substring |
|
838 |
*/ |
|
839 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
23763
950d8bc0554f
8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents:
23375
diff
changeset
|
840 |
public static String slice(final Object self, final Object start, final Object end) { |
16192 | 841 |
|
842 |
final String str = checkObjectToString(self); |
|
843 |
if (end == UNDEFINED) { |
|
844 |
return slice(str, JSType.toInteger(start)); |
|
845 |
} |
|
846 |
return slice(str, JSType.toInteger(start), JSType.toInteger(end)); |
|
847 |
} |
|
16147 | 848 |
|
16192 | 849 |
/** |
850 |
* ECMA 15.5.4.13 String.prototype.slice (start, end) specialized for single int parameter |
|
851 |
* |
|
852 |
* @param self self reference |
|
853 |
* @param start start position for slice |
|
854 |
* @return sliced out substring |
|
855 |
*/ |
|
856 |
@SpecializedFunction |
|
23763
950d8bc0554f
8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents:
23375
diff
changeset
|
857 |
public static String slice(final Object self, final int start) { |
16192 | 858 |
final String str = checkObjectToString(self); |
24720 | 859 |
final int from = start < 0 ? Math.max(str.length() + start, 0) : Math.min(start, str.length()); |
16147 | 860 |
|
16192 | 861 |
return str.substring(from); |
862 |
} |
|
16147 | 863 |
|
16192 | 864 |
/** |
865 |
* ECMA 15.5.4.13 String.prototype.slice (start, end) specialized for single double parameter |
|
866 |
* |
|
867 |
* @param self self reference |
|
868 |
* @param start start position for slice |
|
869 |
* @return sliced out substring |
|
870 |
*/ |
|
871 |
@SpecializedFunction |
|
23763
950d8bc0554f
8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents:
23375
diff
changeset
|
872 |
public static String slice(final Object self, final double start) { |
16192 | 873 |
return slice(self, (int)start); |
874 |
} |
|
16147 | 875 |
|
16192 | 876 |
/** |
877 |
* ECMA 15.5.4.13 String.prototype.slice (start, end) specialized for two int parameters |
|
878 |
* |
|
879 |
* @param self self reference |
|
880 |
* @param start start position for slice |
|
16201
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
881 |
* @param end end position for slice |
16192 | 882 |
* @return sliced out substring |
883 |
*/ |
|
884 |
@SpecializedFunction |
|
23763
950d8bc0554f
8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents:
23375
diff
changeset
|
885 |
public static String slice(final Object self, final int start, final int end) { |
16192 | 886 |
|
887 |
final String str = checkObjectToString(self); |
|
888 |
final int len = str.length(); |
|
16147 | 889 |
|
24720 | 890 |
final int from = start < 0 ? Math.max(len + start, 0) : Math.min(start, len); |
891 |
final int to = end < 0 ? Math.max(len + end, 0) : Math.min(end, len); |
|
16192 | 892 |
|
893 |
return str.substring(Math.min(from, to), to); |
|
894 |
} |
|
895 |
||
896 |
/** |
|
897 |
* ECMA 15.5.4.13 String.prototype.slice (start, end) specialized for two double parameters |
|
898 |
* |
|
899 |
* @param self self reference |
|
900 |
* @param start start position for slice |
|
16201
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
901 |
* @param end end position for slice |
16192 | 902 |
* @return sliced out substring |
903 |
*/ |
|
904 |
@SpecializedFunction |
|
23763
950d8bc0554f
8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents:
23375
diff
changeset
|
905 |
public static String slice(final Object self, final double start, final double end) { |
16192 | 906 |
return slice(self, (int)start, (int)end); |
16147 | 907 |
} |
908 |
||
909 |
/** |
|
910 |
* ECMA 15.5.4.14 String.prototype.split (separator, limit) |
|
911 |
* |
|
912 |
* @param self self reference |
|
913 |
* @param separator separator for split |
|
914 |
* @param limit limit for splits |
|
915 |
* @return array object in which splits have been placed |
|
916 |
*/ |
|
917 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
23763
950d8bc0554f
8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents:
23375
diff
changeset
|
918 |
public static ScriptObject split(final Object self, final Object separator, final Object limit) { |
16192 | 919 |
final String str = checkObjectToString(self); |
24720 | 920 |
final long lim = limit == UNDEFINED ? JSType.MAX_UINT : JSType.toUint32(limit); |
16147 | 921 |
|
922 |
if (separator == UNDEFINED) { |
|
17249
a2014831ae7a
8013325: function named 'arguments' should set DEFINES_ARGUMENTS flag in its parent, not itself
attila
parents:
17245
diff
changeset
|
923 |
return lim == 0 ? new NativeArray() : new NativeArray(new Object[]{str}); |
16147 | 924 |
} |
925 |
||
926 |
if (separator instanceof NativeRegExp) { |
|
927 |
return ((NativeRegExp) separator).split(str, lim); |
|
928 |
} |
|
929 |
||
16192 | 930 |
// when separator is a string, it is treated as a literal search string to be used for splitting. |
931 |
return splitString(str, JSType.toString(separator), lim); |
|
932 |
} |
|
933 |
||
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
24769
diff
changeset
|
934 |
private static ScriptObject splitString(final String str, final String separator, final long limit) { |
16201
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
935 |
if (separator.isEmpty()) { |
17245
c49b17cd464b
8013131: Various compatibility issues in String.prototype.split()
hannesw
parents:
17233
diff
changeset
|
936 |
final int length = (int) Math.min(str.length(), limit); |
c49b17cd464b
8013131: Various compatibility issues in String.prototype.split()
hannesw
parents:
17233
diff
changeset
|
937 |
final Object[] array = new Object[length]; |
c49b17cd464b
8013131: Various compatibility issues in String.prototype.split()
hannesw
parents:
17233
diff
changeset
|
938 |
for (int i = 0; i < length; i++) { |
16192 | 939 |
array[i] = String.valueOf(str.charAt(i)); |
940 |
} |
|
941 |
return new NativeArray(array); |
|
942 |
} |
|
943 |
||
944 |
final List<String> elements = new LinkedList<>(); |
|
945 |
final int strLength = str.length(); |
|
946 |
final int sepLength = separator.length(); |
|
947 |
int pos = 0; |
|
16201
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
948 |
int n = 0; |
16192 | 949 |
|
16201
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
950 |
while (pos < strLength && n < limit) { |
24720 | 951 |
final int found = str.indexOf(separator, pos); |
16192 | 952 |
if (found == -1) { |
953 |
break; |
|
954 |
} |
|
955 |
elements.add(str.substring(pos, found)); |
|
16201
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
956 |
n++; |
16192 | 957 |
pos = found + sepLength; |
958 |
} |
|
16201
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
959 |
if (pos <= strLength && n < limit) { |
16192 | 960 |
elements.add(str.substring(pos)); |
961 |
} |
|
962 |
||
963 |
return new NativeArray(elements.toArray()); |
|
16147 | 964 |
} |
965 |
||
966 |
/** |
|
967 |
* ECMA B.2.3 String.prototype.substr (start, length) |
|
968 |
* |
|
969 |
* @param self self reference |
|
970 |
* @param start start position |
|
971 |
* @param length length of section |
|
972 |
* @return substring given start and length of section |
|
973 |
*/ |
|
974 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
23763
950d8bc0554f
8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents:
23375
diff
changeset
|
975 |
public static String substr(final Object self, final Object start, final Object length) { |
16147 | 976 |
final String str = JSType.toString(self); |
977 |
final int strLength = str.length(); |
|
978 |
||
979 |
int intStart = JSType.toInteger(start); |
|
980 |
if (intStart < 0) { |
|
981 |
intStart = Math.max(intStart + strLength, 0); |
|
982 |
} |
|
983 |
||
24720 | 984 |
final int intLen = Math.min(Math.max(length == UNDEFINED ? Integer.MAX_VALUE : JSType.toInteger(length), 0), strLength - intStart); |
16147 | 985 |
|
986 |
return intLen <= 0 ? "" : str.substring(intStart, intStart + intLen); |
|
987 |
} |
|
988 |
||
989 |
/** |
|
990 |
* ECMA 15.5.4.15 String.prototype.substring (start, end) |
|
991 |
* |
|
992 |
* @param self self reference |
|
993 |
* @param start start position of substring |
|
994 |
* @param end end position of substring |
|
995 |
* @return substring given start and end indexes |
|
996 |
*/ |
|
997 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
23763
950d8bc0554f
8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents:
23375
diff
changeset
|
998 |
public static String substring(final Object self, final Object start, final Object end) { |
16192 | 999 |
|
1000 |
final String str = checkObjectToString(self); |
|
1001 |
if (end == UNDEFINED) { |
|
1002 |
return substring(str, JSType.toInteger(start)); |
|
1003 |
} |
|
1004 |
return substring(str, JSType.toInteger(start), JSType.toInteger(end)); |
|
1005 |
} |
|
16147 | 1006 |
|
16192 | 1007 |
/** |
1008 |
* ECMA 15.5.4.15 String.prototype.substring (start, end) specialized for int start parameter |
|
1009 |
* |
|
1010 |
* @param self self reference |
|
1011 |
* @param start start position of substring |
|
1012 |
* @return substring given start and end indexes |
|
1013 |
*/ |
|
1014 |
@SpecializedFunction |
|
1015 |
public static String substring(final Object self, final int start) { |
|
1016 |
final String str = checkObjectToString(self); |
|
1017 |
if (start < 0) { |
|
1018 |
return str; |
|
1019 |
} else if (start >= str.length()) { |
|
1020 |
return ""; |
|
1021 |
} else { |
|
1022 |
return str.substring(start); |
|
1023 |
} |
|
1024 |
} |
|
16147 | 1025 |
|
16192 | 1026 |
/** |
1027 |
* ECMA 15.5.4.15 String.prototype.substring (start, end) specialized for double start parameter |
|
1028 |
* |
|
1029 |
* @param self self reference |
|
1030 |
* @param start start position of substring |
|
1031 |
* @return substring given start and end indexes |
|
1032 |
*/ |
|
1033 |
@SpecializedFunction |
|
1034 |
public static String substring(final Object self, final double start) { |
|
1035 |
return substring(self, (int)start); |
|
1036 |
} |
|
16147 | 1037 |
|
16192 | 1038 |
/** |
1039 |
* ECMA 15.5.4.15 String.prototype.substring (start, end) specialized for int start and end parameters |
|
1040 |
* |
|
1041 |
* @param self self reference |
|
1042 |
* @param start start position of substring |
|
1043 |
* @param end end position of substring |
|
1044 |
* @return substring given start and end indexes |
|
1045 |
*/ |
|
1046 |
@SpecializedFunction |
|
1047 |
public static String substring(final Object self, final int start, final int end) { |
|
1048 |
final String str = checkObjectToString(self); |
|
1049 |
final int len = str.length(); |
|
24720 | 1050 |
final int validStart = start < 0 ? 0 : start > len ? len : start; |
1051 |
final int validEnd = end < 0 ? 0 : end > len ? len : end; |
|
16192 | 1052 |
|
1053 |
if (validStart < validEnd) { |
|
1054 |
return str.substring(validStart, validEnd); |
|
16147 | 1055 |
} |
16201
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1056 |
return str.substring(validEnd, validStart); |
16192 | 1057 |
} |
1058 |
||
1059 |
/** |
|
1060 |
* ECMA 15.5.4.15 String.prototype.substring (start, end) specialized for double start and end parameters |
|
1061 |
* |
|
1062 |
* @param self self reference |
|
1063 |
* @param start start position of substring |
|
1064 |
* @param end end position of substring |
|
1065 |
* @return substring given start and end indexes |
|
1066 |
*/ |
|
1067 |
@SpecializedFunction |
|
1068 |
public static String substring(final Object self, final double start, final double end) { |
|
1069 |
return substring(self, (int)start, (int)end); |
|
16147 | 1070 |
} |
1071 |
||
1072 |
/** |
|
1073 |
* ECMA 15.5.4.16 String.prototype.toLowerCase ( ) |
|
1074 |
* @param self self reference |
|
1075 |
* @return string to lower case |
|
1076 |
*/ |
|
1077 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
23763
950d8bc0554f
8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents:
23375
diff
changeset
|
1078 |
public static String toLowerCase(final Object self) { |
17776
59fb7b69846d
8015352: "i".toUpperCase() => currently returns "İ", but should be "I" (with Turkish locale)
sundar
parents:
17249
diff
changeset
|
1079 |
return checkObjectToString(self).toLowerCase(Locale.ROOT); |
16147 | 1080 |
} |
1081 |
||
1082 |
/** |
|
1083 |
* ECMA 15.5.4.17 String.prototype.toLocaleLowerCase ( ) |
|
1084 |
* @param self self reference |
|
1085 |
* @return string to locale sensitive lower case |
|
1086 |
*/ |
|
1087 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
23763
950d8bc0554f
8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents:
23375
diff
changeset
|
1088 |
public static String toLocaleLowerCase(final Object self) { |
16262
75513555e603
8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents:
16258
diff
changeset
|
1089 |
return checkObjectToString(self).toLowerCase(Global.getEnv()._locale); |
16147 | 1090 |
} |
1091 |
||
1092 |
/** |
|
1093 |
* ECMA 15.5.4.18 String.prototype.toUpperCase ( ) |
|
1094 |
* @param self self reference |
|
1095 |
* @return string to upper case |
|
1096 |
*/ |
|
1097 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
23763
950d8bc0554f
8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents:
23375
diff
changeset
|
1098 |
public static String toUpperCase(final Object self) { |
17776
59fb7b69846d
8015352: "i".toUpperCase() => currently returns "İ", but should be "I" (with Turkish locale)
sundar
parents:
17249
diff
changeset
|
1099 |
return checkObjectToString(self).toUpperCase(Locale.ROOT); |
16147 | 1100 |
} |
1101 |
||
1102 |
/** |
|
1103 |
* ECMA 15.5.4.19 String.prototype.toLocaleUpperCase ( ) |
|
1104 |
* @param self self reference |
|
1105 |
* @return string to locale sensitive upper case |
|
1106 |
*/ |
|
1107 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
23763
950d8bc0554f
8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents:
23375
diff
changeset
|
1108 |
public static String toLocaleUpperCase(final Object self) { |
16262
75513555e603
8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents:
16258
diff
changeset
|
1109 |
return checkObjectToString(self).toUpperCase(Global.getEnv()._locale); |
16147 | 1110 |
} |
1111 |
||
1112 |
/** |
|
1113 |
* ECMA 15.5.4.20 String.prototype.trim ( ) |
|
1114 |
* @param self self reference |
|
1115 |
* @return string trimmed from whitespace |
|
1116 |
*/ |
|
1117 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
23763
950d8bc0554f
8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents:
23375
diff
changeset
|
1118 |
public static String trim(final Object self) { |
16192 | 1119 |
final String str = checkObjectToString(self); |
16147 | 1120 |
int start = 0; |
19633
e1158153db51
8019987: String trimRight and trimLeft could be defined
jlaskey
parents:
18851
diff
changeset
|
1121 |
int end = str.length() - 1; |
16147 | 1122 |
|
16226
0e4f37e6cc40
8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents:
16203
diff
changeset
|
1123 |
while (start <= end && ScriptRuntime.isJSWhitespace(str.charAt(start))) { |
16147 | 1124 |
start++; |
1125 |
} |
|
16226
0e4f37e6cc40
8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents:
16203
diff
changeset
|
1126 |
while (end > start && ScriptRuntime.isJSWhitespace(str.charAt(end))) { |
16147 | 1127 |
end--; |
1128 |
} |
|
1129 |
||
19633
e1158153db51
8019987: String trimRight and trimLeft could be defined
jlaskey
parents:
18851
diff
changeset
|
1130 |
return str.substring(start, end + 1); |
e1158153db51
8019987: String trimRight and trimLeft could be defined
jlaskey
parents:
18851
diff
changeset
|
1131 |
} |
e1158153db51
8019987: String trimRight and trimLeft could be defined
jlaskey
parents:
18851
diff
changeset
|
1132 |
|
e1158153db51
8019987: String trimRight and trimLeft could be defined
jlaskey
parents:
18851
diff
changeset
|
1133 |
/** |
e1158153db51
8019987: String trimRight and trimLeft could be defined
jlaskey
parents:
18851
diff
changeset
|
1134 |
* Nashorn extension: String.prototype.trimLeft ( ) |
e1158153db51
8019987: String trimRight and trimLeft could be defined
jlaskey
parents:
18851
diff
changeset
|
1135 |
* @param self self reference |
e1158153db51
8019987: String trimRight and trimLeft could be defined
jlaskey
parents:
18851
diff
changeset
|
1136 |
* @return string trimmed left from whitespace |
e1158153db51
8019987: String trimRight and trimLeft could be defined
jlaskey
parents:
18851
diff
changeset
|
1137 |
*/ |
e1158153db51
8019987: String trimRight and trimLeft could be defined
jlaskey
parents:
18851
diff
changeset
|
1138 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
23763
950d8bc0554f
8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents:
23375
diff
changeset
|
1139 |
public static String trimLeft(final Object self) { |
19633
e1158153db51
8019987: String trimRight and trimLeft could be defined
jlaskey
parents:
18851
diff
changeset
|
1140 |
|
e1158153db51
8019987: String trimRight and trimLeft could be defined
jlaskey
parents:
18851
diff
changeset
|
1141 |
final String str = checkObjectToString(self); |
e1158153db51
8019987: String trimRight and trimLeft could be defined
jlaskey
parents:
18851
diff
changeset
|
1142 |
int start = 0; |
24720 | 1143 |
final int end = str.length() - 1; |
19633
e1158153db51
8019987: String trimRight and trimLeft could be defined
jlaskey
parents:
18851
diff
changeset
|
1144 |
|
e1158153db51
8019987: String trimRight and trimLeft could be defined
jlaskey
parents:
18851
diff
changeset
|
1145 |
while (start <= end && ScriptRuntime.isJSWhitespace(str.charAt(start))) { |
e1158153db51
8019987: String trimRight and trimLeft could be defined
jlaskey
parents:
18851
diff
changeset
|
1146 |
start++; |
e1158153db51
8019987: String trimRight and trimLeft could be defined
jlaskey
parents:
18851
diff
changeset
|
1147 |
} |
e1158153db51
8019987: String trimRight and trimLeft could be defined
jlaskey
parents:
18851
diff
changeset
|
1148 |
|
e1158153db51
8019987: String trimRight and trimLeft could be defined
jlaskey
parents:
18851
diff
changeset
|
1149 |
return str.substring(start, end + 1); |
e1158153db51
8019987: String trimRight and trimLeft could be defined
jlaskey
parents:
18851
diff
changeset
|
1150 |
} |
e1158153db51
8019987: String trimRight and trimLeft could be defined
jlaskey
parents:
18851
diff
changeset
|
1151 |
|
e1158153db51
8019987: String trimRight and trimLeft could be defined
jlaskey
parents:
18851
diff
changeset
|
1152 |
/** |
e1158153db51
8019987: String trimRight and trimLeft could be defined
jlaskey
parents:
18851
diff
changeset
|
1153 |
* Nashorn extension: String.prototype.trimRight ( ) |
e1158153db51
8019987: String trimRight and trimLeft could be defined
jlaskey
parents:
18851
diff
changeset
|
1154 |
* @param self self reference |
e1158153db51
8019987: String trimRight and trimLeft could be defined
jlaskey
parents:
18851
diff
changeset
|
1155 |
* @return string trimmed right from whitespace |
e1158153db51
8019987: String trimRight and trimLeft could be defined
jlaskey
parents:
18851
diff
changeset
|
1156 |
*/ |
e1158153db51
8019987: String trimRight and trimLeft could be defined
jlaskey
parents:
18851
diff
changeset
|
1157 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
23763
950d8bc0554f
8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents:
23375
diff
changeset
|
1158 |
public static String trimRight(final Object self) { |
19633
e1158153db51
8019987: String trimRight and trimLeft could be defined
jlaskey
parents:
18851
diff
changeset
|
1159 |
|
e1158153db51
8019987: String trimRight and trimLeft could be defined
jlaskey
parents:
18851
diff
changeset
|
1160 |
final String str = checkObjectToString(self); |
24720 | 1161 |
final int start = 0; |
19633
e1158153db51
8019987: String trimRight and trimLeft could be defined
jlaskey
parents:
18851
diff
changeset
|
1162 |
int end = str.length() - 1; |
e1158153db51
8019987: String trimRight and trimLeft could be defined
jlaskey
parents:
18851
diff
changeset
|
1163 |
|
e1158153db51
8019987: String trimRight and trimLeft could be defined
jlaskey
parents:
18851
diff
changeset
|
1164 |
while (end >= start && ScriptRuntime.isJSWhitespace(str.charAt(end))) { |
e1158153db51
8019987: String trimRight and trimLeft could be defined
jlaskey
parents:
18851
diff
changeset
|
1165 |
end--; |
e1158153db51
8019987: String trimRight and trimLeft could be defined
jlaskey
parents:
18851
diff
changeset
|
1166 |
} |
e1158153db51
8019987: String trimRight and trimLeft could be defined
jlaskey
parents:
18851
diff
changeset
|
1167 |
|
e1158153db51
8019987: String trimRight and trimLeft could be defined
jlaskey
parents:
18851
diff
changeset
|
1168 |
return str.substring(start, end + 1); |
16147 | 1169 |
} |
1170 |
||
24769 | 1171 |
private static ScriptObject newObj(final CharSequence str) { |
18851
bdb92c95f886
8019947: inherited property invalidation does not work with two globals in same context
sundar
parents:
18618
diff
changeset
|
1172 |
return new NativeString(str); |
16147 | 1173 |
} |
1174 |
||
1175 |
/** |
|
1176 |
* ECMA 15.5.2.1 new String ( [ value ] ) |
|
1177 |
* |
|
1178 |
* Constructor |
|
1179 |
* |
|
1180 |
* @param newObj is this constructor invoked with the new operator |
|
1181 |
* @param self self reference |
|
1182 |
* @param args arguments (a value) |
|
1183 |
* |
|
1184 |
* @return new NativeString, empty string if no args, extraneous args ignored |
|
1185 |
*/ |
|
1186 |
@Constructor(arity = 1) |
|
1187 |
public static Object constructor(final boolean newObj, final Object self, final Object... args) { |
|
24720 | 1188 |
final CharSequence str = args.length > 0 ? JSType.toCharSequence(args[0]) : ""; |
24751 | 1189 |
return newObj ? newObj(str) : str.toString(); |
16147 | 1190 |
} |
1191 |
||
1192 |
/** |
|
1193 |
* ECMA 15.5.2.1 new String ( [ value ] ) - special version with no args |
|
1194 |
* |
|
1195 |
* Constructor |
|
1196 |
* |
|
1197 |
* @param newObj is this constructor invoked with the new operator |
|
1198 |
* @param self self reference |
|
1199 |
* |
|
1200 |
* @return new NativeString ("") |
|
1201 |
*/ |
|
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1202 |
@SpecializedFunction(isConstructor=true) |
16147 | 1203 |
public static Object constructor(final boolean newObj, final Object self) { |
24751 | 1204 |
return newObj ? newObj("") : ""; |
16147 | 1205 |
} |
1206 |
||
16192 | 1207 |
/** |
1208 |
* ECMA 15.5.2.1 new String ( [ value ] ) - special version with one arg |
|
1209 |
* |
|
1210 |
* Constructor |
|
1211 |
* |
|
1212 |
* @param newObj is this constructor invoked with the new operator |
|
1213 |
* @param self self reference |
|
1214 |
* @param arg argument |
|
1215 |
* |
|
1216 |
* @return new NativeString (arg) |
|
1217 |
*/ |
|
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1218 |
@SpecializedFunction(isConstructor=true) |
16192 | 1219 |
public static Object constructor(final boolean newObj, final Object self, final Object arg) { |
16203
5c15a03ec3ea
8007109: Regression: String(ConsString) does not flatten argument to String
hannesw
parents:
16201
diff
changeset
|
1220 |
final CharSequence str = JSType.toCharSequence(arg); |
24751 | 1221 |
return newObj ? newObj(str) : str.toString(); |
16192 | 1222 |
} |
16147 | 1223 |
|
1224 |
/** |
|
1225 |
* ECMA 15.5.2.1 new String ( [ value ] ) - special version with exactly one {@code int} arg |
|
1226 |
* |
|
1227 |
* Constructor |
|
1228 |
* |
|
1229 |
* @param newObj is this constructor invoked with the new operator |
|
1230 |
* @param self self reference |
|
1231 |
* @param arg the arg |
|
1232 |
* |
|
1233 |
* @return new NativeString containing the string representation of the arg |
|
1234 |
*/ |
|
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1235 |
@SpecializedFunction(isConstructor=true) |
16147 | 1236 |
public static Object constructor(final boolean newObj, final Object self, final int arg) { |
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1237 |
final String str = Integer.toString(arg); |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1238 |
return newObj ? newObj(str) : str; |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1239 |
} |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1240 |
|
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1241 |
/** |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1242 |
* ECMA 15.5.2.1 new String ( [ value ] ) - special version with exactly one {@code int} arg |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1243 |
* |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1244 |
* Constructor |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1245 |
* |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1246 |
* @param newObj is this constructor invoked with the new operator |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1247 |
* @param self self reference |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1248 |
* @param arg the arg |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1249 |
* |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1250 |
* @return new NativeString containing the string representation of the arg |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1251 |
*/ |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1252 |
@SpecializedFunction(isConstructor=true) |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1253 |
public static Object constructor(final boolean newObj, final Object self, final long arg) { |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1254 |
final String str = Long.toString(arg); |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1255 |
return newObj ? newObj(str) : str; |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1256 |
} |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1257 |
|
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1258 |
/** |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1259 |
* ECMA 15.5.2.1 new String ( [ value ] ) - special version with exactly one {@code int} arg |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1260 |
* |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1261 |
* Constructor |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1262 |
* |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1263 |
* @param newObj is this constructor invoked with the new operator |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1264 |
* @param self self reference |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1265 |
* @param arg the arg |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1266 |
* |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1267 |
* @return new NativeString containing the string representation of the arg |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1268 |
*/ |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1269 |
@SpecializedFunction(isConstructor=true) |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1270 |
public static Object constructor(final boolean newObj, final Object self, final double arg) { |
16203
5c15a03ec3ea
8007109: Regression: String(ConsString) does not flatten argument to String
hannesw
parents:
16201
diff
changeset
|
1271 |
final String str = JSType.toString(arg); |
24751 | 1272 |
return newObj ? newObj(str) : str; |
16147 | 1273 |
} |
1274 |
||
1275 |
/** |
|
24719 | 1276 |
* ECMA 15.5.2.1 new String ( [ value ] ) - special version with exactly one {@code boolean} arg |
1277 |
* |
|
1278 |
* Constructor |
|
1279 |
* |
|
1280 |
* @param newObj is this constructor invoked with the new operator |
|
1281 |
* @param self self reference |
|
1282 |
* @param arg the arg |
|
1283 |
* |
|
1284 |
* @return new NativeString containing the string representation of the arg |
|
1285 |
*/ |
|
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1286 |
@SpecializedFunction(isConstructor=true) |
24719 | 1287 |
public static Object constructor(final boolean newObj, final Object self, final boolean arg) { |
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1288 |
final String str = Boolean.toString(arg); |
24751 | 1289 |
return newObj ? newObj(str) : str; |
16147 | 1290 |
} |
1291 |
||
1292 |
/** |
|
1293 |
* Lookup the appropriate method for an invoke dynamic call. |
|
1294 |
* |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
1295 |
* @param request the link request |
16147 | 1296 |
* @param receiver receiver of call |
1297 |
* @return Link to be invoked at call site. |
|
1298 |
*/ |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
1299 |
public static GuardedInvocation lookupPrimitive(final LinkRequest request, final Object receiver) { |
16147 | 1300 |
final MethodHandle guard = NashornGuards.getInstanceOf2Guard(String.class, ConsString.class); |
23372
09707b3e5fb0
8021350: Share script classes between threads/globals within context
hannesw
parents:
23084
diff
changeset
|
1301 |
return PrimitiveLookup.lookupPrimitive(request, guard, new NativeString((CharSequence)receiver), WRAPFILTER, PROTOFILTER); |
16147 | 1302 |
} |
1303 |
||
1304 |
@SuppressWarnings("unused") |
|
1305 |
private static NativeString wrapFilter(final Object receiver) { |
|
1306 |
return new NativeString((CharSequence)receiver); |
|
1307 |
} |
|
1308 |
||
23372
09707b3e5fb0
8021350: Share script classes between threads/globals within context
hannesw
parents:
23084
diff
changeset
|
1309 |
@SuppressWarnings("unused") |
09707b3e5fb0
8021350: Share script classes between threads/globals within context
hannesw
parents:
23084
diff
changeset
|
1310 |
private static Object protoFilter(final Object object) { |
09707b3e5fb0
8021350: Share script classes between threads/globals within context
hannesw
parents:
23084
diff
changeset
|
1311 |
return Global.instance().getStringPrototype(); |
09707b3e5fb0
8021350: Share script classes between threads/globals within context
hannesw
parents:
23084
diff
changeset
|
1312 |
} |
09707b3e5fb0
8021350: Share script classes between threads/globals within context
hannesw
parents:
23084
diff
changeset
|
1313 |
|
16147 | 1314 |
private static CharSequence getCharSequence(final Object self) { |
29282 | 1315 |
if (JSType.isString(self)) { |
16147 | 1316 |
return (CharSequence)self; |
1317 |
} else if (self instanceof NativeString) { |
|
1318 |
return ((NativeString)self).getValue(); |
|
1319 |
} else if (self != null && self == Global.instance().getStringPrototype()) { |
|
1320 |
return ""; |
|
1321 |
} else { |
|
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16234
diff
changeset
|
1322 |
throw typeError("not.a.string", ScriptRuntime.safeToString(self)); |
16147 | 1323 |
} |
1324 |
} |
|
1325 |
||
1326 |
private static String getString(final Object self) { |
|
1327 |
if (self instanceof String) { |
|
1328 |
return (String)self; |
|
1329 |
} else if (self instanceof ConsString) { |
|
1330 |
return self.toString(); |
|
1331 |
} else if (self instanceof NativeString) { |
|
1332 |
return ((NativeString)self).getStringValue(); |
|
1333 |
} else if (self != null && self == Global.instance().getStringPrototype()) { |
|
1334 |
return ""; |
|
1335 |
} else { |
|
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1336 |
throw typeError("not.a.string", ScriptRuntime.safeToString(self)); |
16147 | 1337 |
} |
1338 |
} |
|
1339 |
||
16192 | 1340 |
/** |
1341 |
* Combines ECMA 9.10 CheckObjectCoercible and ECMA 9.8 ToString with a shortcut for strings. |
|
1342 |
* |
|
1343 |
* @param self the object |
|
1344 |
* @return the object as string |
|
1345 |
*/ |
|
1346 |
private static String checkObjectToString(final Object self) { |
|
1347 |
if (self instanceof String) { |
|
1348 |
return (String)self; |
|
1349 |
} else if (self instanceof ConsString) { |
|
1350 |
return self.toString(); |
|
1351 |
} else { |
|
1352 |
Global.checkObjectCoercible(self); |
|
1353 |
return JSType.toString(self); |
|
1354 |
} |
|
1355 |
} |
|
1356 |
||
24719 | 1357 |
private boolean isValidStringIndex(final int key) { |
16147 | 1358 |
return key >= 0 && key < value.length(); |
1359 |
} |
|
1360 |
||
23372
09707b3e5fb0
8021350: Share script classes between threads/globals within context
hannesw
parents:
23084
diff
changeset
|
1361 |
private static MethodHandle findOwnMH(final String name, final MethodType type) { |
09707b3e5fb0
8021350: Share script classes between threads/globals within context
hannesw
parents:
23084
diff
changeset
|
1362 |
return MH.findStatic(MethodHandles.lookup(), NativeString.class, name, type); |
16147 | 1363 |
} |
24719 | 1364 |
|
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1365 |
@Override |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1366 |
public LinkLogic getLinkLogic(final Class<? extends LinkLogic> clazz) { |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1367 |
if (clazz == CharCodeAtLinkLogic.class) { |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1368 |
return CharCodeAtLinkLogic.INSTANCE; |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1369 |
} |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1370 |
return null; |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1371 |
} |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1372 |
|
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1373 |
@Override |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1374 |
public boolean hasPerInstanceAssumptions() { |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1375 |
return false; |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1376 |
} |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1377 |
|
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1378 |
/** |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1379 |
* This is linker logic charCodeAt - when we specialize further methods in NativeString |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1380 |
* It may be expanded. It's link check makes sure that we are dealing with a char |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1381 |
* sequence and that we are in range |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1382 |
*/ |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1383 |
private static final class CharCodeAtLinkLogic extends SpecializedFunction.LinkLogic { |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1384 |
private static final CharCodeAtLinkLogic INSTANCE = new CharCodeAtLinkLogic(); |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1385 |
|
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1386 |
@Override |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1387 |
public boolean canLink(final Object self, final CallSiteDescriptor desc, final LinkRequest request) { |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1388 |
try { |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1389 |
//check that it's a char sequence or throw cce |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1390 |
final CharSequence cs = (CharSequence)self; |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1391 |
//check that the index, representable as an int, is inside the array |
27362
142fc25a0baf
8062381: Wrong index was used for linking charCodeAt specializations
lagergren
parents:
26768
diff
changeset
|
1392 |
final int intIndex = JSType.toInteger(request.getArguments()[2]); |
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1393 |
return intIndex >= 0 && intIndex < cs.length(); //can link |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1394 |
} catch (final ClassCastException | IndexOutOfBoundsException e) { |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1395 |
//fallthru |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1396 |
} |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1397 |
return false; |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1398 |
} |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1399 |
|
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1400 |
/** |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1401 |
* charCodeAt callsites can throw ClassCastException as a mechanism to have them |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1402 |
* relinked - this enabled fast checks of the kind of ((IntArrayData)arrayData).push(x) |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1403 |
* for an IntArrayData only push - if this fails, a CCE will be thrown and we will relink |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1404 |
*/ |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1405 |
@Override |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1406 |
public Class<? extends Throwable> getRelinkException() { |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1407 |
return ClassCastException.class; |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1408 |
} |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
26508
diff
changeset
|
1409 |
} |
16147 | 1410 |
} |