private MtResult MtMap(ScriptThread thread, object[] args) { var result = new MtResult(); var arrExpression = args[0] as MtResult; var fun = args[1]; arrExpression.GetValue((arr) => { // arr MtResult[] wrkArr = arr.Value as MtResult[]; if (wrkArr == null) { throw new Exception("Array expression is null!"); } MtFunctionObjectBase.ExtractAsFunction(fun, (wrkFun) => { // Launch and wait for all to end var count = wrkArr.Length; var res = new MtResult[count]; if (count > 0) { var waitForEndOfAll = new ManualResetEvent(false); for (var i = 0; i < wrkArr.Length; ++i) { int copy_i = i; var ret = wrkFun.Call(thread, new object[] { wrkArr[i] }); if (ret == null) { throw new Exception("Return of application in map is null!"); } var wrkRet = ret as MtResult; wrkRet.WaitForValue((r) => { res[copy_i] = r; if (Interlocked.Decrement(ref count) == 0) { waitForEndOfAll.Set(); } }); } waitForEndOfAll.WaitOne(); } result.SetValue(new MtObject(res)); }); }); return(result); }
protected override object DoEvaluate(Irony.Interpreter.ScriptThread thread) { // PROLOG thread.CurrentNode = this; try { var appResult = new MtResult(); MtResult[] args = _args.Evaluate(thread) as MtResult[]; if (args == null) { throw new Exception("Args evaluated to null!"); } var subthreadF = _head.NewScriptThread(thread); var headResult = _head.Evaluate(subthreadF); if (headResult == null) { throw new Exception("Head can't evaluate to null!"); } MtFunctionObjectBase.ExtractAsFunction(headResult, (wrkFun) => { #if DEBUG && !SILVERLIGHT if (wrkFun is BuiltInCallTarget) { var builtin = wrkFun as BuiltInCallTarget; Debug.Print("Calling builtin: {0}", builtin.Name); } else { Debug.Print("Calling user function"); } #endif var resultFun = wrkFun.Call(thread, args) as MtResult; resultFun.GetValue((r3) => { appResult.SetValue(r3); }); }); return(appResult); } catch (Exception e) { throw new Exception("Exception on MtApplication.", e); } finally { // EPILOG //thread.CurrentNode = Parent; } }
protected override object DoEvaluate(ScriptThread thread) { thread.CurrentNode = this; try { var ret = new MtResult(); var emitterThread = _eventEmitter.NewScriptThread(thread); var emitter = _eventEmitter.Evaluate(emitterThread) as MtResult; var listThread = _listener.NewScriptThread(thread); var list = _listener.Evaluate(listThread) as MtResult; emitter.GetValue(o => { list.GetValue(l => { // set listener var wrkEmitter = o.Value as IEventEmitter; if (wrkEmitter == null) { #if DEBUG && !SILVERLIGHT Debug.Print("I dont throw events"); #endif } else { ICallTarget callable = null; var haveCallable = new ManualResetEvent(false); // Extract Callable (Compute it once) MtFunctionObjectBase.ExtractAsFunction( l, fun => { callable = fun; haveCallable.Set(); }); wrkEmitter.On(_eventName, args => { #if DEBUG && !SILVERLIGHT Debug.Print("EventEmitter Called {0}", _eventName); #endif var resArgs = new MtResult[args.Length]; for (var i = 0; i < args.Length; ++i) { resArgs[i] = MtResult.CreateAndWrap(args[i]); } haveCallable.WaitOne(); // Call it var result = callable.Call(thread, resArgs) as MtResult; }); } ret.SetValue(o); }); }); return(ret.WaitForValue()); //return ret; } catch (Exception e) { throw new Exception("Exception on listener statement evaluate", e); } finally { //thread.CurrentNode = Parent; } }