Skip to content

Commit

Permalink
Fixes #56 do not crash when loading workspace (#138)
Browse files Browse the repository at this point in the history
Co-authored-by: PavelBal <[email protected]>
  • Loading branch information
rwmcintosh and PavelBal authored Apr 16, 2024
1 parent d027e6f commit 4926ca1
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 11 deletions.
Binary file modified inst/extdata/rSharp.Examples.dll
Binary file not shown.
Binary file modified inst/lib/ClrFacade.dll
Binary file not shown.
Binary file modified inst/lib/DynamicInterop.dll
Binary file not shown.
Binary file modified inst/lib/RDotNet.dll
Binary file not shown.
Binary file modified inst/lib/RsharpMs.dll
Binary file not shown.
40 changes: 33 additions & 7 deletions shared/rSharp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,7 @@ SEXP rSharp_load_assembly(SEXP args)
SEXP r_create_clr_object(SEXP parameters)
{
SEXP sExpressionParameterStack = parameters;
RSharpGenericValue** methodParameters;
SEXP sExpressionMethodParameter;
RSharpGenericValue return_value;
char* ns_qualified_typename = NULL;
Expand All @@ -581,7 +582,15 @@ SEXP r_create_clr_object(SEXP parameters)
sExpressionParameterStack = POP(sExpressionParameterStack);
sExpressionMethodParameter = sExpressionParameterStack;

RSharpGenericValue** methodParameters = sexp_to_parameters(sExpressionMethodParameter);
try
{
methodParameters = sexp_to_parameters(sExpressionMethodParameter);
}
catch (const std::exception& ex)
{
free(ns_qualified_typename);
error_return(ex.what())
}
R_len_t numberOfObjects = Rf_length(sExpressionMethodParameter);

try
Expand Down Expand Up @@ -770,7 +779,7 @@ SEXP r_call_method(SEXP parameters)
{
SEXP sExpressionParameterStack = parameters, instance, sExpressionParameter;
const char* methodName = 0;

RSharpGenericValue** params;

sExpressionParameter = TOPOF(sExpressionParameterStack);
auto functionName = CHAR(STRING_ELT(sExpressionParameter, 0)); // should be "r_call_method"
Expand All @@ -782,9 +791,14 @@ SEXP r_call_method(SEXP parameters)
sExpressionParameter = TOPOF(sExpressionParameterStack); // instance method name is the third SEXP
methodName = CHAR(STRING_ELT(sExpressionParameter, 0));
sExpressionParameterStack = POP(sExpressionParameterStack);

RSharpGenericValue** params = sexp_to_parameters(sExpressionParameterStack);

try
{
params = sexp_to_parameters(sExpressionParameterStack);
}
catch (const std::exception& ex)
{
error_return(ex.what())
}
const R_len_t numberOfObjects = Rf_length(sExpressionParameterStack);

try
Expand All @@ -806,6 +820,7 @@ SEXP r_call_static_method(SEXP parameters)
SEXP sExpressionMethodParameter;
const char* methodName;
char* ns_qualified_typename = NULL; // My.Namespace.MyClass,MyAssemblyName
RSharpGenericValue** methodParameters;

sExpressionParameterStack = POP(sExpressionParameterStack); /* skip the first parameter: function name*/
get_FullTypeName(sExpressionParameterStack, &ns_qualified_typename);
Expand All @@ -815,7 +830,15 @@ SEXP r_call_static_method(SEXP parameters)
sExpressionParameterStack = POP(sExpressionParameterStack);
sExpressionMethodParameter = sExpressionParameterStack;

RSharpGenericValue** methodParameters = sexp_to_parameters(sExpressionMethodParameter);
try
{
methodParameters = sexp_to_parameters(sExpressionMethodParameter);
}
catch (const std::exception& ex)
{
free(ns_qualified_typename);
error_return(ex.what())
}
if (TYPEOF(sExpressionParameter) != STRSXP || LENGTH(sExpressionParameter) != 1)
{
free(ns_qualified_typename);
Expand Down Expand Up @@ -1024,7 +1047,10 @@ RSHARP_BOOL r_has_class(SEXP s, const char* classname) {

RSharpGenericValue* get_rSharp_generic_value_from_extptr(SEXP a)
{
return GET_RSHARP_GENERIC_VALUE_FROM_EXTPTR(a);
auto extptr_ptr = EXTPTR_PTR(a);
if(extptr_ptr == nullptr)
error("External pointer to unallocated memory was found");
return ((RsharpObjectHandle*)extptr_ptr)->objptr;
}

RSharpGenericValue* get_rSharp_generic_value_from_s4(SEXP clrObj)
Expand Down
3 changes: 0 additions & 3 deletions shared/rSharp.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,6 @@ typedef struct {
uint32_t handle;
} RsharpObjectHandle;

#define GET_RSHARP_GENERIC_VALUE_FROM_EXTPTR(extptrsexp) ((RsharpObjectHandle*)EXTPTR_PTR(extptrsexp))->objptr


/////////////////////////////////////////
// Exported methods (exported meaning on Windows platform)
/////////////////////////////////////////
Expand Down
5 changes: 4 additions & 1 deletion vignettes/user-guide.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ A non-static class can also have static members, which can be listed using the `
newInstance$getStaticFields()
```

Setting and getting (static and non-static) fields and properties of an object
Setting and getting non-static fields and properties of an object
is done using the `set` and `get` methods, respectively:

```{r}
Expand All @@ -187,6 +187,9 @@ newInstance$set("FieldDoubleOne", 23)
# Get the value of the static field:
newInstance$get("FieldDoubleOne")
```

To get or set static fields, use the functions `getStatic` and `setStatic`.

Finally, we can call the methods of the object using the `call` method:

```{r}
Expand Down

0 comments on commit 4926ca1

Please sign in to comment.