在调用堆栈中显示特殊的原始函数 [英] Show special primitive functions in call stack

查看:77
本文介绍了在调用堆栈中显示特殊的原始函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题提示了以下问题:是否可以查看调用堆栈中的特殊基本函数?

This question prompted the following question: Is there a way to view the special primitive functions that are in the call stack?

例如,创建一个在退出时返回调用堆栈的函数:

For example, create a function that returns the call stack on exit:

myFun <- function(obj){
  on.exit(print(sys.calls()))
  return(obj)
}

调用此函数并使用 assign 将结果分配给对象避免使用特殊的原始函数:

Calling this function and assigning its result to an object using assign avoids using special primitive functions:

> assign("myObj",myFun(4))
[[1]]
assign("myObj", myFun(4))

[[2]]
myFun(4)

但是使用赋值运算符,它就被排除在堆栈之外

But using the assignment operator, this gets left out of the stack

> `<-`(myObj, myFun(6))
[[1]]
myFun(6)

当然,要想在调用堆栈中看到赋值运算符可能不是很常见,而是其他功能,例如 rep log 也被隐藏

Granted, it might not be all that common to want to see the assignment operator in the call stack, but other functions such as rep and log also get hidden

推荐答案

我不认为有任何办法通过调用堆栈访问对原始函数的调用。这就是为什么。

I don't think there's any way to access calls to primitive functions via the call stack. Here is why.

当评估典型 R函数时:

When a "typical" R function is evaluated:


  1. 提供的参数与形式参数匹配。

  2. 创建一个新环境(带有指向其封闭环境的指针),并将形式参数分配给其中。

  3. 该函数的主体在新创建的环境中进行评估。

包围环境的链当函数调用相互嵌套时,将建立 sys.calls() sys的调用堆栈或框架堆栈。 frame()之类的东西提供了一些访问权限。

The chain of enclosing environments that is built up when function calls are nested within one another is the "call stack" or "frame stack" to which sys.calls(), sys.frames() and the like provide some access.

我强烈怀疑对原始函数的调用未出现在调用堆栈中,因为在评估过程中未创建R端环境。没有创建任何环境,因此没有环境出现在调用堆栈上。

My strong suspicion is that calls to primitive functions don't appear on the call stack because no R-side environment is created during their evaluation. No environment is created, so no environment appears on the call stack.

有关更多信息,请参见John Chambers如何在 用于数据分析的软件

For some more insight, here's how John Chambers describes the evaluation of primitive functions on page 464 of Software for Data Analysis:


对这些函数之一的调用的评估以通常的方式
开始,但是当评估者发现该函数对象是原始的
而不是a R中定义的函数,它分支到完全不同的
计算。该对象似乎只是带有正式
参数的函数对象,以及带有字符串参数的对函数.Primitive()的调用。
实际上,它实际上仅包含表的索引,该表是实现R的核心的C代码的
的一部分。该表的条目标识核心中的
C例程,负责评估对此特定
原语的调用。评估程序将控制权转移到该例程,并期望例程
返回指向R对象的C语言指针,该指针表示调用的
值。

Evaluation of a call to one of these functions starts off in the usual way, but when the evaluator discovers that the function object is a primitive rather than a function defined in R, it branches to an entirely different computation. The object only appears to be a function object with formal arguments and a call to the function .Primitive() with a string argument. In reality, it essentially contains only an index into a table that is part of the C code implementing the core of R. The entry of the table identifies a C routine in the core that is responsible for evaluating calls to this specific primitive. The evaluator will transfer control to that routine, and expects the routine to return a C-language pointer to the R object representing the value of the call.

这篇关于在调用堆栈中显示特殊的原始函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆