aboutsummaryrefslogtreecommitdiff
path: root/src/jdk/nashorn/internal/runtime/GlobalFunctions.java
blob: 4483885c350293311f53c0fbc61d6dd4c7d97c4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/*
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package jdk.nashorn.internal.runtime;

import static jdk.nashorn.internal.lookup.Lookup.MH;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.util.Locale;

/**
 * Utilities used by Global class.
 */
public final class GlobalFunctions {

    /** Methodhandle to implementation of ECMA 15.1.2.2, parseInt */
    public static final MethodHandle PARSEINT = findOwnMH("parseInt", double.class, Object.class, Object.class, Object.class);

    /** Methodhandle (specialized) to implementation of ECMA 15.1.2.2, parseInt */
    public static final MethodHandle PARSEINT_OI = findOwnMH("parseInt", double.class, Object.class, Object.class, int.class);

    /** ParseInt - NaN for booleans (thru string conversion to number conversion) */
    public static final MethodHandle PARSEINT_Z = MH.dropArguments(MH.dropArguments(MH.constant(double.class, Double.NaN), 0, boolean.class), 0, Object.class);

    /** ParseInt - identity for ints */
    public static final MethodHandle PARSEINT_I = MH.dropArguments(MH.identity(int.class), 0, Object.class);

    /** Methodhandle (specialized) to implementation of ECMA 15.1.2.2, parseInt */
    public static final MethodHandle PARSEINT_O = findOwnMH("parseInt", double.class, Object.class, Object.class);

    /** Methodhandle to implementation of ECMA 15.1.2.3, parseFloat */
    public static final MethodHandle PARSEFLOAT = findOwnMH("parseFloat", double.class, Object.class, Object.class);

    /** isNan for integers - always false */
    public static final MethodHandle IS_NAN_I = MH.dropArguments(MH.constant(boolean.class, false), 0, Object.class);

    /** isNan for longs - always false */
    public static final MethodHandle IS_NAN_J = MH.dropArguments(MH.constant(boolean.class, false), 0, Object.class);

    /** IsNan for doubles - use Double.isNaN */
    public static final MethodHandle IS_NAN_D = MH.dropArguments(MH.findStatic(MethodHandles.lookup(), Double.class, "isNaN", MH.type(boolean.class, double.class)), 0, Object.class);

    /** Methodhandle to implementation of ECMA 15.1.2.4, isNaN */
    public static final MethodHandle IS_NAN = findOwnMH("isNaN",      boolean.class, Object.class, Object.class);

    /** Methodhandle to implementation of ECMA 15.1.2.5, isFinite */
    public static final MethodHandle IS_FINITE = findOwnMH("isFinite",   boolean.class, Object.class, Object.class);

    /** Methodhandle to implementation of ECMA 15.1.3.3, encodeURI */
    public static final MethodHandle ENCODE_URI = findOwnMH("encodeURI",  Object.class, Object.class, Object.class);

    /** Methodhandle to implementation of ECMA 15.1.3.4, encodeURIComponent */
    public static final MethodHandle ENCODE_URICOMPONENT = findOwnMH("encodeURIComponent", Object.class, Object.class, Object.class);

    /** Methodhandle to implementation of ECMA 15.1.3.1, decodeURI */
    public static final MethodHandle DECODE_URI = findOwnMH("decodeURI", Object.class, Object.class, Object.class);

    /** Methodhandle to implementation of ECMA 15.1.3.2, decodeURIComponent */
    public static final MethodHandle DECODE_URICOMPONENT = findOwnMH("decodeURIComponent", Object.class, Object.class, Object.class);

    /** Methodhandle to implementation of ECMA B.2.1, escape */
    public static final MethodHandle ESCAPE = findOwnMH("escape",    String.class, Object.class, Object.class);

    /** Methodhandle to implementation of ECMA B.2.2, unescape */
    public static final MethodHandle UNESCAPE = findOwnMH("unescape",  String.class, Object.class, Object.class);

    /** Methodhandle to implementation of ECMA 15.3.4, "anonymous" - Properties of the Function Prototype Object. */
    public static final MethodHandle ANONYMOUS = findOwnMH("anonymous", Object.class, Object.class);

    private static final String UNESCAPED = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@*_+-./";

    private GlobalFunctions() {
    }

    /**
     * ECMA 15.1.2.2 parseInt implementation
     *
     * @param self   self reference
     * @param string string to parse
     * @param rad    radix
     *
     * @return numeric type representing string contents as an int
     */
    public static double parseInt(final Object self, final Object string, final Object rad) {
        return parseIntInternal(JSType.trimLeft(JSType.toString(string)), JSType.toInt32(rad));
    }

    /**
     * ECMA 15.1.2.2 parseInt implementation specialized for int radix
     *
     * @param self   self reference
     * @param string string to parse
     * @param rad    radix
     *
     * @return numeric type representing string contents as an int
     */
    public static double parseInt(final Object self, final Object string, final int rad) {
        return parseIntInternal(JSType.trimLeft(JSType.toString(string)), rad);
    }

    /**
     * ECMA 15.1.2.2 parseInt implementation specialized for no radix argument
     *
     * @param self   self reference
     * @param string string to parse
     *
     * @return numeric type representing string contents as an int
     */
    public static double parseInt(final Object self, final Object string) {
        return parseIntInternal(JSType.trimLeft(JSType.toString(string)), 0);
    }

    private static double parseIntInternal(final String str, final int rad) {
        final int length = str.length();
        int radix = rad;

        // empty string is not valid
        if (length == 0) {
            return Double.NaN;
        }

        boolean negative = false;
        int idx = 0;

        // checking for the sign character
        final char firstChar = str.charAt(idx);
        if (firstChar < '0') {
            // Possible leading "+" or "-"
            if (firstChar == '-') {
                negative = true;
            } else if (firstChar != '+') {
                return Double.NaN;
            }
            // skip the sign character
            idx++;
        }

        boolean stripPrefix = true;

        if (radix != 0) {
            if (radix < 2 || radix > 36) {
                return Double.NaN;
            }
            if (radix != 16) {
                stripPrefix = false;
            }
        } else {
            // default radix
            radix = 10;
        }
        // strip "0x" or "0X" and treat radix as 16
        if (stripPrefix && ((idx + 1) < length)) {
            final char c1 = str.charAt(idx);
            final char c2 = str.charAt(idx + 1);
            if (c1 == '0' && (c2 == 'x' || c2 == 'X')) {
                radix = 16;
                // skip "0x" or "0X"
                idx += 2;
            }
        }

        double result = 0.0;
        int digit;
        // we should see at least one valid digit
        boolean entered = false;
        while (idx < length) {
            digit = fastDigit(str.charAt(idx++), radix);
            if (digit < 0) {
                break;
            }
            // we have seen at least one valid digit in the specified radix
            entered = true;
            result *= radix;
            result += digit;
        }

        return entered ? (negative ? -result : result) : Double.NaN;
    }

    /**
     * ECMA 15.1.2.3 parseFloat implementation
     *
     * @param self   self reference
     * @param string string to parse
     *
     * @return numeric type representing string contents
     */
    public static double parseFloat(final Object self, final Object string) {
        final String str    = JSType.trimLeft(JSType.toString(string));
        final int    length = str.length();

        // empty string is not valid
        if (length == 0) {
            return Double.NaN;
        }

        int     start    = 0;
        boolean negative = false;
        char    ch       = str.charAt(0);

        if (ch == '-') {
            start++;
            negative = true;
        } else if (ch == '+') {
            start++;
        } else if (ch == 'N') {
            if (str.startsWith("NaN")) {
                return Double.NaN;
            }
        }

        if (start == length) {
            // just the sign character
            return Double.NaN;
        }

        ch = str.charAt(start);
        if (ch == 'I') {
            if (str.substring(start).startsWith("Infinity")) {
                return negative? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY;
            }
        }

        boolean dotSeen    = false;
        boolean exponentOk = false;
        int exponentOffset = -1;
        int end;

loop:
        for (end = start; end < length; end++) {
            ch = str.charAt(end);

            switch (ch) {
            case '.':
                // dot allowed only once
                if (exponentOffset != -1 || dotSeen) {
                    break loop;
                }
                dotSeen = true;
                break;

            case 'e':
            case 'E':
                // 'e'/'E' allow only once
                if (exponentOffset != -1) {
                    break loop;
                }
                exponentOffset = end;
                break;

            case '+':
            case '-':
                // Sign of the exponent. But allowed only if the
                // previous char in the string was 'e' or 'E'.
                if (exponentOffset != end - 1) {
                    break loop;
                }
                break;

            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
                if (exponentOffset != -1) {
                    // seeing digit after 'e' or 'E'
                    exponentOk = true;
                }
                break;

            default: // ignore garbage at the end
                break loop;
            }
        }

        // ignore 'e'/'E' followed by '+/-' if not real exponent found
        if (exponentOffset != -1 && !exponentOk) {
            end = exponentOffset;
        }

        if (start == end) {
            return Double.NaN;
        }

        try {
            final double result = Double.valueOf(str.substring(start, end));
            return negative ? -result : result;
        } catch (final NumberFormatException e) {
            return Double.NaN;
        }
    }

    /**
     * ECMA 15.1.2.4, isNaN implementation
     *
     * @param self    self reference
     * @param number  number to check
     *
     * @return true if number is NaN
     */
    public static boolean isNaN(final Object self, final Object number) {
        return Double.isNaN(JSType.toNumber(number));
    }

    /**
     * ECMA 15.1.2.5, isFinite implementation
     *
     * @param self   self reference
     * @param number number to check
     *
     * @return true if number is infinite
     */
    public static boolean isFinite(final Object self, final Object number) {
        final double value = JSType.toNumber(number);
        return ! (Double.isInfinite(value) || Double.isNaN(value));
    }


    /**
     * ECMA 15.1.3.3, encodeURI implementation
     *
     * @param self  self reference
     * @param uri   URI to encode
     *
     * @return encoded URI
     */
    public static Object encodeURI(final Object self, final Object uri) {
        return URIUtils.encodeURI(self, JSType.toString(uri));
    }

    /**
     * ECMA 15.1.3.4, encodeURIComponent implementation
     *
     * @param self  self reference
     * @param uri   URI component to encode
     *
     * @return encoded URIComponent
     */
    public static Object encodeURIComponent(final Object self, final Object uri) {
        return URIUtils.encodeURIComponent(self, JSType.toString(uri));
    }

    /**
     * ECMA 15.1.3.1, decodeURI implementation
     *
     * @param self  self reference
     * @param uri   URI to decode
     *
     * @return decoded URI
     */
    public static Object decodeURI(final Object self, final Object uri) {
        return URIUtils.decodeURI(self, JSType.toString(uri));
    }

    /**
     * ECMA 15.1.3.2, decodeURIComponent implementation
     *
     * @param self  self reference
     * @param uri   URI component to encode
     *
     * @return decoded URI
     */
    public static Object decodeURIComponent(final Object self, final Object uri) {
        return URIUtils.decodeURIComponent(self, JSType.toString(uri));
    }

    /**
     * ECMA B.2.1, escape implementation
     *
     * @param self    self reference
     * @param string  string to escape
     *
     * @return escaped string
     */
    public static String escape(final Object self, final Object string) {
        final String str = JSType.toString(string);
        final int length = str.length();

        if (length == 0) {
            return str;
        }

        final StringBuilder sb = new StringBuilder();
        for (int k = 0; k < length; k++) {
            final char ch = str.charAt(k);
            if (UNESCAPED.indexOf(ch) != -1) {
                sb.append(ch);
            } else if (ch < 256) {
                sb.append('%');
                if (ch < 16) {
                    sb.append('0');
                }
                sb.append(Integer.toHexString(ch).toUpperCase(Locale.ENGLISH));
            } else {
                sb.append("%u");
                if (ch < 4096) {
                    sb.append('0');
                }
                sb.append(Integer.toHexString(ch).toUpperCase(Locale.ENGLISH));
            }
        }

        return sb.toString();
    }

    /**
     * ECMA B.2.2, unescape implementation
     *
     * @param self    self reference
     * @param string  string to unescape
     *
     * @return unescaped string
     */
    public static String unescape(final Object self, final Object string) {
        final String str    = JSType.toString(string);
        final int    length = str.length();

        if (length == 0) {
            return str;
        }

        final StringBuilder sb = new StringBuilder();
        for (int k = 0; k < length; k++) {
            char ch = str.charAt(k);
            if (ch != '%') {
                sb.append(ch);
            } else {
                if (k < (length - 5)) {
                   if (str.charAt(k + 1) == 'u') {
                       try {
                           ch = (char) Integer.parseInt(str.substring(k + 2, k + 6), 16);
                           sb.append(ch);
                           k += 5;
                           continue;
                       } catch (final NumberFormatException e) {
                           //ignored
                       }
                   }
                }

                if (k < (length - 2)) {
                    try {
                        ch = (char) Integer.parseInt(str.substring(k + 1, k + 3), 16);
                        sb.append(ch);
                        k += 2;
                        continue;
                    } catch (final NumberFormatException e) {
                        //ignored
                    }
                }

                // everything fails
                sb.append(ch);
            }
        }

        return sb.toString();
    }


    /**
     * ECMA 15.3.4 Properties of the Function Prototype Object.
     * The Function prototype object is itself a Function object
     * (its [[Class]] is "Function") that, when invoked, accepts
     * any arguments and returns undefined. This method is used to
     * implement that anonymous function.
     *
     * @param self  self reference
     *
     * @return undefined
     */
    public static Object anonymous(final Object self) {
        return ScriptRuntime.UNDEFINED;
    }

    private static int fastDigit(final int ch, final int radix) {
        int n = -1;
        if (ch >= '0' && ch <= '9') {
            n = ch - '0';
        } else if (radix > 10) {
            if (ch >= 'a' && ch <= 'z') {
                n = ch - 'a' + 10;
            } else if (ch >= 'A' && ch <= 'Z') {
                n = ch - 'A' + 10;
            }
        }
        return n < radix ? n : -1;
    }

    private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) {
        return MH.findStatic(MethodHandles.lookup(), GlobalFunctions.class, name, MH.type(rtype, types));
    }
}