六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 38|回复: 0

关于erlang:apply/2/3

[复制链接]

升级  24%

22

主题

22

主题

22

主题

秀才

Rank: 2

积分
86
 楼主| 发表于 2013-1-26 13:59:13 | 显示全部楼层 |阅读模式
 
57 apply(Fun, Args) -> 58     apply(Fun,Args).                         59  60 apply(Mod, Name, Args) -> 61     apply(Mod, Name, Args). 
 上面代码是erlang.erl文件里看到的。这里aplly/3方法里面又调用一次apply/3 请问是什么意思
老大: 在虚拟机里面实现的
langzhe: 谢谢
erlang.erl代码编译的时候 进行了特殊编译?所以再调用时候 不会默认调用erlang:apply/3?
老大:
需要进一步 看看是怎么编译的?
来自http://erlang.2086793.n4.nabble.com/Erlang-about-quot-apply-quot-function-in-the-erlang-erl-td3528347.html#a3531504的部分解释
apply is a built in function (BIF). It is implemented in C, not Erlang.
>
> apply is a built in function (BIF). It is implemented in C, not Erlang.
Does it work in a similar manner to that of Smalltalks, or differently?

In Smalltalks, `Boolean ifTrue: aBlock ifFalse: aBlock` literal forms
are usually compiled to call into native code directly, so the
implementation of this message in Smalltalk code in Smalltalk VMs looks
like this:

    ifTrue:trueBlock ifFalse:falseBlock
        ^self ifTrue:[ trueBlock call ] ifFalse: [ falseBlock call ]

which looks like it would not work, but when the form
`var ifTrue:aBlockLiteral ifFalse:aBlockLiteral` is encountered the
Smalltalk compiler takes over and replaces this with native code. So the
Smalltalk implementation is only ever hit for non-literal sends (either
non-literal blocks, or usage of selectors), and its only role is to
redirect execution to the native version.

Is the purpose (and behavior) of the pure version of erlang:apply
similar? 
 
----------引用Richard
It's a fairly common "bootstrapping" sort of thing.
Presumably
 - when the compiler sees apply/2 or apply/3 in an expression
   it generates special in-line code
 - there needs to be a "real" function definition so that it
   can be called indirectly itself, discoverable in the
   module exports, traced if calls arrive that way in the
   debugger, &c
 
 
apply的调用在beam加载的时候vm会调整相关的指令,实际上是在vm的opcode里面执行的。
专注高性能容错分布式服务器的研究和实现
http://blog.yufeng.info
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表