From 49f2223d76d037e49a656c4cdddc9e7cd4d41604 Mon Sep 17 00:00:00 2001 From: Scott Waye Date: Mon, 24 Aug 2020 16:59:36 -0500 Subject: [PATCH 1/2] add support for DefaultConstructorOf and hence Activator.CreateInstance --- .../src/CodeGen/ILToWebAssemblyImporter.cs | 20 +++++++++++++++++++ tests/src/Simple/HelloWasm/Program.cs | 9 +++++++++ 2 files changed, 29 insertions(+) diff --git a/src/ILCompiler.WebAssembly/src/CodeGen/ILToWebAssemblyImporter.cs b/src/ILCompiler.WebAssembly/src/CodeGen/ILToWebAssemblyImporter.cs index 2e79f8a8543..73ce61eb859 100644 --- a/src/ILCompiler.WebAssembly/src/CodeGen/ILToWebAssemblyImporter.cs +++ b/src/ILCompiler.WebAssembly/src/CodeGen/ILToWebAssemblyImporter.cs @@ -2503,6 +2503,26 @@ private bool ImportIntrinsicCall(MethodDesc method, MethodDesc runtimeDetermined return true; } break; + case "DefaultConstructorOf": + if (metadataType.Namespace == "System" && metadataType.Name == "Activator" && method.Instantiation.Length == 1) + { + if (runtimeDeterminedMethod.IsRuntimeDeterminedExactMethod) + { + var ctorRef = CallGenericHelper(ReadyToRunHelperId.DefaultConstructor, runtimeDeterminedMethod.Instantiation[0]); + PushExpression(StackValueKind.Int32, "ctor", ctorRef, GetWellKnownType(WellKnownType.IntPtr)); + } + else + { + IMethodNode methodNode = (IMethodNode)_compilation.ComputeConstantLookup(ReadyToRunHelperId.DefaultConstructor, method.Instantiation[0]); + _dependencies.Add(methodNode); + + MethodDesc ctor = methodNode.Method; + PushExpression(StackValueKind.Int32, "eeType", LLVMFunctionForMethod(ctor, ctor, null, false, null, ctor, out bool _, out LLVMValueRef _, out LLVMValueRef _), GetWellKnownType(WellKnownType.IntPtr)); + } + + return true; + } + break; } return false; diff --git a/tests/src/Simple/HelloWasm/Program.cs b/tests/src/Simple/HelloWasm/Program.cs index 86803907590..e79ce363bd2 100644 --- a/tests/src/Simple/HelloWasm/Program.cs +++ b/tests/src/Simple/HelloWasm/Program.cs @@ -360,6 +360,8 @@ private static unsafe int Main(string[] args) TestJavascriptCall(); + TestDefaultConstructorOf(); + // This test should remain last to get other results before stopping the debugger PrintLine("Debugger.Break() test: Ok if debugger is open and breaks."); System.Diagnostics.Debugger.Break(); @@ -2608,6 +2610,13 @@ static void TestJavascriptCall() EndTest(resultPtr.ToInt32() == 42); } + static void TestDefaultConstructorOf() + { + StartTest("Test DefaultConstructorOf"); + var c = Activator.CreateInstance(); + EndTest(c != null); + } + static ushort ReadUInt16() { // something with MSB set From ea1812f1885ccb58075471c886c33a85faf4e5af Mon Sep 17 00:00:00 2001 From: Scott Waye Date: Tue, 25 Aug 2020 11:21:02 -0500 Subject: [PATCH 2/2] correct expression name --- .../src/CodeGen/ILToWebAssemblyImporter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ILCompiler.WebAssembly/src/CodeGen/ILToWebAssemblyImporter.cs b/src/ILCompiler.WebAssembly/src/CodeGen/ILToWebAssemblyImporter.cs index 73ce61eb859..2986dbc0b03 100644 --- a/src/ILCompiler.WebAssembly/src/CodeGen/ILToWebAssemblyImporter.cs +++ b/src/ILCompiler.WebAssembly/src/CodeGen/ILToWebAssemblyImporter.cs @@ -2517,7 +2517,7 @@ private bool ImportIntrinsicCall(MethodDesc method, MethodDesc runtimeDetermined _dependencies.Add(methodNode); MethodDesc ctor = methodNode.Method; - PushExpression(StackValueKind.Int32, "eeType", LLVMFunctionForMethod(ctor, ctor, null, false, null, ctor, out bool _, out LLVMValueRef _, out LLVMValueRef _), GetWellKnownType(WellKnownType.IntPtr)); + PushExpression(StackValueKind.Int32, "ctor", LLVMFunctionForMethod(ctor, ctor, null, false, null, ctor, out bool _, out LLVMValueRef _, out LLVMValueRef _), GetWellKnownType(WellKnownType.IntPtr)); } return true;