diff --git a/DESCRIPTION b/DESCRIPTION index 18c8610..1f93542 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Package: swiftr Type: Package Title: Seamless R and Swift Integration -Version: 0.1.0 -Date: 2021-01-24 +Version: 0.2.0 +Date: 2021-04-14 Authors@R: c( person("Bob", "Rudis", email = "bob@rud.is", role = c("aut", "cre"), comment = c(ORCID = "0000-0001-5670-2640")) diff --git a/R/swift-function.R b/R/swift-function.R index fe84600..e1da5d8 100644 --- a/R/swift-function.R +++ b/R/swift-function.R @@ -28,6 +28,7 @@ swift_function <- function(code, env = globalenv(), imports = c("Foundation"), c writeLines( text = c( sprintf("import %s", imports), + readLines(system.file("include", "utils.swift", package = "swiftr"), warn = FALSE), code ), con = file.path(cache_dir, source_file) @@ -165,7 +166,7 @@ swift_function <- function(code, env = globalenv(), imports = c("Foundation"), c message("SYNTAX ERROR") - # cat(readLines(stderr)) + invisible(readLines(stderr)) } diff --git a/inst/include/utils.swift b/inst/include/utils.swift new file mode 100644 index 0000000..59de747 --- /dev/null +++ b/inst/include/utils.swift @@ -0,0 +1,57 @@ + +extension SEXP { + + var count: R_len_t { + Rf_length(self) + } + + var typeOf: SEXPTYPE { + SEXPTYPE(TYPEOF(self)) + } + + var isSTRING: Bool { + self.typeOf == STRSXP + } + +} + +extension String { + + init?(_ sexp: SEXP) { + if ((sexp != R_NilValue) && (Rf_length(sexp) == 1)) { + switch (TYPEOF(sexp)) { + case STRSXP: self = String(cString: R_CHAR(STRING_ELT(sexp, 0))) + default: return(nil) + } + } else { + return(nil) + } + } + +} + +extension Array where Element == String { + + init?(_ sexp: SEXP) { + if (sexp.isSTRING) { + var val : [String] = [String]() + val.reserveCapacity(Int(sexp.count)) + for idx in 0..