javascript 在javascript中调用C函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11138929/
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-10-26 12:10:36  来源:igfitidea点击:

Calling C functions inside javascript

javascripthtmlctranslation

提问by ahmet

The javascriptis printing out the HTMLonto the page example below, is it possible to call a Cfunction on it for example in Cto convert something to another language there is a function LANG_Str("text")which converts the text into the specified language. Would it be possible to use this function on the below text inside Javascript?.

javascript被打印出HTML到页面下方例如,是否可以调用一个C函数就可以了,例如C转换的东西为另一种语言有一个函数 LANG_Str("text"),其将文字转换成指定的语言。是否可以在 Javascript 中的以下文本中使用此函数?。

"<tr><th>Service</th><th>Target Allocation (%)</th><th></th>"

EDIT:

编辑:

I'm basically wanting to do a human language translation. The site already supports multi-language, the problem is on the custom screen like the one shown above which gets generated in Javascript, cannot use the function used to translate text the way its done normally in C.

我基本上想做一个人类语言翻译。该站点已经支持多语言,问题是在上面显示的自定义屏幕上,它是用 Javascript 生成的,无法像在 C 中正常完成的那样使用用于翻译文本的功能。

采纳答案by Joe

If it's running in the browser: no. Sorry.

如果它在浏览器中运行:否。对不起。

You might be able to do it in server-side code beforehand (e.g. Python or PHP which can call C) when putting together the page content. Alternatively you can make an AJAX request to a server which exposes the C function as an HTTP API/Endpoint (via, GCI, FCGI or Python/PHP/Perl). But not in the browser.

在将页面内容放在一起时,您可能可以事先在服务器端代码(例如可以调用 C 的 Python 或 PHP)中执行此操作。或者,您可以向服务器发出 AJAX 请求,该服务器将 C 函数公开为 HTTP API/端点(通过 GCI、FCGI 或 Python/PHP/Perl)。但不是在浏览器中。

This is because the JS runs in a sandboxed virtual environment which has no access to system calls or anything outside the runtime.

这是因为 JS 在沙盒虚拟环境中运行,该环境无法访问系统调用或运行时之外的任何内容。

EDIT

编辑

In response to your comment "The script is ran in the C using HTML_WriteToCgi", this suggests that you are putting together the HTML in C on your server. If this is correct, go for my option 1 above, by injecting the values directly into the JS source code if all values come out of some data known by the server.

为了回应您的评论“脚本是使用 HTML_WriteToCgi 在 C 中运行的”,这表明您正在将 C 中的 HTML 放在您的服务器上。如果这是正确的,请选择我上面的选项 1,如果所有值都来自服务器已知的某些数据,则将值直接注入 JS 源代码。

You might consider moving some functionality out of browser JS and back into server-side code to solve your problem.

您可能会考虑将某些功能从浏览器 JS 中移出并移回服务器端代码中以解决您的问题。

回答by Joe

You can make a special request, so the webserver can use that request and send it to the webpage.

您可以提出特殊请求,以便网络服务器可以使用该请求并将其发送到网页。

回答by zzzzBov

JavaScript can't access any other processes directly, but it canmake a server request for the information. The server can call a C function if need be.

JavaScript 不能直接访问任何其他进程,但它可以向服务器请求信息。如果需要,服务器可以调用 C 函数。

In the end, it's not JavaScript calling the C function, it's the server (and whatever language it's using: Python, PHP, ASP.NET, JSP, etc) that would be calling the C function.

最后,调用 C 函数的不是 JavaScript,而是调用 C 函数的服务器(以及它使用的任何语言:Python、PHP、ASP.NET、JSP 等)。

回答by Jonathan M

You might consider creating a RESTful web serviceon your server that will receive the source text and target language id, then return the translated text. You could then access it from your webpage via an ajax call.

您可能会考虑在您的服务器上创建一个RESTful Web 服务,该服务将接收源文本和目标语言 ID,然后返回翻译后的文本。然后,您可以通过ajax 调用从您的网页访问它。

回答by ndrewxie

My interpretation is that your goal is to call a C function within HTML / Javascript and capture the output.

我的解释是,您的目标是在 HTML/Javascript 中调用 C 函数并捕获输出。

What you could do is make a VM. Basically, you have a huge array "memory", a couple of "registers", etc... The hardest part is to make sure that they instruction set and the bytecodes of your VM mirrors some common instruction set that there is a C compiler for. You compile the C code that VM on your computer, save it to a file, and run it on the VM. If doing that is too hard, you could just get a C to assembly converter, and just define a couple of Assembly instructions instead. There is a Linux emulatorin pure javascript with no server calls that does precisely that.

你可以做的是制作一个虚拟机。基本上,你有一个巨大的数组“内存”,几个“寄存器”等等......最难的部分是确保它们的指令集和你的VM的字节码反映了一些有C编译器的常见指令集为了。您在计算机上编译该 VM 的 C 代码,将其保存到文件中,然后在 VM 上运行它。如果这样做太难了,你可以得到一个 C 到汇编转换器,然后定义几个汇编指令。有一个纯 javascript的Linux 模拟器,没有服务器调用可以做到这一点。