summaryrefslogtreecommitdiff
path: root/src/dlls/mscoree/unixinterface.cpp
diff options
context:
space:
mode:
authorJan Vorlicek <janvorli@microsoft.com>2015-07-15 23:08:09 +0200
committerJan Vorlicek <janvorli@microsoft.com>2015-07-16 21:47:36 +0200
commit441a97709757ec1b6edcaadb733941e64af8fb60 (patch)
tree201d0ac6ee252f936ec6d3df1bec6d8566694b58 /src/dlls/mscoree/unixinterface.cpp
parentbff51f77734b6eca0cf31d823687c167fcfc7d57 (diff)
downloadcoreclr-441a97709757ec1b6edcaadb733941e64af8fb60.tar.gz
coreclr-441a97709757ec1b6edcaadb733941e64af8fb60.tar.bz2
coreclr-441a97709757ec1b6edcaadb733941e64af8fb60.zip
Extend the Unix hosting API
This change modifies the Unix hosting API so that the hosting app can create as many managed delegates as it needs and execute them as many times it wants. The new API contains separate functions to initialize and shutdown CoreCLR and a function to create a delegate. The current ExecuteAssembly function behavior stays unmodified for now to ensure that dnx that uses that API and that pulls the binary libcoreclr is not broken. After the dnx is updated to use the new coreclr_create_delegate API, I'll remove the ExecuteAssembly. Also done: 1) Added support for comments and skipping empty lines in the mscorwks_unixexports.src. 2) Restructured the mscorwks_unixexports.src 3) Added coreclr_execute_assembly to the unixinterface.cpp / exports 4) Modified coreruncommon.cpp to use the new hosting API
Diffstat (limited to 'src/dlls/mscoree/unixinterface.cpp')
-rw-r--r--src/dlls/mscoree/unixinterface.cpp215
1 files changed, 177 insertions, 38 deletions
diff --git a/src/dlls/mscoree/unixinterface.cpp b/src/dlls/mscoree/unixinterface.cpp
index 21419173e9..5c5e1dacc7 100644
--- a/src/dlls/mscoree/unixinterface.cpp
+++ b/src/dlls/mscoree/unixinterface.cpp
@@ -88,48 +88,30 @@ static LPCWSTR* StringArrayToUnicode(int argc, LPCSTR* argv)
}
//
-// Execute a managed assembly with given arguments
+// Initialize the CoreCLR. Creates and starts CoreCLR host and creates an app domain
//
// Parameters:
// exePath - Absolute path of the executable that invoked the ExecuteAssembly
-// coreClrPath - Absolute path of the libcoreclr.so
// appDomainFriendlyName - Friendly name of the app domain that will be created to execute the assembly
// propertyCount - Number of properties (elements of the following two arguments)
// propertyKeys - Keys of properties of the app domain
// propertyValues - Values of properties of the app domain
-// argc - Number of arguments passed to the executed assembly
-// argv - Array of arguments passed to the executed assembly
-// managedAssemblyPath - Path of the managed assembly to execute (or NULL if using a custom entrypoint).
-// enntyPointAssemblyName - Name of the assembly which holds the custom entry point (or NULL to use managedAssemblyPath).
-// entryPointTypeName - Name of the type which holds the custom entry point (or NULL to use managedAssemblyPath).
-// entryPointMethodName - Name of the method which is the custom entry point (or NULL to use managedAssemblyPath).
-// exitCode - Exit code returned by the executed assembly
+// hostHandle - Output parameter, handle of the created host
+// domainId - Output parameter, id of the created app domain
//
// Returns:
// HRESULT indicating status of the operation. S_OK if the assembly was successfully executed
//
extern "C"
-HRESULT ExecuteAssembly(
- LPCSTR exePath,
- LPCSTR coreClrPath,
- LPCSTR appDomainFriendlyName,
+int coreclr_initialize(
+ const char* exePath,
+ const char* appDomainFriendlyName,
int propertyCount,
- LPCSTR* propertyKeys,
- LPCSTR* propertyValues,
- int argc,
- LPCSTR* argv,
- LPCSTR managedAssemblyPath,
- LPCSTR entryPointAssemblyName,
- LPCSTR entryPointTypeName,
- LPCSTR entryPointMethodName,
- DWORD* exitCode)
+ const char** propertyKeys,
+ const char** propertyValues,
+ void** hostHandle,
+ unsigned int* domainId)
{
- if (exitCode == NULL)
- {
- return HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER);
- }
- *exitCode = -1;
-
DWORD error = PAL_InitializeCoreCLR(exePath);
HRESULT hr = HRESULT_FROM_WIN32(error);
@@ -161,8 +143,6 @@ HRESULT ExecuteAssembly(
ConstWStringArrayHolder propertyValuesW;
propertyValuesW.Set(StringArrayToUnicode(propertyCount, propertyValues), propertyCount);
- DWORD domainId;
-
hr = host->CreateAppDomainWithManager(
appDomainFriendlyNameW,
// Flags:
@@ -187,7 +167,172 @@ HRESULT ExecuteAssembly(
propertyCount,
propertyKeysW,
propertyValuesW,
- &domainId);
+ domainId);
+
+ if (SUCCEEDED(hr))
+ {
+ host.SuppressRelease();
+ *hostHandle = host;
+ }
+
+ return hr;
+}
+
+//
+// Shutdown CoreCLR. It unloads the app domain and stops the CoreCLR host.
+//
+// Parameters:
+// hostHandle - Handle of the host
+// domainId - Id of the domain
+//
+// Returns:
+// HRESULT indicating status of the operation. S_OK if the assembly was successfully executed
+//
+extern "C"
+int coreclr_shutdown(
+ void* hostHandle,
+ unsigned int domainId)
+{
+ ReleaseHolder<ICLRRuntimeHost2> host(reinterpret_cast<ICLRRuntimeHost2*>(hostHandle));
+ HRESULT hr = host->UnloadAppDomain(domainId,
+ true); // Wait until done
+ IfFailRet(hr);
+
+ hr = host->Stop();
+
+ // The PAL_Terminate is not called here since it would terminate the current process.
+
+ return hr;
+}
+
+//
+// Create a native callable delegate for a managed method.
+//
+// Parameters:
+// hostHandle - Handle of the host
+// domainId - Id of the domain
+// entryPointAssemblyName - Name of the assembly which holds the custom entry point
+// entryPointTypeName - Name of the type which holds the custom entry point
+// entryPointMethodName - Name of the method which is the custom entry point
+// delegate - Output parameter, the function stores a pointer to the delegate at the specified address
+//
+// Returns:
+// HRESULT indicating status of the operation. S_OK if the assembly was successfully executed
+//
+extern "C"
+int coreclr_create_delegate(
+ void* hostHandle,
+ unsigned int domainId,
+ const char* entryPointAssemblyName,
+ const char* entryPointTypeName,
+ const char* entryPointMethodName,
+ void** delegate)
+{
+ ICLRRuntimeHost2* host = reinterpret_cast<ICLRRuntimeHost2*>(hostHandle);
+
+ ConstWStringHolder entryPointAssemblyNameW = StringToUnicode(entryPointAssemblyName);
+ ConstWStringHolder entryPointTypeNameW = StringToUnicode(entryPointTypeName);
+ ConstWStringHolder entryPointMethodNameW = StringToUnicode(entryPointMethodName);
+
+ HRESULT hr = host->CreateDelegate(
+ domainId,
+ entryPointAssemblyNameW,
+ entryPointTypeNameW,
+ entryPointMethodNameW,
+ (INT_PTR*)delegate);
+
+ return hr;
+}
+
+//
+// Execute a managed assembly with given arguments
+//
+// Parameters:
+// hostHandle - Handle of the host
+// domainId - Id of the domain
+// argc - Number of arguments passed to the executed assembly
+// argv - Array of arguments passed to the executed assembly
+// managedAssemblyPath - Path of the managed assembly to execute (or NULL if using a custom entrypoint).
+// exitCode - Exit code returned by the executed assembly
+//
+// Returns:
+// HRESULT indicating status of the operation. S_OK if the assembly was successfully executed
+//
+extern "C"
+int coreclr_execute_assembly(
+ void* hostHandle,
+ unsigned int domainId,
+ int argc,
+ const char** argv,
+ const char* managedAssemblyPath,
+ unsigned int* exitCode)
+{
+ if (exitCode == NULL)
+ {
+ return HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER);
+ }
+ *exitCode = -1;
+
+ ICLRRuntimeHost2* host = reinterpret_cast<ICLRRuntimeHost2*>(hostHandle);
+
+ ConstWStringArrayHolder argvW;
+ argvW.Set(StringArrayToUnicode(argc, argv), argc);
+
+ ConstWStringHolder managedAssemblyPathW = StringToUnicode(managedAssemblyPath);
+
+ HRESULT hr = host->ExecuteAssembly(domainId, managedAssemblyPathW, argc, argvW, exitCode);
+ IfFailRet(hr);
+
+ return hr;
+}
+
+//
+// Execute a managed assembly with given arguments
+//
+// Parameters:
+// exePath - Absolute path of the executable that invoked the ExecuteAssembly
+// coreClrPath - Absolute path of the libcoreclr.so
+// appDomainFriendlyName - Friendly name of the app domain that will be created to execute the assembly
+// propertyCount - Number of properties (elements of the following two arguments)
+// propertyKeys - Keys of properties of the app domain
+// propertyValues - Values of properties of the app domain
+// argc - Number of arguments passed to the executed assembly
+// argv - Array of arguments passed to the executed assembly
+// managedAssemblyPath - Path of the managed assembly to execute (or NULL if using a custom entrypoint).
+// enntyPointAssemblyName - Name of the assembly which holds the custom entry point (or NULL to use managedAssemblyPath).
+// entryPointTypeName - Name of the type which holds the custom entry point (or NULL to use managedAssemblyPath).
+// entryPointMethodName - Name of the method which is the custom entry point (or NULL to use managedAssemblyPath).
+// exitCode - Exit code returned by the executed assembly
+//
+// Returns:
+// HRESULT indicating status of the operation. S_OK if the assembly was successfully executed
+//
+extern "C"
+HRESULT ExecuteAssembly(
+ LPCSTR exePath,
+ LPCSTR coreClrPath,
+ LPCSTR appDomainFriendlyName,
+ int propertyCount,
+ LPCSTR* propertyKeys,
+ LPCSTR* propertyValues,
+ int argc,
+ LPCSTR* argv,
+ LPCSTR managedAssemblyPath,
+ LPCSTR entryPointAssemblyName,
+ LPCSTR entryPointTypeName,
+ LPCSTR entryPointMethodName,
+ DWORD* exitCode)
+{
+ if (exitCode == NULL)
+ {
+ return HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER);
+ }
+ *exitCode = -1;
+
+ ReleaseHolder<ICLRRuntimeHost2> host; //(reinterpret_cast<ICLRRuntimeHost2*>(hostHandle));
+ DWORD domainId;
+
+ HRESULT hr = coreclr_initialize(exePath, appDomainFriendlyName, propertyCount, propertyKeys, propertyValues, &host, &domainId);
IfFailRet(hr);
ConstWStringArrayHolder argvW;
@@ -219,14 +364,8 @@ HRESULT ExecuteAssembly(
*exitCode = pHostMain(argc, argvW);
}
-
- hr = host->UnloadAppDomain(domainId,
- true); // Wait until done
- IfFailRet(hr);
-
- hr = host->Stop();
- // The PAL_Terminate is not called here since it would terminate the current process.
+ hr = coreclr_shutdown(host, domainId);
return hr;
}