diff --git a/NEWS.md b/NEWS.md index a2139c46..a222ba82 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # rSharp (development version) +- Arrays of .NET objects are now supported as arguments to methods. However, the signature of the .NET method must accept `Object[]` as parameter type. + # rSharp 1.1.2 ## Minor improvements and bug fixes diff --git a/R/rSharp-internal.R b/R/rSharp-internal.R index 1268ef7d..4a3c4367 100644 --- a/R/rSharp-internal.R +++ b/R/rSharp-internal.R @@ -67,6 +67,15 @@ if (inherits(args[[i]], "NetObject")) { args[[i]] <- args[[i]]$pointer } + # Check if the entry is a list. If so, check if the first element is a NetObject + # and extract the pointers for all elements + if (is.list(args[[i]]) && length(args[[i]]) > 0) { + if (inherits(args[[i]][[1]], "NetObject")) { + args[[i]] <- lapply(args[[i]], function(x) { + if (inherits(x, "NetObject")) x$pointer else x + }) + } + } } return(args) } diff --git a/inst/extdata/rSharp.Examples.dll b/inst/extdata/rSharp.Examples.dll index 0c054bc2..dc2a8b19 100644 Binary files a/inst/extdata/rSharp.Examples.dll and b/inst/extdata/rSharp.Examples.dll differ diff --git a/inst/lib/ClrFacade.dll b/inst/lib/ClrFacade.dll index 8d7f2545..523a4ccb 100644 Binary files a/inst/lib/ClrFacade.dll and b/inst/lib/ClrFacade.dll differ diff --git a/inst/lib/DynamicInterop.dll b/inst/lib/DynamicInterop.dll index f5d6d224..9e3c12a4 100644 Binary files a/inst/lib/DynamicInterop.dll and b/inst/lib/DynamicInterop.dll differ diff --git a/inst/lib/RDotNet.dll b/inst/lib/RDotNet.dll index 21a9cd8d..9d4be243 100644 Binary files a/inst/lib/RDotNet.dll and b/inst/lib/RDotNet.dll differ diff --git a/inst/lib/rSharp.dll b/inst/lib/rSharp.dll index 1d87caeb..88804a2a 100644 Binary files a/inst/lib/rSharp.dll and b/inst/lib/rSharp.dll differ diff --git a/inst/lib/rSharp.linux.so b/inst/lib/rSharp.linux.so index 25372ec8..c0a36832 100755 Binary files a/inst/lib/rSharp.linux.so and b/inst/lib/rSharp.linux.so differ diff --git a/inst/lib/rSharp.mac.arm64.so b/inst/lib/rSharp.mac.arm64.so index 9d89d9f8..6ff01119 100755 Binary files a/inst/lib/rSharp.mac.arm64.so and b/inst/lib/rSharp.mac.arm64.so differ diff --git a/shared/ClrFacade/TestCases.cs b/shared/ClrFacade/TestCases.cs index c8a25335..e8dda845 100644 --- a/shared/ClrFacade/TestCases.cs +++ b/shared/ClrFacade/TestCases.cs @@ -14,6 +14,25 @@ namespace ClrFacade; // Do not change the names or signatures without also updating usages in R public class TestCases { + public static bool SingleObjectArgument(TestObject arg) + { + return arg != null && arg.GetType() == typeof(TestObject); + } + + public static bool ArrayOfObjectsArgument(Object[] arg) + { + if (arg == null) + return false; + + for (var i = 0; i < arg.Length; i++) + { + if (arg[i] == null || arg[i].GetType() != typeof(TestObject)) + return false; + } + + return true; + } + public static bool GetTrue() { return true; diff --git a/tests/testthat/test-net-object.R b/tests/testthat/test-net-object.R index b8f78911..86ab7b3a 100644 --- a/tests/testthat/test-net-object.R +++ b/tests/testthat/test-net-object.R @@ -252,3 +252,23 @@ test_that("Deprecated printing methods show appropriate warnings", { class = "lifecycle_warning_deprecated" ) }) + +test_that("A NetObject can be passed as argument to callStatic", { + testObj <- newObjectFromName(rSharpEnv$testObjectTypeName) + + expect_true(callTestCase( + "SingleObjectArgument", + testObj + )) +}) + +test_that("An array of NetObjects can be passed as argument to callStatic", { + testObj1 <- newObjectFromName(rSharpEnv$testObjectTypeName) + testObj2 <- newObjectFromName(rSharpEnv$testObjectTypeName) + testArray <- array(c(testObj1, testObj2), dim = c(2)) + + expect_true(callTestCase( + "ArrayOfObjectsArgument", + testArray + )) +}) diff --git a/vignettes/user-guide.Rmd b/vignettes/user-guide.Rmd index 36e3accb..ad46038e 100644 --- a/vignettes/user-guide.Rmd +++ b/vignettes/user-guide.Rmd @@ -46,7 +46,7 @@ to interact with these objects through the `NetObject`. If, for any reason, the user needs to access the raw pointer, it can be done by accessing the field `$pointer` of the R6 object. -`NetObject` instances can be passed to .NET methods as arguments. +`NetObject` instances can be passed to .NET methods as arguments. If a method expects an array of .NET objects, all objects within the R list must be instances of `NetObject`. Mixed lists of native R types and `NetObject` instances are not supported. Furthermore, the type of the array will be interpreted as `Object[]`. ## Loading an assembly