在Go中运行用户提交的代码

huangapple go评论93阅读模式
英文:

Run user-submitted code in Go

问题

我正在开发一个应用程序,允许用户比较不同的字符串比较算法的执行效果。除了包含几种算法(包括Boyer-MooreKMP和其他“传统”算法)之外,我希望允许用户输入他们自己的算法(可以是他们自己的算法或对现有算法的修改)进行比较。

在Go语言中,有没有一种方法可以接收用户的代码(例如来自HTML文本区域)并执行它?

更具体地说,我希望实现以下特性:

  • 我提供一个方法签名,他们可以在方法中填入任何他们想要的内容。
  • 他们代码中的崩溃或语法错误不应导致我的整个程序崩溃。相反,我希望能够捕获错误并显示错误消息。

(在这种情况下,我不担心针对恶意代码的安全性,因为用户只会在自己的机器上执行我的程序,所以安全性是他们自己的责任。)

如果在Go语言中无法实现这一点,我可以考虑嵌入以下语言之一来用于比较函数(按优先顺序):JavaScript、Python、Ruby、C。是否有任何方法可以实现其中任何一种语言?

英文:

I am working on an application which allows users to compare the execution of different string comparison algorithms. In addition to several algorithms (including Boyer-Moore, KMP, and other "traditional" ones) that are included, I want to allow users to put in their own algorithms (these could be their own algorithms or modifications to the existing ones) to compare them.

Is there some way in Go to take code from the user (for example, from an HTML textarea) and execute it?

More specifically, I want the following characteristics:

  • I provide a method signature and they fill in whatever they want in the method.
  • A crash or a syntax error in their code should not cause my whole program to crash. It should instead allow me to catch the error and display an error message.

(In this case, I am not worried about security against malicious code because users will only be executing my program on their own machines, so security is their own responsibility.)

If it is not possible to do this natively with Go, I am open to embedding one of the following languages to use for the comparison functions (in order of preference): JavaScript, Python, Ruby, C. Is there any way to do any of those?

答案1

得分: 1

一个明确的否定。

但是你可以做一些花哨的事情:为什么不重新编译包括用户提供的代码的程序呢?
将代码分成两部分:一个驱动程序用于收集用户代码,重新编译实际代码,执行实际代码并报告结果。

可以包括其他语言的解释器,例如 Otto 是一个 JavaScript 解释器。(C 会比较困难 :-))

英文:

A clear No.

But you can do fancy stuff: Why not recompile the program including the user provided code?
Split the stuff into two: One driver which collects user code, recompiles the actual code, executes the actual code and reports the outcome.

Including other interpreters for other languages can be done, e.g. Otto is a Javascript interpreter. (C will be hard 在Go中运行用户提交的代码

答案2

得分: 1

你是否考虑过做类似于gopherjs playground的东西?根据这个链接,编译是在客户端完成的。

英文:

Have you considered doing something similar to the gopherjs playground? According to this, the compilation is being done client-side.

huangapple
  • 本文由 发表于 2014年11月19日 00:17:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/26998911.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定