Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Convert assembly load methods to exceptions versus status return

  • Loading branch information...
commit e6febd45d53c983d696b83de62cf794be1d165aa 1 parent 46e8f4b
@dmiller dmiller authored
Showing with 169 additions and 66 deletions.
  1. +140 −15 Clojure/Clojure/CljCompiler/Compiler.cs
  2. +29 −51 Clojure/Clojure/Lib/RT.cs
View
155 Clojure/Clojure/CljCompiler/Compiler.cs
@@ -1410,20 +1410,55 @@ private static void Compile1(TypeBuilder tb, CljILGen ilg, ObjExpr objx, object
#region Loading
- internal static bool LoadAssembly(FileInfo assyInfo, string relativePath)
+ internal static void LoadAssembly(FileInfo assyInfo, string relativePath)
{
- Assembly assy = Assembly.LoadFrom(assyInfo.FullName);
- return InitAssembly(assy, relativePath);
+ Assembly assy;
+
+ try
+ {
+ assy = Assembly.LoadFrom(assyInfo.FullName);
+ }
+ catch (IOException e)
+ {
+ throw new AssemblyNotFoundException(e.Message,e);
+ }
+ catch (ArgumentException e)
+ {
+ throw new AssemblyNotFoundException(e.Message,e);
+ }
+ catch (BadImageFormatException e)
+ {
+ throw new AssemblyNotFoundException(e.Message, e);
+ }
+ catch (System.Security.SecurityException e)
+ {
+ throw new AssemblyNotFoundException(e.Message, e);
+ }
+
+ InitAssembly(assy, relativePath);
}
- internal static bool LoadAssembly(byte[] assyData, string relativePath)
+ internal static void LoadAssembly(byte[] assyData, string relativePath)
{
- Assembly assy = Assembly.Load(assyData);
- return InitAssembly(assy, relativePath);
+ Assembly assy;
+ try
+ {
+ assy = Assembly.Load(assyData);
+ }
+ catch (ArgumentException e)
+ {
+ throw new AssemblyNotFoundException(e.Message, e);
+ }
+ catch (BadImageFormatException e)
+ {
+ throw new AssemblyNotFoundException(e.Message, e);
+ }
+
+ InitAssembly(assy, relativePath);
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
- private static bool InitAssembly(Assembly assy, string relativePath)
+ private static void InitAssembly(Assembly assy, string relativePath)
{
Type initType = assy.GetType(InitClassName(relativePath));
if (initType == null)
@@ -1431,24 +1466,21 @@ private static bool InitAssembly(Assembly assy, string relativePath)
initType = assy.GetType("__Init__"); // old init class name
if (initType == null)
{
- Console.WriteLine("Bad assembly");
- return false;
+ throw new AssemblyInitializationException(String.Format("Cannot find initializer for {0}.{1}",assy.FullName,relativePath));
}
}
- return InvokeInitType(assy, initType);
+ InvokeInitType(assy, initType);
}
- private static bool InvokeInitType(Assembly assy, Type initType)
+ private static void InvokeInitType(Assembly assy, Type initType)
{
try
{
initType.InvokeMember("Initialize", BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public, Type.DefaultBinder, null, new object[0]);
- return true;
}
catch (Exception e)
{
- Console.WriteLine("Error initializing {0}: {1}", assy.FullName, e);
- return false;
+ throw new AssemblyInitializationException(String.Format("Error initializing {0}: {1}", assy.FullName, e.Message),e);
}
}
@@ -1470,7 +1502,15 @@ internal static bool TryLoadInitType(string relativePath)
}
if (initType == null)
return false;
- return InvokeInitType(initType.Assembly, initType);
+ try
+ {
+ InvokeInitType(initType.Assembly, initType);
+ return true;
+ }
+ catch (Exception)
+ {
+ return false;
+ }
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "load")]
@@ -1799,5 +1839,90 @@ public override void GetObjectData(SerializationInfo info, StreamingContext cont
#endregion
}
+ #endregion
+
+ #region AssemblyLoadException
+
+ [Serializable]
+ public class AssemblyLoadException : Exception
+ {
+ #region C-tors
+
+ public AssemblyLoadException()
+ {
+ }
+
+ public AssemblyLoadException(string msg)
+ : base(msg)
+ {
+ }
+
+ public AssemblyLoadException(string msg, Exception innerException)
+ : base(msg, innerException)
+ {
+ }
+
+ protected AssemblyLoadException(SerializationInfo info, StreamingContext context)
+ : base(info, context)
+ {
+ }
+
+ #endregion
+ }
+
+ [Serializable]
+ public sealed class AssemblyNotFoundException : AssemblyLoadException
+ {
+ #region C-tors
+
+ public AssemblyNotFoundException()
+ {
+ }
+
+ public AssemblyNotFoundException(string msg)
+ : base(msg)
+ {
+ }
+
+ public AssemblyNotFoundException(string msg, Exception innerException)
+ : base(msg, innerException)
+ {
+ }
+
+ private AssemblyNotFoundException(SerializationInfo info, StreamingContext context)
+ : base(info, context)
+ {
+ }
+
+ #endregion
+ }
+
+ [Serializable]
+ public sealed class AssemblyInitializationException : AssemblyLoadException
+ {
+ #region C-tors
+
+ public AssemblyInitializationException()
+ {
+ }
+
+ public AssemblyInitializationException(string msg)
+ : base(msg)
+ {
+ }
+
+ public AssemblyInitializationException(string msg, Exception innerException)
+ : base(msg, innerException)
+ {
+ }
+
+ private AssemblyInitializationException(SerializationInfo info, StreamingContext context)
+ : base(info, context)
+ {
+ }
+
+ #endregion
+ }
+
#endregion
}
View
80 Clojure/Clojure/Lib/RT.cs
@@ -3158,7 +3158,6 @@ public static void load(String relativePath)
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly")]
public static void load(String relativePath, Boolean failIfNotFound)
{
- bool loaded = false;
string cljname = relativePath + ".clj";
string assemblyname = relativePath.Replace('/', '.') + ".clj.dll";
@@ -3173,10 +3172,11 @@ public static void load(String relativePath, Boolean failIfNotFound)
{
try
{
- Var.pushThreadBindings(RT.mapUniqueKeys(CurrentNSVar, CurrentNSVar.deref(),
- WarnOnReflectionVar, WarnOnReflectionVar.deref(),
- RT.UncheckedMathVar, RT.UncheckedMathVar.deref()));
- loaded = Compiler.LoadAssembly(assyInfo, relativePath);
+ Var.pushThreadBindings(RT.mapUniqueKeys(CurrentNSVar, CurrentNSVar.deref(),
+ WarnOnReflectionVar, WarnOnReflectionVar.deref(),
+ RT.UncheckedMathVar, RT.UncheckedMathVar.deref()));
+ Compiler.LoadAssembly(assyInfo, relativePath);
+ return;
}
finally
{
@@ -3184,38 +3184,33 @@ public static void load(String relativePath, Boolean failIfNotFound)
}
}
- if(!loaded)
+ if (cljInfo != null)
{
- if (cljInfo != null)
- {
- if (booleanCast(Compiler.CompileFilesVar.deref()))
- Compile(cljInfo, cljname);
- else
- LoadScript(cljInfo, cljname);
- loaded = true;
- }
+ if (booleanCast(Compiler.CompileFilesVar.deref()))
+ Compile(cljInfo, cljname);
+ else
+ LoadScript(cljInfo, cljname);
+ return;
}
}
-
- if (!loaded)
+ try
{
- try
- {
- Var.pushThreadBindings(RT.map(CurrentNSVar, CurrentNSVar.deref(),
- WarnOnReflectionVar, WarnOnReflectionVar.deref(),
- RT.UncheckedMathVar, RT.UncheckedMathVar.deref()));
- loaded = Compiler.TryLoadInitType(relativePath);
- }
- finally
- {
- Var.popThreadBindings();
- }
- if (!loaded)
- {
- loaded = TryLoadFromEmbeddedResource(relativePath, assemblyname);
- }
+ Var.pushThreadBindings(RT.map(CurrentNSVar, CurrentNSVar.deref(),
+ WarnOnReflectionVar, WarnOnReflectionVar.deref(),
+ RT.UncheckedMathVar, RT.UncheckedMathVar.deref()));
+ if (Compiler.TryLoadInitType(relativePath))
+ return;
}
+ finally
+ {
+ Var.popThreadBindings();
+ }
+
+
+ bool loaded = TryLoadFromEmbeddedResource(relativePath, assemblyname);
+
+
if (!loaded && failIfNotFound)
throw new FileNotFoundException(String.Format("Could not locate {0} or {1} on load path.", assemblyname, cljname));
@@ -3232,14 +3227,15 @@ private static bool TryLoadFromEmbeddedResource(string relativePath, string asse
Var.pushThreadBindings(RT.map(CurrentNSVar, CurrentNSVar.deref(),
WarnOnReflectionVar, WarnOnReflectionVar.deref(),
RT.UncheckedMathVar, RT.UncheckedMathVar.deref()));
- if (Compiler.LoadAssembly(ReadStreamBytes(asmStream), relativePath))
- return true;
+ Compiler.LoadAssembly(ReadStreamBytes(asmStream), relativePath);
+ return true;
}
finally
{
Var.popThreadBindings();
}
}
+
var embeddedCljName = relativePath.Replace("/", ".") + ".clj";
var stream = GetEmbeddedResourceStream(embeddedCljName, out containingAssembly);
if (stream != null)
@@ -3292,24 +3288,6 @@ private static void Compile(FileInfo cljInfo, string relativePath)
Compile(cljInfo.Directory.FullName, cljInfo.Name, rdr, relativePath);
}
- // TODO: Get rid of this when DLR gone
- //public static bool CompileDLR = true;
- //public static void CompileNoDLR(String relativePath)
- //{
- // try
- // {
- // CompileDLR = false;
- // string cljname = relativePath + ".clj";
- // FileInfo cljInfo = FindFile(cljname);
- // using (TextReader rdr = cljInfo.OpenText())
- // Compiler.CompileNoDlr(rdr, cljInfo.Directory.FullName, cljInfo.Name, cljname);
- // }
- // finally
- // {
- // CompileDLR = true;
- // }
- //}
-
private static void Compile(string dirName, string name, TextReader rdr, string relativePath)
{
Compiler.Compile(rdr, dirName, name, relativePath);
Please sign in to comment.
Something went wrong with that request. Please try again.