Javascript 将javascript代码转换为c代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5192223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:05:37  来源:igfitidea点击:

Convert javascript code to c code

javascriptccompiler-construction

提问by Wassim AZIRAR

Is there any way to convert C code to JavaScript and from JavaScript to C? I found V8 juicewhich can generate JavaScript-sideclasses from C++, but it's only one way (C++ to JavaScript).

有什么方法可以将 C 代码转换为 JavaScript 并从 JavaScript 转换为 C?我发现V8 juice可以JavaScript-side从 C++生成类,但这只是一种方式(C++ 到 JavaScript)。

I'm not looking for a software.

我不是在寻找软件。

采纳答案by David Given

Very, very tricky --- Javascript is a heavily dynamic language where pretty much everything can be changed at run time: names of variables, functions, types, etc. As such it maps very badly onto C. And that's not even considering eval(), which will let you construct arbitrary chunks of Javascript in strings and run them.

非常非常棘手 --- Javascript 是一种高度动态的语言,其中几乎所有内容都可以在运行时更改:变量名、函数名、类型等。因此,它在 C 上的映射非常糟糕。这甚至没有考虑eval(),将让您在字符串中构造任意的 Javascript 块并运行它们。

Any Javascript translator would have to be able to cope with such things, which means it would have to translate the Javascript into C at run-time --- which makes it a JIT, which you're already using.

任何 Javascript 翻译器都必须能够处理这些事情,这意味着它必须在运行时将 Javascript 翻译成 C --- 这使它成为您已经在使用的 JIT。

You may want to look at writing C bindings for Javascript instead. These will allow your Javascript code to call out to C code and vice versa. This would allow people to write plugins in C, compile them into .soshared libraries, which you can now load and run from your Javascript code. This means you don't need to translate anything.

您可能需要考虑为 Javascript 编写 C 绑定。这些将允许您的 Javascript 代码调用 C 代码,反之亦然。这将允许人们用 C 编写插件,将它们编译成.so共享库,您现在可以从您的 Javascript 代码加载和运行这些库。这意味着您无需翻译任何内容。

Javascript's not my area so I can't recommend any particular mechanism, I'm afraid --- but I'd be very surprised if V8Juice, which you've already found, didn't let you do this.

Javascript 不是我的领域,所以我不能推荐任何特定的机制,恐怕——但是如果您已经找到的 V8Juice 不让您这样做,我会感到非常惊讶。

回答by Anderson Green

It may be possible to translate a subset of JavaScript to Lua using Castl, and then translate the Lua code into C using lua2c.

有可能翻译使用JavaScript的到Lua一个子集Castl,然后使用Lua代码翻译成C lua2c

Alternatively, it might be possible to compile JavaScript to Python, and then convert the Python source code into Cusing the RPython compiler. Since RPython is a subset of Python, this should allow a subset of JavaScript to be translated into C.

或者,也可以将JavaScript 编译为 Python,然后使用 RPython 编译器将 Python 源代码转换为 C。由于 RPython 是 Python 的一个子集,这应该允许将 JavaScript 的一个子集翻译成 C。

There is also a compiler called ts2c, which apparently is able to translate JavaScript and TypeScript source code into C.

还有一个名为ts2c的编译器,它显然能够将 JavaScript 和 TypeScript 源代码翻译成 C。

Yet another option is to compile JavaScript to C or WebAssembly using NectarJS.

另一种选择是使用NectarJS将 JavaScript 编译为 C 或 WebAssembly 。

回答by user3015682

Why convert when you can simply embed?

当您可以简单地嵌入时,为什么要转换?

https://code.google.com/p/v8/"V8 can run standalone, or can be embedded into any C++ application."

https://code.google.com/p/v8/“V8 可以独立运行,也可以嵌入到任何 C++ 应用程序中。”

Being embedded into a C++ application allows JavaScript to access any system that the C++ application has access to, eliminating the need to convert in the first place. I would limit what is has access to somewhat for security reasons though. Web-browsers are obviously the most prominent form of JavaScript being embedded in a C++ application. As implied by the name, JavaScript is a scripting language, not intended to be compiled into assembly/machine code as is C code.

嵌入到 C++ 应用程序中后,JavaScript 可以访问 C++ 应用程序可以访问的任何系统,而无需首先进行转换。不过,出于安全原因,我会限制可以访问的内容。Web 浏览器显然是嵌入在 C++ 应用程序中的最突出的 JavaScript 形式。顾名思义,JavaScript 是一种脚本语言,不打算像 C 代码那样编译成汇编/机器代码。

回答by arpo

This project looks quite promising, even though it is a work in progress.

这个项目看起来很有前途,尽管它是一项正在进行的工作。

https://github.com/andrei-markeev/ts2c

https://github.com/andrei-markeev/ts2c

You can convert JavaScript to C online here:

您可以在此处在线将 JavaScript 转换为 C:

https://andrei-markeev.github.io/ts2c/

https://andrei-markeev.github.io/ts2c/

回答by Mallik

C language can do hell of lot stuff that javascript or v8 engine can't do, specifically system program such as memory management, direct memory access, interrupts, assembly code, thread management. So it is not possible to convert c to javascript unless it is very simple code without some of system code. With nodeJS (not v8 javascript) along with system wrapper code do some of system level functions. Again it is complex part of conversion. Even if we able to convert this simple code the execution of c and converted javascript differs as it it is based on v8 engine.

C 语言可以做很多 javascript 或 v8 引擎做不到的事情,特别是系统程序,如内存管理、直接内存访问、中断、汇编代码、线程管理。所以不可能将 c 转换为 javascript,除非它是没有一些系统代码的非常简单的代码。使用 nodeJS(不是 v8 javascript)以及系统包装器代码执行一些系统级功能。同样,它是转换的复杂部分。即使我们能够转换这个简单的代码,c 和转换后的 javascript 的执行也不同,因为它基于 v8 引擎。

It is possible to convert javascript to C. Because C can virtually handle any execution stuff including that of interpreters.

可以将 javascript 转换为 C。因为 C 几乎可以处理任何执行内容,包括解释器的执行内容。

回答by ceilingcat

The QuickJSJavaScript interpreter from Fabrice Bellardalso includes a JS to C compiler.

Fabrice BellardQuickJSJavaScript 解释器还包括一个JS 到 C 的编译器