示例#1
0
        public MapInstance Construct(object iterable)
        {
            // Create a new set.
            var result = new MapInstance(this.InstancePrototype);

            // If iterable is not null or undefined, then iterate through the values and add them to the set.
            if (iterable != Undefined.Value && iterable != Null.Value)
            {
                var iterator = TypeUtilities.GetIterator(Engine, TypeConverter.ToObject(Engine, iterable));
                if (iterator != null)
                {
                    // Get a reference to the set function.
                    var setFunc = result["set"] as FunctionInstance;
                    if (setFunc == null)
                        throw new JavaScriptException(Engine, ErrorType.TypeError, "Missing 'set' function");

                    // Call the set function for each value.
                    foreach (var value in TypeUtilities.Iterate(Engine, iterator))
                    {
                        var obj = value as ObjectInstance;
                        if (obj == null)
                            throw new JavaScriptException(Engine, ErrorType.TypeError, $"Expected iterator return value to be an object, but was {value}");
                        setFunc.Call(result, obj[0], obj[1]);
                    }
                }
            }

            return result;
        }
示例#2
0
 //     INITIALIZATION
 //_________________________________________________________________________________________
 /// <summary>
 /// Creates a new map iterator.
 /// </summary>
 /// <param name="prototype"> The next object in the prototype chain. </param>
 /// <param name="map"> The map to iterate over. </param>
 /// <param name="list"> The linked list to iterate over. </param>
 /// <param name="kind"> The type of values to return. </param>
 internal MapIterator(ObjectInstance prototype, MapInstance map, LinkedList<KeyValuePair<object, object>> list, Kind kind)
     : base(prototype)
 {
     this.map = map;
     this.map.BeforeDelete += Map_BeforeDelete;
     this.list = list;
     this.kind = kind;
 }