[Rd] PROTECT and OCaml GC.

Simon Urbanek simon.urbanek at r-project.org
Mon Nov 30 22:25:59 CET 2009


On Nov 30, 2009, at 16:07 , Guillaume Yziquel wrote:

> Simon Urbanek a écrit :
>>>
>>> And it goes then to my other question: How can you pass to eval a  
>>> LANGSXP where the CAR is an *anonymous* function, no SYMSXP  
>>> involved?
>> You just pass it as value of the call. I suspect the reason it  
>> doesn't work is in your code, not in the facility (note that the  
>> link above is useless since the construction is mystery - if you  
>> were constructing it right, it would work ;)).
>> Small example:
>> SEXP myEval(SEXP FN, SEXP first_arg) {
>>  return eval(LCONS(FN, CONS(first_arg, R_NilValue)), R_GlobalEnv);
>> }
>> > .Call("myEval",function(x) x + 1, 10)
>> [1] 11
>
> In the eval function in eval.c, you have:
>
>>    case LANGSXP:
>>        if (TYPEOF(CAR(e)) == SYMSXP)
>>            /* This will throw an error if the function is not found  
>> */
>>            PROTECT(op = findFun(CAR(e), rho));
>>        else
>>            PROTECT(op = eval(CAR(e), rho));
>
> So imagine you have a LANGSXP whose CAR is a CLOSXP, the execution  
> goes into the last line of the code snippet above.
> And re-entring eval with a CLOSXP, the code goes into
>
>>    tmp = R_NilValue;           /* -Wall */
>> #ifdef Win32
>>    /* This is an inlined version of Rwin_fpreset (src/gnuwin/extra.c)
>>       and resets the precision, rounding and exception modes of a  
>> ix86
>>       fpu.
>>     */
>>    __asm__ ( "fninit" );
>> #endif
>>    R_Visible = TRUE;
>>    switch (TYPEOF(e)) {
>>    case NILSXP:
>>    case LISTSXP:
>>    case LGLSXP:
>>    case INTSXP:
>>    case REALSXP:
>>    case STRSXP:
>>    case CPLXSXP:
>>    case RAWSXP:
>>    case S4SXP:
>>    case SPECIALSXP:
>>    case BUILTINSXP:
>>    case ENVSXP:
>>    case CLOSXP:
>>    case VECSXP:
>>    case EXTPTRSXP:
>
> so PROTECT(op = eval(CAR(e), rho)) evaluates to R_NilValue.
>

Nope, it simply evaluates to itself (with ref count increased) - see  
tmp = e; ..; return(tmp).


> I figured out that's why evaluating a LANGSXP with CAR a CLOSXP  
> simply fails.
>

Wrong ;). A closure is a constant like any other object so it  
evaluates to itself.

Cheers,
Simon



> I'll have a look at the code snippet you gave, since I do not  
> understand why it doesn't fail the same way mine does.
>
> Thanks a lot.
>
> -- 
>     Guillaume Yziquel
> http://yziquel.homelinux.org/
>
>



More information about the R-devel mailing list