src/jdk/nashorn/internal/runtime/GlobalFunctions.java

Print this page




  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.runtime;
  27 
  28 import static jdk.nashorn.internal.runtime.JSType.digit;
  29 import static jdk.nashorn.internal.lookup.Lookup.MH;
  30 
  31 import java.lang.invoke.MethodHandle;
  32 import java.lang.invoke.MethodHandles;

  33 
  34 /**
  35  * Utilities used by Global class.
  36  *
  37  * These are actual implementation methods for functions exposed by global
  38  * scope. The code lives here to share the code across the contexts.
  39  */
  40 public final class GlobalFunctions {
  41 
  42     /** Methodhandle to implementation of ECMA 15.1.2.2, parseInt */
  43     public static final MethodHandle PARSEINT = findOwnMH("parseInt",   double.class, Object.class, Object.class, Object.class);
  44 
  45     /** Methodhandle to implementation of ECMA 15.1.2.3, parseFloat */
  46     public static final MethodHandle PARSEFLOAT = findOwnMH("parseFloat", double.class, Object.class, Object.class);
  47 
  48     /** Methodhandle to implementation of ECMA 15.1.2.4, isNaN */
  49     public static final MethodHandle IS_NAN = findOwnMH("isNaN",      boolean.class, Object.class, Object.class);
  50 
  51     /** Methodhandle to implementation of ECMA 15.1.2.5, isFinite */
  52     public static final MethodHandle IS_FINITE = findOwnMH("isFinite",   boolean.class, Object.class, Object.class);


 356      * @param string  string to escape
 357      *
 358      * @return escaped string
 359      */
 360     public static String escape(final Object self, final Object string) {
 361         final String str = JSType.toString(string);
 362         final int length = str.length();
 363 
 364         if (length == 0) {
 365             return str;
 366         }
 367 
 368         final StringBuilder sb = new StringBuilder();
 369         for (int k = 0; k < length; k++) {
 370             final char ch = str.charAt(k);
 371             if (UNESCAPED.indexOf(ch) != -1) {
 372                 sb.append(ch);
 373             } else if (ch < 256) {
 374                 sb.append('%');
 375                 final byte b = (byte)ch;
 376                 sb.append(Integer.toHexString(b & 0xFF).toUpperCase());
 377             } else {
 378                 sb.append("%u");
 379                 sb.append(Integer.toHexString(ch & 0xFFFF).toUpperCase());
 380             }
 381         }
 382 
 383         return sb.toString();
 384     }
 385 
 386     /**
 387      * ECMA B.2.2, unescape implementation
 388      *
 389      * @param self    self reference
 390      * @param string  string to unescape
 391      *
 392      * @return unescaped string
 393      */
 394     public static String unescape(final Object self, final Object string) {
 395         final String str    = JSType.toString(string);
 396         final int    length = str.length();
 397 
 398         if (length == 0) {
 399             return str;




  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.runtime;
  27 
  28 import static jdk.nashorn.internal.runtime.JSType.digit;
  29 import static jdk.nashorn.internal.lookup.Lookup.MH;
  30 
  31 import java.lang.invoke.MethodHandle;
  32 import java.lang.invoke.MethodHandles;
  33 import java.util.Locale;
  34 
  35 /**
  36  * Utilities used by Global class.
  37  *
  38  * These are actual implementation methods for functions exposed by global
  39  * scope. The code lives here to share the code across the contexts.
  40  */
  41 public final class GlobalFunctions {
  42 
  43     /** Methodhandle to implementation of ECMA 15.1.2.2, parseInt */
  44     public static final MethodHandle PARSEINT = findOwnMH("parseInt",   double.class, Object.class, Object.class, Object.class);
  45 
  46     /** Methodhandle to implementation of ECMA 15.1.2.3, parseFloat */
  47     public static final MethodHandle PARSEFLOAT = findOwnMH("parseFloat", double.class, Object.class, Object.class);
  48 
  49     /** Methodhandle to implementation of ECMA 15.1.2.4, isNaN */
  50     public static final MethodHandle IS_NAN = findOwnMH("isNaN",      boolean.class, Object.class, Object.class);
  51 
  52     /** Methodhandle to implementation of ECMA 15.1.2.5, isFinite */
  53     public static final MethodHandle IS_FINITE = findOwnMH("isFinite",   boolean.class, Object.class, Object.class);


 357      * @param string  string to escape
 358      *
 359      * @return escaped string
 360      */
 361     public static String escape(final Object self, final Object string) {
 362         final String str = JSType.toString(string);
 363         final int length = str.length();
 364 
 365         if (length == 0) {
 366             return str;
 367         }
 368 
 369         final StringBuilder sb = new StringBuilder();
 370         for (int k = 0; k < length; k++) {
 371             final char ch = str.charAt(k);
 372             if (UNESCAPED.indexOf(ch) != -1) {
 373                 sb.append(ch);
 374             } else if (ch < 256) {
 375                 sb.append('%');
 376                 final byte b = (byte)ch;
 377                 sb.append(Integer.toHexString(b & 0xFF).toUpperCase(Locale.ENGLISH));
 378             } else {
 379                 sb.append("%u");
 380                 sb.append(Integer.toHexString(ch & 0xFFFF).toUpperCase(Locale.ENGLISH));
 381             }
 382         }
 383 
 384         return sb.toString();
 385     }
 386 
 387     /**
 388      * ECMA B.2.2, unescape implementation
 389      *
 390      * @param self    self reference
 391      * @param string  string to unescape
 392      *
 393      * @return unescaped string
 394      */
 395     public static String unescape(final Object self, final Object string) {
 396         final String str    = JSType.toString(string);
 397         final int    length = str.length();
 398 
 399         if (length == 0) {
 400             return str;