Skip to content

Commit

Permalink
Fixes #31 Fixes#30 (#65)
Browse files Browse the repository at this point in the history
Co-authored-by: Robert McIntosh <261477+rwmcintosh@users.noreply.github.com>
  • Loading branch information
Ian-Du and rwmcintosh authored Mar 5, 2024
1 parent 1a41143 commit 54767f4
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 31 deletions.
11 changes: 0 additions & 11 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,6 @@ loadAndInit <- function(chname, pkgname, libname, srcPkgLibPath=NULL) {
# srcPkgLibPath ends with platform separator (e.g. '/')
f <- file.path(srcPkgLibPath, paste0(chname, ext))

# save current working directory
og_wd <- getwd()

# change working directory to load libs
message("Temporary working directory:", srcPkgLibPath)
setwd(srcPkgLibPath)
dyn.load(f)

} else {
Expand All @@ -91,11 +85,6 @@ loadAndInit <- function(chname, pkgname, libname, srcPkgLibPath=NULL) {
clrInit(debug_flag!="")
appendStartupMsg(paste('Loaded Common Language Runtime version', getClrVersionString()))
setRDotNet(TRUE)

if (exists("og_wd")) {
# restore the original working directory
setwd(og_wd)
}
}

appendStartupMsg <- function(msg) {
Expand Down
2 changes: 0 additions & 2 deletions src/Makefile.win.in
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ DOTNET_CMD=dotnet
MSB=@MSBUILD_EXE_PATH@

INSTDIR= ../inst
NETHOST=nethost
RUNTIMECONFIG=RSharp.runtimeconfig

# This can be helpful to diagnose the msbuild procedure
Expand Down Expand Up @@ -81,7 +80,6 @@ rSharpLib: rSharpLibComp
$(ROBOCP_CMD) $$bin_dir $(INSTDIR)/libs/ $$RSHARPBINS;\


$(ROBOCP_CMD) ./packages/Microsoft.NETCore.App.Host.win-x64.8.0.2/runtimes/win-x64/native $(INSTDIR)/libs/ $(NETHOST).dll
$(ROBOCP_CMD) ./ $(INSTDIR)/libs/ $(RUNTIMECONFIG).json;
-$(ROBOCP_CMD) ./ClrFacade/bin/$(BuildConfiguration)/net8.0/ $(INSTDIR)/libs/ $(CLR_FACADE_BINS)
-$(ROBOCP_CMD) ./ClrFacade/bin/$(BuildConfiguration)/net8.0/ $(INSTDIR)/libs/ $(RDOTNET_BINS)
Expand Down
54 changes: 40 additions & 14 deletions src/rSharp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

using string_t = std::basic_string<char_t>;

const string_t dotnetlib_path = STR("./ClrFacade.dll");
const wchar_t* dotnetlib_path = nullptr;
const auto loadAssemblyDelegate = STR("ClrFacade.ClrFacade+LoadFromDelegate, ClrFacade");
void* create_instance_fn_ptr = nullptr;
void* load_from_fn_ptr = nullptr;
Expand All @@ -30,14 +30,14 @@ typedef int (CORECLR_DELEGATE_CALLTYPE* CreateSEXPWrapperDelegate)(intptr_t poin
typedef int (CORECLR_DELEGATE_CALLTYPE* CreateInstanceDelegate)(const char*, RSharpGenericValue** objects, int num_objects, RSharpGenericValue* returnValue);
typedef void (CORECLR_DELEGATE_CALLTYPE* LoadFromDelegate)(const char*);


wchar_t* MergeLibraryPath(const wchar_t* libraryPath, const wchar_t* additionalPath);
void freeObject(RSharpGenericValue* instance);

/////////////////////////////////////////
// Initialization and disposal of the CLR
/////////////////////////////////////////
void rSharp_create_domain() {

void rSharp_create_domain(char ** libraryPath)
{
// if already loaded in this process, do not load again
if (load_assembly_and_get_function_pointer != nullptr)
return;
Expand All @@ -50,10 +50,36 @@ void rSharp_create_domain() {
}

// STEP 2: Initialize and start the .NET Core runtime
const string_t config_path = STR("./RSharp.runtimeconfig.json");
size_t lengthInWideFormat = 0;
//Gets the length of libPath in terms of a wide string.
mbstowcs_s(&lengthInWideFormat, nullptr, 0, *libraryPath, 0);
wchar_t* wideStringLibraryPath = new wchar_t[lengthInWideFormat + 1];
//Copies the libraryPath to a wchar_t with the size lengthInWideFormat
mbstowcs_s(nullptr, wideStringLibraryPath, lengthInWideFormat + 1, *libraryPath, lengthInWideFormat);

wchar_t* wideStringPath = MergeLibraryPath(wideStringLibraryPath, STR("/RSharp.runtimeconfig.json"));
dotnetlib_path = MergeLibraryPath(wideStringLibraryPath, STR("/ClrFacade.dll"));

load_assembly_and_get_function_pointer = nullptr;
load_assembly_and_get_function_pointer = get_dotnet_load_assembly(config_path.c_str());
load_assembly_and_get_function_pointer = get_dotnet_load_assembly(wideStringPath);
assert(load_assembly_and_get_function_pointer != nullptr && "Failure: get_dotnet_load_assembly()");

delete[] wideStringPath;
delete[] wideStringLibraryPath;
}

wchar_t* MergeLibraryPath(const wchar_t* libraryPath, const wchar_t* additionalPath)
{
size_t libraryPathLength = wcslen(libraryPath);
size_t additionalPathLength = wcslen(additionalPath);
size_t totalLength = libraryPathLength + additionalPathLength + 1; // +1 for null terminator

wchar_t* mergedPaths = new wchar_t[totalLength];

wcscpy_s(mergedPaths, totalLength, libraryPath);
wcscat_s(mergedPaths, totalLength, additionalPath);

return mergedPaths;
}

void rSharp_shutdown_clr()
Expand Down Expand Up @@ -179,7 +205,7 @@ void initializeCreateInstance()
auto functionDelegate = STR("ClrFacade.ClrFacade+CreateInstanceDelegate, ClrFacade");

int rc_1 = load_assembly_and_get_function_pointer(
dotnetlib_path.c_str(),
dotnetlib_path,
dotnet_type,
STR("CreateInstance"),
functionDelegate,//delegate_type_name
Expand All @@ -195,7 +221,7 @@ void initializeLoadAssembly()
auto functionDelegate = STR("ClrFacade.ClrFacade+LoadFromDelegate, ClrFacade");

int rc_1 = load_assembly_and_get_function_pointer(
dotnetlib_path.c_str(),
dotnetlib_path,
dotnet_type,
STR("LoadFrom"),
loadAssemblyDelegate,//delegate_type_name
Expand All @@ -211,7 +237,7 @@ void initializeGetFullTypeNameFunction()
auto functionDelegate = STR("ClrFacade.ClrFacade+GetObjectTypeNameDelegate, ClrFacade");

int rc_1 = load_assembly_and_get_function_pointer(
dotnetlib_path.c_str(),
dotnetlib_path,
dotnet_type,
STR("GetObjectTypeName"),
functionDelegate,//delegate_type_name
Expand All @@ -227,7 +253,7 @@ void initializeGetObjectDirectFunction()
auto functionDelegate = STR("ClrFacade.ClrFacade+CurrentObjectDelegate, ClrFacade");

int rc_1 = load_assembly_and_get_function_pointer(
dotnetlib_path.c_str(),
dotnetlib_path,
dotnet_type,
STR("CurrentObject"),
functionDelegate,//delegate_type_name
Expand All @@ -243,7 +269,7 @@ void initializeCreateSEXPFunction()
auto functionDelegate = STR("ClrFacade.ClrFacade+CreateSexpWrapperDelegate, ClrFacade");

int rc_1 = load_assembly_and_get_function_pointer(
dotnetlib_path.c_str(),
dotnetlib_path,
dotnet_type,
STR("CreateSexpWrapperLong"),
functionDelegate,//delegate_type_name
Expand All @@ -260,7 +286,7 @@ void initializeCallInstanceFunction()
auto functionDelegate = STR("ClrFacade.ClrFacade+CallInstanceMethodDelegate, ClrFacade");

int rc_1 = load_assembly_and_get_function_pointer(
dotnetlib_path.c_str(),
dotnetlib_path,
dotnet_type,
STR("CallInstanceMethod"),
functionDelegate,//delegate_type_name
Expand All @@ -276,7 +302,7 @@ void initializeFreeObjectFunction()
auto functionDelegate = STR("ClrFacade.ClrFacade+FreeObjectDelegate, ClrFacade");

int rc_1 = load_assembly_and_get_function_pointer(
dotnetlib_path.c_str(),
dotnetlib_path,
dotnet_type,
STR("FreeObject"),
functionDelegate,//delegate_type_name
Expand All @@ -293,7 +319,7 @@ void initializeCallStaticFunction()
auto functionDelegate = STR("ClrFacade.ClrFacade+CallStaticMethodDelegate, ClrFacade");

int rc_1 = load_assembly_and_get_function_pointer(
dotnetlib_path.c_str(),
dotnetlib_path,
dotnet_type,
STR("CallStaticMethod"),
functionDelegate,//delegate_type_name
Expand Down
2 changes: 1 addition & 1 deletion src/rSharp.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ extern "C" {
*/
void get_FullTypeName( SEXP p, char ** tname);
void rSharp_load_assembly(char ** filename);
void rSharp_create_domain();
void rSharp_create_domain(char** libPath);
int use_rdotnet = 0;


Expand Down
8 changes: 5 additions & 3 deletions src/rSharp.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -241,16 +241,17 @@
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions);MS_CLR;MS_CLR_TLB;USE_COR_RUNTIME_HOST;WINDOWS</PreprocessorDefinitions>
<PreprocessorDefinitions>%(PreprocessorDefinitions);MS_CLR;MS_CLR_TLB;USE_COR_RUNTIME_HOST;WINDOWS</PreprocessorDefinitions>
<AdditionalIncludeDirectories>%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>
</AdditionalIncludeDirectories>
<AdditionalOptions>/TP %(AdditionalOptions)</AdditionalOptions>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
</ClCompile>
<Link>
<SubSystem>NotSet</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>Rdll.lib;packages\Microsoft.NETCore.App.Host.win-x64.8.0.2\runtimes\win-x64\native\nethost.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>Rdll.lib;packages\Microsoft.NETCore.App.Host.win-x64.8.0.2\runtimes\win-x64\native\libnethost.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>
</AdditionalLibraryDirectories>
<ModuleDefinitionFile>rSharp-win.def</ModuleDefinitionFile>
Expand Down Expand Up @@ -300,12 +301,13 @@
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions);MS_CLR;MS_CLR_TLB;USE_COR_RUNTIME_HOST;WINDOWS</PreprocessorDefinitions>
<AdditionalOptions>/TP %(AdditionalOptions)</AdditionalOptions>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies>packages\Microsoft.NETCore.App.Host.win-x64.8.0.2\runtimes\win-x64\native\nethost.lib;Rdll.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>packages\Microsoft.NETCore.App.Host.win-x64.8.0.2\runtimes\win-x64\native\libnethost.lib;Rdll.lib;%(AdditionalDependencies)</AdditionalDependencies>
<ModuleDefinitionFile>rSharp-win.def</ModuleDefinitionFile>
</Link>
</ItemDefinitionGroup>
Expand Down

0 comments on commit 54767f4

Please sign in to comment.