/// <summary> /// Projects the [[iterator]] protocol on IEnumerable objects. /// </summary> /// <param name="context"></param> /// <param name="targetObject"></param> /// <param name="reflector"></param> private void ProjectIEnumerable(BaristaContext context, JsObject targetObject, ObjectReflector reflector) { if (typeof(IEnumerable).IsAssignableFrom(reflector.Type)) { var fnIterator = context.CreateFunction(new Func <JsObject, JsValue>((thisObj) => { IEnumerable targetObj = null; if (thisObj == null) { context.CurrentScope.SetException(context.CreateTypeError($"Could not retrieve iterator on object {targetObject.ToString()}: Invalid 'this' context.")); return(context.Undefined); } if (thisObj.TryGetBean(out JsExternalObject xoObj)) { targetObj = xoObj.Target as IEnumerable; } return(context.CreateIterator(targetObj.GetEnumerator())); })); var iteratorDescriptor = context.CreateObject(); iteratorDescriptor.SetProperty("value", fnIterator); targetObject.SetProperty(context.Symbol.Iterator, iteratorDescriptor); } }