引用 RubyVM::InstructionSequence 文档:
RubyVM::InstructionSequence 类是对运行在 Ruby 虚拟机上的指令序列的抽象,使用该类,咱们能够将 Ruby 源代码字符串编译成虚拟机内部的指令序列,以此来探究虚拟机内部的工做原理,同时经过灵活的编译参数,咱们能够控制 Ruby 指令生成过程 函数
因此经过挖掘 RubyVM & InstructionSequence 类的实现能够从另外一个角度来分析 Ruby 语言的解释过程code
RubyVM 和 Thread 类同样,也是一个 Ruby native 类,在 Init_VM 中定义:文档
// vm.c void Init_VM(void) { ... rb_cRubyVM = rb_define_class("RubyVM", rb_cObject); rb_undef_alloc_func(rb_cRubyVM); rb_undef_method(CLASS_OF(rb_cRubyVM), "new"); rb_define_singleton_method(rb_cRubyVM, "stat", vm_stat, -1); ... }
new 方法被 undef 掉,增长了一个 stat 方法,具体实如今 vm_stat 函数中字符串
InstructionSequence 类的定义在 Init_ISeq 函数中:虚拟机
// iseq.c void Init_ISeq(void) { /* declare ::RubyVM::InstructionSequence */ // 定义类 rb_cISeq = rb_define_class_under(rb_cRubyVM, "InstructionSequence", rb_cObject); rb_undef_alloc_func(rb_cISeq); // 经过 rb_define_method 定义 inspect, disasm 等方法 rb_define_method(rb_cISeq, "inspect", iseqw_inspect, 0); rb_deifne_method(rb_cISeq, "disasm", iseqw_disasm, 0); ... }
InstructionSequence 类方法的 native 实现大多以 iseqw_ 为前缀string
compile 方法用于将 Ruby 源代码字符串编译成指令序列,对应的 native 实现是 iseqw_s_compile 函数:it
static VALUE iseqw_s_compile(int argc, VALUE *argv, VALUE self) { VALUE src, file = Qnil, path = Qnil, line = INT2FIX(1), opt = Qnil; int i; rb_secure(1); i = rb_scan_args(argc, argv, "1*:", &src, NULL, &opt); if (i > 4+NIL_P(opt)) rb_error_arity(argc, 1, 5); switch (i) { case 5: opt = argv[--i]; case 4: line = argv[--i]; case 3: path = argv[--i]; case 2: file = argv[--i]; } // file 默认值 if (NIL_P(file)) file = rb_fstring_cstr("<compiled>"); // line 默认值 if (NIL_P(line)) line = INT2FIX(1); return iseqw_new(rb_iseq_compile_with_option(src, file, path, line, 0, opt)); }