src/jdk/nashorn/internal/objects/NativeString.java

Print this page




  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.lookup.Lookup.MH;
  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;
  32 
  33 import java.lang.invoke.MethodHandle;
  34 import java.lang.invoke.MethodHandles;
  35 import java.lang.invoke.MethodType;
  36 import java.text.Collator;
  37 import java.util.ArrayList;
  38 import java.util.Arrays;
  39 import java.util.LinkedList;
  40 import java.util.List;
  41 import java.util.Locale;

  42 import jdk.internal.dynalink.CallSiteDescriptor;
  43 import jdk.internal.dynalink.linker.GuardedInvocation;
  44 import jdk.internal.dynalink.linker.LinkRequest;
  45 import jdk.nashorn.internal.lookup.MethodHandleFactory.LookupException;
  46 import jdk.nashorn.internal.objects.annotations.Attribute;
  47 import jdk.nashorn.internal.objects.annotations.Constructor;
  48 import jdk.nashorn.internal.objects.annotations.Function;
  49 import jdk.nashorn.internal.objects.annotations.Getter;
  50 import jdk.nashorn.internal.objects.annotations.ScriptClass;
  51 import jdk.nashorn.internal.objects.annotations.SpecializedConstructor;
  52 import jdk.nashorn.internal.objects.annotations.SpecializedFunction;
  53 import jdk.nashorn.internal.objects.annotations.Where;
  54 import jdk.nashorn.internal.runtime.ConsString;
  55 import jdk.nashorn.internal.runtime.JSType;
  56 import jdk.nashorn.internal.runtime.PropertyMap;
  57 import jdk.nashorn.internal.runtime.ScriptFunction;
  58 import jdk.nashorn.internal.runtime.ScriptObject;
  59 import jdk.nashorn.internal.runtime.ScriptRuntime;
  60 import jdk.nashorn.internal.runtime.arrays.ArrayIndex;
  61 import jdk.nashorn.internal.runtime.linker.NashornGuards;


 374             return true;
 375         }
 376 
 377         return false;
 378     }
 379 
 380     @Override
 381     public Object getOwnPropertyDescriptor(final String key) {
 382         final int index = ArrayIndex.getArrayIndex(key);
 383         if (index >= 0 && index < value.length()) {
 384             final Global global = Global.instance();
 385             return global.newDataDescriptor(String.valueOf(value.charAt(index)), false, true, false);
 386         }
 387 
 388         return super.getOwnPropertyDescriptor(key);
 389     }
 390 
 391     /**
 392      * return a List of own keys associated with the object.
 393      * @param all True if to include non-enumerable keys.


 394      * @return Array of keys.
 395      */
 396     @Override
 397     public String[] getOwnKeys(final boolean all) {
 398         final List<Object> keys = new ArrayList<>();
 399 
 400         // add string index keys
 401         for (int i = 0; i < value.length(); i++) {
 402             keys.add(JSType.toString(i));
 403         }
 404 
 405         // add super class properties
 406         keys.addAll(Arrays.asList(super.getOwnKeys(all)));
 407         return keys.toArray(new String[keys.size()]);
 408     }
 409 
 410     /**
 411      * ECMA 15.5.3 String.length
 412      * @param self self reference
 413      * @return     value of length property for string
 414      */
 415     @Getter(attributes = Attribute.NOT_ENUMERABLE | Attribute.NOT_WRITABLE | Attribute.NOT_CONFIGURABLE)
 416     public static Object length(final Object self) {
 417         return getCharSequence(self).length();
 418     }
 419 
 420     /**
 421      * ECMA 15.5.3.2 String.fromCharCode ( [ char0 [ , char1 [ , ... ] ] ] )
 422      * @param self  self reference
 423      * @param args  array of arguments to be interpreted as char
 424      * @return string with arguments translated to charcodes
 425      */
 426     @Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1, where = Where.CONSTRUCTOR)




  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.lookup.Lookup.MH;
  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;
  32 
  33 import java.lang.invoke.MethodHandle;
  34 import java.lang.invoke.MethodHandles;
  35 import java.lang.invoke.MethodType;
  36 import java.text.Collator;
  37 import java.util.ArrayList;
  38 import java.util.Arrays;
  39 import java.util.LinkedList;
  40 import java.util.List;
  41 import java.util.Locale;
  42 import java.util.Set;
  43 import jdk.internal.dynalink.CallSiteDescriptor;
  44 import jdk.internal.dynalink.linker.GuardedInvocation;
  45 import jdk.internal.dynalink.linker.LinkRequest;
  46 import jdk.nashorn.internal.lookup.MethodHandleFactory.LookupException;
  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.SpecializedConstructor;
  53 import jdk.nashorn.internal.objects.annotations.SpecializedFunction;
  54 import jdk.nashorn.internal.objects.annotations.Where;
  55 import jdk.nashorn.internal.runtime.ConsString;
  56 import jdk.nashorn.internal.runtime.JSType;
  57 import jdk.nashorn.internal.runtime.PropertyMap;
  58 import jdk.nashorn.internal.runtime.ScriptFunction;
  59 import jdk.nashorn.internal.runtime.ScriptObject;
  60 import jdk.nashorn.internal.runtime.ScriptRuntime;
  61 import jdk.nashorn.internal.runtime.arrays.ArrayIndex;
  62 import jdk.nashorn.internal.runtime.linker.NashornGuards;


 375             return true;
 376         }
 377 
 378         return false;
 379     }
 380 
 381     @Override
 382     public Object getOwnPropertyDescriptor(final String key) {
 383         final int index = ArrayIndex.getArrayIndex(key);
 384         if (index >= 0 && index < value.length()) {
 385             final Global global = Global.instance();
 386             return global.newDataDescriptor(String.valueOf(value.charAt(index)), false, true, false);
 387         }
 388 
 389         return super.getOwnPropertyDescriptor(key);
 390     }
 391 
 392     /**
 393      * return a List of own keys associated with the object.
 394      * @param all True if to include non-enumerable keys.
 395      * @param nonEnumerable set of non-enumerable properties seen already.Used
 396      * to filter out shadowed, but enumerable properties from proto children.
 397      * @return Array of keys.
 398      */
 399     @Override
 400     protected String[] getOwnKeys(final boolean all, final Set<String> nonEnumerable) {
 401         final List<Object> keys = new ArrayList<>();
 402 
 403         // add string index keys
 404         for (int i = 0; i < value.length(); i++) {
 405             keys.add(JSType.toString(i));
 406         }
 407 
 408         // add super class properties
 409         keys.addAll(Arrays.asList(super.getOwnKeys(all, nonEnumerable)));
 410         return keys.toArray(new String[keys.size()]);
 411     }
 412 
 413     /**
 414      * ECMA 15.5.3 String.length
 415      * @param self self reference
 416      * @return     value of length property for string
 417      */
 418     @Getter(attributes = Attribute.NOT_ENUMERABLE | Attribute.NOT_WRITABLE | Attribute.NOT_CONFIGURABLE)
 419     public static Object length(final Object self) {
 420         return getCharSequence(self).length();
 421     }
 422 
 423     /**
 424      * ECMA 15.5.3.2 String.fromCharCode ( [ char0 [ , char1 [ , ... ] ] ] )
 425      * @param self  self reference
 426      * @param args  array of arguments to be interpreted as char
 427      * @return string with arguments translated to charcodes
 428      */
 429     @Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1, where = Where.CONSTRUCTOR)