From ch@riie@g@o m@iii@g oii shikokuchuo@@et  Sun Nov 13 12:44:16 2022
From: ch@riie@g@o m@iii@g oii shikokuchuo@@et (ch@riie@g@o m@iii@g oii shikokuchuo@@et)
Date: Sun, 13 Nov 2022 11:44:16 +0000
Subject: [Rd] R_GetCurrentEnv() not working as intended
In-Reply-To: <51be8f23-d595-eb4d-9e0c-1b3f49b23a59@shikokuchuo.net>
References: <51be8f23-d595-eb4d-9e0c-1b3f49b23a59@shikokuchuo.net>
Message-ID: <c39614311353b6ada6e7c5c592a4ed64@shikokuchuo.net>

Perhaps my original question was too complicated, so I will just ask: is anyone using R_GetCurrentEnv() in their C code? If so, grateful if you could point me to an example where it is working for you.

I have searched Github and only come across a couple of trivial uses as an argument to Rf_eval(), where it probably returns the global environment, with the result being indistinguishable in normal use.

Thanks,

Charlie

October 22, 2022 12:52 AM, "Charlie Gao" <charlie.gao at shikokuchuo.net> wrote:

> Dear all,
> 
> I am attempting to use `R_GetCurrentEnv()` to return the current environment within C code, but it
> seems to always return the global environment.
> 
> Specifically, I would like to use it as an argument to R_NewEnv() so it is created with the correct
> enclosing environment. I also have functions in the environment that reference symbols in the
> closure and I would also like to use `R_GetCurrentEnv()` as an argument to `SET_CLOENV()`.
> 
> My workaround at the moment is to pass `environment()` as one of the arguments to the `.Call()`.
> For the actual code I am referring to:
> 
> https://github.com/shikokuchuo/nanonext/blob/main/src/aio.c#L516-L535
> 
> where I am currently passing `environment()` as 'clo' whereas ideally I would be able to use
> `R_GetCurrentEnv()` instead.
> 
> There is an open Bugzilla report from 2020 that says `R_GetCurrentEnv()` only returns the base
> namespace from within a `.Call()`, however I see that the proposed patch has already been adopted
> in the R source.
> 
> It seems that the function was introduced (fairly) recently in R 3.6, presumably for such uses. I
> would like to know if this is not the case or else confirmation that this is an outstanding bug.
> 
> Thanks,
> 
> Charlie



[Rd] R_GetCurrentEnv() not working as intended
ch@riie@g@o m@iii@g oii shikokuchuo@@et