看到 Ruby 的底层了吗?
最近,我一直在学习《编程语言语用学》第三版,以了解有关语言底层工作原理的更多信息,并且通过查看由真正基本的 GCC 编译的 C 代码生成的程序集,我获得了很多收获。我开始对 C 系列的静态语言更加熟悉,并且我也想开始研究解释型语言。
Ruby 作为我最喜欢的脚本语言,将是一个很好的候选者。我认为从 MRI 开始进行学习是可以的,并弄清楚所有扫描/解析/语义分析/绑定/范围界定和其他魔法到底是如何在幕后发生的。
这是否在任何地方都有详细描述,或者我有什么方法可以深入研究它,比如通过反汇编已编译的程序?我还没有对解释语言进行太多挖掘,所以就 MRI 而言,我不太知道从哪里开始。
谢谢!
I've been recently working my way through Programming Language Pragmatics 3rd ed to learn more about how languages work underneath, and I've gotten a lot of mileage out of looking at the assembly produced by really basic GCC-compiled C code. I'm starting to get more comfortable with static languages from the C family, and I would like to begin looking into interpreted languages as well.
Ruby, being my favorite scripting language, would be an excellent candidate for this. I think it'd be OK to start with MRI for learning purposes, and figure out how exactly all of the scanning/parsing/semantic analysis/binding/scoping and other magic happens behind the scenes.
Is this described in detail anywhere, or is there any way for me to dig into it, like say, with the disassembly of a compiled program? I haven't done much digging with interpreted languages, so I wouldn't quite know where to start as far as MRI is concerned.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 RubyVM::InstructionSequence 查看任意 Ruby 源代码的 YARV 字节码
。这是一个简单的示例:
然后您可以编译它并反汇编它:
并获得如下输出:
You can peek at the YARV bytecode of any bit of Ruby source code with
RubyVM::InstructionSequence
Here's a quick example:
Then you can compile it and disassemble it:
And get some output like this:
Ruby 本身是用 C 编写的,因此可能值得看一下代码。 https://github.com/ruby/ruby
我还推荐 Patrick Farley 关于方法调度的文章。 http://www.klankboomklang.com/2007/09/14/method-调度/
Ruby itself is written in C, so it might be worth taking a look at the code. https://github.com/ruby/ruby
I also recommend Patrick Farley's article about method dispatch. http://www.klankboomklang.com/2007/09/14/method-dispatch/
有一本一本日本书描述了 MRI 1.7.3 的内部工作原理,称为 Ruby Hacking指导。 部分英文翻译可用。
尽管翻译并不完整并且这本书基于旧版本的 ruby,但我仍然认为它是一个极好的资源。本书中涵盖的许多内容可能仍然与当前的 Ruby 版本相关,例如对象的结构、实例变量如何存储、方法查找如何完成等等。
这是一本非常有趣的读物。不过,您必须注意版本之间的差异。始终将 ruby 源代码放在手边。
There is a Japanese book that describes inner workings of MRI 1.7.3 called the Ruby Hacking Guide. A partial English translation is available.
Even though the translation is not complete and the book was based on an older version of ruby, I still find it to be a superb resource. Many things covered by the book are probably still relevant in current ruby versions, such as the structure of objects, how instance variables are stored, how method lookup is done and so on.
It is a very interesting read. You have to watch out for the differences between versions, though. Always keep the ruby source code at hand.