44 import java.util.concurrent.Callable;
45 import jdk.dynalink.CallSiteDescriptor;
46 import jdk.dynalink.linker.GuardedInvocation;
47 import jdk.dynalink.linker.LinkRequest;
48 import jdk.nashorn.api.scripting.JSObject;
49 import jdk.nashorn.internal.objects.annotations.Attribute;
50 import jdk.nashorn.internal.objects.annotations.Constructor;
51 import jdk.nashorn.internal.objects.annotations.Function;
52 import jdk.nashorn.internal.objects.annotations.Getter;
53 import jdk.nashorn.internal.objects.annotations.ScriptClass;
54 import jdk.nashorn.internal.objects.annotations.Setter;
55 import jdk.nashorn.internal.objects.annotations.SpecializedFunction;
56 import jdk.nashorn.internal.objects.annotations.SpecializedFunction.LinkLogic;
57 import jdk.nashorn.internal.objects.annotations.Where;
58 import jdk.nashorn.internal.runtime.Context;
59 import jdk.nashorn.internal.runtime.Debug;
60 import jdk.nashorn.internal.runtime.JSType;
61 import jdk.nashorn.internal.runtime.OptimisticBuiltins;
62 import jdk.nashorn.internal.runtime.PropertyDescriptor;
63 import jdk.nashorn.internal.runtime.PropertyMap;
64 import jdk.nashorn.internal.runtime.ScriptFunction;
65 import jdk.nashorn.internal.runtime.ScriptObject;
66 import jdk.nashorn.internal.runtime.ScriptRuntime;
67 import jdk.nashorn.internal.runtime.Undefined;
68 import jdk.nashorn.internal.runtime.arrays.ArrayData;
69 import jdk.nashorn.internal.runtime.arrays.ArrayIndex;
70 import jdk.nashorn.internal.runtime.arrays.ArrayLikeIterator;
71 import jdk.nashorn.internal.runtime.arrays.ContinuousArrayData;
72 import jdk.nashorn.internal.runtime.arrays.IntElements;
73 import jdk.nashorn.internal.runtime.arrays.IteratorAction;
74 import jdk.nashorn.internal.runtime.arrays.NumericElements;
75 import jdk.nashorn.internal.runtime.linker.Bootstrap;
76 import jdk.nashorn.internal.runtime.linker.InvokeByName;
77
78 /**
79 * Runtime representation of a JavaScript array. NativeArray only holds numeric
80 * keyed values. All other values are stored in spill.
81 */
82 @ScriptClass("Array")
83 public final class NativeArray extends ScriptObject implements OptimisticBuiltins {
84 private static final Object JOIN = new Object();
731 * @return the new NativeArray
732 */
733 @SpecializedFunction(isConstructor=true)
734 public static NativeArray construct(final boolean newObj, final Object self, final double length) {
735 final long uint32length = JSType.toUint32(length);
736
737 if (uint32length == length) {
738 return new NativeArray(uint32length);
739 }
740
741 return construct(newObj, self, new Object[]{length});
742 }
743
744 /**
745 * ECMA 15.4.4.4 Array.prototype.concat ( [ item1 [ , item2 [ , ... ] ] ] )
746 *
747 * @param self self reference
748 * @param arg argument
749 * @return resulting NativeArray
750 */
751 @SpecializedFunction(linkLogic=ConcatLinkLogic.class)
752 public static NativeArray concat(final Object self, final int arg) {
753 final ContinuousArrayData newData = getContinuousArrayDataCCE(self, Integer.class).copy(); //get at least an integer data copy of this data
754 newData.fastPush(arg); //add an integer to its end
755 return new NativeArray(newData);
756 }
757
758 /**
759 * ECMA 15.4.4.4 Array.prototype.concat ( [ item1 [ , item2 [ , ... ] ] ] )
760 *
761 * @param self self reference
762 * @param arg argument
763 * @return resulting NativeArray
764 */
765 @SpecializedFunction(linkLogic=ConcatLinkLogic.class)
766 public static NativeArray concat(final Object self, final long arg) {
767 final ContinuousArrayData newData = getContinuousArrayDataCCE(self, Long.class).copy(); //get at least a long array data copy of this data
768 newData.fastPush(arg); //add a long at the end
769 return new NativeArray(newData);
770 }
771
772 /**
773 * ECMA 15.4.4.4 Array.prototype.concat ( [ item1 [ , item2 [ , ... ] ] ] )
774 *
775 * @param self self reference
776 * @param arg argument
777 * @return resulting NativeArray
778 */
779 @SpecializedFunction(linkLogic=ConcatLinkLogic.class)
780 public static NativeArray concat(final Object self, final double arg) {
781 final ContinuousArrayData newData = getContinuousArrayDataCCE(self, Double.class).copy(); //get at least a number array data copy of this data
782 newData.fastPush(arg); //add a double at the end
783 return new NativeArray(newData);
784 }
785
786 /**
787 * ECMA 15.4.4.4 Array.prototype.concat ( [ item1 [ , item2 [ , ... ] ] ] )
788 *
789 * @param self self reference
790 * @param arg argument
791 * @return resulting NativeArray
792 */
793 @SpecializedFunction(linkLogic=ConcatLinkLogic.class)
794 public static NativeArray concat(final Object self, final Object arg) {
795 //arg is [NativeArray] of same type.
796 final ContinuousArrayData selfData = getContinuousArrayDataCCE(self);
797 final ContinuousArrayData newData;
798
799 if (arg instanceof NativeArray) {
963 final Object element = sobj.get(index);
964
965 sobj.delete(index, true);
966 sobj.set("length", index, CALLSITE_STRICT);
967
968 return element;
969 } catch (final ClassCastException | NullPointerException e) {
970 throw typeError("not.an.object", ScriptRuntime.safeToString(self));
971 }
972 }
973
974 /**
975 * ECMA 15.4.4.7 Array.prototype.push (args...)
976 *
977 * Primitive specialization, {@link LinkLogic}
978 *
979 * @param self self reference
980 * @param arg a primitive to push
981 * @return array length after push
982 */
983 @SpecializedFunction(linkLogic=PushLinkLogic.class)
984 public static double push(final Object self, final int arg) {
985 return getContinuousArrayDataCCE(self, Integer.class).fastPush(arg);
986 }
987
988 /**
989 * ECMA 15.4.4.7 Array.prototype.push (args...)
990 *
991 * Primitive specialization, {@link LinkLogic}
992 *
993 * @param self self reference
994 * @param arg a primitive to push
995 * @return array length after push
996 */
997 @SpecializedFunction(linkLogic=PushLinkLogic.class)
998 public static double push(final Object self, final long arg) {
999 return getContinuousArrayDataCCE(self, Long.class).fastPush(arg);
1000 }
1001
1002 /**
1003 * ECMA 15.4.4.7 Array.prototype.push (args...)
1004 *
1005 * Primitive specialization, {@link LinkLogic}
1006 *
1007 * @param self self reference
1008 * @param arg a primitive to push
1009 * @return array length after push
1010 */
1011 @SpecializedFunction(linkLogic=PushLinkLogic.class)
1012 public static double push(final Object self, final double arg) {
1013 return getContinuousArrayDataCCE(self, Double.class).fastPush(arg);
1014 }
1015
1016 /**
1017 * ECMA 15.4.4.7 Array.prototype.push (args...)
1018 *
1019 * Primitive specialization, {@link LinkLogic}
1020 *
1021 * @param self self reference
1022 * @param arg a primitive to push
1023 * @return array length after push
1024 */
1025 @SpecializedFunction(name="push", linkLogic=PushLinkLogic.class)
1026 public static double pushObject(final Object self, final Object arg) {
1027 return getContinuousArrayDataCCE(self, Object.class).fastPush(arg);
1028 }
1029
1030 /**
1031 * ECMA 15.4.4.7 Array.prototype.push (args...)
|
44 import java.util.concurrent.Callable;
45 import jdk.dynalink.CallSiteDescriptor;
46 import jdk.dynalink.linker.GuardedInvocation;
47 import jdk.dynalink.linker.LinkRequest;
48 import jdk.nashorn.api.scripting.JSObject;
49 import jdk.nashorn.internal.objects.annotations.Attribute;
50 import jdk.nashorn.internal.objects.annotations.Constructor;
51 import jdk.nashorn.internal.objects.annotations.Function;
52 import jdk.nashorn.internal.objects.annotations.Getter;
53 import jdk.nashorn.internal.objects.annotations.ScriptClass;
54 import jdk.nashorn.internal.objects.annotations.Setter;
55 import jdk.nashorn.internal.objects.annotations.SpecializedFunction;
56 import jdk.nashorn.internal.objects.annotations.SpecializedFunction.LinkLogic;
57 import jdk.nashorn.internal.objects.annotations.Where;
58 import jdk.nashorn.internal.runtime.Context;
59 import jdk.nashorn.internal.runtime.Debug;
60 import jdk.nashorn.internal.runtime.JSType;
61 import jdk.nashorn.internal.runtime.OptimisticBuiltins;
62 import jdk.nashorn.internal.runtime.PropertyDescriptor;
63 import jdk.nashorn.internal.runtime.PropertyMap;
64 import jdk.nashorn.internal.runtime.ScriptObject;
65 import jdk.nashorn.internal.runtime.ScriptRuntime;
66 import jdk.nashorn.internal.runtime.Undefined;
67 import jdk.nashorn.internal.runtime.arrays.ArrayData;
68 import jdk.nashorn.internal.runtime.arrays.ArrayIndex;
69 import jdk.nashorn.internal.runtime.arrays.ArrayLikeIterator;
70 import jdk.nashorn.internal.runtime.arrays.ContinuousArrayData;
71 import jdk.nashorn.internal.runtime.arrays.IntElements;
72 import jdk.nashorn.internal.runtime.arrays.IteratorAction;
73 import jdk.nashorn.internal.runtime.arrays.NumericElements;
74 import jdk.nashorn.internal.runtime.linker.Bootstrap;
75 import jdk.nashorn.internal.runtime.linker.InvokeByName;
76
77 /**
78 * Runtime representation of a JavaScript array. NativeArray only holds numeric
79 * keyed values. All other values are stored in spill.
80 */
81 @ScriptClass("Array")
82 public final class NativeArray extends ScriptObject implements OptimisticBuiltins {
83 private static final Object JOIN = new Object();
730 * @return the new NativeArray
731 */
732 @SpecializedFunction(isConstructor=true)
733 public static NativeArray construct(final boolean newObj, final Object self, final double length) {
734 final long uint32length = JSType.toUint32(length);
735
736 if (uint32length == length) {
737 return new NativeArray(uint32length);
738 }
739
740 return construct(newObj, self, new Object[]{length});
741 }
742
743 /**
744 * ECMA 15.4.4.4 Array.prototype.concat ( [ item1 [ , item2 [ , ... ] ] ] )
745 *
746 * @param self self reference
747 * @param arg argument
748 * @return resulting NativeArray
749 */
750 @SpecializedFunction(linkLogic=ConcatLinkLogic.class, convertsNumericArgs = false)
751 public static NativeArray concat(final Object self, final int arg) {
752 final ContinuousArrayData newData = getContinuousArrayDataCCE(self, Integer.class).copy(); //get at least an integer data copy of this data
753 newData.fastPush(arg); //add an integer to its end
754 return new NativeArray(newData);
755 }
756
757 /**
758 * ECMA 15.4.4.4 Array.prototype.concat ( [ item1 [ , item2 [ , ... ] ] ] )
759 *
760 * @param self self reference
761 * @param arg argument
762 * @return resulting NativeArray
763 */
764 @SpecializedFunction(linkLogic=ConcatLinkLogic.class, convertsNumericArgs = false)
765 public static NativeArray concat(final Object self, final double arg) {
766 final ContinuousArrayData newData = getContinuousArrayDataCCE(self, Double.class).copy(); //get at least a number array data copy of this data
767 newData.fastPush(arg); //add a double at the end
768 return new NativeArray(newData);
769 }
770
771 /**
772 * ECMA 15.4.4.4 Array.prototype.concat ( [ item1 [ , item2 [ , ... ] ] ] )
773 *
774 * @param self self reference
775 * @param arg argument
776 * @return resulting NativeArray
777 */
778 @SpecializedFunction(linkLogic=ConcatLinkLogic.class)
779 public static NativeArray concat(final Object self, final Object arg) {
780 //arg is [NativeArray] of same type.
781 final ContinuousArrayData selfData = getContinuousArrayDataCCE(self);
782 final ContinuousArrayData newData;
783
784 if (arg instanceof NativeArray) {
948 final Object element = sobj.get(index);
949
950 sobj.delete(index, true);
951 sobj.set("length", index, CALLSITE_STRICT);
952
953 return element;
954 } catch (final ClassCastException | NullPointerException e) {
955 throw typeError("not.an.object", ScriptRuntime.safeToString(self));
956 }
957 }
958
959 /**
960 * ECMA 15.4.4.7 Array.prototype.push (args...)
961 *
962 * Primitive specialization, {@link LinkLogic}
963 *
964 * @param self self reference
965 * @param arg a primitive to push
966 * @return array length after push
967 */
968 @SpecializedFunction(linkLogic=PushLinkLogic.class, convertsNumericArgs = false)
969 public static double push(final Object self, final int arg) {
970 return getContinuousArrayDataCCE(self, Integer.class).fastPush(arg);
971 }
972
973 /**
974 * ECMA 15.4.4.7 Array.prototype.push (args...)
975 *
976 * Primitive specialization, {@link LinkLogic}
977 *
978 * @param self self reference
979 * @param arg a primitive to push
980 * @return array length after push
981 */
982 @SpecializedFunction(linkLogic=PushLinkLogic.class, convertsNumericArgs = false)
983 public static double push(final Object self, final double arg) {
984 return getContinuousArrayDataCCE(self, Double.class).fastPush(arg);
985 }
986
987 /**
988 * ECMA 15.4.4.7 Array.prototype.push (args...)
989 *
990 * Primitive specialization, {@link LinkLogic}
991 *
992 * @param self self reference
993 * @param arg a primitive to push
994 * @return array length after push
995 */
996 @SpecializedFunction(name="push", linkLogic=PushLinkLogic.class)
997 public static double pushObject(final Object self, final Object arg) {
998 return getContinuousArrayDataCCE(self, Object.class).fastPush(arg);
999 }
1000
1001 /**
1002 * ECMA 15.4.4.7 Array.prototype.push (args...)
|