use of com.github.anba.es6draft.runtime.types.ScriptObject in project es6draft by anba.
the class ArrayPrototype method inheritedKeys.
private static long[] inheritedKeys(OrdinaryObject array, long from, long to) {
long[] indices = array.indices(from, to);
for (ScriptObject prototype = array.getPrototype(); prototype != null; ) {
assert prototype instanceof OrdinaryObject : "Wrong class " + prototype.getClass();
OrdinaryObject proto = (OrdinaryObject) prototype;
if (proto.hasIndexedProperties()) {
long[] protoIndices = proto.indices(from, to);
long[] newIndices = new long[indices.length + protoIndices.length];
System.arraycopy(indices, 0, newIndices, 0, indices.length);
System.arraycopy(protoIndices, 0, newIndices, indices.length, protoIndices.length);
indices = newIndices;
}
prototype = proto.getPrototype();
}
Arrays.sort(indices);
return indices;
}
use of com.github.anba.es6draft.runtime.types.ScriptObject in project es6draft by anba.
the class ArrayPrototype method iterationKindForSparse.
private static IterationKind iterationKindForSparse(OrdinaryObject arrayLike, long length) {
IterationKind iteration = IterationKind.SparseOwnKeys;
int protoDepth = 0;
long indexed = 0;
for (OrdinaryObject object = arrayLike; ; ) {
indexed += object.getIndexedSize();
ScriptObject prototype = object.getPrototype();
if (prototype == null) {
break;
}
if (!(prototype instanceof OrdinaryObject)) {
return IterationKind.Slow;
}
object = (OrdinaryObject) prototype;
if (object.hasSpecialIndexedProperties()) {
return IterationKind.Slow;
}
if (object.hasIndexedProperties()) {
if (object.hasIndexedAccessors()) {
return IterationKind.Slow;
}
iteration = IterationKind.InheritedKeys;
}
if (++protoDepth == MAX_PROTO_DEPTH) {
return IterationKind.Slow;
}
}
double density = indexed / (double) length;
if (density > 0.75) {
return IterationKind.Slow;
}
return iteration;
}
use of com.github.anba.es6draft.runtime.types.ScriptObject in project es6draft by anba.
the class IntlAbstractOperations method CanonicalizeLocaleList.
/**
* 9.2.1 CanonicalizeLocaleList (locales)
*
* @param cx
* the execution context
* @param locales
* the locales array
* @return the set of canonicalized locales
*/
public static Set<String> CanonicalizeLocaleList(ExecutionContext cx, Object locales) {
/* step 1 */
if (Type.isUndefined(locales)) {
return emptySet();
}
/* steps 2-8 (string only) */
if (Type.isString(locales)) {
// handle the string-only case directly
String tag = ToFlatString(cx, locales);
LanguageTag langTag = IsStructurallyValidLanguageTag(tag);
if (langTag == null) {
throw newRangeError(cx, Messages.Key.IntlStructurallyInvalidLanguageTag, tag);
}
tag = CanonicalizeLanguageTag(langTag);
return singleton(tag);
}
/* step 2 */
LinkedHashSet<String> seen = new LinkedHashSet<>();
/* step 3 (not applicable) */
/* step 4 */
ScriptObject o = ToObject(cx, locales);
/* step 5 */
long len = ToLength(cx, Get(cx, o, "length"));
/* steps 6-7 */
for (long k = 0; k < len; ++k) {
/* step 7.a */
long pk = k;
/* step 7.b */
boolean kPresent = HasProperty(cx, o, pk);
/* step 7.c */
if (kPresent) {
/* step 7.c.i */
Object kValue = Get(cx, o, pk);
/* step 7.c.ii */
if (!(Type.isString(kValue) || Type.isObject(kValue))) {
throw newTypeError(cx, Messages.Key.IntlInvalidLanguageTagType, Type.of(kValue).toString());
}
/* step 7.c.iii */
String tag = ToFlatString(cx, kValue);
/* step 7.c.iv */
LanguageTag langTag = IsStructurallyValidLanguageTag(tag);
if (langTag == null) {
throw newRangeError(cx, Messages.Key.IntlStructurallyInvalidLanguageTag, tag);
}
/* step 7.c.v */
tag = CanonicalizeLanguageTag(langTag);
/* step 7.c.vi */
seen.add(tag);
}
}
/* step 8 */
return seen;
}
use of com.github.anba.es6draft.runtime.types.ScriptObject in project es6draft by anba.
the class NumberFormatConstructor method InitializeNumberFormat.
/**
* 11.1.1 InitializeNumberFormat (numberFormat, locales, options)
*
* @param cx
* the execution context
* @param numberFormat
* the number format object
* @param locales
* the locales array
* @param opts
* the options object
*/
public static void InitializeNumberFormat(ExecutionContext cx, NumberFormatObject numberFormat, Object locales, Object opts) {
/* steps 1-2 (FIXME: spec bug - unnecessary internal slot) */
/* step 3 */
Set<String> requestedLocales = CanonicalizeLocaleList(cx, locales);
/* steps 4-5 */
ScriptObject options;
if (Type.isUndefined(opts)) {
options = ObjectCreate(cx, Intrinsics.ObjectPrototype);
} else {
options = ToObject(cx, opts);
}
/* step 7 */
String matcher = GetStringOption(cx, options, "localeMatcher", set("lookup", "best fit"), "best fit");
/* step 6, 8 */
OptionsRecord opt = new OptionsRecord(OptionsRecord.MatcherType.forName(matcher));
/* step 9 */
NumberFormatLocaleData localeData = new NumberFormatLocaleData();
/* step 10 */
ResolvedLocale r = ResolveLocale(cx.getRealm(), getAvailableLocalesLazy(cx), requestedLocales, opt, relevantExtensionKeys, localeData);
/* step 11 */
numberFormat.setLocale(r.getLocale());
/* step 12 */
numberFormat.setNumberingSystem(r.getValue(ExtensionKey.nu));
/* step 13 (not applicable) */
/* step 14 */
String s = GetStringOption(cx, options, "style", set("decimal", "percent", "currency"), "decimal");
/* step 15 */
numberFormat.setStyle(s);
/* step 16 */
String c = GetStringOption(cx, options, "currency", null, null);
/* step 17 */
if (c != null && !IsWellFormedCurrencyCode(c)) {
throw newRangeError(cx, Messages.Key.IntlInvalidCurrency, c);
}
/* step 18 */
if ("currency".equals(s) && c == null) {
throw newTypeError(cx, Messages.Key.IntlInvalidCurrency, "null");
}
/* step 19 */
int cDigits = -1;
if ("currency".equals(s)) {
c = ToUpperCase(c);
numberFormat.setCurrency(c);
cDigits = CurrencyDigits(c);
}
/* step 20 */
String cd = GetStringOption(cx, options, "currencyDisplay", set("code", "symbol", "name"), "symbol");
/* step 21 */
if ("currency".equals(s)) {
numberFormat.setCurrencyDisplay(cd);
}
/* step 22 */
int mnid = GetNumberOption(cx, options, "minimumIntegerDigits", 1, 21, 1);
/* step 23 */
numberFormat.setMinimumIntegerDigits(mnid);
/* step 24 */
int mnfdDefault = "currency".equals(s) ? cDigits : 0;
/* step 25 */
int mnfd = GetNumberOption(cx, options, "minimumFractionDigits", 0, 20, mnfdDefault);
/* step 26 */
numberFormat.setMinimumFractionDigits(mnfd);
/* step 27 */
int mxfdDefault = "currency".equals(s) ? Math.max(mnfd, cDigits) : "percent".equals(s) ? Math.max(mnfd, 0) : Math.max(mnfd, 3);
/* step 28 */
int mxfd = GetNumberOption(cx, options, "maximumFractionDigits", mnfd, 20, mxfdDefault);
/* step 29 */
numberFormat.setMaximumFractionDigits(mxfd);
/* step 30 */
Object mnsd = Get(cx, options, "minimumSignificantDigits");
/* step 31 */
Object mxsd = Get(cx, options, "maximumSignificantDigits");
/* step 32 */
if (!Type.isUndefined(mnsd) || !Type.isUndefined(mxsd)) {
int _mnsd = GetNumberOption(cx, options, "minimumSignificantDigits", 1, 21, 1);
int _mxsd = GetNumberOption(cx, options, "maximumSignificantDigits", _mnsd, 21, 21);
numberFormat.setMinimumSignificantDigits(_mnsd);
numberFormat.setMaximumSignificantDigits(_mxsd);
}
/* step 33 */
boolean g = GetBooleanOption(cx, options, "useGrouping", true);
/* step 34 */
numberFormat.setUseGrouping(g);
/* steps 35-40 (not applicable) */
/* step 41 */
numberFormat.setBoundFormat(null);
/* step 42 (FIXME: spec bug - unnecessary internal slot) */
/* step 43 (omitted) */
}
use of com.github.anba.es6draft.runtime.types.ScriptObject in project es6draft by anba.
the class JSONObject method SerializeJSONArray.
/**
* 24.3.2.4 Runtime Semantics: SerializeJSONArray( value )
*
* @param cx
* the execution context
* @param serializer
* the serializer state
* @param value
* the script array object
* @param stack
* the current stack
*/
private static void SerializeJSONArray(ExecutionContext cx, JSONSerializer serializer, ScriptObject value) {
/* steps 1-2 */
if (!serializer.stack.add(value)) {
throw newTypeError(cx, Messages.Key.JSONCyclicValue);
}
/* steps 3-5 (not applicable) */
/* steps 6-7 */
long len = ToLength(cx, Get(cx, value, "length"));
/* steps 8-11 */
String gap = serializer.gap;
StringBuilder result = serializer.result;
result.append('[');
if (len > 0) {
serializer.level += 1;
for (long index = 0; index < len; ++index) {
if (!gap.isEmpty()) {
indent(serializer, result);
}
// Inlined: SerializeJSONProperty
Object v = Get(cx, value, index);
v = TransformJSONValue(cx, serializer, value, ToString(index), v);
if (!IsJSONSerializable(v)) {
result.append("null");
} else {
SerializeJSONValue(cx, serializer, v);
}
if (index + 1 < len) {
result.append(',');
}
}
serializer.level -= 1;
if (!gap.isEmpty()) {
indent(serializer, result);
}
}
result.append(']');
/* step 12 */
serializer.stack.remove(value);
/* steps 13-14 (not applicable) */
}
Aggregations