Error upon compiling script with functions on smart-compiler branch (.NET)
Opened this issue · 2 comments
Filing this for future reference; yours truly and polyethene have researched this bug extensively and as of yet did not come up with with a solution.
To reproduce, check out the smart-compiler branch and enable merge, either at compile-time or manually in Program.cs. Then compile the following:
f() {
MsgBox, test
}
f()
On .NET a TypeLoadException of sorts will be thrown (poly, could you please insert the actual stacktrace?).
Running the following code on Windows 7 x64 via Visual Studio 2010:
#AssemblyMerge
f() {
MsgBox, test
}
f()
Results in:
System.TypeLoadException was unhandled
Message=Could not load type 'Core' from assembly 'test.exe, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
Source=mscorlib
TypeName=Core
StackTrace:
at System.RuntimeTypeHandle.GetDeclaringType()
at System.RuntimeType.RuntimeTypeCache.GetEnclosingType()
at System.RuntimeType.get_DeclaringType()
at System.Reflection.Emit.ModuleBuilder.GetTypeRefNested(Type type, Module refedModule, String strRefedModuleFileName)
at System.Reflection.Emit.ModuleBuilder.GetTypeTokenWorkerNoLock(Type type, Boolean getGenericDefinition)
at System.Reflection.Emit.ModuleBuilder.GetTypeTokenInternal(Type type, Boolean getGenericDefinition)
at System.Reflection.Emit.ModuleBuilder.InternalGetConstructorToken(ConstructorInfo con, Boolean usingRef)
at System.Reflection.Emit.ILGenerator.GetMethodToken(MethodBase method, Type[] optionalParameterTypes)
at System.Reflection.Emit.ILGenerator.Emit(OpCode opcode, ConstructorInfo con)
at IronAHK.Scripting.MethodWriter.EmitDelegateCreateExpression(CodeDelegateCreateExpression del) in X:\dev\IronAHK\Scripting\Compiler\Emission\EmitExpr.cs:line 122
at IronAHK.Scripting.MethodWriter.EmitExpression(CodeExpression Expression, Boolean ForceTypes) in X:\dev\IronAHK\Scripting\Compiler\Emission\EmitExpr.cs:line 74
at IronAHK.Scripting.MethodWriter.EmitAssignment(CodeExpression Left, CodeExpression Right, Boolean ForceTypes) in X:\dev\IronAHK\Scripting\Compiler\Emission\EmitVars.cs:line 84
at IronAHK.Scripting.MethodWriter.EmitBinaryOperator(CodeBinaryOperatorExpression Binary, Boolean ForceTypes) in X:\dev\IronAHK\Scripting\Compiler\Emission\EmitExpr.cs:line 155
at IronAHK.Scripting.MethodWriter.EmitExpression(CodeExpression Expression, Boolean ForceTypes) in X:\dev\IronAHK\Scripting\Compiler\Emission\EmitExpr.cs:line 48
at IronAHK.Scripting.MethodWriter.EmitExpressionStatement(CodeExpressionStatement Expression, Boolean ForceTypes) in X:\dev\IronAHK\Scripting\Compiler\Emission\EmitExpr.cs:line 14
at IronAHK.Scripting.MethodWriter.EmitStatement(CodeStatement Statement, Boolean ForceTypes) in X:\dev\IronAHK\Scripting\Compiler\Emission\MethodWriter.cs:line 50
at IronAHK.Scripting.MethodWriter.EmitStatement(CodeStatement Statement) in X:\dev\IronAHK\Scripting\Compiler\Emission\MethodWriter.cs:line 35
at IronAHK.Scripting.MethodWriter.EmitStatementCollection(CodeStatementCollection Statements) in X:\dev\IronAHK\Scripting\Compiler\Emission\MethodWriter.cs:line 27
at IronAHK.Scripting.MethodWriter.Emit() in X:\dev\IronAHK\Scripting\Compiler\Emission\MethodWriter.cs:line 11
at IronAHK.Scripting.Compiler.EmitType(ModuleBuilder Parent, CodeTypeDeclaration Decl) in X:\dev\IronAHK\Scripting\Compiler\Peripheral\TypeEmitter.cs:line 30
at IronAHK.Scripting.Compiler.EmitNamespace(AssemblyBuilder Parent, CodeNamespace Namespace) in X:\dev\IronAHK\Scripting\Compiler\Peripheral\NamespaceEmitter.cs:line 25
at IronAHK.Scripting.Compiler.CompileAssemblyFromDomBatch(CompilerParameters options, CodeCompileUnit[] compilationUnits) in X:\dev\IronAHK\Scripting\Compiler\Compiler.cs:line 20
at IronAHK.Scripting.IACodeProvider.CompileAssemblyFromDom(CompilerParameters options, CodeCompileUnit[] compilationUnits) in X:\dev\IronAHK\Scripting\IACodeProvider.cs:line 111
at IronAHK.Scripting.IACodeProvider.CompileAssemblyFromFile(CompilerParameters options, String[] fileNames) in X:\dev\IronAHK\Scripting\IACodeProvider.cs:line 96
at IronAHK.Program.Main(String[] args) in X:\dev\IronAHK\IronAHK\Program.cs:line 201
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
In commit 11c8f14 I added a force
parameter for the #AssemblyMerge
directive so one can assert assembly merging for scripts.
The intention of this was to allow a fallback mode to overcome the current limitation which only seems to happen when there are user defined functions in the code. Not all scripts use this and it would be beneficial for .NET users to have smaller independent binaries for their simple scripts.
Could you please modify the compiler to recognise the MergeFallbackToLink
option, if it is true
(and Merge
is true
) then it should catch the TypeLoadException
and silently revert to the regular linking mode for compilation. For testing, specify #AssemblyMerge force
in a script which will instruct the parser to leave the MergeFallbackToLink
option as false
, which would result in the same error I posted above.