Esempio n. 1
0
        public static EcmaValue ArrayPrototypeToLocaleString([This] EcmaValue thisValue, EcmaValue locales, EcmaValue options)
        {
            RuntimeObject obj    = thisValue.ToObject();
            long          length = obj.Get(WellKnownProperty.Length).ToLength();

            if (length == 0)
            {
                return(String.Empty);
            }
            StringBuilder sb = new StringBuilder();

            for (long i = 0; i < length; i++)
            {
                if (i > 0)
                {
                    sb.Append(",");
                }
                EcmaValue item = obj.Get(i);
                if (!item.IsNullOrUndefined)
                {
                    sb.Append(item.Invoke("toLocaleString", locales, options).ToStringOrThrow());
                }
            }
            return(sb.ToString());
        }
Esempio n. 2
0
        public static EcmaValue Join([This] EcmaValue thisValue, EcmaValue separater)
        {
            RuntimeObject obj    = thisValue.ToObject();
            long          length = obj.Get(WellKnownProperty.Length).ToLength();

            if (length == 0)
            {
                return(String.Empty);
            }
            string        sep = separater.Type == EcmaValueType.Undefined ? "," : separater.ToString();
            StringBuilder sb  = new StringBuilder();

            for (long i = 0; i < length; i++)
            {
                if (i > 0)
                {
                    sb.Append(sep);
                }
                EcmaValue item = obj.Get(i);
                if (!item.IsNullOrUndefined)
                {
                    sb.Append(item.ToStringOrThrow());
                }
            }
            return(sb.ToString());
        }
Esempio n. 3
0
 public bool MoveNext()
 {
     if (++nextIndex < target.Get(WellKnownProperty.Length).ToLength())
     {
         this.Current = new KeyValuePair <EcmaValue, EcmaValue>(nextIndex, target.Get(nextIndex));
         return(true);
     }
     return(false);
 }
Esempio n. 4
0
        public static EcmaValue Get(EcmaValue target, EcmaValue key, EcmaValue?receiver = default)
        {
            Guard.ArgumentIsObject(target);
            RuntimeObject obj = target.ToObject();

            return(obj.Get(EcmaPropertyKey.FromValue(key), receiver.HasValue ? receiver.Value.ToObject() : obj));
        }
Esempio n. 5
0
        private void SerializeJsonProperty(TextWriter writer, Stack <RuntimeObject> stack, RuntimeObject holder, EcmaPropertyKey property)
        {
            EcmaValue value = TransformValue(holder, property, holder.Get(property));

            if (value.Type != EcmaValueType.Undefined)
            {
                WritePropertyValue(writer, stack, value);
            }
        }
Esempio n. 6
0
        public static EcmaValue From([This] EcmaValue thisValue, EcmaValue source, EcmaValue mapFn, EcmaValue thisArg)
        {
            if (!thisValue.IsCallable || !thisValue.ToObject().IsConstructor)
            {
                throw new EcmaTypeErrorException(InternalString.Error.NotConstructor);
            }
            if (mapFn != default)
            {
                Guard.ArgumentIsCallable(mapFn);
            }
            if (source.IsNullOrUndefined)
            {
                return(thisValue.Construct(0));
            }
            RuntimeObject obj      = source.ToObject();
            RuntimeObject iterator = obj.GetMethod(WellKnownSymbol.Iterator);
            TypedArray    array;

            if (iterator != null)
            {
                List <EcmaValue> values = new List <EcmaValue>(obj.GetIterator());
                array = thisValue.Construct(values.Count).GetUnderlyingObject <TypedArray>();
                if (array.Length < values.Count)
                {
                    throw new EcmaTypeErrorException(InternalString.Error.TypedArrayBufferTooSmall);
                }
                int i = 0;
                foreach (EcmaValue v in values)
                {
                    EcmaValue value = v;
                    if (mapFn != default)
                    {
                        value = mapFn.Call(thisArg, value, i);
                    }
                    array.Set(i++, value);
                }
            }
            else
            {
                long length = source[WellKnownProperty.Length].ToLength();
                array = thisValue.Construct(length).GetUnderlyingObject <TypedArray>();
                if (array.Length < length)
                {
                    throw new EcmaTypeErrorException(InternalString.Error.TypedArrayBufferTooSmall);
                }
                for (int i = 0; i < length; i++)
                {
                    EcmaValue value = obj.Get(i);
                    if (mapFn != default)
                    {
                        value = mapFn.Call(thisArg, value, i);
                    }
                    array.Set(i, value);
                }
            }
            return(array);
        }
Esempio n. 7
0
        private void SerializeJsonArray(TextWriter writer, Stack <RuntimeObject> stack, RuntimeObject value)
        {
            if (stack.Contains(value))
            {
                throw new EcmaTypeErrorException(InternalString.Error.CircularJsonObject);
            }
            string prependString = String.Join("", stack.Select(v => indentString).ToArray());

            stack.Push(value);
            writer.Write("[");

            long length = value.Get(WellKnownProperty.Length).ToLength();

            for (long i = 0; i < length; i++)
            {
                if (i > 0)
                {
                    writer.Write(",");
                }
                if (indentString != String.Empty)
                {
                    writer.Write(Environment.NewLine);
                    writer.Write(prependString);
                    writer.Write(indentString);
                }
                EcmaValue item = value.Get(i);
                if (item.Type == EcmaValueType.Undefined)
                {
                    writer.Write("null");
                }
                else
                {
                    SerializeJsonProperty(writer, stack, value, i);
                }
            }
            if (length > 0 && indentString != String.Empty)
            {
                writer.Write(Environment.NewLine);
                writer.Write(prependString);
            }
            writer.Write("]");
            stack.Pop();
        }
Esempio n. 8
0
        private void SerializeJsonObject(TextWriter writer, Stack <RuntimeObject> stack, RuntimeObject value)
        {
            bool   hasEntries    = false;
            string prependString = String.Join("", stack.Select(v => indentString).ToArray());

            if (stack.Contains(value))
            {
                throw new EcmaTypeErrorException(InternalString.Error.CircularJsonObject);
            }
            stack.Push(value);
            writer.Write("{");

            List <EcmaPropertyEntry> entries = new List <EcmaPropertyEntry>();

            if (propertyList != null)
            {
                entries.AddRange(propertyList.Select(v => new EcmaPropertyEntry(v, value.Get(v))));
            }
            else
            {
                entries.AddRange(value.GetEnumerableOwnProperties(false));
            }
            foreach (EcmaPropertyEntry e in entries)
            {
                EcmaValue v = TransformValue(value, e.Key, e.Value);
                if (v.Type != EcmaValueType.Undefined)
                {
                    if (hasEntries)
                    {
                        writer.Write(",");
                    }
                    hasEntries = true;
                    if (indentString != String.Empty)
                    {
                        writer.Write(Environment.NewLine);
                        writer.Write(prependString);
                        writer.Write(indentString);
                    }
                    WriteString(writer, e.Key.ToString());
                    writer.Write(":");
                    if (indentString != String.Empty)
                    {
                        writer.Write(" ");
                    }
                    WritePropertyValue(writer, stack, v);
                }
            }
            if (hasEntries && indentString != String.Empty)
            {
                writer.Write(Environment.NewLine);
                writer.Write(prependString);
            }
            writer.Write("}");
            stack.Pop();
        }
Esempio n. 9
0
        public static EcmaValue Pop([This] EcmaValue thisValue)
        {
            RuntimeObject obj = thisValue.ToObject();

            if (obj is EcmaArray arr && !arr.FallbackMode)
            {
                return(arr.Pop());
            }
            long len = obj.Get(WellKnownProperty.Length).ToLength();

            if (len == 0)
            {
                obj.SetOrThrow(WellKnownProperty.Length, 0);
                return(default);
Esempio n. 10
0
        private static EcmaValue InternalizeJsonProperty(RuntimeObject holder, EcmaPropertyKey property, EcmaValue reviver)
        {
            EcmaValue value = holder.Get(property);

            if (value.Type == EcmaValueType.Object)
            {
                RuntimeObject obj = value.ToObject();
                if (EcmaArray.IsArray(value))
                {
                    for (long i = 0, length = value[WellKnownProperty.Length].ToLength(); i < length; i++)
                    {
                        EcmaValue newValue = InternalizeJsonProperty(obj, i, reviver);
                        if (newValue.Type == EcmaValueType.Undefined)
                        {
                            obj.Delete(i);
                        }
                        else
                        {
                            obj.CreateDataProperty(i, newValue);
                        }
                    }
                }
                else
                {
                    foreach (EcmaPropertyKey key in obj.GetEnumerableOwnPropertyKeys().ToArray())
                    {
                        EcmaValue newValue = InternalizeJsonProperty(obj, key, reviver);
                        if (newValue.Type == EcmaValueType.Undefined)
                        {
                            obj.Delete(key);
                        }
                        else
                        {
                            obj.CreateDataProperty(key, newValue);
                        }
                    }
                }
            }
            return(reviver.Call(holder, property.ToValue(), value));
        }