-
Notifications
You must be signed in to change notification settings - Fork 26
Refactor netfx #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -1,16 +1,25 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Globalization; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Reflection; | ||
using System.Runtime.InteropServices; | ||
using NXPorts.Attributes; | ||
|
||
namespace ClrLoader | ||
{ | ||
public static class ClrLoader | ||
{ | ||
delegate int EntryPoint(IntPtr buffer, int size); | ||
static bool _initialized = false; | ||
static List<DomainData> _domains = new List<DomainData>(); | ||
|
||
[DllExport("pyclr_initialize", CallingConvention.Cdecl)] | ||
public static void Initialize() | ||
{ | ||
if (!_initialized) | ||
{ | ||
_domains.Add(new DomainData(AppDomain.CurrentDomain)); | ||
_initialized = true; | ||
} | ||
} | ||
|
||
[DllExport("pyclr_create_appdomain", CallingConvention.Cdecl)] | ||
public static IntPtr CreateAppDomain( | ||
|
@@ -29,11 +38,9 @@ public static IntPtr CreateAppDomain( | |
|
||
Print($"Located domain {domain}"); | ||
|
||
var handle = GCHandle.Alloc(domain, GCHandleType.Pinned); | ||
|
||
Print($"Created handle {handle}"); | ||
|
||
return handle.AddrOfPinnedObject(); | ||
var domainData = new DomainData(domain); | ||
_domains.Add(domainData); | ||
return new IntPtr(_domains.Count - 1); | ||
} | ||
else | ||
{ | ||
|
@@ -51,18 +58,8 @@ public static IntPtr GetFunction( | |
{ | ||
try | ||
{ | ||
var domainObj = AppDomain.CurrentDomain; | ||
if (domain != IntPtr.Zero) | ||
{ | ||
var handle = GCHandle.FromIntPtr(domain); | ||
domainObj = (AppDomain)handle.Target; | ||
} | ||
|
||
var assembly = domainObj.Load(AssemblyName.GetAssemblyName(assemblyPath)); | ||
var type = assembly.GetType(typeName, throwOnError: true); | ||
Print($"Loaded type {type}"); | ||
var deleg = Delegate.CreateDelegate(typeof(EntryPoint), type, function); | ||
|
||
var domainData = _domains[(int)domain]; | ||
var deleg = domainData.GetEntryPoint(assemblyPath, typeName, function); | ||
return Marshal.GetFunctionPointerForDelegate(deleg); | ||
} | ||
catch (Exception exc) | ||
|
@@ -77,21 +74,38 @@ public static void CloseAppDomain(IntPtr domain) | |
{ | ||
if (domain != IntPtr.Zero) | ||
{ | ||
var handle = GCHandle.FromIntPtr(domain); | ||
var domainObj = (AppDomain)handle.Target; | ||
AppDomain.Unload(domainObj); | ||
handle.Free(); | ||
try | ||
{ | ||
var domainData = _domains[(int)domain]; | ||
domainData.Dispose(); | ||
} | ||
catch (Exception exc) | ||
{ | ||
Print($"Exception in {nameof(CloseAppDomain)}: {exc.GetType().Name} {exc.Message}\n{exc.StackTrace}"); | ||
} | ||
} | ||
} | ||
|
||
[DllExport("pyclr_finalize", CallingConvention.Cdecl)] | ||
public static void Close() | ||
{ | ||
foreach (var domainData in _domains) | ||
{ | ||
domainData.Dispose(); | ||
} | ||
|
||
_domains.Clear(); | ||
_initialized = false; | ||
} | ||
|
||
#if DEBUG | ||
static void Print(string s) | ||
#if DEBUG | ||
internal static void Print(string s) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder why not just use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I'll probably do that later, thanks for the hint :) |
||
{ | ||
Console.WriteLine(s); | ||
} | ||
#else | ||
static void Print(string s) {} | ||
#endif | ||
#else | ||
internal static void Print(string s) { } | ||
#endif | ||
} | ||
|
||
} |
This file contains hidden or 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,61 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
|
||
namespace ClrLoader | ||
{ | ||
using static ClrLoader; | ||
|
||
class DomainData : IDisposable | ||
{ | ||
public delegate int EntryPoint(IntPtr buffer, int size); | ||
|
||
bool _disposed = false; | ||
|
||
public AppDomain Domain { get; } | ||
public Dictionary<(string, string, string), EntryPoint> _delegates; | ||
|
||
public DomainData(AppDomain domain) | ||
{ | ||
Domain = domain; | ||
_delegates = new Dictionary<(string, string, string), EntryPoint>(); | ||
} | ||
|
||
public EntryPoint GetEntryPoint(string assemblyPath, string typeName, string function) | ||
{ | ||
if (_disposed) | ||
throw new InvalidOperationException("Domain is already disposed"); | ||
|
||
var key = (assemblyPath, typeName, function); | ||
|
||
EntryPoint result; | ||
|
||
if (!_delegates.TryGetValue(key, out result)) | ||
{ | ||
var assembly = Domain.Load(AssemblyName.GetAssemblyName(assemblyPath)); | ||
var type = assembly.GetType(typeName, throwOnError: true); | ||
|
||
Print($"Loaded type {type}"); | ||
result = (EntryPoint)Delegate.CreateDelegate(typeof(EntryPoint), type, function); | ||
|
||
_delegates[key] = result; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (!_disposed) | ||
{ | ||
_delegates.Clear(); | ||
|
||
if (Domain != AppDomain.CurrentDomain) | ||
AppDomain.Unload(Domain); | ||
|
||
_disposed = true; | ||
} | ||
} | ||
|
||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.