如何在Dart中动态执行(如Eval)? [英] How do I execute Dynamically (like Eval) in Dart?

查看:617
本文介绍了如何在Dart中动态执行(如Eval)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从Dart入门以来,我一直在寻找一种将Code当作Dart(文本)源(同一程序可能会动态生成)的方法。就像臭名昭著的 eval()函数一样。

Since getting started in Dart I've been watching for a way to execute Dart (Text) Source (that the same program may well be generating dynamically) as Code. Like the infamous "eval()" function.

最近,我发现一些提示,即隔离之间的通信端口支持某种 Spawn,似乎可以允许这个把戏。在Ruby中,还可以动态地将模块作为语言功能加载,也许在Dart中有某种方法可以做到这一点?

Recently I have caught a few hints that the communication port between Isolates support some sort of "Spawn" that seems like it could allow this "trick". In Ruby there is also the possibility to load a module dynamically as a language feature, perhaps there is some way to do this in Dart?

任何线索或简单示例都会非常感谢。

Any clues or a simple example will be greatly appreciated.

预先感谢!

推荐答案

Ladislav Thon提供了此答案(在Dart论坛上),

Ladislav Thon provided this answer (on the Dart forum),

我相信可以肯定地说Dart永远不会出现。但是它将具有其他更结构化的方式来动态生成代码(代码名镜像生成器)。

I believe it's very safe to say that Dart will never have eval. But it will have other, more structured ways of dynamically generating code (code name mirror builders). There is nothing like that right now, though.

有两种方法产生隔离对象:spawnFunction,它通过新隔离对象中的现有代码运行现有功能,因此,您不需要任何东西,而spawnUri可以从给定的URI下载代码,并在新的隔离环境中运行它。这实质上是动态代码加载-但是动态加载的代码与现有代码隔离。它在新的隔离环境中运行,因此与它进行通讯的唯一方法是通过消息传递(通过端口)。

There are two ways of spawning an isolate: spawnFunction, which runs an existing function from the existing code in a new isolate, so nothing you are looking for, and spawnUri, which downloads code from given URI and runs it in new isolate. That is essentially dynamic code loading -- but the dynamically loaded code is isolated from the existing code. It runs in a new isolate, so the only means of communicating with it is via message passing (through ports).

谢谢拉迪斯拉夫...

Thank you Ladislav...

这篇关于如何在Dart中动态执行(如Eval)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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