From 5f5aff199de48fb7d481b20f0d94433c91dbc183 Mon Sep 17 00:00:00 2001 From: shikokuchuo <53399081+shikokuchuo@users.noreply.github.com> Date: Fri, 21 Jun 2024 12:44:07 +0100 Subject: [PATCH] consistent error handling --- R/ncurl.R | 1 - src/aio.c | 45 ++++++++++++++++++++++++++++++++------------- src/init.c | 3 +++ src/nanonext.h | 2 +- src/utils.c | 22 +++++++++------------- 5 files changed, 45 insertions(+), 28 deletions(-) diff --git a/R/ncurl.R b/R/ncurl.R index 9504c2716..31b147533 100644 --- a/R/ncurl.R +++ b/R/ncurl.R @@ -266,4 +266,3 @@ as.promise.ncurlAio <- function(x) { #' @exportS3Method promises::is.promising #' is.promising.ncurlAio <- function(x) TRUE - diff --git a/src/aio.c b/src/aio.c index d5143bd0e..8f2a1ed55 100644 --- a/src/aio.c +++ b/src/aio.c @@ -50,27 +50,28 @@ typedef struct nano_handle_s { nng_tls_config *cfg; } nano_handle; -static SEXP mk_error_data(const int xc) { +static SEXP mk_error_aio(const int xc, SEXP env) { - const char *names[] = {xc < 0 ? "result" : "data", "value", ""}; - SEXP out = PROTECT(Rf_mkNamed(VECSXP, names)); - SEXP err = Rf_ScalarInteger(abs(xc)); + SEXP err = PROTECT(Rf_ScalarInteger(xc)); Rf_classgets(err, nano_error); - SET_VECTOR_ELT(out, 0, err); - SET_VECTOR_ELT(out, 1, err); + Rf_defineVar(nano_ValueSymbol, err, env); + Rf_defineVar(nano_AioSymbol, R_NilValue, env); UNPROTECT(1); - return out; + return err; } -static SEXP mk_error_aio(const int xc, SEXP env) { +static SEXP mk_error_data(const int xc) { - SEXP err = PROTECT(Rf_ScalarInteger(xc)); + SEXP env, err; + PROTECT(env = Rf_allocSExp(ENVSXP)); + Rf_classgets(env, xc < 0 ? nano_sendAio : nano_recvAio); + PROTECT(err = Rf_ScalarInteger(abs(xc))); Rf_classgets(err, nano_error); Rf_defineVar(nano_ValueSymbol, err, env); - Rf_defineVar(nano_AioSymbol, R_NilValue, env); - UNPROTECT(1); - return err; + Rf_defineVar(xc < 0 ? nano_ResultSymbol : nano_DataSymbol, err, env); + UNPROTECT(2); + return env; } @@ -87,6 +88,24 @@ static SEXP mk_error_haio(const int xc, SEXP env) { } +static SEXP mk_error_ncurl(const int xc) { + + SEXP env, err; + PROTECT(env = Rf_allocSExp(ENVSXP)); + NANO_CLASS2(env, "ncurlAio", "recvAio"); + PROTECT(err = Rf_ScalarInteger(xc)); + Rf_classgets(err, nano_error); + Rf_defineVar(nano_ResultSymbol, err, env); + Rf_defineVar(nano_StatusSymbol, err, env); + Rf_defineVar(nano_ProtocolSymbol, err, env); + Rf_defineVar(nano_HeadersSymbol, err, env); + Rf_defineVar(nano_ValueSymbol, err, env); + Rf_defineVar(nano_DataSymbol, err, env); + UNPROTECT(2); + return env; + +} + // aio completion callbacks ---------------------------------------------------- static void pipe_cb_signal(nng_pipe p, nng_pipe_ev ev, void *arg) { @@ -797,7 +816,7 @@ SEXP rnng_send_aio(SEXP con, SEXP data, SEXP mode, SEXP timeout, SEXP clo) { SEXP env, fun; PROTECT(env = Rf_allocSExp(ENVSXP)); - Rf_classgets(env, Rf_mkString("sendAio")); + Rf_classgets(env, nano_sendAio); Rf_defineVar(nano_AioSymbol, aio, env); PROTECT(fun = R_mkClosure(R_NilValue, nano_aioFuncRes, clo)); diff --git a/src/init.c b/src/init.c index aadb9338e..b0c44b821 100644 --- a/src/init.c +++ b/src/init.c @@ -50,6 +50,7 @@ SEXP nano_precious; SEXP nano_recvAio; SEXP nano_reqAio; SEXP nano_refHook; +SEXP nano_sendAio; SEXP nano_success; SEXP nano_unresolved; @@ -107,6 +108,7 @@ static void PreserveObjects(void) { SET_STRING_ELT(nano_reqAio, 0, Rf_mkChar("mirai")); SET_STRING_ELT(nano_reqAio, 1, Rf_mkChar("recvAio")); R_PreserveObject(nano_refHook = Rf_list2(R_NilValue, R_NilValue)); + R_PreserveObject(nano_sendAio = Rf_mkString("sendAio")); R_PreserveObject(nano_success = Rf_ScalarInteger(0)); R_PreserveObject(nano_unresolved = Rf_shallow_duplicate(Rf_ScalarLogical(NA_LOGICAL))); Rf_classgets(nano_unresolved, Rf_mkString("unresolvedValue")); @@ -116,6 +118,7 @@ static void PreserveObjects(void) { static void ReleaseObjects(void) { R_ReleaseObject(nano_unresolved); R_ReleaseObject(nano_success); + R_ReleaseObject(nano_sendAio); R_ReleaseObject(nano_refHook); R_ReleaseObject(nano_reqAio); R_ReleaseObject(nano_recvAio); diff --git a/src/nanonext.h b/src/nanonext.h index a6ba5497a..4b1ad112e 100644 --- a/src/nanonext.h +++ b/src/nanonext.h @@ -192,7 +192,6 @@ void eln2dummy(void (*)(void *), void *, double, int); SEXP nano_PreserveObject(SEXP); void nano_ReleaseObject(SEXP); SEXP mk_error(const int); -SEXP mk_error_ncurl(const int); nano_buf nano_char_buf(const SEXP); SEXP nano_decode(unsigned char *, const size_t, const int); void nano_encode(nano_buf *, const SEXP); @@ -309,6 +308,7 @@ extern SEXP nano_precious; extern SEXP nano_recvAio; extern SEXP nano_reqAio; extern SEXP nano_refHook; +extern SEXP nano_sendAio; extern SEXP nano_success; extern SEXP nano_unresolved; diff --git a/src/utils.c b/src/utils.c index 9024e48dc..154fc2f2d 100644 --- a/src/utils.c +++ b/src/utils.c @@ -23,21 +23,17 @@ // internals ------------------------------------------------------------------- -SEXP mk_error_ncurl(const int xc) { +static SEXP mk_error_ncurl(const int xc) { - SEXP env; - PROTECT(env = Rf_allocSExp(ENVSXP)); - NANO_CLASS2(env, "ncurlAio", "recvAio"); - SEXP err = PROTECT(Rf_ScalarInteger(xc)); + const char *names[] = {"status", "headers", "data", ""}; + SEXP out = PROTECT(Rf_mkNamed(VECSXP, names)); + SEXP err = Rf_ScalarInteger(xc); Rf_classgets(err, nano_error); - Rf_defineVar(nano_ResultSymbol, err, env); - Rf_defineVar(nano_StatusSymbol, err, env); - Rf_defineVar(nano_ProtocolSymbol, err, env); - Rf_defineVar(nano_HeadersSymbol, err, env); - Rf_defineVar(nano_ValueSymbol, err, env); - Rf_defineVar(nano_DataSymbol, err, env); - UNPROTECT(2); - return env; + SET_VECTOR_ELT(out, 0, err); + SET_VECTOR_ELT(out, 1, err); + SET_VECTOR_ELT(out, 2, err); + UNPROTECT(1); + return out; }