Calling C functions inside javascript

javascript outputs the HTML to the example page below, is it possible to call the C function on it, for example, in C , to convert something to another language, there is a LANG_Str("text") function that converts text to the specified language. Is it possible to use this function in the lower text inside Javascript ?.

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

EDIT:

Basically I want to do a translation into human language. The site already supports multilingualism, the problem is that a custom screen like the one shown above, which is generated in Javascript, cannot use the function used to translate the text as it usually does in C.

+6
source share
5 answers

If it is running in a browser: no. Unfortunately.

You may be able to do this in the server code in advance (for example, Python or PHP, which can call C) when linking the contents of the page. Alternatively, you can make an AJAX request to a server that provides the C function as an HTTP API / endpoint (via, GCI, FCGI, or Python / PHP / Perl). But not in the browser.

This is because JS works in an isolated virtual environment that does not have access to system calls or anything outside the runtime.

EDIT

In response to your comment, "the script is executed in C using HTML_WriteToCgi", this means that you are collecting HTML code in C on your server. If this is correct, go to my option 1 above by entering the values ​​directly into the JS source code if all the values ​​come from some data known to the server.

You might consider porting some features from the JS browser and back to server code to solve your problem.

+5
source

JavaScript cannot directly access any other processes, but it can make a server request for this information. The server can call function C, if necessary.

After all, this is not JavaScript calling the C function, it is the server (and any language that it uses: Python, PHP, ASP.NET, JSP, etc.) that will call the C function.

+2
source

You can make a special request so that the web server can use this request and send it to the web page.

+2
source

You might consider creating a RESTful web service on your server that will receive the source text and the identifier of the target language, and then return the translated text. You can then access it from your webpage using an ajax call .

0
source

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

What you can do is create a virtual machine. Basically, you have a huge array of "memory", a couple of "registers", etc. The most difficult task is to make sure that the instruction set and bytecodes of your virtual machine mirror some common instruction set that there is a C compiler for. You compile the C code that is on your computer, save it in a file and run it in a virtual machine. If this is too complicated, you can just get the C to assembly converter and just define a couple of Assembly instructions. There is a Linux emulator in pure javascript without server calls that do just that.

0
source

Source: https://habr.com/ru/post/918659/


All Articles