1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
#region license
// Copyright (c) 2005, Peter Johanson (latexer@gentoo.org)
// All rights reserved.
//
// BooBinding is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// BooBinding is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with BooBinding; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#endregion
namespace BooBinding.BooShell
import System
import System.Collections
import System.Threading
import System.Text.RegularExpressions
import Boo.Lang.Interpreter
import Gtk
import MonoDevelop.Core.Execution;
class BooShell (RemoteProcessObject):
private _interpreter = InteractiveInterpreter(RememberLastValue: true, Print: print)
private _commandQueue = Queue()
private _outputQueue = Queue()
private _thread as System.Threading.Thread
private _processing as string = "true"
override def InitializeLifetimeService ():
return null
def Reset() as bool:
EnqueueCommand (ShellCommand (ShellCommandType.Reset, null))
return true
def LoadAssembly (assemblyPath as string) as bool:
EnqueueCommand (ShellCommand (ShellCommandType.Load, assemblyPath))
return true
References as IList:
get:
list = []
Monitor.Enter (_interpreter)
for assembly as System.Reflection.Assembly in _interpreter.References:
try:
loc = assembly.Location
list.Add (loc)
except x:
continue
Monitor.Exit (_interpreter)
return list
def GetOutput() as (string):
ret as (string)
try:
Monitor.Enter (_outputQueue)
if _processing == "true":
Monitor.Wait (_outputQueue)
if _outputQueue.Count > 0:
ret = array (string, _outputQueue.Count)
_outputQueue.CopyTo (ret, 0)
_outputQueue.Clear()
ensure:
Monitor.Pulse (_outputQueue)
Monitor.Exit (_outputQueue)
return ret
def QueueInput (line as string):
EnqueueCommand (ShellCommand (ShellCommandType.Eval, line))
def ThreadRun():
Application.Init()
GLib.Idle.Add(ProcessCommands)
try:
Application.Run()
except e as System.Threading.ThreadAbortException:
return
private def ProcessCommands() as bool:
com as ShellCommand
try:
Monitor.Enter (_commandQueue)
if _commandQueue.Count == 0:
Monitor.Exit (_commandQueue)
System.Threading.Thread.Sleep (100)
return true
com = _commandQueue.Dequeue()
Monitor.Enter(_interpreter)
if com.Type == ShellCommandType.Eval:
if com.Data is not null:
try:
_interpreter.LoopEval(com.Data)
except e:
// Sanitize stack trace to not show method calls
// from the boo interpreter
exception = e.InnerException
message = exception.ToString ()
reg = Regex ('((.*\\n)*).*Input\\dModule.*', RegexOptions.Multiline)
match = reg.Match (exception.StackTrace)
if match is not null:
if match.Groups.Count >= 3:
// the [0:-1] is to trim the extra \n hiding on the end of the match
message = String.Format ("{0}: {1}\n{2}", exception.GetType (),
exception.Message,
match.Groups[1].Value[0:-1])
self.print (message)
elif com.Type == ShellCommandType.Reset:
_interpreter.Reset()
elif com.Type == ShellCommandType.Load:
if com.Data is not null:
_interpreter.load(com.Data)
Monitor.Exit(_interpreter)
com.Type = ShellCommandType.NoOp
if _commandQueue.Count == 0:
Monitor.Enter (_outputQueue)
_processing = "false"
Monitor.Pulse (_outputQueue)
Monitor.Exit (_outputQueue)
except e as ThreadAbortException:
System.Threading.Thread.ResetAbort ()
return false
ensure:
Monitor.Exit (_commandQueue)
return true
def Run():
kickOffGuiThread()
private def kickOffGuiThread():
_start as ThreadStart = ThreadRun
_thread = System.Threading.Thread (_start)
_thread.IsBackground = true
_thread.Start ()
private def print(obj):
Monitor.Enter (_outputQueue)
_outputQueue.Enqueue(obj.ToString ())
Monitor.Exit (_outputQueue)
private def EnqueueCommand (command as ShellCommand):
if not _thread.IsAlive:
kickOffGuiThread()
try:
Monitor.Enter (_commandQueue)
_commandQueue.Enqueue (command)
Monitor.Enter (_outputQueue)
_processing = "true"
Monitor.Pulse (_outputQueue)
Monitor.Exit (_outputQueue)
ensure:
Monitor.Pulse (_commandQueue)
Monitor.Exit (_commandQueue)
def Dispose ():
if _thread.IsAlive:
_thread.Abort ()
super ()
public enum ShellCommandType:
NoOp
Reset
Load
Eval
public struct ShellCommand:
Type as ShellCommandType
Data as string
def constructor (type, data):
self.Type = type
self.Data = data
|