internal static IDynamic Iterate(IEnvironment environment, IArgs args) { var iterator = new Iterator(environment, args[1]); var callback = args[0] as ICallable; if (callback == null) throw environment.CreateTypeError("The argument 'callback' must be a callable function."); while (iterator.Next()) { var callArgs = environment.CreateArgs(new[] { iterator.Current }); callback.Call(environment, environment.Undefined, callArgs); } return environment.Undefined; }
public override IDynamic CreateIterator(IEnvironment environment, IArgs args) { var iterator = new Iterator(environment, _iterable); return new HFilterIterator(environment, iterator, _predicate); }
public void ForeachLoop(string identifier, IDynamic iterable, Code loopBodyCode) { var emptyArgs = EmptyArgs; var iterator = new Iterator(this, iterable); var oldEnv = Context.LexicalEnviroment; var newEnv = oldEnv.NewDeclarativeEnvironment(); var newRec = newEnv.Record; newRec.CreateMutableBinding(identifier, false); Context.LexicalEnviroment = newEnv; try { while (iterator.Next()) { newRec.SetMutableBinding(identifier, iterator.Current, true); loopBodyCode(this, emptyArgs); } } finally { Context.LexicalEnviroment = oldEnv; } }
internal static IDynamic ToArray(IEnvironment environment, IArgs args) { var array = ((IConstructable)environment.ArrayConstructor).Construct(environment, environment.EmptyArgs); var builder = environment.CreateObjectBuilder(array).SetAttributes(true, true, true); var iterator = new Iterator(environment, args[0]); uint index = 0; while (iterator.Next()) { builder.AppendDataProperty(index.ToString(), iterator.Current); index++; } array.Put("length", environment.CreateNumber(index), true); return array; }
public HFilterIterator(IEnvironment environment, Iterator iterator, ICallable predicate) : base(environment) { _iterator = iterator; _predicate = predicate; }
public HMapIterator(IEnvironment environment, Iterator iterator, ICallable mapping) : base(environment) { _iterator = iterator; _mapping = mapping; }
public override IDynamic CreateIterator(IEnvironment environment, IArgs args) { var iterator = new Iterator(environment, _iterable); return new HMapIterator(environment, iterator, _mapping); }