问题描述
有什么方法可以将 C 代码转换为 JavaScript 并从 JavaScript 转换为 C?我发现 V8 juice
可以从 C++ 生成 JavaScript-side
类,但这只是一种方式(C++ 到 JavaScript).
Is there any way to convert C code to JavaScript and from JavaScript to C? I found V8 juice
which can generate JavaScript-side
classes from C++, but it's only one way (C++ to JavaScript).
我不是在寻找软件.
推荐答案
非常非常棘手 --- Javascript 是一种高度动态的语言,几乎所有东西都可以在运行时更改:变量名、函数名、类型等. 因此,它非常糟糕地映射到 C 上.这甚至没有考虑 eval()
,它可以让您在字符串中构造任意 Javascript 块并运行它们.
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 翻译器都必须能够处理这些事情,这意味着它必须在运行时将 Javascript 翻译成 C --- 这使它成为 JIT,您已经在使用它.
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 编写 C 绑定.这些将允许您的 Javascript 代码调用 C 代码,反之亦然.这将允许人们用 C 编写插件,将它们编译成 .so
共享库,您现在可以从 Javascript 代码加载和运行这些共享库.这意味着您不需要翻译任何内容.
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 .so
shared libraries, which you can now load and run from your Javascript code. This means you don't need to translate anything.
Javascript 不是我的领域,所以我不能推荐任何特定的机制,恐怕 --- 但如果你已经找到的 V8Juice 不允许你这样做,我会感到非常惊讶.
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代码转换为c代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!