DEV Community

Cover image for Executing C code with JS -- using Node’s exec() function

Executing C code with JS -- using Node’s exec() function

Siddhant Khare on May 12, 2024

As I've been delving into the fundamentals of Linux, hardware, and networking, I've grown increasingly fascinated by how programs in various langua...
Collapse
 
pcockerell profile image
Peter Cockerell

Am I the only one who thought, from the title of this article, that it was going to be about somehow interpreting a compiled C program from JavaScript in the browser? Admittedly that would be a total waste of time, but at least it would have made the title accurate. The titie of this article should be "Using Node"s exec() function". It has nothing specifically to do with C programs, and barely anything to do with JavaScript as most people would expect, in the context of a browser environment. I doubt that title would have garnered as many clicks, though.

Collapse
 
siddhantkcode profile image
Siddhant Khare

Sorry, that was not the intent. I am changing the title right now as you suggested

Thanks for the feedback <3

Collapse
 
amythical profile image
amythical

I assumed it was something using Emscripten and C.
I have been using Emscripten and will make a post on calling a C program converted to a webassembly using emscripten and called via javascript.

Collapse
 
pcockerell profile image
Peter Cockerell

Now that would be an interesting article! I looked at emscripten, but for my purposes, AssemblyScript was a better choice for writing Wasm functions.

Thread Thread
 
amythical profile image
amythical

Will make one soon, I am working on some emscripten and its been a struggle but it's all worth it when you see pointers exchanged between C and javascript.

Thread Thread
 
amythical profile image
amythical
Collapse
 
pauljlucas profile image
Paul J. Lucas

Agreed. The language the called program is written in is irrelevant. It could be written in any language.

Collapse
 
ezpieco profile image
Ezpie

Just like they said, "if something can be made with JavaScript, it will be made with JavaScript".

Collapse
 
siddhantkcode profile image
Siddhant Khare

haha :D

Collapse
 
kervyntjw profile image
Kervyn

This is cool, I've been looking for a solution to run shell scripts in my EC2 without resorting to changing from JavaScript, thanks for the insight!

Collapse
 
legationpro profile image
Anze

Awesome post! Do you have a solution for executing FFI from C in JavaScript?

Collapse
 
siddhantkcode profile image
Siddhant Khare

Ahh! Good question, i've never tried it. But, found this node-ffi from google search, maybe it will work.

Collapse
 
jim_noord_85b359570083682 profile image
Jim Noord

JavaScript can do some amazing things, though not necessarily quickly. for instance, I once implemented Brenham's line and circle algorithms using JavaScript. Thing to keep in mind is that JavaScript was specifically designed to not be able to be used to access the file system of the client except on Server Side Javascript, and even then, pretty dangerous. When you give shell access to a program, the permissions run at the system level. That means a hacker could gain access to the machine and anything on it, and even use it to access other machines on the network

Collapse
 
eugrus profile image
Info Comment hidden by post author - thread only accessible via permalink
Evgeny Fishgalov

Of course you can invoke a binary from any (system scripting language) script. The amount of filler text before and after, as well as the overly complicated wrap around exec() makes me wonder if it's mostly a ChatGPT generated article. The title "Executing C Code with JavaScript" is extremely misleading.

Collapse
 
bcamphart profile image
Info Comment hidden by post author - thread only accessible via permalink
B. Camphart

My thoughts too. I came in expecting a C compiler or interpreter written in JavaScript.

Collapse
 
neurabot profile image
Neurabot

learnful.

Some comments have been hidden by the post's author - find out more