Calling C functions inside javascript

19,612

Solution 1

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.

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

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.

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

Solution 2

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

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.

Solution 3

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

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 emulator in pure javascript with no server calls that does precisely that.

Share:
19,612
ahmet
Author by

ahmet

Ruby (1.8 - 2.4), Rails (3-5), RubyMotion, Docker, Javascript/TypeScript, ReactJS, Angular, Meteor, Node, AWS, Heroku, RSpec, Cucumber, TDD, C, C++, C#, Objective-C, Swift, Elixir, Crystal, Go, Redis, Ionic, React-Native. Conferences Brighton Ruby 2014 WXG 2014 Bath Ruby 2015 WXG 2015 ElixirLDN 2016 http://ahmetabdi.github.io/ https://github.com/ahmetabdi http://twitter.com/ahmet1up

Updated on June 18, 2022

Comments

  • ahmet
    ahmet about 2 years

    The javascript is printing out the HTML onto the page example below, is it possible to call a C function on it for example in C to 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?.

    "<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.