Skip to content
This repository has been archived by the owner on Nov 1, 2020. It is now read-only.

Wasm: Add support for DefaultConstructorOf and hence Activator.CreateInstance<T> #8279

Merged
merged 2 commits into from
Aug 25, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/ILCompiler.WebAssembly/src/CodeGen/ILToWebAssemblyImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eeType -> ctor ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, corrected

}

return true;
}
break;
}

return false;
Expand Down
9 changes: 9 additions & 0 deletions tests/src/Simple/HelloWasm/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -2608,6 +2610,13 @@ static void TestJavascriptCall()
EndTest(resultPtr.ToInt32() == 42);
}

static void TestDefaultConstructorOf()
{
StartTest("Test DefaultConstructorOf");
var c = Activator.CreateInstance<ClassForNre>();
EndTest(c != null);
}

static ushort ReadUInt16()
{
// something with MSB set
Expand Down