Skip to content

Commit

Permalink
Beginning to add tests to the weaver. Working on brining HostedMode back
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank Laub committed Nov 2, 2009
1 parent 293c720 commit 3325642
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 38 deletions.
22 changes: 12 additions & 10 deletions src/DotWeb.Runtime/HostedMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Reflection;
using System.IO;
using DotWeb.Utility;
using DotWeb.Tools.Weaver;

namespace DotWeb.Runtime
{
Expand Down Expand Up @@ -69,17 +70,18 @@ public HostedMode(string binPath) {

public IPEndPoint EndPoint { get { return (IPEndPoint)this.listener.LocalEndpoint; } }

//public string PrepareType(string fullTypeName) {
// string[] parts = fullTypeName.Split(',');
// string typeName = parts[0].Trim();
// string asmName = parts[1].Trim();

// var fixup = new AssemblyReferenceFixup(this.binPath);
// var altName = fixup.FixupReferences(asmName, false);
public string PrepareType(AssemblyQualifiedTypeName aqtn) {
var weaver = new HostingWeaver(this.binPath, this.binPath, new string[] { this.binPath });
string path = Path.Combine(this.binPath, aqtn.AssemblyName.Name);
if (!path.EndsWith(".dll")) {
path += ".dll";
}

// var ret = string.Format("{0}, {1}", typeName, altName.Name);
// return ret;
//}
var asm = weaver.ProcessAssembly(path);
var asmName = asm.GetName();
aqtn.AssemblyName.Name = asmName.Name;
return aqtn.ToString();
}

public void Start() {
this.listener.Start();
Expand Down
4 changes: 4 additions & 0 deletions src/DotWeb.Utility/AssemblyQualifiedTypeName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,9 @@ public AssemblyQualifiedTypeName(string asmQualifiedTypeName) {

public AssemblyName AssemblyName { get; private set; }
public string TypeName { get; private set; }

public override string ToString() {
return string.Format("{0}, {1}", TypeName, AssemblyName);
}
}
}
5 changes: 3 additions & 2 deletions src/DotWeb.Web/ClientCodeRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ private void RenderHostedMode(HtmlTextWriter writer) {
}

var ip = hostedMode.EndPoint;
// var src = hostedMode.PrepareType(Source);
var aqtn = new AssemblyQualifiedTypeName(Source);
var src = hostedMode.PrepareType(aqtn);

//<embed id="__$plugin" type="application/x-dotweb"/>
writer.AddAttribute(HtmlTextWriterAttribute.Id, "__$plugin");
Expand All @@ -146,7 +147,7 @@ private void RenderHostedMode(HtmlTextWriter writer) {
writer.RenderBeginTag(HtmlTextWriterTag.Script);

writer.WriteLine(Resources.JsHelper);
string js = string.Format(Resources.HostedEntry, ip.Port, Source);
string js = string.Format(Resources.HostedEntry, ip.Port, src);

writer.WriteLine(js);
writer.RenderEndTag();
Expand Down
5 changes: 5 additions & 0 deletions test/DotWeb.Hosting.Test/DotWeb.Hosting.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="CachingObjectFactory.cs" />
<Compile Include="HostingWeaverTest.cs" />
<Compile Include="JsBridgeTest.cs" />
<Compile Include="JsFunctionTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand All @@ -76,6 +77,10 @@
<Project>{F02F94A5-422E-4585-BB7B-160A8C6B488F}</Project>
<Name>Hosted-DotWeb.System</Name>
</ProjectReference>
<ProjectReference Include="..\..\tools\DotWeb.Tools.Weaver\DotWeb.Tools.Weaver.csproj">
<Project>{DE8A6C27-B315-41E3-82BF-7A3741C7653D}</Project>
<Name>DotWeb.Tools.Weaver</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
55 changes: 55 additions & 0 deletions test/DotWeb.Hosting.Test/HostingWeaverTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using DotWeb.Tools.Weaver;

namespace DotWeb.Hosting.Test
{
[TestFixture]
public class HostingWeaverTest
{
[Test]
public void SanityTest() {
var weaver = new HostingWeaver(inputDir, outputDir, new string[] { searchDir });
weaver.ProcessAssembly(asmPath);
}

[Test]
public void ExternTest() {
}

[Test]
public void EnumTest() {
}

[Test]
public void TypeTest() {
}

[Test]
public void GenericTest() {
}

[Test]
public void CastTest() {
}

[Test]
public void IsInstanceTest() {
}

[Test]
public void ArrayTest() {
}

[Test]
public void ExceptionTest() {
}

[Test]
public void DependencyTest() {
}
}
}
7 changes: 5 additions & 2 deletions tools/DotWeb.Tools.Weaver/AssemblyProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,16 @@ public IType ResolveTypeReference(TypeReference typeRef) {

private IType ProcessType(TypeReference typeRef) {
var typeDef = typeRef.Resolve();
if (typeRef is ArrayType) {
}

if (typeDef.IsEnum) {
var enumProc = new EnumProcessor(this.resolver, this, typeRef, this.moduleBuilder);
var enumProc = new EnumProcessor(this.resolver, this, typeDef, this.moduleBuilder);
RegisterType(typeRef, enumProc);
return enumProc;
}
else {
var typeProc = new TypeProcessor(this.resolver, this, typeRef, this.moduleBuilder);
var typeProc = new TypeProcessor(this.resolver, this, typeDef, this.moduleBuilder);
RegisterType(typeRef, typeProc);
return typeProc;
}
Expand Down
4 changes: 2 additions & 2 deletions tools/DotWeb.Tools.Weaver/EnumProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ class EnumProcessor : IType
private TypeDefinition typeDef;
private EnumBuilder enumBuilder;

public EnumProcessor(IResolver resolver, AssemblyProcessor parent, TypeReference typeRef, ModuleBuilder moduleBuilder) {
public EnumProcessor(IResolver resolver, AssemblyProcessor parent, TypeDefinition typeDef, ModuleBuilder moduleBuilder) {
this.resolver = resolver;
this.parent = parent;
this.typeDef = typeRef.Resolve();
this.typeDef = typeDef;
var attributes = (SR.TypeAttributes)this.typeDef.Attributes & System.Reflection.TypeAttributes.VisibilityMask;

var valueField = typeDef.Fields[0];
Expand Down
41 changes: 22 additions & 19 deletions tools/DotWeb.Tools.Weaver/HostingWeaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,19 @@ namespace DotWeb.Tools.Weaver
{
public class HostingWeaver : IResolver
{
class ConstantNames
{
public const string DotWebSystem = "DotWeb.System";
public const string DotWebSystemDll = "DotWeb.System.dll";
public const string Mscorlib = "mscorlib";
public const string AssemblyWeavedAttribute = "AssemblyWeavedAttribute";
}

private string inputDir;
private string outputDir;
private DefaultAssemblyResolver asmResolver = new DefaultAssemblyResolver();
private Dictionary<string, ITypeResolver> modules = new Dictionary<string, ITypeResolver>();
private static ExternalAssembly asmMscorlib;

public HostingWeaver(string inputDir, string outputDir, string[] searchDirs) {
this.inputDir = inputDir;
Expand All @@ -33,39 +42,33 @@ public HostingWeaver(string inputDir, string outputDir, string[] searchDirs) {
}

private void PrepareMscorlib() {
var sysType = typeof(object);
var asm = sysType.Assembly;
var asmName = AssemblyNameReference.Parse(asm.FullName);
if (asmMscorlib == null) {
var sysType = typeof(object);
var asm = sysType.Assembly;
var asmName = AssemblyNameReference.Parse(asm.FullName);

var asmDef = this.asmResolver.Resolve(asmName);
var extAsm = new ExternalAssembly(this, asm);
this.modules.Add("mscorlib", extAsm);
var asmDef = this.asmResolver.Resolve(asmName);
asmMscorlib = new ExternalAssembly(this, asm);
}
this.modules.Add(ConstantNames.Mscorlib, asmMscorlib);
}

private void PrepareDotWebSystem() {
var proc = new DotWebSystemAssembly(this);
this.modules.Add("DotWeb.System", proc);
this.modules.Add("DotWeb.System.dll", proc);
this.modules.Add(ConstantNames.DotWebSystem, proc);
this.modules.Add(ConstantNames.DotWebSystemDll, proc);
}

public Assembly ProcessAssembly(string asmPath) {
var asmDef = AssemblyFactory.GetAssembly(asmPath);

foreach (CustomAttribute item in asmDef.CustomAttributes) {
if (item.Constructor.DeclaringType.Name == "AssemblyWeavedAttribute") {
if (item.Constructor.DeclaringType.Name == ConstantNames.AssemblyWeavedAttribute) {
Console.WriteLine("This assembly has already been weaved and is ready for hosted mode");
return Assembly.LoadFrom(asmPath);
}
}

//string name = Path.GetFileNameWithoutExtension(fileName);
//var asmName = new AssemblyName(name);
//var ext = Path.GetExtension(asmPath);
//string fileName = asmName.Name + ext;

// this.asmBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(asmName, AssemblyBuilderAccess.RunAndSave, this.outputDir);
// var moduleBuilder = this.asmBuilder.DefineDynamicModule(asmDef.Name.Name, fileName, true);

return ProcessAssembly(asmDef);
}

Expand All @@ -74,7 +77,7 @@ private Assembly ProcessAssembly(AssemblyDefinition asmDef) {
if (this.modules.ContainsKey(asmRef.Name))
continue;

if (asmRef.Name == "DotWeb.System") {
if (asmRef.Name == ConstantNames.DotWebSystem) {
PrepareDotWebSystem();
continue;
}
Expand All @@ -97,7 +100,7 @@ private IType ResolveType(TypeReference typeRef) {
var scope = typeRef.Scope;
string key;
if (scope == null)
key = "mscorlib";
key = ConstantNames.Mscorlib;
else
key = scope.Name;
var moduleProc = this.modules[key];
Expand Down
5 changes: 2 additions & 3 deletions tools/DotWeb.Tools.Weaver/TypeProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,15 @@ class TypeProcessor : IType
private TypeBuilder typeBuilder;
private Dictionary<MetadataToken, MethodBase> methods = new Dictionary<MetadataToken, MethodBase>();
private Dictionary<FieldDefinition, FieldBuilder> fields = new Dictionary<FieldDefinition, FieldBuilder>();
// private List<IType> referencedTypes = new List<IType>();
private IResolver resolver;
private GenericTypeProcessor genericProc;
private bool isClosing = false;
private List<TypeProcessor> dependentTypes = new List<TypeProcessor>();

public TypeProcessor(IResolver resolver, AssemblyProcessor parent, TypeReference typeRef, ModuleBuilder moduleBuilder) {
public TypeProcessor(IResolver resolver, AssemblyProcessor parent, TypeDefinition typeDef, ModuleBuilder moduleBuilder) {
this.resolver = resolver;
this.parent = parent;
this.typeDef = typeRef.Resolve();
this.typeDef = typeDef;
this.ModuleBuilder = moduleBuilder;
this.genericProc = new GenericTypeProcessor(this.resolver);

Expand Down

0 comments on commit 3325642

Please sign in to comment.