CosmosOS / Cosmos

Cosmos is an operating system "construction kit". Build your own OS using managed languages such as C#, VB.NET, and more!
https://www.goCosmos.org
BSD 3-Clause "New" or "Revised" License
2.94k stars 552 forks source link

`Native Code Encountered` when trying to subscribe to an event. #3046

Closed Vardan2009 closed 2 weeks ago

Vardan2009 commented 4 months ago

Area of Cosmos - What area of Cosmos are we dealing with?

Events

Expected Behaviour - What do you think that should happen?

Be able to subscribe to a custom event

Actual Behaviour - What unexpectedly happens?

The compilation fails with the following message Exception: System.Exception: Native code encountered, plug required. Chech build output for more information I assume this means that Cosmos is missing a plug for Event Handling, but I thought Cosmos had an implementation for that?

Reproduction - How did you get this error to appear?

Create custom event args

public class MouseEventArgs : EventArgs
{
    public uint x;
    public uint y;
}

Create an event in another class

public event EventHandler<MouseEventArgs> MouseClick;
// Call this when need to invoke event
protected virtual void OnMouseClick(MouseEventArgs e) => MouseClick?.Invoke(this, e);

Try to subscribe to the event from other class

// if I only remove this single line, the whole code compiles successfully, the only part that causes
// the compiler to throw an error is when trying to subscribe to the event
clickBtn.MouseClick += OnClickBtnClick;
/// ... ///
private void OnClickBtnClick(object sender, MouseEventArgs e)
{
    clickCount++;
    countLabel.Text = $"Clicked the button {clickCount} times";
}

Version - Were you using the User Kit or Dev Kit? And what User Kit version or Dev Kit commit (Cosmos, IL2CPU, X#)?

Latest DevKit with C#

9xbt commented 4 months ago

That means something isn't plugged. Could you show us the full output log so we can see which method needs to be plugged?

Vardan2009 commented 4 months ago

This is the part of the output since the error

1>IL2CPU : error : Exception: System.Exception: Native code encountered, plug required. Check build output for more information.
1>  DO NOT REPORT THIS AS A BUG.
1>  Please see http://www.gocosmos.org/docs/plugs/missing/
1>  Need plug for: System.MulticastDelegate  System.Delegate.InternalAllocLike(System.Delegate)(Plug Signature: System_MulticastDelegate__System_Delegate_InternalAllocLike_System_Delegate_ ).
1>  Static: True
1>  Assembly: System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e
1>  Called from:
1>System.MulticastDelegate::System.MulticastDelegate NewMulticastDelegate(System.Object[], Int32, Boolean)
1>System.MulticastDelegate::System.Delegate CombineImpl(System.Delegate)
1>System.Delegate::System.Delegate CombineImpl(System.Delegate)
1>
1>
1>   at Cosmos.IL2CPU.ILScanner.ScanMethod(MethodBase aMethod, Boolean aIsPlug, String sourceItem) in C:\Users\user\Documents\GitHub\CosmosOS\IL2CPU\source\Cosmos.IL2CPU\ILScanner.cs:line 562
1>   at Cosmos.IL2CPU.ILScanner.ScanQueue() in C:\Users\user\Documents\GitHub\CosmosOS\IL2CPU\source\Cosmos.IL2CPU\ILScanner.cs:line 751
1>   at Cosmos.IL2CPU.ILScanner.Execute(MethodBase aStartMethod, IEnumerable`1 plugsAssemblies) in C:\Users\user\Documents\GitHub\CosmosOS\IL2CPU\source\Cosmos.IL2CPU\ILScanner.cs:line 270
1>   at Cosmos.IL2CPU.CompilerEngine.Execute() in C:\Users\user\Documents\GitHub\CosmosOS\IL2CPU\source\Cosmos.IL2CPU\CompilerEngine.cs:line 187
1>IL2CPU task took 00:00:03.1548308
ADev531 commented 3 weeks ago

Sorry @gamma63 , EventHandler is not working as bug of IL2CPU. (Bug of MulticastDelegate Plugs) You need to use List to do it.

ascpixi commented 2 weeks ago

Bit late, but this indeed is a bug and is tracked in #2765.