public MtResult MtPrint(ScriptThread thread, object[] args) { try { var r = new MtResult(); Monitor.Enter(_mtPrintLock); try { Reduce(args, (o) => { String toWrite = o == null ? "<null>" : o.ToString(); if (OutputStream != null) { byte[] raw = Encoding.UTF8.GetBytes(toWrite); OutputStream.Write(raw, 0, raw.Length); } thread.App.Write(toWrite); }, () => { // Write new lines if (OutputStream != null) { OutputStream.Write(_newLineRaw, 0, _newLineRaw.Length); OutputStream.Flush(); } thread.App.WriteLine(""); r.SetValue(MtObject.True); }); } catch (Exception e) { throw new Exception("Error printing stuff.", e); } finally { // Sync wait to avoid race conditions // while printing ... r.WaitForValue(); Monitor.Exit(_mtPrintLock); } return(r); } catch (Exception e) { throw new Exception("Exception on Runtime function: print", e); } }
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; } }