author | sundar |
Sat, 09 Mar 2013 21:49:32 +0530 | |
changeset 16277 | fd698c5ee684 |
parent 16262 | 75513555e603 |
child 16534 | 6993b3541b28 |
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 |
||
28 |
import static jdk.nashorn.internal.runtime.ECMAErrors.typeError; |
|
29 |
import static jdk.nashorn.internal.runtime.JSType.isRepresentableAsInt; |
|
30 |
import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED; |
|
31 |
import static jdk.nashorn.internal.runtime.arrays.ArrayIndex.getArrayIndexNoThrow; |
|
16277 | 32 |
import static jdk.nashorn.internal.lookup.Lookup.MH; |
16147 | 33 |
|
34 |
import java.lang.invoke.MethodHandle; |
|
35 |
import java.lang.invoke.MethodHandles; |
|
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; |
16234
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16226
diff
changeset
|
41 |
import jdk.internal.dynalink.CallSiteDescriptor; |
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16226
diff
changeset
|
42 |
import jdk.internal.dynalink.linker.GuardedInvocation; |
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16226
diff
changeset
|
43 |
import jdk.internal.dynalink.linker.LinkRequest; |
16147 | 44 |
import jdk.nashorn.internal.objects.annotations.Attribute; |
45 |
import jdk.nashorn.internal.objects.annotations.Constructor; |
|
46 |
import jdk.nashorn.internal.objects.annotations.Function; |
|
47 |
import jdk.nashorn.internal.objects.annotations.Getter; |
|
48 |
import jdk.nashorn.internal.objects.annotations.ScriptClass; |
|
49 |
import jdk.nashorn.internal.objects.annotations.SpecializedConstructor; |
|
50 |
import jdk.nashorn.internal.objects.annotations.SpecializedFunction; |
|
51 |
import jdk.nashorn.internal.objects.annotations.Where; |
|
52 |
import jdk.nashorn.internal.runtime.ConsString; |
|
53 |
import jdk.nashorn.internal.runtime.JSType; |
|
54 |
import jdk.nashorn.internal.runtime.ScriptFunction; |
|
55 |
import jdk.nashorn.internal.runtime.ScriptObject; |
|
56 |
import jdk.nashorn.internal.runtime.ScriptRuntime; |
|
57 |
import jdk.nashorn.internal.runtime.arrays.ArrayIndex; |
|
16277 | 58 |
import jdk.nashorn.internal.lookup.MethodHandleFactory; |
16147 | 59 |
import jdk.nashorn.internal.runtime.linker.NashornGuards; |
60 |
import jdk.nashorn.internal.runtime.linker.PrimitiveLookup; |
|
61 |
||
62 |
||
63 |
/** |
|
64 |
* ECMA 15.5 String Objects. |
|
65 |
*/ |
|
66 |
@ScriptClass("String") |
|
67 |
public final class NativeString extends ScriptObject { |
|
68 |
||
69 |
private final CharSequence value; |
|
70 |
||
16186
91bd9d2aa2f5
8006570: This-value for non-strict functions should be converted to object
hannesw
parents:
16181
diff
changeset
|
71 |
static final MethodHandle WRAPFILTER = findWrapFilter(); |
16147 | 72 |
|
73 |
NativeString(final CharSequence value) { |
|
74 |
this(value, Global.instance().getStringPrototype()); |
|
75 |
} |
|
76 |
||
77 |
private NativeString(final CharSequence value, final ScriptObject proto) { |
|
78 |
assert value instanceof String || value instanceof ConsString; |
|
79 |
this.value = value; |
|
80 |
this.setProto(proto); |
|
81 |
} |
|
82 |
||
83 |
@Override |
|
84 |
public String safeToString() { |
|
85 |
return "[String " + toString() + "]"; |
|
86 |
} |
|
87 |
||
88 |
@Override |
|
89 |
public String toString() { |
|
90 |
return getStringValue(); |
|
91 |
} |
|
92 |
||
93 |
@Override |
|
94 |
public boolean equals(final Object other) { |
|
95 |
if (other instanceof NativeString) { |
|
96 |
return getStringValue().equals(((NativeString) other).getStringValue()); |
|
97 |
} |
|
98 |
||
99 |
return false; |
|
100 |
} |
|
101 |
||
102 |
@Override |
|
103 |
public int hashCode() { |
|
104 |
return getStringValue().hashCode(); |
|
105 |
} |
|
106 |
||
107 |
private String getStringValue() { |
|
108 |
return value instanceof String ? (String) value : value.toString(); |
|
109 |
} |
|
110 |
||
111 |
private CharSequence getValue() { |
|
112 |
return value; |
|
113 |
} |
|
114 |
||
115 |
@Override |
|
116 |
public String getClassName() { |
|
117 |
return "String"; |
|
118 |
} |
|
119 |
||
120 |
@Override |
|
121 |
public Object getLength() { |
|
122 |
return value.length(); |
|
123 |
} |
|
124 |
||
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
125 |
// 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
|
126 |
@Override |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
127 |
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
|
128 |
final Object self = request.getReceiver(); |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
129 |
final Class<?> returnType = desc.getMethodType().returnType(); |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
130 |
|
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
131 |
if (returnType == Object.class && (self instanceof String || self instanceof ConsString)) { |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
132 |
try { |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
133 |
MethodHandle mh = MethodHandles.lookup().findStatic(NativeString.class, "get", desc.getMethodType()); |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
134 |
return new GuardedInvocation(mh, NashornGuards.getInstanceOf2Guard(String.class, ConsString.class)); |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
135 |
} catch (final NoSuchMethodException | IllegalAccessException e) { |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
136 |
// Shouldn't happen. Fall back to super |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
137 |
} |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
138 |
} |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
139 |
return super.findGetIndexMethod(desc, request); |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
140 |
} |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
141 |
|
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
142 |
@SuppressWarnings("unused") |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
143 |
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
|
144 |
final CharSequence cs = JSType.toCharSequence(self); |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
145 |
final int index = getArrayIndexNoThrow(key); |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
146 |
if (index >= 0 && index < cs.length()) { |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
147 |
return String.valueOf(cs.charAt(index)); |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
148 |
} |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
149 |
return ((ScriptObject) Global.toObject(self)).get(key); |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
150 |
} |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
151 |
|
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
152 |
@SuppressWarnings("unused") |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
153 |
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
|
154 |
if (isRepresentableAsInt(key)) { |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
155 |
return get(self, (int)key); |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
156 |
} |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
157 |
return ((ScriptObject) Global.toObject(self)).get(key); |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
158 |
} |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
159 |
|
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
160 |
@SuppressWarnings("unused") |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
161 |
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
|
162 |
final CharSequence cs = JSType.toCharSequence(self); |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
163 |
if (key >= 0 && key < cs.length()) { |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
164 |
return String.valueOf(cs.charAt((int)key)); |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
165 |
} |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
166 |
return ((ScriptObject) Global.toObject(self)).get(key); |
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 |
|
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
169 |
@SuppressWarnings("unused") |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
170 |
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
|
171 |
final CharSequence cs = JSType.toCharSequence(self); |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
172 |
if (key >= 0 && key < cs.length()) { |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
173 |
return String.valueOf(cs.charAt(key)); |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
174 |
} |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
175 |
return ((ScriptObject) Global.toObject(self)).get(key); |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
176 |
} |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
177 |
|
16147 | 178 |
// String characters can be accessed with array-like indexing.. |
179 |
@Override |
|
180 |
public Object get(final Object key) { |
|
181 |
final int index = getArrayIndexNoThrow(key); |
|
182 |
if (index >= 0 && index < value.length()) { |
|
183 |
return String.valueOf(value.charAt(index)); |
|
184 |
} |
|
185 |
return super.get(key); |
|
186 |
} |
|
187 |
||
188 |
@Override |
|
189 |
public Object get(final double key) { |
|
190 |
if (isRepresentableAsInt(key)) { |
|
191 |
return get((int)key); |
|
192 |
} |
|
193 |
return super.get(key); |
|
194 |
} |
|
195 |
||
196 |
@Override |
|
197 |
public Object get(final long key) { |
|
198 |
if (key >= 0 && key < value.length()) { |
|
199 |
return String.valueOf(value.charAt((int)key)); |
|
200 |
} |
|
201 |
return super.get(key); |
|
202 |
} |
|
203 |
||
204 |
@Override |
|
205 |
public Object get(final int key) { |
|
206 |
if (key >= 0 && key < value.length()) { |
|
207 |
return String.valueOf(value.charAt(key)); |
|
208 |
} |
|
209 |
return super.get(key); |
|
210 |
} |
|
211 |
||
212 |
@Override |
|
213 |
public int getInt(final Object key) { |
|
214 |
return JSType.toInt32(get(key)); |
|
215 |
} |
|
216 |
||
217 |
@Override |
|
218 |
public int getInt(final double key) { |
|
219 |
return JSType.toInt32(get(key)); |
|
220 |
} |
|
221 |
||
222 |
@Override |
|
223 |
public int getInt(final long key) { |
|
224 |
return JSType.toInt32(get(key)); |
|
225 |
} |
|
226 |
||
227 |
@Override |
|
228 |
public int getInt(final int key) { |
|
229 |
return JSType.toInt32(get(key)); |
|
230 |
} |
|
231 |
||
232 |
@Override |
|
233 |
public long getLong(final Object key) { |
|
234 |
return JSType.toUint32(get(key)); |
|
235 |
} |
|
236 |
||
237 |
@Override |
|
238 |
public long getLong(final double key) { |
|
239 |
return JSType.toUint32(get(key)); |
|
240 |
} |
|
241 |
||
242 |
@Override |
|
243 |
public long getLong(final long key) { |
|
244 |
return JSType.toUint32(get(key)); |
|
245 |
} |
|
246 |
||
247 |
@Override |
|
248 |
public long getLong(final int key) { |
|
249 |
return JSType.toUint32(get(key)); |
|
250 |
} |
|
251 |
||
252 |
@Override |
|
253 |
public double getDouble(final Object key) { |
|
254 |
return JSType.toNumber(get(key)); |
|
255 |
} |
|
256 |
||
257 |
@Override |
|
258 |
public double getDouble(final double key) { |
|
259 |
return JSType.toNumber(get(key)); |
|
260 |
} |
|
261 |
||
262 |
@Override |
|
263 |
public double getDouble(final long key) { |
|
264 |
return JSType.toNumber(get(key)); |
|
265 |
} |
|
266 |
||
267 |
@Override |
|
268 |
public double getDouble(final int key) { |
|
269 |
return JSType.toNumber(get(key)); |
|
270 |
} |
|
271 |
||
272 |
@Override |
|
273 |
public boolean has(final Object key) { |
|
274 |
final int index = getArrayIndexNoThrow(key); |
|
275 |
return isValid(index) || super.has(key); |
|
276 |
} |
|
277 |
||
278 |
@Override |
|
279 |
public boolean has(final int key) { |
|
280 |
return isValid(key) || super.has(key); |
|
281 |
} |
|
282 |
||
283 |
@Override |
|
284 |
public boolean has(final long key) { |
|
285 |
final int index = getArrayIndexNoThrow(key); |
|
286 |
return isValid(index) || super.has(key); |
|
287 |
} |
|
288 |
||
289 |
@Override |
|
290 |
public boolean has(final double key) { |
|
291 |
final int index = getArrayIndexNoThrow(key); |
|
292 |
return isValid(index) || super.has(key); |
|
293 |
} |
|
294 |
||
295 |
@Override |
|
296 |
public boolean hasOwnProperty(final Object key) { |
|
297 |
final int index = getArrayIndexNoThrow(key); |
|
298 |
return isValid(index) || super.hasOwnProperty(key); |
|
299 |
} |
|
300 |
||
301 |
@Override |
|
302 |
public boolean hasOwnProperty(final int key) { |
|
303 |
return isValid(key) || super.hasOwnProperty(key); |
|
304 |
} |
|
305 |
||
306 |
@Override |
|
307 |
public boolean hasOwnProperty(final long key) { |
|
308 |
final int index = getArrayIndexNoThrow(key); |
|
309 |
return isValid(index) || super.hasOwnProperty(key); |
|
310 |
} |
|
311 |
||
312 |
@Override |
|
313 |
public boolean hasOwnProperty(final double key) { |
|
314 |
final int index = getArrayIndexNoThrow(key); |
|
315 |
return isValid(index) || super.hasOwnProperty(key); |
|
316 |
} |
|
317 |
||
318 |
@Override |
|
319 |
public boolean delete(final int key, final boolean strict) { |
|
320 |
return checkDeleteIndex(key, strict)? false : super.delete(key, strict); |
|
321 |
} |
|
322 |
||
323 |
@Override |
|
324 |
public boolean delete(final long key, final boolean strict) { |
|
325 |
final int index = getArrayIndexNoThrow(key); |
|
326 |
return checkDeleteIndex(index, strict)? false : super.delete(key, strict); |
|
327 |
} |
|
328 |
||
329 |
@Override |
|
330 |
public boolean delete(final double key, final boolean strict) { |
|
331 |
final int index = getArrayIndexNoThrow(key); |
|
332 |
return checkDeleteIndex(index, strict)? false : super.delete(key, strict); |
|
333 |
} |
|
334 |
||
335 |
@Override |
|
336 |
public boolean delete(final Object key, final boolean strict) { |
|
337 |
final int index = getArrayIndexNoThrow(key); |
|
338 |
return checkDeleteIndex(index, strict)? false : super.delete(key, strict); |
|
339 |
} |
|
340 |
||
341 |
private boolean checkDeleteIndex(final int index, final boolean strict) { |
|
342 |
if (isValid(index)) { |
|
343 |
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
|
344 |
throw typeError("cant.delete.property", Integer.toString(index), ScriptRuntime.safeToString(this)); |
16147 | 345 |
} |
346 |
return true; |
|
347 |
} |
|
348 |
||
349 |
return false; |
|
350 |
} |
|
351 |
||
352 |
@Override |
|
353 |
public Object getOwnPropertyDescriptor(final String key) { |
|
354 |
final int index = ArrayIndex.getArrayIndexNoThrow(key); |
|
355 |
if (index >= 0 && index < value.length()) { |
|
356 |
final Global global = Global.instance(); |
|
357 |
return global.newDataDescriptor(String.valueOf(value.charAt(index)), false, true, false); |
|
358 |
} |
|
359 |
||
360 |
return super.getOwnPropertyDescriptor(key); |
|
361 |
} |
|
362 |
||
363 |
/** |
|
364 |
* return a List of own keys associated with the object. |
|
365 |
* @param all True if to include non-enumerable keys. |
|
366 |
* @return Array of keys. |
|
367 |
*/ |
|
368 |
@Override |
|
369 |
public String[] getOwnKeys(final boolean all) { |
|
370 |
final List<Object> keys = new ArrayList<>(); |
|
371 |
||
372 |
// add string index keys |
|
373 |
for (int i = 0; i < value.length(); i++) { |
|
374 |
keys.add(JSType.toString(i)); |
|
375 |
} |
|
376 |
||
377 |
// add super class properties |
|
378 |
keys.addAll(Arrays.asList(super.getOwnKeys(all))); |
|
379 |
return keys.toArray(new String[keys.size()]); |
|
380 |
} |
|
381 |
||
382 |
/** |
|
383 |
* ECMA 15.5.3 String.length |
|
384 |
* @param self self reference |
|
385 |
* @return value of length property for string |
|
386 |
*/ |
|
387 |
@Getter(attributes = Attribute.NOT_ENUMERABLE | Attribute.NOT_WRITABLE | Attribute.NOT_CONFIGURABLE) |
|
388 |
public static Object length(final Object self) { |
|
389 |
return getCharSequence(self).length(); |
|
390 |
} |
|
391 |
||
392 |
/** |
|
393 |
* ECMA 15.5.3.2 String.fromCharCode ( [ char0 [ , char1 [ , ... ] ] ] ) |
|
394 |
* @param self self reference |
|
395 |
* @param args array of arguments to be interpreted as char |
|
396 |
* @return string with arguments translated to charcodes |
|
397 |
*/ |
|
398 |
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1, where = Where.CONSTRUCTOR) |
|
399 |
public static Object fromCharCode(final Object self, final Object... args) { |
|
400 |
final char[] buf = new char[args.length]; |
|
401 |
int index = 0; |
|
402 |
for (final Object arg : args) { |
|
403 |
buf[index++] = (char)JSType.toUint16(arg); |
|
404 |
} |
|
405 |
return new String(buf); |
|
406 |
} |
|
407 |
||
408 |
/** |
|
409 |
* ECMA 15.5.3.2 - specialization for one char |
|
410 |
* @param self self reference |
|
411 |
* @param value one argument to be interpreted as char |
|
412 |
* @return string with one charcode |
|
413 |
*/ |
|
414 |
@SpecializedFunction |
|
415 |
public static Object fromCharCode(final Object self, final Object value) { |
|
416 |
try { |
|
417 |
return "" + (char)JSType.toUint16(((Number)value).doubleValue()); |
|
418 |
} catch (final ClassCastException e) { |
|
419 |
return fromCharCode(self, new Object[] { value }); |
|
420 |
} |
|
421 |
} |
|
422 |
||
423 |
/** |
|
424 |
* ECMA 15.5.3.2 - specialization for one char of int type |
|
425 |
* @param self self reference |
|
426 |
* @param value one argument to be interpreted as char |
|
427 |
* @return string with one charcode |
|
428 |
*/ |
|
429 |
@SpecializedFunction |
|
430 |
public static Object fromCharCode(final Object self, final int value) { |
|
431 |
return "" + (char)(value & 0xffff); |
|
432 |
} |
|
433 |
||
434 |
/** |
|
435 |
* ECMA 15.5.3.2 - specialization for one char of long type |
|
436 |
* @param self self reference |
|
437 |
* @param value one argument to be interpreted as char |
|
438 |
* @return string with one charcode |
|
439 |
*/ |
|
440 |
@SpecializedFunction |
|
441 |
public static Object fromCharCode(final Object self, final long value) { |
|
442 |
return "" + (char)((int)value & 0xffff); |
|
443 |
} |
|
444 |
||
445 |
/** |
|
446 |
* ECMA 15.5.3.2 - specialization for one char of double type |
|
447 |
* @param self self reference |
|
448 |
* @param value one argument to be interpreted as char |
|
449 |
* @return string with one charcode |
|
450 |
*/ |
|
451 |
@SpecializedFunction |
|
452 |
public static Object fromCharCode(final Object self, final double value) { |
|
453 |
return "" + (char)JSType.toUint16(value); |
|
454 |
} |
|
455 |
||
456 |
/** |
|
457 |
* ECMA 15.5.4.2 String.prototype.toString ( ) |
|
458 |
* @param self self reference |
|
459 |
* @return self as string |
|
460 |
*/ |
|
461 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
462 |
public static Object toString(final Object self) { |
|
463 |
return getString(self); |
|
464 |
} |
|
465 |
||
466 |
/** |
|
467 |
* ECMA 15.5.4.3 String.prototype.valueOf ( ) |
|
468 |
* @param self self reference |
|
469 |
* @return self as string |
|
470 |
*/ |
|
471 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
472 |
public static Object valueOf(final Object self) { |
|
473 |
return getString(self); |
|
474 |
} |
|
475 |
||
476 |
/** |
|
477 |
* ECMA 15.5.4.4 String.prototype.charAt (pos) |
|
478 |
* @param self self reference |
|
479 |
* @param pos position in string |
|
480 |
* @return string representing the char at the given position |
|
481 |
*/ |
|
482 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
483 |
public static Object charAt(final Object self, final Object pos) { |
|
16192 | 484 |
return charAt(self, JSType.toInteger(pos)); |
485 |
} |
|
486 |
||
487 |
/** |
|
488 |
* ECMA 15.5.4.4 String.prototype.charAt (pos) - specialized version for double position |
|
489 |
* @param self self reference |
|
490 |
* @param pos position in string |
|
491 |
* @return string representing the char at the given position |
|
492 |
*/ |
|
493 |
@SpecializedFunction |
|
494 |
public static String charAt(final Object self, final double pos) { |
|
495 |
return charAt(self, (int)pos); |
|
496 |
} |
|
497 |
||
498 |
/** |
|
499 |
* ECMA 15.5.4.4 String.prototype.charAt (pos) - specialized version for int position |
|
500 |
* @param self self reference |
|
501 |
* @param pos position in string |
|
502 |
* @return string representing the char at the given position |
|
503 |
*/ |
|
504 |
@SpecializedFunction |
|
505 |
public static String charAt(final Object self, final int pos) { |
|
506 |
final String str = checkObjectToString(self); |
|
507 |
return (pos < 0 || pos >= str.length()) ? "" : String.valueOf(str.charAt(pos)); |
|
16147 | 508 |
} |
509 |
||
510 |
/** |
|
511 |
* ECMA 15.5.4.5 String.prototype.charCodeAt (pos) |
|
512 |
* @param self self reference |
|
513 |
* @param pos position in string |
|
514 |
* @return number representing charcode at position |
|
515 |
*/ |
|
516 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
517 |
public static Object charCodeAt(final Object self, final Object pos) { |
|
16192 | 518 |
return charCodeAt(self, JSType.toInteger(pos)); |
519 |
} |
|
16147 | 520 |
|
16192 | 521 |
/** |
522 |
* ECMA 15.5.4.5 String.prototype.charCodeAt (pos) - specialized version for double position |
|
523 |
* @param self self reference |
|
524 |
* @param pos position in string |
|
525 |
* @return number representing charcode at position |
|
526 |
*/ |
|
527 |
@SpecializedFunction |
|
528 |
public static double charCodeAt(final Object self, final double pos) { |
|
529 |
return charCodeAt(self, (int) pos); |
|
530 |
} |
|
531 |
||
532 |
/** |
|
533 |
* ECMA 15.5.4.5 String.prototype.charCodeAt (pos) - specialized version for int position |
|
534 |
* @param self self reference |
|
535 |
* @param pos position in string |
|
536 |
* @return number representing charcode at position |
|
537 |
*/ |
|
538 |
@SpecializedFunction |
|
539 |
public static double charCodeAt(final Object self, final int pos) { |
|
540 |
final String str = checkObjectToString(self); |
|
541 |
return (pos < 0 || pos >= str.length()) ? Double.NaN : str.charAt(pos); |
|
16147 | 542 |
} |
543 |
||
544 |
/** |
|
545 |
* ECMA 15.5.4.6 String.prototype.concat ( [ string1 [ , string2 [ , ... ] ] ] ) |
|
546 |
* @param self self reference |
|
547 |
* @param args list of string to concatenate |
|
548 |
* @return concatenated string |
|
549 |
*/ |
|
550 |
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1) |
|
551 |
public static Object concat(final Object self, final Object... args) { |
|
16192 | 552 |
CharSequence cs = checkObjectToString(self); |
16147 | 553 |
if (args != null) { |
554 |
for (final Object obj : args) { |
|
16192 | 555 |
cs = new ConsString(cs, JSType.toCharSequence(obj)); |
16147 | 556 |
} |
557 |
} |
|
16192 | 558 |
return cs; |
16147 | 559 |
} |
560 |
||
561 |
/** |
|
562 |
* ECMA 15.5.4.7 String.prototype.indexOf (searchString, position) |
|
563 |
* @param self self reference |
|
564 |
* @param search string to search for |
|
565 |
* @param pos position to start search |
|
566 |
* @return position of first match or -1 |
|
567 |
*/ |
|
568 |
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1) |
|
569 |
public static Object indexOf(final Object self, final Object search, final Object pos) { |
|
16192 | 570 |
final String str = checkObjectToString(self); |
571 |
return str.indexOf(JSType.toString(search), JSType.toInteger(pos)); |
|
572 |
} |
|
573 |
||
574 |
/** |
|
575 |
* ECMA 15.5.4.7 String.prototype.indexOf (searchString, position) specialized for no position parameter |
|
576 |
* @param self self reference |
|
577 |
* @param search string to search for |
|
578 |
* @return position of first match or -1 |
|
579 |
*/ |
|
580 |
@SpecializedFunction |
|
581 |
public static int indexOf(final Object self, final Object search) { |
|
582 |
return indexOf(self, search, 0); |
|
583 |
} |
|
584 |
||
585 |
/** |
|
586 |
* ECMA 15.5.4.7 String.prototype.indexOf (searchString, position) specialized for double position parameter |
|
587 |
* @param self self reference |
|
588 |
* @param search string to search for |
|
589 |
* @param pos position to start search |
|
590 |
* @return position of first match or -1 |
|
591 |
*/ |
|
592 |
@SpecializedFunction |
|
593 |
public static int indexOf(final Object self, final Object search, final double pos) { |
|
594 |
return indexOf(self, search, (int) pos); |
|
595 |
} |
|
596 |
||
597 |
/** |
|
598 |
* ECMA 15.5.4.7 String.prototype.indexOf (searchString, position) specialized for int position parameter |
|
599 |
* @param self self reference |
|
600 |
* @param search string to search for |
|
601 |
* @param pos position to start search |
|
602 |
* @return position of first match or -1 |
|
603 |
*/ |
|
604 |
@SpecializedFunction |
|
605 |
public static int indexOf(final Object self, final Object search, final int pos) { |
|
606 |
return checkObjectToString(self).indexOf(JSType.toString(search), pos); |
|
16147 | 607 |
} |
608 |
||
609 |
/** |
|
610 |
* ECMA 15.5.4.8 String.prototype.lastIndexOf (searchString, position) |
|
611 |
* @param self self reference |
|
612 |
* @param search string to search for |
|
613 |
* @param pos position to start search |
|
614 |
* @return last position of match or -1 |
|
615 |
*/ |
|
616 |
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1) |
|
617 |
public static Object lastIndexOf(final Object self, final Object search, final Object pos) { |
|
618 |
||
16192 | 619 |
final String str = checkObjectToString(self); |
16147 | 620 |
final String searchStr = JSType.toString(search); |
621 |
||
622 |
int from; |
|
623 |
||
624 |
if (pos == UNDEFINED) { |
|
625 |
from = str.length(); |
|
626 |
} else { |
|
627 |
final double numPos = JSType.toNumber(pos); |
|
628 |
from = !Double.isNaN(numPos) ? (int)numPos : (int)Double.POSITIVE_INFINITY; |
|
629 |
} |
|
630 |
||
631 |
return str.lastIndexOf(searchStr, from); |
|
632 |
} |
|
633 |
||
634 |
/** |
|
635 |
* ECMA 15.5.4.9 String.prototype.localeCompare (that) |
|
636 |
* @param self self reference |
|
637 |
* @param that comparison object |
|
638 |
* @return result of locale sensitive comparison operation between {@code self} and {@code that} |
|
639 |
*/ |
|
640 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
641 |
public static Object localeCompare(final Object self, final Object that) { |
|
642 |
||
16192 | 643 |
final String str = checkObjectToString(self); |
16262
75513555e603
8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents:
16258
diff
changeset
|
644 |
final Collator collator = Collator.getInstance(Global.getEnv()._locale); |
16147 | 645 |
|
646 |
collator.setStrength(Collator.IDENTICAL); |
|
647 |
collator.setDecomposition(Collator.CANONICAL_DECOMPOSITION); |
|
648 |
||
649 |
return (double)collator.compare(str, JSType.toString(that)); |
|
650 |
} |
|
651 |
||
652 |
/** |
|
653 |
* ECMA 15.5.4.10 String.prototype.match (regexp) |
|
654 |
* @param self self reference |
|
655 |
* @param regexp regexp expression |
|
656 |
* @return array of regexp matches |
|
657 |
*/ |
|
658 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
659 |
public static Object match(final Object self, final Object regexp) { |
|
660 |
||
16192 | 661 |
final String str = checkObjectToString(self); |
16147 | 662 |
|
663 |
NativeRegExp nativeRegExp; |
|
664 |
if (regexp == UNDEFINED) { |
|
665 |
nativeRegExp = new NativeRegExp(""); |
|
666 |
} else { |
|
667 |
nativeRegExp = Global.toRegExp(regexp); |
|
668 |
} |
|
669 |
||
670 |
if (!nativeRegExp.getGlobal()) { |
|
671 |
return nativeRegExp.exec(str); |
|
672 |
} |
|
673 |
||
674 |
nativeRegExp.setLastIndex(0); |
|
675 |
||
676 |
int previousLastIndex = 0; |
|
677 |
final List<Object> matches = new ArrayList<>(); |
|
678 |
||
679 |
Object result; |
|
680 |
while ((result = nativeRegExp.exec(str)) != null) { |
|
681 |
final int thisIndex = nativeRegExp.getLastIndex(); |
|
682 |
if (thisIndex == previousLastIndex) { |
|
683 |
nativeRegExp.setLastIndex(thisIndex + 1); |
|
684 |
previousLastIndex = thisIndex + 1; |
|
685 |
} else { |
|
686 |
previousLastIndex = thisIndex; |
|
687 |
} |
|
688 |
matches.add(((ScriptObject)result).get(0)); |
|
689 |
} |
|
690 |
||
691 |
if (matches.isEmpty()) { |
|
692 |
return null; |
|
693 |
} |
|
694 |
||
695 |
return new NativeArray(matches.toArray()); |
|
696 |
} |
|
697 |
||
698 |
/** |
|
699 |
* ECMA 15.5.4.11 String.prototype.replace (searchValue, replaceValue) |
|
700 |
* @param self self reference |
|
701 |
* @param string item to replace |
|
702 |
* @param replacement item to replace it with |
|
703 |
* @return string after replacement |
|
704 |
*/ |
|
705 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
706 |
public static Object replace(final Object self, final Object string, final Object replacement) { |
|
707 |
||
16192 | 708 |
final String str = checkObjectToString(self); |
16147 | 709 |
|
710 |
final NativeRegExp nativeRegExp; |
|
711 |
if (string instanceof NativeRegExp) { |
|
712 |
nativeRegExp = (NativeRegExp) string; |
|
713 |
} else { |
|
16258 | 714 |
nativeRegExp = NativeRegExp.flatRegExp(JSType.toString(string)); |
16147 | 715 |
} |
716 |
||
717 |
if (replacement instanceof ScriptFunction) { |
|
718 |
return nativeRegExp.replace(str, "", (ScriptFunction)replacement); |
|
719 |
} |
|
720 |
||
721 |
return nativeRegExp.replace(str, JSType.toString(replacement), null); |
|
722 |
} |
|
723 |
||
724 |
/** |
|
725 |
* ECMA 15.5.4.12 String.prototype.search (regexp) |
|
726 |
* |
|
727 |
* @param self self reference |
|
728 |
* @param string string to search for |
|
729 |
* @return offset where match occurred |
|
730 |
*/ |
|
731 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
732 |
public static Object search(final Object self, final Object string) { |
|
733 |
||
16192 | 734 |
final String str = checkObjectToString(self); |
16147 | 735 |
final NativeRegExp nativeRegExp = Global.toRegExp(string == UNDEFINED ? "" : string); |
736 |
||
737 |
return nativeRegExp.search(str); |
|
738 |
} |
|
739 |
||
740 |
/** |
|
741 |
* ECMA 15.5.4.13 String.prototype.slice (start, end) |
|
742 |
* |
|
743 |
* @param self self reference |
|
744 |
* @param start start position for slice |
|
745 |
* @param end end position for slice |
|
746 |
* @return sliced out substring |
|
747 |
*/ |
|
748 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
749 |
public static Object slice(final Object self, final Object start, final Object end) { |
|
16192 | 750 |
|
751 |
final String str = checkObjectToString(self); |
|
752 |
if (end == UNDEFINED) { |
|
753 |
return slice(str, JSType.toInteger(start)); |
|
754 |
} |
|
755 |
return slice(str, JSType.toInteger(start), JSType.toInteger(end)); |
|
756 |
} |
|
16147 | 757 |
|
16192 | 758 |
/** |
759 |
* ECMA 15.5.4.13 String.prototype.slice (start, end) specialized for single int parameter |
|
760 |
* |
|
761 |
* @param self self reference |
|
762 |
* @param start start position for slice |
|
763 |
* @return sliced out substring |
|
764 |
*/ |
|
765 |
@SpecializedFunction |
|
766 |
public static Object slice(final Object self, final int start) { |
|
767 |
final String str = checkObjectToString(self); |
|
768 |
final int from = (start < 0) ? Math.max(str.length() + start, 0) : Math.min(start, str.length()); |
|
16147 | 769 |
|
16192 | 770 |
return str.substring(from); |
771 |
} |
|
16147 | 772 |
|
16192 | 773 |
/** |
774 |
* ECMA 15.5.4.13 String.prototype.slice (start, end) specialized for single double parameter |
|
775 |
* |
|
776 |
* @param self self reference |
|
777 |
* @param start start position for slice |
|
778 |
* @return sliced out substring |
|
779 |
*/ |
|
780 |
@SpecializedFunction |
|
781 |
public static Object slice(final Object self, final double start) { |
|
782 |
return slice(self, (int)start); |
|
783 |
} |
|
16147 | 784 |
|
16192 | 785 |
/** |
786 |
* ECMA 15.5.4.13 String.prototype.slice (start, end) specialized for two int parameters |
|
787 |
* |
|
788 |
* @param self self reference |
|
789 |
* @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
|
790 |
* @param end end position for slice |
16192 | 791 |
* @return sliced out substring |
792 |
*/ |
|
793 |
@SpecializedFunction |
|
794 |
public static Object slice(final Object self, final int start, final int end) { |
|
795 |
||
796 |
final String str = checkObjectToString(self); |
|
797 |
final int len = str.length(); |
|
16147 | 798 |
|
16192 | 799 |
final int from = (start < 0) ? Math.max(len + start, 0) : Math.min(start, len); |
800 |
final int to = (end < 0) ? Math.max(len + end, 0) : Math.min(end, len); |
|
801 |
||
802 |
return str.substring(Math.min(from, to), to); |
|
803 |
} |
|
804 |
||
805 |
/** |
|
806 |
* ECMA 15.5.4.13 String.prototype.slice (start, end) specialized for two double parameters |
|
807 |
* |
|
808 |
* @param self self reference |
|
809 |
* @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
|
810 |
* @param end end position for slice |
16192 | 811 |
* @return sliced out substring |
812 |
*/ |
|
813 |
@SpecializedFunction |
|
814 |
public static Object slice(final Object self, final double start, final double end) { |
|
815 |
return slice(self, (int)start, (int)end); |
|
16147 | 816 |
} |
817 |
||
818 |
/** |
|
819 |
* ECMA 15.5.4.14 String.prototype.split (separator, limit) |
|
820 |
* |
|
821 |
* @param self self reference |
|
822 |
* @param separator separator for split |
|
823 |
* @param limit limit for splits |
|
824 |
* @return array object in which splits have been placed |
|
825 |
*/ |
|
826 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
827 |
public static Object split(final Object self, final Object separator, final Object limit) { |
|
828 |
||
16192 | 829 |
final String str = checkObjectToString(self); |
16147 | 830 |
|
831 |
if (separator == UNDEFINED) { |
|
832 |
return new NativeArray(new Object[]{str}); |
|
833 |
} |
|
834 |
||
835 |
final long lim = (limit == UNDEFINED) ? JSType.MAX_UINT : JSType.toUint32(limit); |
|
836 |
||
837 |
if (separator instanceof NativeRegExp) { |
|
838 |
return ((NativeRegExp) separator).split(str, lim); |
|
839 |
} |
|
840 |
||
16192 | 841 |
// when separator is a string, it is treated as a literal search string to be used for splitting. |
842 |
return splitString(str, JSType.toString(separator), lim); |
|
843 |
} |
|
844 |
||
845 |
private static Object splitString(String str, String separator, long limit) { |
|
16201
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
846 |
if (separator.isEmpty()) { |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
847 |
final Object[] array = new Object[str.length()]; |
16192 | 848 |
for (int i = 0; i < array.length; i++) { |
849 |
array[i] = String.valueOf(str.charAt(i)); |
|
850 |
} |
|
851 |
return new NativeArray(array); |
|
852 |
} |
|
853 |
||
854 |
final List<String> elements = new LinkedList<>(); |
|
855 |
final int strLength = str.length(); |
|
856 |
final int sepLength = separator.length(); |
|
857 |
int pos = 0; |
|
16201
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
858 |
int n = 0; |
16192 | 859 |
|
16201
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
860 |
while (pos < strLength && n < limit) { |
16192 | 861 |
int found = str.indexOf(separator, pos); |
862 |
if (found == -1) { |
|
863 |
break; |
|
864 |
} |
|
865 |
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
|
866 |
n++; |
16192 | 867 |
pos = found + sepLength; |
868 |
} |
|
16201
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
869 |
if (pos <= strLength && n < limit) { |
16192 | 870 |
elements.add(str.substring(pos)); |
871 |
} |
|
872 |
||
873 |
return new NativeArray(elements.toArray()); |
|
16147 | 874 |
} |
875 |
||
876 |
/** |
|
877 |
* ECMA B.2.3 String.prototype.substr (start, length) |
|
878 |
* |
|
879 |
* @param self self reference |
|
880 |
* @param start start position |
|
881 |
* @param length length of section |
|
882 |
* @return substring given start and length of section |
|
883 |
*/ |
|
884 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
885 |
public static Object substr(final Object self, final Object start, final Object length) { |
|
886 |
final String str = JSType.toString(self); |
|
887 |
final int strLength = str.length(); |
|
888 |
||
889 |
int intStart = JSType.toInteger(start); |
|
890 |
if (intStart < 0) { |
|
891 |
intStart = Math.max(intStart + strLength, 0); |
|
892 |
} |
|
893 |
||
894 |
final int intLen = Math.min(Math.max((length == UNDEFINED) ? Integer.MAX_VALUE : JSType.toInteger(length), 0), strLength - intStart); |
|
895 |
||
896 |
return intLen <= 0 ? "" : str.substring(intStart, intStart + intLen); |
|
897 |
} |
|
898 |
||
899 |
/** |
|
900 |
* ECMA 15.5.4.15 String.prototype.substring (start, end) |
|
901 |
* |
|
902 |
* @param self self reference |
|
903 |
* @param start start position of substring |
|
904 |
* @param end end position of substring |
|
905 |
* @return substring given start and end indexes |
|
906 |
*/ |
|
907 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
908 |
public static Object substring(final Object self, final Object start, final Object end) { |
|
16192 | 909 |
|
910 |
final String str = checkObjectToString(self); |
|
911 |
if (end == UNDEFINED) { |
|
912 |
return substring(str, JSType.toInteger(start)); |
|
913 |
} |
|
914 |
return substring(str, JSType.toInteger(start), JSType.toInteger(end)); |
|
915 |
} |
|
16147 | 916 |
|
16192 | 917 |
/** |
918 |
* ECMA 15.5.4.15 String.prototype.substring (start, end) specialized for int start parameter |
|
919 |
* |
|
920 |
* @param self self reference |
|
921 |
* @param start start position of substring |
|
922 |
* @return substring given start and end indexes |
|
923 |
*/ |
|
924 |
@SpecializedFunction |
|
925 |
public static String substring(final Object self, final int start) { |
|
926 |
final String str = checkObjectToString(self); |
|
927 |
if (start < 0) { |
|
928 |
return str; |
|
929 |
} else if (start >= str.length()) { |
|
930 |
return ""; |
|
931 |
} else { |
|
932 |
return str.substring(start); |
|
933 |
} |
|
934 |
} |
|
16147 | 935 |
|
16192 | 936 |
/** |
937 |
* ECMA 15.5.4.15 String.prototype.substring (start, end) specialized for double start parameter |
|
938 |
* |
|
939 |
* @param self self reference |
|
940 |
* @param start start position of substring |
|
941 |
* @return substring given start and end indexes |
|
942 |
*/ |
|
943 |
@SpecializedFunction |
|
944 |
public static String substring(final Object self, final double start) { |
|
945 |
return substring(self, (int)start); |
|
946 |
} |
|
16147 | 947 |
|
16192 | 948 |
/** |
949 |
* ECMA 15.5.4.15 String.prototype.substring (start, end) specialized for int start and end parameters |
|
950 |
* |
|
951 |
* @param self self reference |
|
952 |
* @param start start position of substring |
|
953 |
* @param end end position of substring |
|
954 |
* @return substring given start and end indexes |
|
955 |
*/ |
|
956 |
@SpecializedFunction |
|
957 |
public static String substring(final Object self, final int start, final int end) { |
|
958 |
final String str = checkObjectToString(self); |
|
959 |
final int len = str.length(); |
|
960 |
final int validStart = start < 0 ? 0 : (start > len ? len : start); |
|
961 |
final int validEnd = end < 0 ? 0 : (end > len ? len : end); |
|
962 |
||
963 |
if (validStart < validEnd) { |
|
964 |
return str.substring(validStart, validEnd); |
|
16147 | 965 |
} |
16201
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
966 |
return str.substring(validEnd, validStart); |
16192 | 967 |
} |
968 |
||
969 |
/** |
|
970 |
* ECMA 15.5.4.15 String.prototype.substring (start, end) specialized for double start and end parameters |
|
971 |
* |
|
972 |
* @param self self reference |
|
973 |
* @param start start position of substring |
|
974 |
* @param end end position of substring |
|
975 |
* @return substring given start and end indexes |
|
976 |
*/ |
|
977 |
@SpecializedFunction |
|
978 |
public static String substring(final Object self, final double start, final double end) { |
|
979 |
return substring(self, (int)start, (int)end); |
|
16147 | 980 |
} |
981 |
||
982 |
/** |
|
983 |
* ECMA 15.5.4.16 String.prototype.toLowerCase ( ) |
|
984 |
* @param self self reference |
|
985 |
* @return string to lower case |
|
986 |
*/ |
|
987 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
988 |
public static Object toLowerCase(final Object self) { |
|
16192 | 989 |
return checkObjectToString(self).toLowerCase(); |
16147 | 990 |
} |
991 |
||
992 |
/** |
|
993 |
* ECMA 15.5.4.17 String.prototype.toLocaleLowerCase ( ) |
|
994 |
* @param self self reference |
|
995 |
* @return string to locale sensitive lower case |
|
996 |
*/ |
|
997 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
998 |
public static Object toLocaleLowerCase(final Object self) { |
|
16262
75513555e603
8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents:
16258
diff
changeset
|
999 |
return checkObjectToString(self).toLowerCase(Global.getEnv()._locale); |
16147 | 1000 |
} |
1001 |
||
1002 |
/** |
|
1003 |
* ECMA 15.5.4.18 String.prototype.toUpperCase ( ) |
|
1004 |
* @param self self reference |
|
1005 |
* @return string to upper case |
|
1006 |
*/ |
|
1007 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
1008 |
public static Object toUpperCase(final Object self) { |
|
16192 | 1009 |
return checkObjectToString(self).toUpperCase(); |
16147 | 1010 |
} |
1011 |
||
1012 |
/** |
|
1013 |
* ECMA 15.5.4.19 String.prototype.toLocaleUpperCase ( ) |
|
1014 |
* @param self self reference |
|
1015 |
* @return string to locale sensitive upper case |
|
1016 |
*/ |
|
1017 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
1018 |
public static Object toLocaleUpperCase(final Object self) { |
|
16262
75513555e603
8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents:
16258
diff
changeset
|
1019 |
return checkObjectToString(self).toUpperCase(Global.getEnv()._locale); |
16147 | 1020 |
} |
1021 |
||
1022 |
/** |
|
1023 |
* ECMA 15.5.4.20 String.prototype.trim ( ) |
|
1024 |
* @param self self reference |
|
1025 |
* @return string trimmed from whitespace |
|
1026 |
*/ |
|
1027 |
@Function(attributes = Attribute.NOT_ENUMERABLE) |
|
1028 |
public static Object trim(final Object self) { |
|
1029 |
||
16192 | 1030 |
final String str = checkObjectToString(self); |
1031 |
final int len = str.length(); |
|
16147 | 1032 |
int start = 0; |
16192 | 1033 |
int end = len - 1; |
16147 | 1034 |
|
16226
0e4f37e6cc40
8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents:
16203
diff
changeset
|
1035 |
while (start <= end && ScriptRuntime.isJSWhitespace(str.charAt(start))) { |
16147 | 1036 |
start++; |
1037 |
} |
|
16226
0e4f37e6cc40
8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents:
16203
diff
changeset
|
1038 |
while (end > start && ScriptRuntime.isJSWhitespace(str.charAt(end))) { |
16147 | 1039 |
end--; |
1040 |
} |
|
1041 |
||
16192 | 1042 |
return start == 0 && end + 1 == len ? str : str.substring(start, end + 1); |
16147 | 1043 |
} |
1044 |
||
1045 |
private static Object newObj(final Object self, final CharSequence str) { |
|
1046 |
if (self instanceof ScriptObject) { |
|
1047 |
return new NativeString(str, ((ScriptObject)self).getProto()); |
|
1048 |
} |
|
1049 |
return new NativeString(str, Global.instance().getStringPrototype()); |
|
1050 |
} |
|
1051 |
||
1052 |
/** |
|
1053 |
* ECMA 15.5.2.1 new String ( [ value ] ) |
|
1054 |
* |
|
1055 |
* Constructor |
|
1056 |
* |
|
1057 |
* @param newObj is this constructor invoked with the new operator |
|
1058 |
* @param self self reference |
|
1059 |
* @param args arguments (a value) |
|
1060 |
* |
|
1061 |
* @return new NativeString, empty string if no args, extraneous args ignored |
|
1062 |
*/ |
|
1063 |
@Constructor(arity = 1) |
|
1064 |
public static Object constructor(final boolean newObj, final Object self, final Object... args) { |
|
1065 |
final CharSequence str = (args.length > 0) ? JSType.toCharSequence(args[0]) : ""; |
|
1066 |
return newObj ? newObj(self, str) : str.toString(); |
|
1067 |
} |
|
1068 |
||
1069 |
/** |
|
1070 |
* ECMA 15.5.2.1 new String ( [ value ] ) - special version with no args |
|
1071 |
* |
|
1072 |
* Constructor |
|
1073 |
* |
|
1074 |
* @param newObj is this constructor invoked with the new operator |
|
1075 |
* @param self self reference |
|
1076 |
* |
|
1077 |
* @return new NativeString ("") |
|
1078 |
*/ |
|
1079 |
@SpecializedConstructor |
|
1080 |
public static Object constructor(final boolean newObj, final Object self) { |
|
1081 |
return newObj ? newObj(self, "") : ""; |
|
1082 |
} |
|
1083 |
||
16192 | 1084 |
/** |
1085 |
* ECMA 15.5.2.1 new String ( [ value ] ) - special version with one arg |
|
1086 |
* |
|
1087 |
* Constructor |
|
1088 |
* |
|
1089 |
* @param newObj is this constructor invoked with the new operator |
|
1090 |
* @param self self reference |
|
1091 |
* @param arg argument |
|
1092 |
* |
|
1093 |
* @return new NativeString (arg) |
|
1094 |
*/ |
|
1095 |
@SpecializedConstructor |
|
1096 |
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
|
1097 |
final CharSequence str = JSType.toCharSequence(arg); |
5c15a03ec3ea
8007109: Regression: String(ConsString) does not flatten argument to String
hannesw
parents:
16201
diff
changeset
|
1098 |
return newObj ? newObj(self, str) : str.toString(); |
16192 | 1099 |
} |
16147 | 1100 |
|
1101 |
/** |
|
1102 |
* ECMA 15.5.2.1 new String ( [ value ] ) - special version with exactly one {@code int} arg |
|
1103 |
* |
|
1104 |
* Constructor |
|
1105 |
* |
|
1106 |
* @param newObj is this constructor invoked with the new operator |
|
1107 |
* @param self self reference |
|
1108 |
* @param arg the arg |
|
1109 |
* |
|
1110 |
* @return new NativeString containing the string representation of the arg |
|
1111 |
*/ |
|
1112 |
@SpecializedConstructor |
|
1113 |
public static Object constructor(final boolean newObj, final Object self, final int arg) { |
|
16203
5c15a03ec3ea
8007109: Regression: String(ConsString) does not flatten argument to String
hannesw
parents:
16201
diff
changeset
|
1114 |
final String str = JSType.toString(arg); |
16147 | 1115 |
return newObj ? newObj(self, str) : str; |
1116 |
} |
|
1117 |
||
1118 |
/** |
|
1119 |
* Lookup the appropriate method for an invoke dynamic call. |
|
1120 |
* |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
1121 |
* @param request the link request |
16147 | 1122 |
* @param receiver receiver of call |
1123 |
* @return Link to be invoked at call site. |
|
1124 |
*/ |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
1125 |
public static GuardedInvocation lookupPrimitive(final LinkRequest request, final Object receiver) { |
16147 | 1126 |
final MethodHandle guard = NashornGuards.getInstanceOf2Guard(String.class, ConsString.class); |
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16192
diff
changeset
|
1127 |
return PrimitiveLookup.lookupPrimitive(request, guard, new NativeString((CharSequence)receiver), WRAPFILTER); |
16147 | 1128 |
} |
1129 |
||
1130 |
@SuppressWarnings("unused") |
|
1131 |
private static NativeString wrapFilter(final Object receiver) { |
|
1132 |
return new NativeString((CharSequence)receiver); |
|
1133 |
} |
|
1134 |
||
1135 |
private static CharSequence getCharSequence(final Object self) { |
|
1136 |
if (self instanceof String || self instanceof ConsString) { |
|
1137 |
return (CharSequence)self; |
|
1138 |
} else if (self instanceof NativeString) { |
|
1139 |
return ((NativeString)self).getValue(); |
|
1140 |
} else if (self != null && self == Global.instance().getStringPrototype()) { |
|
1141 |
return ""; |
|
1142 |
} else { |
|
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16234
diff
changeset
|
1143 |
throw typeError("not.a.string", ScriptRuntime.safeToString(self)); |
16147 | 1144 |
} |
1145 |
} |
|
1146 |
||
1147 |
private static String getString(final Object self) { |
|
1148 |
if (self instanceof String) { |
|
1149 |
return (String)self; |
|
1150 |
} else if (self instanceof ConsString) { |
|
1151 |
return self.toString(); |
|
1152 |
} else if (self instanceof NativeString) { |
|
1153 |
return ((NativeString)self).getStringValue(); |
|
1154 |
} else if (self != null && self == Global.instance().getStringPrototype()) { |
|
1155 |
return ""; |
|
1156 |
} else { |
|
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16234
diff
changeset
|
1157 |
throw typeError( "not.a.string", ScriptRuntime.safeToString(self)); |
16147 | 1158 |
} |
1159 |
} |
|
1160 |
||
16192 | 1161 |
/** |
1162 |
* Combines ECMA 9.10 CheckObjectCoercible and ECMA 9.8 ToString with a shortcut for strings. |
|
1163 |
* |
|
1164 |
* @param self the object |
|
1165 |
* @return the object as string |
|
1166 |
*/ |
|
1167 |
private static String checkObjectToString(final Object self) { |
|
1168 |
if (self instanceof String) { |
|
1169 |
return (String)self; |
|
1170 |
} else if (self instanceof ConsString) { |
|
1171 |
return self.toString(); |
|
1172 |
} else { |
|
1173 |
Global.checkObjectCoercible(self); |
|
1174 |
return JSType.toString(self); |
|
1175 |
} |
|
1176 |
} |
|
1177 |
||
16147 | 1178 |
private boolean isValid(final int key) { |
1179 |
return key >= 0 && key < value.length(); |
|
1180 |
} |
|
1181 |
||
1182 |
private static MethodHandle findWrapFilter() { |
|
1183 |
try { |
|
1184 |
return MethodHandles.lookup().findStatic(NativeString.class, "wrapFilter", MH.type(NativeString.class, Object.class)); |
|
1185 |
} catch (final NoSuchMethodException | IllegalAccessException e) { |
|
16181
f6a125580f62
8006562: findOwnMH in nashorn "objects" package should be cleaned up
sundar
parents:
16151
diff
changeset
|
1186 |
throw new MethodHandleFactory.LookupException(e); |
16147 | 1187 |
} |
1188 |
} |
|
1189 |
} |