Пример #1
0
        private static ScriptValue Values([NotNull] ScriptArguments arg)
        {
            //https://tc39.github.io/ecma262/#sec-object.values
            var obj      = arg.Agent.ToObject(arg[0]);
            var nameList = EnumerableOwnProperties(arg.Agent, obj, EnumerateType.Value);

            return(ArrayIntrinsics.CreateArrayFromList(arg.Agent, nameList));
        }
Пример #2
0
        private static ScriptValue GetOwnPropertyKeys([NotNull] Agent agent, ScriptValue value, ScriptValue.Type type)
        {
            //https://tc39.github.io/ecma262/#sec-getownpropertykeys
            var obj      = agent.ToObject(value);
            var keys     = obj.OwnPropertyKeys();
            var nameList = keys.Where(x => x.ValueType == type);

            return(ArrayIntrinsics.CreateArrayFromList(agent, nameList));
        }
Пример #3
0
        private static ScriptValue OwnKeys([NotNull] ScriptArguments arg)
        {
            //https://tc39.github.io/ecma262/#sec-reflect.ownkeys
            if (!arg[0].IsObject)
            {
                throw arg.Agent.CreateTypeError();
            }

            var keys = ((ScriptObject)arg[0]).OwnPropertyKeys();

            return(ArrayIntrinsics.CreateArrayFromList(arg.Agent, keys));
        }
Пример #4
0
        private static IEnumerable <ScriptValue> EnumerableOwnProperties([NotNull] Agent agent, [NotNull] ScriptObject obj, EnumerateType type)
        {
            //https://tc39.github.io/ecma262/#sec-enumerableownproperties

            var ownKeys    = obj.OwnPropertyKeys();
            var properties = new List <ScriptValue>();

            foreach (var key in ownKeys)
            {
                if (key.IsString)
                {
                    var descriptor = obj.GetOwnProperty(key);
                    if (descriptor != null && descriptor.Enumerable)
                    {
                        switch (type)
                        {
                        case EnumerateType.Key:
                            properties.Add(key);
                            break;

                        case EnumerateType.Value:
                            properties.Add(obj.Get(key));
                            break;

                        case EnumerateType.KeyValue:
                            properties.Add(ArrayIntrinsics.CreateArrayFromList(agent, new[] { key, obj.Get(key) }));
                            break;

                        default:
                            throw new ArgumentOutOfRangeException(nameof(type), type, null);
                        }
                    }
                }
            }
            return(properties);
        }
Пример #5
0
        private static ScriptValue Split([NotNull] ScriptArguments arg)
        {
            //https://tc39.github.io/ecma262/#sec-string.prototype.split
            var obj = arg.Agent.RequireObjectCoercible(arg.ThisValue);

            if (arg[0] != ScriptValue.Undefined && arg[0] != ScriptValue.Null)
            {
                var splitter = arg.Agent.GetMethod(arg[0], Symbol.Split);
                if (splitter != null)
                {
                    return(arg.Agent.Call(splitter, arg[0], obj, arg[1]));
                }
            }

            var str         = arg.Agent.ToString(obj);
            var array       = ArrayIntrinsics.ArrayCreate(arg.Agent, 0);
            var arrayLength = 0u;
            var limit       = arg[1] == ScriptValue.Undefined ? uint.MaxValue : arg.Agent.ToUint32(arg[1]);
            var seperator   = arg.Agent.ToString(arg[0]);

            if (limit == 0)
            {
                return(array);
            }

            if (arg[0] == ScriptValue.Undefined)
            {
                array.CreateDataProperty("0", str);
                return(array);
            }

            if (str.Length == 0)
            {
                var z = SplitMatch(str, 0, seperator);
                if (z.Item1)
                {
                    return(array);
                }

                array.CreateDataProperty("0", str);
                return(array);
            }

            var    p = 0;
            var    q = p;
            string substring;

            while (q != str.Length)
            {
                var e = SplitMatch(str, q, seperator);
                if (!e.Item1)
                {
                    q++;
                }
                else
                {
                    if (e.Item2 == p)
                    {
                        q = q + 1;
                    }
                    else
                    {
                        substring = str.Substring(p, q - p);
                        array.CreateDataProperty(arrayLength.ToString(), substring);
                        arrayLength++;
                        if (arrayLength == limit)
                        {
                            return(array);
                        }
                        q = p = e.Item2;
                    }
                }
            }

            substring = str.Substring(p);
            array.CreateDataProperty(arrayLength.ToString(), substring);
            return(array);
        }