Skip to content

NetCore 2.2.3 Support #12

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 8 commits into from
Aug 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,15 @@ This section describes all programming languages that **METACALL** supports, if

- Currently supported languages and run-times:

| Language | Runtime | Version | Tag |
|--------------------------------------------------------------------|----------------------------------------------------------------------------------------------|:------------------------:|:----:|
| [Python](https://www.python.org/) | [Python C API](https://docs.python.org/3/c-api/intro.html) | **>= 3.2 <= 3.7** | py |
| [NodeJS](https://nodejs.org/) | [N API](https://nodejs.org/api/n-api.html) | **>= 8.11.1 <= 10.15.3** | node |
| [JavaScript](https://developer.mozilla.org/bm/docs/Web/JavaScript) | [V8](https://v8.dev/) | **5.1.117** | js |
| [C#](https://dotnet.microsoft.com/) | [NetCore](https://github.com/dotnet/docs/blob/master/docs/core/tutorials/netcore-hosting.md) | **1.1.10** | cs |
| [Ruby](https://ruby-lang.org/) | [Ruby C API](https://silverhammermba.github.io/emberb/c/) | **>= 2.1 <= 2.3** | rb |
| [File](/source/loaders/file_loader) | **∅** | **0.1.0** | file |
| [Mock](/source/loaders/mock_loader) | **∅** | **0.1.0** | mock |
| Language | Runtime | Version | Tag |
|--------------------------------------------------------------------|----------------------------------------------------------------------------------------------|:------------------------------:|:----:|
| [Python](https://www.python.org/) | [Python C API](https://docs.python.org/3/c-api/intro.html) | **>= 3.2 <= 3.7** | py |
| [NodeJS](https://nodejs.org/) | [N API](https://nodejs.org/api/n-api.html) | **>= 8.11.1 <= 10.15.3** | node |
| [JavaScript](https://developer.mozilla.org/bm/docs/Web/JavaScript) | [V8](https://v8.dev/) | **5.1.117** | js |
| [C#](https://dotnet.microsoft.com/) | [NetCore](https://github.com/dotnet/docs/blob/master/docs/core/tutorials/netcore-hosting.md) | **>= 1.0.0-preview2 <= 2.2.6** | cs |
| [Ruby](https://ruby-lang.org/) | [Ruby C API](https://silverhammermba.github.io/emberb/c/) | **>= 2.1 <= 2.3** | rb |
| [File](/source/loaders/file_loader) | **∅** | **0.1.0** | file |
| [Mock](/source/loaders/mock_loader) | **∅** | **0.1.0** | mock |

- Languages and run-times under construction:

Expand Down
2 changes: 2 additions & 0 deletions deploy/packages/postinst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

NETCORE_INSTALL_DIR=/usr/share/dotnet

# TODO: NetCore 2

# Download and install NetCore
if [ ! -f $NETCORE_INSTALL_DIR/dotnet ]; then
curl -s https://dot.net/v1/dotnet-install.sh &> dotnet-install.sh \
Expand Down
6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ services:
METACALL_BASE_IMAGE: $METACALL_BASE_IMAGE
METACALL_PATH: $METACALL_PATH
METACALL_TOOLS_PATH: $METACALL_PATH/tools
METACALL_INSTALL_OPTIONS: root base python ruby netcore v8rep51 nodejs file rapidjson funchook swig pack # coverage
METACALL_INSTALL_OPTIONS: root base python ruby netcore2 v8rep51 nodejs file rapidjson funchook swig pack # coverage
environment:
DEBIAN_FRONTEND: noninteractive
LTTNG_UST_REGISTER_TIMEOUT: 0
Expand All @@ -60,7 +60,7 @@ services:
args:
METACALL_PATH: $METACALL_PATH
METACALL_BUILD_TYPE: $METACALL_CORE_BUILD_TYPE
METACALL_BUILD_OPTIONS: root python ruby netcore v8 nodejs file examples distributable tests scripts ports dynamic install pack # coverage
METACALL_BUILD_OPTIONS: root python ruby netcore2 v8 nodejs file examples distributable tests scripts ports dynamic install pack # coverage
environment:
DEBIAN_FRONTEND: noninteractive
LTTNG_UST_REGISTER_TIMEOUT: 0
Expand All @@ -83,7 +83,7 @@ services:
args:
METACALL_PATH: $METACALL_PATH
METACALL_BASE_IMAGE: $METACALL_BASE_IMAGE
METACALL_RUNTIME_OPTIONS: root base python ruby netcore v8 nodejs file ports clean
METACALL_RUNTIME_OPTIONS: root base python ruby netcore2 v8 nodejs file ports clean
environment:
DEBIAN_FRONTEND: noninteractive
LTTNG_UST_REGISTER_TIMEOUT: 0
Expand Down
31 changes: 22 additions & 9 deletions source/loaders/cs_loader/netcore/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,24 @@ message(STATUS "Plugin ${target} implementation")
# Create target
#

if(DOTNET_MIGRATE)
if(DOTNET_VERSION VERSION_EQUAL "2.0" OR DOTNET_VERSION VERSION_GREATER "2.0")
add_custom_target(${target} ALL
COMMAND dotnet migrate ${CMAKE_CURRENT_SOURCE_DIR}/source/
COMMAND dotnet restore ${CMAKE_CURRENT_SOURCE_DIR}/source/
COMMAND dotnet publish ${CMAKE_CURRENT_SOURCE_DIR}/source/ -o ${CMAKE_BINARY_DIR}
COMMAND dotnet restore ${CMAKE_CURRENT_SOURCE_DIR}/source/project.csproj
COMMAND dotnet publish ${CMAKE_CURRENT_SOURCE_DIR}/source/project.csproj -o ${CMAKE_BINARY_DIR}
)
else()
add_custom_target(${target} ALL
COMMAND dotnet restore ${CMAKE_CURRENT_SOURCE_DIR}/source/
COMMAND dotnet publish ${CMAKE_CURRENT_SOURCE_DIR}/source/ -o ${CMAKE_BINARY_DIR}
)
if(DOTNET_MIGRATE)
add_custom_target(${target} ALL
COMMAND dotnet migrate ${CMAKE_CURRENT_SOURCE_DIR}/source/project.json
COMMAND dotnet restore ${CMAKE_CURRENT_SOURCE_DIR}/source/project.json
COMMAND dotnet publish ${CMAKE_CURRENT_SOURCE_DIR}/source/project.json -o ${CMAKE_BINARY_DIR}
)
else()
add_custom_target(${target} ALL
COMMAND dotnet restore ${CMAKE_CURRENT_SOURCE_DIR}/source/project.json
COMMAND dotnet publish ${CMAKE_CURRENT_SOURCE_DIR}/source/project.json -o ${CMAKE_BINARY_DIR}
)
endif()
endif()

#
Expand All @@ -56,10 +63,16 @@ set_target_properties(${target}
# Deployment
#

if(DOTNET_VERSION VERSION_EQUAL "2.0" OR DOTNET_VERSION VERSION_GREATER "2.0")
set(DOTNET_INSTALL_DEPENDENCIES)
else()
set(DOTNET_INSTALL_DEPENDENCIES ${CMAKE_BINARY_DIR}/System.Runtime.Loader.dll)
endif()

# Library
install(FILES
${CMAKE_BINARY_DIR}/CSLoader.dll
${CMAKE_BINARY_DIR}/System.Runtime.Loader.dll
${DOTNET_INSTALL_DEPENDENCIES}
DESTINATION ${INSTALL_LIB}
COMPONENT runtime
)
154 changes: 117 additions & 37 deletions source/loaders/cs_loader/netcore/source/Loader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,76 @@ namespace CSLoader
{
public class Loader
{
[System.Diagnostics.Conditional("DEBUG_FILE")]
private static void Log(string text)
{
// TODO: Expose logs from host and use them here
var message= $"{DateTime.Now.Ticks}: {text}\n";
System.IO.File.AppendAllText("cs_loader_log.txt", message);
}


#if NETCOREAPP1_0 || NETCOREAPP1_1
private static Assembly Context_Resolving(AssemblyLoadContext context, AssemblyName name)
{
Assembly asm = null;

foreach (var path in paths)
{
try
{
asm = context.LoadFromAssemblyPath(path + "\\" + name.Name + ".dll");

if (asm != null)
{
return asm;
}
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.Message);
}
}

return asm;
}
#elif NETCOREAPP2_0 || NETCOREAPP2_1 || NETCOREAPP2_2
private static Assembly AssemblyResolveEventHandler(object sender, ResolveEventArgs args)
{
Assembly asm = null;

Log("AssemblyResolveEventHandler " + paths.Count.ToString());

foreach (var path in paths)
{
try
{
var p = path + "\\" + args.Name + ".dll";

Log(p);

asm = Assembly.LoadFile(p);

if (asm != null)
{
return asm;
}
else
{
// TODO: Write proper error message handling
Console.Error.WriteLine("Invalid Assembly.LoadFile: " + p);
}
}
catch (Exception ex)
{
// TODO: Write proper error message handling
Console.Error.WriteLine("Exception when loading the Assembly {0}: {1}", args.Name, ex.Message);
}
}

return asm;
}
#endif

public static void Main(string[] args)
{
Expand All @@ -26,7 +96,15 @@ public static void Main(string[] args)

static Loader()
{
AssemblyLoadContext.Default.Resolving += Context_Resolving;
Log("CSLoader static initialization");

#if NETCOREAPP1_0 || NETCOREAPP1_1
AssemblyLoadContext.Default.Resolving += Context_Resolving;
#elif NETCOREAPP2_0 || NETCOREAPP2_1 || NETCOREAPP2_2
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(AssemblyResolveEventHandler);
AppDomain.CurrentDomain.TypeResolve+= new ResolveEventHandler(AssemblyResolveEventHandler);
#endif

Init();
}

Expand Down Expand Up @@ -74,7 +152,6 @@ public static ReflectFunction[] GetFunctionsInternal()
{
return loader.Functions();
}

public static void GetFunctions(ref int count, IntPtr p)
{
var f = loader.Functions();
Expand Down Expand Up @@ -189,11 +266,22 @@ public bool LoadFromSourceFunctions(string[] source)

string assemblyName = Path.GetRandomFileName();

MetadataReference[] references = new MetadataReference[]
{
MetadataReference.CreateFromFile(typeof(object).GetTypeInfo().Assembly.Location),
MetadataReference.CreateFromFile(typeof(Enumerable).GetTypeInfo().Assembly.Location)
};
#if NETCOREAPP1_0 || NETCOREAPP1_1
MetadataReference[] references = new MetadataReference[]
{
MetadataReference.CreateFromFile(typeof(object).GetTypeInfo().Assembly.Location),
MetadataReference.CreateFromFile(typeof(Enumerable).GetTypeInfo().Assembly.Location)
};
#elif NETCOREAPP2_0 || NETCOREAPP2_1 || NETCOREAPP2_2
MetadataReference[] references;

var mainPath = Path.GetDirectoryName( typeof(object).GetTypeInfo().Assembly.Location) + "/";
var assemblyFiles = System.IO.Directory.GetFiles(mainPath, "*.dll");

assemblyFiles = assemblyFiles.Concat(System.IO.Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.dll")).Distinct().ToArray();

references = assemblyFiles.Select(x => MetadataReference.CreateFromFile(x)).ToArray();
#endif

CSharpCompilation compilation = CSharpCompilation.Create(
assemblyName,
Expand All @@ -213,15 +301,20 @@ public bool LoadFromSourceFunctions(string[] source)

foreach (Diagnostic diagnostic in failures)
{
// TODO: Write proper error message handling
Console.Error.WriteLine("{0}: {1}", diagnostic.Id, diagnostic.GetMessage());
}
}
else
{
ms.Seek(0, SeekOrigin.Begin);

AssemblyLoadContext context = AssemblyLoadContext.Default;
assembly = context.LoadFromStream(ms);
#if NETCOREAPP1_0 || NETCOREAPP1_1
AssemblyLoadContext context = AssemblyLoadContext.Default;
assembly = context.LoadFromStream(ms);
#elif NETCOREAPP2_0 || NETCOREAPP2_1 || NETCOREAPP2_2
assembly = Assembly.Load(ms.ToArray());
#endif
}
}

Expand All @@ -240,7 +333,9 @@ public bool LoadFromSourceFunctions(string[] source)

public static bool LoadFromAssembly(string assemblyFile)
{
AssemblyLoadContext context = AssemblyLoadContext.Default;
#if NETCOREAPP1_0 || NETCOREAPP1_1
AssemblyLoadContext context = AssemblyLoadContext.Default;
#endif
Assembly asm = null;

string path = System.IO.Path.GetDirectoryName(assemblyFile);
Expand All @@ -252,7 +347,11 @@ public static bool LoadFromAssembly(string assemblyFile)

try
{
asm = context.LoadFromAssemblyPath(assemblyFile);
#if NETCOREAPP1_0 || NETCOREAPP1_1
asm = context.LoadFromAssemblyPath(assemblyFile);
#elif NETCOREAPP2_0 || NETCOREAPP2_1 || NETCOREAPP2_2
asm = Assembly.LoadFile(assemblyFile);
#endif
}
catch (Exception)
{
Expand All @@ -261,7 +360,11 @@ public static bool LoadFromAssembly(string assemblyFile)
{
try
{
asm = context.LoadFromAssemblyName(new AssemblyName(System.IO.Path.GetFileNameWithoutExtension(assemblyFile)));
#if NETCOREAPP1_0 || NETCOREAPP1_1
asm = context.LoadFromAssemblyName(new AssemblyName(System.IO.Path.GetFileNameWithoutExtension(assemblyFile)));
#elif NETCOREAPP2_0 || NETCOREAPP2_1 || NETCOREAPP2_2
asm = Assembly.Load(new AssemblyName(System.IO.Path.GetFileNameWithoutExtension(assemblyFile)));
#endif
}
catch (Exception)
{
Expand All @@ -283,30 +386,6 @@ public static bool LoadFromAssembly(string assemblyFile)
return false;
}

private static Assembly Context_Resolving(AssemblyLoadContext context, AssemblyName name)
{
Assembly asm = null;

foreach (var path in paths)
{
try
{
asm = context.LoadFromAssemblyPath(path + "\\" + name.Name + ".dll");

if (asm != null)
{
return asm;
}
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.Message);
}
}

return asm;
}

public static bool LoadFromAssemblyC([System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)] string assemblyFile)
{
return LoadFromAssembly(assemblyFile);
Expand Down Expand Up @@ -377,7 +456,8 @@ public void LoadFunctions(Assembly assembly)
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.Message);
// TODO: Write proper error message handling
Console.Error.WriteLine("Error executing function {0}: {1}", function, ex.Message);
}

return null;
Expand Down
16 changes: 16 additions & 0 deletions source/loaders/cs_loader/netcore/source/project.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<AssemblyName>CSLoader</AssemblyName>
<PackageId>CSLoader</PackageId>
<VersionPrefix>1.0.0</VersionPrefix>
<TargetFramework>netcoreapp2.2</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>

</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.2.1" />
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions source/loaders/cs_loader/source/netcore_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ bool netcore_linux::CreateHost()

return true;
}

bool netcore_linux::LoadMain()
{
return true;
Expand Down
Loading