From c7dfe5fd6ce268fc605393be199f2eae634c76a8 Mon Sep 17 00:00:00 2001 From: Stephan Sahm Date: Thu, 2 May 2024 11:37:48 +0200 Subject: [PATCH 1/2] raise julia errors --- src/callback.jl | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/callback.jl b/src/callback.jl index b733880e..c2ccb566 100644 --- a/src/callback.jl +++ b/src/callback.jl @@ -100,6 +100,9 @@ const juliaCallback = RObject{ExtPtrSxp}() function setup_callbacks() julia_extptr_callback_ptr = @cfunction(julia_extptr_callback,Ptr{UnknownSxp},(Ptr{ListSxp},)) juliaCallback.p = makeNativeSymbolRef(julia_extptr_callback_ptr) + + # helper to throw Julia errors on R side + reval("...stop_if_error <- function (obj) if (inherits(obj, 'error')) stop(obj) else obj") end @@ -124,24 +127,31 @@ Wrap a callable Julia object `f` an a R `ClosSxpPtr`. Constructs the following R code - function(...) .External(juliaCallback, fExPtr, ...) + function(...) ...stop_if_error(.External(juliaCallback, fExPtr, ...)) """ function sexp(::Type{RClass{:function}}, f) fptr = protect(sexp(RClass{:externalptr}, f)) - body = protect(rlang_p(Symbol(".External"), - juliaCallback, - fptr, - Const.DotsSymbol)) + body = protect( + rlang_p( + Symbol("...stop_if_error"), + rlang_p( + Symbol(".External"), + juliaCallback, + fptr, + Const.DotsSymbol + ) + ) + ) nprotect = 2 local clos try - args = protect(sexp_arglist_dots()) + args = RCall.protect(RCall.sexp_arglist_dots()) nprotect += 1 - lang = rlang_p(:function, args, body) - clos = reval_p(lang) + lang = RCall.rlang_p(:function, args, body) + clos = RCall.reval_p(lang) finally - unprotect(nprotect) + RCall.unprotect(nprotect) end clos end From 05a680e0bc8d48bfa1d3658e108d985876b9a6f7 Mon Sep 17 00:00:00 2001 From: Stephan Sahm Date: Mon, 6 May 2024 12:13:39 +0200 Subject: [PATCH 2/2] no RCall. needed --- src/callback.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/callback.jl b/src/callback.jl index c2ccb566..819b084c 100644 --- a/src/callback.jl +++ b/src/callback.jl @@ -146,12 +146,12 @@ function sexp(::Type{RClass{:function}}, f) nprotect = 2 local clos try - args = RCall.protect(RCall.sexp_arglist_dots()) + args = protect(sexp_arglist_dots()) nprotect += 1 - lang = RCall.rlang_p(:function, args, body) - clos = RCall.reval_p(lang) + lang = rlang_p(:function, args, body) + clos = reval_p(lang) finally - RCall.unprotect(nprotect) + unprotect(nprotect) end clos end