-
-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- runtime respects `event` members and provides them as a PHP property with special type "Pchp.Core.ClrEvent<TDelegate>" - `ClrEvent` has method `add()`
- Loading branch information
1 parent
9b9f741
commit 57bab37
Showing
3 changed files
with
195 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Text; | ||
using Pchp.Core.Reflection; | ||
|
||
namespace Pchp.Core | ||
{ | ||
public class ClrEvent<TDelegate> : IPhpCallable where TDelegate : Delegate | ||
{ | ||
sealed class Hook : IDisposable | ||
{ | ||
public Hook(object target, EventInfo eventInfo, TDelegate callback) | ||
{ | ||
EventInfo = eventInfo ?? throw new ArgumentNullException(nameof(eventInfo)); | ||
Target = target ?? throw new ArgumentNullException(nameof(target)); | ||
Callback = callback ?? throw new ArgumentNullException(nameof(callback)); | ||
} | ||
|
||
[PhpHidden] | ||
public EventInfo EventInfo { get; } | ||
|
||
[PhpHidden] | ||
public object Target { get; } | ||
|
||
[PhpHidden] | ||
public TDelegate Callback { get; } | ||
|
||
/// <summary>Alias to <see cref="Dispose"/></summary> | ||
public void Close() => Dispose(); | ||
|
||
/// <summary>Alias to <see cref="Dispose"/></summary> | ||
public void Remove() => Dispose(); | ||
|
||
public void Dispose() | ||
{ | ||
EventInfo.RemoveEventHandler(Target, Callback); | ||
} | ||
} | ||
|
||
[PhpHidden] | ||
public EventInfo EventInfo { get; } | ||
|
||
[PhpHidden] | ||
public object Target { get; } | ||
|
||
/// <summary> | ||
/// The event field name. | ||
/// </summary> | ||
public string name => EventInfo.Name; | ||
|
||
/// <summary> | ||
/// Reference to the owning object instance. | ||
/// </summary> | ||
public string @class => EventInfo.DeclaringType.GetPhpTypeInfo().Name; | ||
|
||
internal ClrEvent(Context ctx, object target, EventInfo eventInfo) | ||
{ | ||
this.Target = target; | ||
this.EventInfo = eventInfo ?? throw new ArgumentNullException(nameof(eventInfo)); | ||
} | ||
|
||
public IDisposable/*!*/add(TDelegate callback) | ||
{ | ||
if (callback == null) | ||
{ | ||
throw new ArgumentNullException(nameof(callback)); | ||
} | ||
|
||
EventInfo.AddEventHandler(Target, callback); | ||
return new Hook(Target, EventInfo, callback); | ||
} | ||
|
||
public void remove(TDelegate callback) | ||
{ | ||
EventInfo.RemoveEventHandler(Target, callback); | ||
|
||
throw new NotSupportedException(); | ||
} | ||
|
||
#region IPhpCallable | ||
|
||
PhpValue IPhpCallable.Invoke(Context ctx, params PhpValue[] arguments) | ||
{ | ||
//TODO: check caller, invoke EventInfo.RaiseMethod?.Invoke(Target, arguments) | ||
throw new NotSupportedException(); | ||
} | ||
|
||
PhpValue IPhpCallable.ToPhpValue() => PhpValue.FromClr(this); | ||
|
||
//public PhpValue __invoke([ImportValue(ImportValueAttribute.ValueSpec.CallerClass)] RuntimeTypeHandle caller, params PhpValue[] arguments) | ||
//{ | ||
// if (caller.Equals(default(RuntimeTypeHandle))) | ||
// { | ||
// return false; | ||
// } | ||
|
||
// return PhpValue.Null; | ||
//} | ||
|
||
#endregion | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters