Ruby 2.0字节码导出/导入 [英] Ruby 2.0 Bytecode Export / Import

查看:103
本文介绍了Ruby 2.0字节码导出/导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读有关新的ruby 2.0功能,并发现它将支持字节码导入/导出:

I've been reading about the new ruby 2.0 features, and found that it will support bytecode import / export:


Ruby 2.0有望简化将预编译的Ruby脚本保存为字节码表示形式,然后直接运行它们的过程。

Ruby 2.0 is expected to make it simple to save pre-compiled Ruby scripts to bytecode representations and to then run these directly.

I'我已经安装了ruby-2.0.0-p0,但是我没有找到有关如何导出字节码的任何信息(或者关于该问题的一般文档)。此功能是否已经实现?如果可以,该如何使用?

I've installed ruby-2.0.0-p0, but I didn't find any information on how to export the bytecode (or generally documentation on that matter). Is this feature already implemented, and if so, how do I use it?

我也想知道其中的一些细节。 YARV字节码是否应该与平台无关?

I'm also wondering about some of the details. Is YARV-bytecode supposed to be platform-independent? Are all gems automatically included in the bytecode?

推荐答案

在有人掌握了更好的信息之前,我做了一些研究: / p>

Until someone with better information looks at this question, I did some research:


此功能是否已经实现,如果可以,该如何使用?

Is this feature already implemented, and if so, how do I use it?

它已实现,但似乎没有暴露(例如 ruby​​ --dump-bytecode 不存在)。另外,文档也很少。据我所知,您正在寻找的东西是这样的:

It is implemented, but it doesn't seem to be exposed (e.g ruby --dump-bytecode doesn't exist). Also there's not very much documentation. As far as I can tell, what you are looking for is something like:

seq = RubyVM::InstructionSequence.compile_file("./example.rb")

seq.disassemble 将为您提供格式良好的字符串,您可以将其转储到文件中,或者 seq.to_a 将生成一个数组,如下所示:

seq.disassemble will give you a nicely formatted string that you could dump to a file, or seq.to_a will generate an an array that looks like:

["YARVInstructionSequence/SimpleDataFormat",
 2,
 0,
 1,
 {:arg_size=>0, :local_size=>1, :stack_max=>2},
 "<main>",
 "./example.rb",
 "./example.rb",
 1,
 :top,
 [],
 0,
 [],
 [[:trace, 1],
  [:putspecialobject, 3],
  [:putnil],
  [:defineclass,
   :User,
   ["YARVInstructionSequence/SimpleDataFormat",
    2,
    0,
    1,
    {:arg_size=>0, :local_size=>1, :stack_max=>6},
    "<class:User>",
    ....

如果您要坚持下去s到文件,您可以执行以下操作:

If you want to persist this to a file, you can do something like:

File.write("out.dump", Marshal.dump(seq.to_a))

然后再次加载:

arr = Marshal.load(File.read("out.dump"))

不幸的是,鉴于上面加载的数组,我似乎无法弄清楚如何创建新的 InstructionSequence

Unfortunately I can't seem to figure out how to create a new InstructionSequence given the array loaded above.


我也想知道一些细节。 YARV字节码是否应该与平台无关?

I'm also wondering about some of the details. Is YARV-bytecode supposed to be platform-independent? Are all gems automatically included in the bytecode?

在上面的示例中,不包括宝石。您的 InstructionSequence 将包括与 require‘active_record’或您拥有的字节码等效的字节码。我怀疑如果 ruby​​ 可执行文件直接提供了字节码的转储和加载,这种行为将保持不变。

In the example above, gems are not included. Your InstructionSequence would include the bytecode equivalent of a require 'active_record' or what have you. I suspect that if dumping and loading of bytecode were provided directly by a ruby executable, this behavior would stay the same.

如果其他人有更多信息,我希望看到它!

If anyone else has more information I'd love to see it!

这篇关于Ruby 2.0字节码导出/导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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