是否有可能产生并在运行时执行锈code? [英] Is it possible to generate and execute Rust code at runtime?

查看:203
本文介绍了是否有可能产生并在运行时执行锈code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用C,在运行时,我可以:

Using C, at runtime, I can:


  1. 创建一个函数的源代码code,

  2. 召唤出到GCC把它编译成一个.so(Linux)的(或使用LLVM等),

  3. 加载。所以,和

  4. 通话功能。

是一个类似的事情可能拉斯特?

Is a similar thing possible in Rust?

在我特别想用代数数据类型,因此使用的防锈的功能,一个C子集,是不够的。

In particular I want to use Algebraic Data Types, so using a C subset of Rust's features is not sufficient.

推荐答案

尚未正式,但它至少应与可能没有太多的黑客攻击。最大的障碍是,图书馆没有任何做动态加载尚未能力。
下面就来使其工作(上锈的传入分支)的潜在战略。

Not yet, officially, though it should be at least possible with not too much hacking. The biggest obstacle is that the libraries do not have any ability to do dynamic loading yet. Here's a potential strategy to make it work (on Rust's incoming branch).


  • 链接到rustc箱子以编程方式驱动的编译器。请注意,编译器不是线程安全所以只能运行一个在建的过程在时间。

  • 标记你想用#[no_mangle] 来调用该函数。这应该(我还没有尝试过)产生了未重整符号名称,因此很容易找到。

  • 创建绑定最少执行dlopen / dlsym进行

  • 找到函数指针和不安全其转换为锈闭合类型(目前在 SYS ::闭幕中定义)。

  • 通话结束。

  • Link to the rustc crate to drive the compiler programmatically. Be aware that the compiler is not threadsafe so only run one in-process build at a time.
  • Mark the function you want to call with #[no_mangle]. This should (I haven't tried it) produce an unmangled symbol name so it's easy to find.
  • Create minimal bindings to dlopen/dlsym
  • Find the function pointer and unsafely convert it to a Rust closure type (currently defined in sys::Closure).
  • Call the closure.

锈也有一个最小测试JIT可用于这种类型的事情,但它具有一些主要的缺陷。

Rust also has a minimally-tested JIT that can be used for this type of thing, but it has some major bugs.

这篇关于是否有可能产生并在运行时执行锈code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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