是否有 JavaScript 的本机机器代码编译器?

发布于 2024-07-26 18:17:50 字数 1542 浏览 10 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

纵性 2024-08-02 18:17:50

据我所知,JavaScript 没有静态编译器。 这在理论上当然是可能的; 然而,JavaScript 的静态编译需要非常重量级的运行时来支持其所有功能(例如动态类型和 eval)。 顺便说一句,当需要静态编译 Python(另一种动态语言)时, PyPy 开发人员最终创建了一种语言,它是 Python 的一个非常有限的子集(称为 RPython),缺乏 Python 的一些更动态的功能,但能够进行静态编译。

如果你问这个是为了从 JavaScript 代码创建一个独立的可执行文件,我确信一定有包装器,它本质上会创建一个包含你的脚本和嵌入式 JavaScript VM 的可执行文件(遗憾的是,我不知道任何临时的) )。

As far as I know, there are no static compilers for JavaScript. It is certainly theoretically possible; however, a static compilation of JavaScript would need a very heavyweight runtime to support all of its features (such as dynamic typing and eval). As a small aside, when presented with the need to statically compile Python (another dynamic language), the PyPy developers ended up creating a language which was a very restricted subset of Python (called RPython), void of some of Python's more dynamic features, that was capable of being statically compiled.

If you're asking this for the purpose of creating a standalone executable from JavaScript code, I'm sure there must be wrappers which essentially would create an executable containing your script and an embedded JavaScript VM (sadly, I don't know any offhand).

梦途 2024-08-02 18:17:50

这绝对是可行的,尽管我目前知道如何做到这一点的唯一方法是两步过程...

  1. 使用 Mozilla Rhino JSC
  2. 使用诸如 GNU 的 GCJ 之类的东西将生成的 java 类文件编译为可执行文件。

但你为什么要这么做呢? 您期望找到什么优势?

It's definitely doable, although the only way I know how to do it at the moment is a two step process...

  1. Compile the javascript to Java using Mozilla Rhino JSC.
  2. Compile the resulting java class file to executable using something like GNU's GCJ.

Why would you want to, though? What advantage do you expect to find?

極樂鬼 2024-08-02 18:17:50

Google V8 引擎将 JavaScript 编译为本机机器代码。 此功能在我为 node.js 和 io.js 项目编写的 EncloseJS 编译器中使用。

Google V8 engine compiles JavaScript into native machine code. This feature is used in the EncloseJS compiler which I wrote for for node.js and io.js projects.

晌融 2024-08-02 18:17:50

请注意,所有这些解决方案都是 DOMless,因此没有像 angular.jsjquery 这样的库,只有 underscore.js< /code>/lodash

Falaina 的回答后,
PyPy 确实有 JavaScript

另外,Appcelerator Titanium 有一个 js > JavaBit > > JavaBit android

最后,node.js 可以使用 nexe
在另一个答案中进行了解释

Please note that all of these solutions are DOMless, so no libraries like angular.js or jquery, only underscore.js/lodash

Following up to Falaina's answer,
PyPy does have a dist for JavaScript

Also, Appcelerator Titanium has a js > JavaBit > android

Finally, node.js can use nexe
Explained in this other answer

树深时见影 2024-08-02 18:17:50

理论上是可能的,但会涉及很多运行时支持包袱(甚至需要完整的 Javascript 编译器或解释器来支持eval)。

您是否正在寻找一个真正的本机代码编译器,或者您是否正在寻找可以将 Javascript 代码与运行时捆绑到单个可执行二进制文件中的东西?

It is theoretically possible, but there will be a lot of runtime support baggage involved (and even a full Javascript compiler or interpreter to support eval).

Are you looking for an actual native code compiler, or are you looking for something that can bundle Javascript code along with a runtime into a single executable binary?

吝吻 2024-08-02 18:17:50

我确信微软有一个可下载的 Jscript(“微软的 javascript”)编译器

如果你有 Windows, :
如果你编写一个 javascript 文本文件,你可以通过以下方式编译它:
打开命令提示符
使用 cd 进入文本文件目录
(如果您有 Windows)输入:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\jsc.exe textfile.js

否则:
下载 jscript 编译器
键入 jscript 编译器的目录,然后键入 javascript 文本文件的名称,

如果您不想向编译器键入整个目录名称,则可以添加环境变量。

这家伙解释得更好:
http://www.phpied.com/make-your-javascript- a-windows-exe/

我个人认为这真的很酷。 我只是希望有更多的文档。

我很确定这就是您正在寻找的东西,但我有点新。

I'm sure microsoft has a downloadable Jscript ("Microsoft's javascript") compiler

if you have windows:
If you write a javascript text file you can compile it by:
opening the command prompt
using cd to get to the text file directory
(if you have windows) type: C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\jsc.exe textfile.js

otherwise:
download a jscript compiler
type the directory of the jscript compiler and then the name of the javascript text file

you can add environment variables if you don't want to type out the whole directory name to the compiler.

this guy explains it better:
http://www.phpied.com/make-your-javascript-a-windows-exe/

I personally think this is really cool. I just wish there was more documentation for it.

I'm pretty sure that is what you're looking for, but I'm a bit new.

箜明 2024-08-02 18:17:50

适用于 iOS 的 Adob​​e AIR AOT 编译器将名为 Actionscript 3.0 的 JavaScript 超集静态编译为 ABC 字节码,然后通过 LLVM 编译为机器代码。 如果您要编写没有类和类型注释的 AS3 代码,那么它本质上就是 JavaScript,然后编译器会很乐意将其编译为机器代码。 遗憾的是,这不是开源软件,并且您无法访问任何 DOM(许多人在想到 JavaScript 时都会想到 DOM),因为它本质上是在 Flash Player 实例内运行的。

Adobe AIR's AOT compiler for iOS statically compiles a superset of JavaScript called Actionscript 3.0 down to ABC bytecode, then machine code through LLVM. If you were to write your AS3 code without classes and without type annotation, it would essentially be JavaScript, and then the compiler would happily compile it down to machine code. Sadly this is not open source software, and you don't get access to any DOM (which many people think of when they think of JavaScript) because it's running inside essentially a Flash Player instance.

毁梦 2024-08-02 18:17:50

FF3.5 中的 TraceMonkey 对 JavaScript 代码的某些部分执行此操作。 您也许可以从那里获得一些指示!

TraceMonkey in FF3.5 do this to some parts of the javascript code. You may be able to get some directions from there!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文